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