]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/bookmark.php
WordPress 4.2.5
[autoinstalls/wordpress.git] / wp-includes / bookmark.php
1 <?php
2 /**
3  * Link/Bookmark API
4  *
5  * @package WordPress
6  * @subpackage Bookmark
7  */
8
9 /**
10  * Retrieve Bookmark data
11  *
12  * @since 2.1.0
13  *
14  * @global wpdb $wpdb WordPress database abstraction object.
15  *
16  * @param int|stdClass $bookmark
17  * @param string $output Optional. Either OBJECT, ARRAY_N, or ARRAY_A constant
18  * @param string $filter Optional, default is 'raw'.
19  * @return array|object Type returned depends on $output value.
20  */
21 function get_bookmark($bookmark, $output = OBJECT, $filter = 'raw') {
22         global $wpdb;
23
24         if ( empty($bookmark) ) {
25                 if ( isset($GLOBALS['link']) )
26                         $_bookmark = & $GLOBALS['link'];
27                 else
28                         $_bookmark = null;
29         } elseif ( is_object($bookmark) ) {
30                 wp_cache_add($bookmark->link_id, $bookmark, 'bookmark');
31                 $_bookmark = $bookmark;
32         } else {
33                 if ( isset($GLOBALS['link']) && ($GLOBALS['link']->link_id == $bookmark) ) {
34                         $_bookmark = & $GLOBALS['link'];
35                 } elseif ( ! $_bookmark = wp_cache_get($bookmark, 'bookmark') ) {
36                         $_bookmark = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark));
37                         if ( $_bookmark ) {
38                                 $_bookmark->link_category = array_unique( wp_get_object_terms( $_bookmark->link_id, 'link_category', array( 'fields' => 'ids' ) ) );
39                                 wp_cache_add( $_bookmark->link_id, $_bookmark, 'bookmark' );
40                         }
41                 }
42         }
43
44         if ( ! $_bookmark )
45                 return $_bookmark;
46
47         $_bookmark = sanitize_bookmark($_bookmark, $filter);
48
49         if ( $output == OBJECT ) {
50                 return $_bookmark;
51         } elseif ( $output == ARRAY_A ) {
52                 return get_object_vars($_bookmark);
53         } elseif ( $output == ARRAY_N ) {
54                 return array_values(get_object_vars($_bookmark));
55         } else {
56                 return $_bookmark;
57         }
58 }
59
60 /**
61  * Retrieve single bookmark data item or field.
62  *
63  * @since 2.3.0
64  *
65  * @param string $field The name of the data field to return
66  * @param int $bookmark The bookmark ID to get field
67  * @param string $context Optional. The context of how the field will be used.
68  * @return string
69  */
70 function get_bookmark_field( $field, $bookmark, $context = 'display' ) {
71         $bookmark = (int) $bookmark;
72         $bookmark = get_bookmark( $bookmark );
73
74         if ( is_wp_error($bookmark) )
75                 return $bookmark;
76
77         if ( !is_object($bookmark) )
78                 return '';
79
80         if ( !isset($bookmark->$field) )
81                 return '';
82
83         return sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
84 }
85
86 /**
87  * Retrieves the list of bookmarks
88  *
89  * Attempts to retrieve from the cache first based on MD5 hash of arguments. If
90  * that fails, then the query will be built from the arguments and executed. The
91  * results will be stored to the cache.
92  *
93  * @since 2.1.0
94  *
95  * @global wpdb $wpdb WordPress database abstraction object.
96  *
97  * @param string|array $args {
98  *     Optional. String or array of arguments to retrieve bookmarks.
99  *
100  *     @type string   $orderby        How to order the links by. Accepts post fields. Default 'name'.
101  *     @type string   $order          Whether to order bookmarks in ascending or descending order.
102  *                                    Accepts 'ASC' (ascending) or 'DESC' (descending). Default 'ASC'.
103  *     @type int      $limit          Amount of bookmarks to display. Accepts 1+ or -1 for all.
104  *                                    Default -1.
105  *     @type string   $category       Comma-separated list of category ids to include links from.
106  *                                    Default empty.
107  *     @type string   $category_name  Category to retrieve links for by name. Default empty.
108  *     @type int|bool $hide_invisible Whether to show or hide links marked as 'invisible'. Accepts
109  *                                    1|true or 0|false. Default 1|true.
110  *     @type int|bool $show_updated   Whether to display the time the bookmark was last updated.
111  *                                    Accepts 1|true or 0|false. Default 0|false.
112  *     @type string   $include        Comma-separated list of bookmark IDs to include. Default empty.
113  *     @type string   $exclude        Comma-separated list of bookmark IDs to exclude. Default empty.
114  * }
115  * @return array List of bookmark row objects.
116  */
117 function get_bookmarks( $args = '' ) {
118         global $wpdb;
119
120         $defaults = array(
121                 'orderby' => 'name', 'order' => 'ASC',
122                 'limit' => -1, 'category' => '',
123                 'category_name' => '', 'hide_invisible' => 1,
124                 'show_updated' => 0, 'include' => '',
125                 'exclude' => '', 'search' => ''
126         );
127
128         $r = wp_parse_args( $args, $defaults );
129
130         $key = md5( serialize( $r ) );
131         if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) {
132                 if ( is_array( $cache ) && isset( $cache[ $key ] ) ) {
133                         $bookmarks = $cache[ $key ];
134                         /**
135                          * Filter the returned list of bookmarks.
136                          *
137                          * The first time the hook is evaluated in this file, it returns the cached
138                          * bookmarks list. The second evaluation returns a cached bookmarks list if the
139                          * link category is passed but does not exist. The third evaluation returns
140                          * the full cached results.
141                          *
142                          * @since 2.1.0
143                          *
144                          * @see get_bookmarks()
145                          *
146                          * @param array $bookmarks List of the cached bookmarks.
147                          * @param array $r         An array of bookmark query arguments.
148                          */
149                         return apply_filters( 'get_bookmarks', $bookmarks, $r );
150                 }
151         }
152
153         if ( ! is_array( $cache ) ) {
154                 $cache = array();
155         }
156
157         $inclusions = '';
158         if ( ! empty( $r['include'] ) ) {
159                 $r['exclude'] = '';  //ignore exclude, category, and category_name params if using include
160                 $r['category'] = '';
161                 $r['category_name'] = '';
162                 $inclinks = preg_split( '/[\s,]+/', $r['include'] );
163                 if ( count( $inclinks ) ) {
164                         foreach ( $inclinks as $inclink ) {
165                                 if ( empty( $inclusions ) ) {
166                                         $inclusions = ' AND ( link_id = ' . intval( $inclink ) . ' ';
167                                 } else {
168                                         $inclusions .= ' OR link_id = ' . intval( $inclink ) . ' ';
169                                 }
170                         }
171                 }
172         }
173         if (! empty( $inclusions ) ) {
174                 $inclusions .= ')';
175         }
176
177         $exclusions = '';
178         if ( ! empty( $r['exclude'] ) ) {
179                 $exlinks = preg_split( '/[\s,]+/', $r['exclude'] );
180                 if ( count( $exlinks ) ) {
181                         foreach ( $exlinks as $exlink ) {
182                                 if ( empty( $exclusions ) ) {
183                                         $exclusions = ' AND ( link_id <> ' . intval( $exlink ) . ' ';
184                                 } else {
185                                         $exclusions .= ' AND link_id <> ' . intval( $exlink ) . ' ';
186                                 }
187                         }
188                 }
189         }
190         if ( ! empty( $exclusions ) ) {
191                 $exclusions .= ')';
192         }
193
194         if ( ! empty( $r['category_name'] ) ) {
195                 if ( $r['category'] = get_term_by('name', $r['category_name'], 'link_category') ) {
196                         $r['category'] = $r['category']->term_id;
197                 } else {
198                         $cache[ $key ] = array();
199                         wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
200                         /** This filter is documented in wp-includes/bookmark.php */
201                         return apply_filters( 'get_bookmarks', array(), $r );
202                 }
203         }
204
205         $search = '';
206         if ( ! empty( $r['search'] ) ) {
207                 $like = '%' . $wpdb->esc_like( $r['search'] ) . '%';
208                 $search = $wpdb->prepare(" AND ( (link_url LIKE %s) OR (link_name LIKE %s) OR (link_description LIKE %s) ) ", $like, $like, $like );
209         }
210
211         $category_query = '';
212         $join = '';
213         if ( ! empty( $r['category'] ) ) {
214                 $incategories = preg_split( '/[\s,]+/', $r['category'] );
215                 if ( count($incategories) ) {
216                         foreach ( $incategories as $incat ) {
217                                 if ( empty( $category_query ) ) {
218                                         $category_query = ' AND ( tt.term_id = ' . intval( $incat ) . ' ';
219                                 } else {
220                                         $category_query .= ' OR tt.term_id = ' . intval( $incat ) . ' ';
221                                 }
222                         }
223                 }
224         }
225         if ( ! empty( $category_query ) ) {
226                 $category_query .= ") AND taxonomy = 'link_category'";
227                 $join = " INNER JOIN $wpdb->term_relationships AS tr ON ($wpdb->links.link_id = tr.object_id) INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id";
228         }
229
230         if ( $r['show_updated'] ) {
231                 $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated ";
232         } else {
233                 $recently_updated_test = '';
234         }
235
236         $get_updated = ( $r['show_updated'] ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
237
238         $orderby = strtolower( $r['orderby'] );
239         $length = '';
240         switch ( $orderby ) {
241                 case 'length':
242                         $length = ", CHAR_LENGTH(link_name) AS length";
243                         break;
244                 case 'rand':
245                         $orderby = 'rand()';
246                         break;
247                 case 'link_id':
248                         $orderby = "$wpdb->links.link_id";
249                         break;
250                 default:
251                         $orderparams = array();
252                         $keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes', 'link_description' );
253                         foreach ( explode( ',', $orderby ) as $ordparam ) {
254                                 $ordparam = trim( $ordparam );
255
256                                 if ( in_array( 'link_' . $ordparam, $keys ) ) {
257                                         $orderparams[] = 'link_' . $ordparam;
258                                 } elseif ( in_array( $ordparam, $keys ) ) {
259                                         $orderparams[] = $ordparam;
260                                 }
261                         }
262                         $orderby = implode( ',', $orderparams );
263         }
264
265         if ( empty( $orderby ) ) {
266                 $orderby = 'link_name';
267         }
268
269         $order = strtoupper( $r['order'] );
270         if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) {
271                 $order = 'ASC';
272         }
273
274         $visible = '';
275         if ( $r['hide_invisible'] ) {
276                 $visible = "AND link_visible = 'Y'";
277         }
278
279         $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
280         $query .= " $exclusions $inclusions $search";
281         $query .= " ORDER BY $orderby $order";
282         if ( $r['limit'] != -1 ) {
283                 $query .= ' LIMIT ' . $r['limit'];
284         }
285
286         $results = $wpdb->get_results( $query );
287
288         $cache[ $key ] = $results;
289         wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
290
291         /** This filter is documented in wp-includes/bookmark.php */
292         return apply_filters( 'get_bookmarks', $results, $r );
293 }
294
295 /**
296  * Sanitizes all bookmark fields
297  *
298  * @since 2.3.0
299  *
300  * @param object|array $bookmark Bookmark row
301  * @param string $context Optional, default is 'display'. How to filter the
302  *              fields
303  * @return object|array Same type as $bookmark but with fields sanitized.
304  */
305 function sanitize_bookmark($bookmark, $context = 'display') {
306         $fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category',
307                 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated',
308                 'link_rel', 'link_notes', 'link_rss', );
309
310         if ( is_object($bookmark) ) {
311                 $do_object = true;
312                 $link_id = $bookmark->link_id;
313         } else {
314                 $do_object = false;
315                 $link_id = $bookmark['link_id'];
316         }
317
318         foreach ( $fields as $field ) {
319                 if ( $do_object ) {
320                         if ( isset($bookmark->$field) )
321                                 $bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $link_id, $context);
322                 } else {
323                         if ( isset($bookmark[$field]) )
324                                 $bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $link_id, $context);
325                 }
326         }
327
328         return $bookmark;
329 }
330
331 /**
332  * Sanitizes a bookmark field
333  *
334  * Sanitizes the bookmark fields based on what the field name is. If the field
335  * has a strict value set, then it will be tested for that, else a more generic
336  * filtering is applied. After the more strict filter is applied, if the
337  * $context is 'raw' then the value is immediately return.
338  *
339  * Hooks exist for the more generic cases. With the 'edit' context, the
340  * 'edit_$field' filter will be called and passed the $value and $bookmark_id
341  * respectively. With the 'db' context, the 'pre_$field' filter is called and
342  * passed the value. The 'display' context is the final context and has the
343  * $field has the filter name and is passed the $value, $bookmark_id, and
344  * $context respectively.
345  *
346  * @since 2.3.0
347  *
348  * @param string $field The bookmark field
349  * @param mixed $value The bookmark field value
350  * @param int $bookmark_id Bookmark ID
351  * @param string $context How to filter the field value. Either 'raw', 'edit',
352  *              'attribute', 'js', 'db', or 'display'
353  * @return mixed The filtered value
354  */
355 function sanitize_bookmark_field($field, $value, $bookmark_id, $context) {
356         switch ( $field ) {
357         case 'link_id' : // ints
358         case 'link_rating' :
359                 $value = (int) $value;
360                 break;
361         case 'link_category' : // array( ints )
362                 $value = array_map('absint', (array) $value);
363                 // We return here so that the categories aren't filtered.
364                 // The 'link_category' filter is for the name of a link category, not an array of a link's link categories
365                 return $value;
366
367         case 'link_visible' : // bool stored as Y|N
368                 $value = preg_replace('/[^YNyn]/', '', $value);
369                 break;
370         case 'link_target' : // "enum"
371                 $targets = array('_top', '_blank');
372                 if ( ! in_array($value, $targets) )
373                         $value = '';
374                 break;
375         }
376
377         if ( 'raw' == $context )
378                 return $value;
379
380         if ( 'edit' == $context ) {
381                 /** This filter is documented in wp-includes/post.php */
382                 $value = apply_filters( "edit_$field", $value, $bookmark_id );
383
384                 if ( 'link_notes' == $field ) {
385                         $value = esc_html( $value ); // textarea_escaped
386                 } else {
387                         $value = esc_attr($value);
388                 }
389         } elseif ( 'db' == $context ) {
390                 /** This filter is documented in wp-includes/post.php */
391                 $value = apply_filters( "pre_$field", $value );
392         } else {
393                 /** This filter is documented in wp-includes/post.php */
394                 $value = apply_filters( $field, $value, $bookmark_id, $context );
395
396                 if ( 'attribute' == $context ) {
397                         $value = esc_attr( $value );
398                 } elseif ( 'js' == $context ) {
399                         $value = esc_js( $value );
400                 }
401         }
402
403         return $value;
404 }
405
406 /**
407  * Deletes bookmark cache
408  *
409  * @since 2.7.0
410  */
411 function clean_bookmark_cache( $bookmark_id ) {
412         wp_cache_delete( $bookmark_id, 'bookmark' );
413         wp_cache_delete( 'get_bookmarks', 'bookmark' );
414         clean_object_term_cache( $bookmark_id, 'link');
415 }