root/trunk/wp-includes/bookmark.php

Revision 7990, 10.4 kB (checked in by ryan, 1 month ago)

phpdoc updates from jacobsantos. see #7038

  • Property svn:eol-style set to native
Line 
1 <?php
2 /**
3  * Link/Bookmark API
4  *
5  * @package WordPress
6  * @subpackage Bookmark
7  */
8
9 /**
10  * Retrieve Bookmark data based on ID
11  *
12  * @since 2.1
13  * @uses $wpdb Database Object
14  *
15  * @param int $bookmark_id
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_id, $output = OBJECT, $filter = 'raw') {
21     global $wpdb;
22
23     $link = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark_id));
24     $link->link_category = array_unique( wp_get_object_terms($link->link_id, 'link_category', 'fields=ids') );
25
26     $link = sanitize_bookmark($link, $filter);
27
28     if ( $output == OBJECT ) {
29         return $link;
30     } elseif ( $output == ARRAY_A ) {
31         return get_object_vars($link);
32     } elseif ( $output == ARRAY_N ) {
33         return array_values(get_object_vars($link));
34     } else {
35         return $link;
36     }
37 }
38
39 /**
40  * Retrieve single bookmark data item or field.
41  *
42  * @since 2.3
43  * @uses get_bookmark() Gets bookmark object using $bookmark as ID
44  * @uses sanitize_bookmark_field() Sanitizes Bookmark field based on $context.
45  *
46  * @param string $field The name of the data field to return
47  * @param int $bookmark The bookmark ID to get field
48  * @param string $context Optional. The context of how the field will be used.
49  * @return string
50  */
51 function get_bookmark_field( $field, $bookmark, $context = 'display' ) {
52     $bookmark = (int) $bookmark;
53     $bookmark = get_bookmark( $bookmark );
54
55     if ( is_wp_error($bookmark) )
56         return $bookmark;
57
58     if ( !is_object($bookmark) )
59         return '';
60
61     if ( !isset($bookmark->$field) )
62         return '';
63
64     return sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
65 }
66
67 /**
68  * Retrieve bookmark data based on ID.
69  *
70  * @since 2.0
71  * @deprecated Use get_bookmark()
72  * @see get_bookmark()
73  *
74  * @param int $bookmark_id ID of link
75  * @param string $output Either OBJECT, ARRAY_N, or ARRAY_A
76  * @return object|array
77  */
78 function get_link($bookmark_id, $output = OBJECT, $filter = 'raw') {
79     return get_bookmark($bookmark_id, $output, $filter);
80 }
81
82 /**
83  * Retrieves the list of bookmarks
84  *
85  * Attempts to retrieve from the cache first based on MD5 hash of arguments. If
86  * that fails, then the query will be built from the arguments and executed. The
87  * results will be stored to the cache.
88  *
89  * List of default arguments are as follows:
90  * 'orderby' - Default is 'name' (string). How to order the links by. String is
91  *        based off of the bookmark scheme.
92  * 'order' - Default is 'ASC' (string). Either 'ASC' or 'DESC'. Orders in either
93  *        ascending or descending order.
94  * 'limit' - Default is -1 (integer) or show all. The amount of bookmarks to
95  *        display.
96  * 'category' - Default is empty string (string). Include the links in what
97  *        category ID(s).
98  * 'category_name' - Default is empty string (string). Get links by category
99  *        name.
100  * 'hide_invisible' - Default is 1 (integer). Whether to show (default) or hide
101  *        links marked as 'invisible'.
102  * 'show_updated' - Default is 0 (integer). Will show the time of when the
103  *        bookmark was last updated.
104  * 'include' - Default is empty string (string). Include other categories
105  *        separated by commas.
106  * 'exclude' - Default is empty string (string). Exclude other categories
107  *        separated by commas.
108  *
109  * @since 2.1
110  * @uses $wpdb Database Object
111  * @link http://codex.wordpress.org/Template_Tags/get_bookmarks
112  *
113  * @param string|array $args List of arguments to overwrite the defaults
114  * @return array List of bookmark row objects
115  */
116 function get_bookmarks($args = '') {
117     global $wpdb;
118
119     $defaults = array(
120         'orderby' => 'name', 'order' => 'ASC',
121         'limit' => -1, 'category' => '',
122         'category_name' => '', 'hide_invisible' => 1,
123         'show_updated' => 0, 'include' => '',
124         'exclude' => '', 'search' => ''
125     );
126
127     $r = wp_parse_args( $args, $defaults );
128     extract( $r, EXTR_SKIP );
129
130     $key = md5( serialize( $r ) );
131     if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) )
132         if ( isset( $cache[ $key ] ) )
133             return apply_filters('get_bookmarks', $cache[ $key ], $r );
134
135     $inclusions = '';
136     if ( !empty($include) ) {
137         $exclude = ''//ignore exclude, category, and category_name params if using include
138         $category = '';
139         $category_name = '';
140         $inclinks = preg_split('/[\s,]+/',$include);
141         if ( count($inclinks) ) {
142             foreach ( $inclinks as $inclink ) {
143                 if (empty($inclusions))
144                     $inclusions = ' AND ( link_id = ' . intval($inclink) . ' ';
145                 else
146                     $inclusions .= ' OR link_id = ' . intval($inclink) . ' ';
147             }
148         }
149     }
150     if (!empty($inclusions))
151         $inclusions .= ')';
152
153     $exclusions = '';
154     if ( !empty($exclude) ) {
155         $exlinks = preg_split('/[\s,]+/',$exclude);
156         if ( count($exlinks) ) {
157             foreach ( $exlinks as $exlink ) {
158                 if (empty($exclusions))
159                     $exclusions = ' AND ( link_id <> ' . intval($exlink) . ' ';
160                 else
161                     $exclusions .= ' AND link_id <> ' . intval($exlink) . ' ';
162             }
163         }
164     }
165     if (!empty($exclusions))
166         $exclusions .= ')';
167
168     if ( ! empty($category_name) ) {
169         if ( $category = get_term_by('name', $category_name, 'link_category') )
170             $category = $category->term_id;
171     }
172
173     if ( ! empty($search) ) {
174         $search = like_escape($search);
175         $search = " AND ( (link_url LIKE '%$search%') OR (link_name LIKE '%$search%') OR (link_description LIKE '%$search%') ) ";
176     }
177
178     $category_query = '';
179     $join = '';
180     if ( !empty($category) ) {
181         $incategories = preg_split('/[\s,]+/',$category);
182         if ( count($incategories) ) {
183             foreach ( $incategories as $incat ) {
184                 if (empty($category_query))
185                     $category_query = ' AND ( tt.term_id = ' . intval($incat) . ' ';
186                 else
187                     $category_query .= ' OR tt.term_id = ' . intval($incat) . ' ';
188             }
189         }
190     }
191     if (!empty($category_query)) {
192         $category_query .= ") AND taxonomy = 'link_category'";
193         $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";
194     }
195
196     if (get_option('links_recently_updated_time')) {
197         $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_option('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
198     } else {
199         $recently_updated_test = '';
200     }
201
202     $get_updated = ( $show_updated ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
203
204     $orderby = strtolower($orderby);
205     $length = '';
206     switch ($orderby) {
207         case 'length':
208             $length = ", CHAR_LENGTH(link_name) AS length";
209             break;
210         case 'rand':
211             $orderby = 'rand()';
212             break;
213         default:
214             $orderby = "link_" . $orderby;
215     }
216
217     if ( 'link_id' == $orderby )
218         $orderby = "$wpdb->links.link_id";
219
220     $visible = '';
221     if ( $hide_invisible )
222         $visible = "AND link_visible = 'Y'";
223
224     $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
225     $query .= " $exclusions $inclusions $search";
226     $query .= " ORDER BY $orderby $order";
227     if ($limit != -1)
228         $query .= " LIMIT $limit";
229
230     $results = $wpdb->get_results($query);
231
232     $cache[ $key ] = $results;
233     wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
234
235     return apply_filters('get_bookmarks', $results, $r);
236 }
237
238 /**
239  * Sanitizes all bookmark fields
240  *
241  * @since 2.3
242  *
243  * @param object|array $bookmark Bookmark row
244  * @param string $context Optional, default is 'display'. How to filter the
245  *        fields
246  * @return object|array Same type as $bookmark but with fields sanitized.
247  */
248 function sanitize_bookmark($bookmark, $context = 'display') {
249     $fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category',
250         'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated',
251         'link_rel', 'link_notes', 'link_rss', );
252
253     $do_object = false;
254     if ( is_object($bookmark) )
255         $do_object = true;
256
257     foreach ( $fields as $field ) {
258         if ( $do_object )
259             $bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
260         else
261             $bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $bookmark['link_id'], $context);
262     }
263
264     return $bookmark;
265 }
266
267 /**
268  * Sanitizes a bookmark field
269  *
270  * Sanitizes the bookmark fields based on what the field name is. If the field
271  * has a strict value set, then it will be tested for that, else a more generic
272  * filtering is applied. After the more strict filter is applied, if the
273  * $context is 'raw' then the value is immediately return.
274  *
275  * Hooks exist for the more generic cases. With the 'edit' context, the
276  * 'edit_$field' filter will be called and passed the $value and $bookmark_id
277  * respectively. With the 'db' context, the 'pre_$field' filter is called and
278  * passed the value. The 'display' context is the final context and has the
279  * $field has the filter name and is passed the $value, $bookmark_id, and
280  * $context respectively.
281  *
282  * @since 2.3
283  *
284  * @param string $field The bookmark field
285  * @param mixed $value The bookmark field value
286  * @param int $bookmark_id Bookmark ID
287  * @param string $context How to filter the field value. Either 'raw', 'edit',
288  *        'attribute', 'js', 'db', or 'display'
289  * @return mixed The filtered value
290  */
291 function sanitize_bookmark_field($field, $value, $bookmark_id, $context) {
292     $int_fields = array('link_id', 'link_rating');
293     if ( in_array($field, $int_fields) )
294         $value = (int) $value;
295
296     $yesno = array('link_visible');
297     if ( in_array($field, $yesno) )
298         $value = preg_replace('/[^YNyn]/', '', $value);
299
300     if ( 'link_target' == $field ) {
301         $targets = array('_top', '_blank');
302         if ( ! in_array($value, $targets) )
303             $value = '';
304     }
305
306     if ( 'raw' == $context )
307         return $value;
308
309     if ( 'edit' == $context ) {
310         $format_to_edit = array('link_notes');
311         $value = apply_filters("edit_$field", $value, $bookmark_id);
312
313         if ( in_array($field, $format_to_edit) ) {
314             $value = format_to_edit($value);
315         } else {
316             $value = attribute_escape($value);
317         }
318     } else if ( 'db' == $context ) {
319         $value = apply_filters("pre_$field", $value);
320     } else {
321         // Use display filters by default.
322         $value = apply_filters($field, $value, $bookmark_id, $context);
323     }
324
325     if ( 'attribute' == $context )
326         $value = attribute_escape($value);
327     else if ( 'js' == $context )
328         $value = js_escape($value);
329
330     return $value;
331 }
332
333 /**
334  * Deletes entire bookmark cache
335  *
336  * @since 2.1
337  * @uses wp_cache_delete() Deletes the contents of 'get_bookmarks'
338  */
339 function delete_get_bookmark_cache() {
340     wp_cache_delete( 'get_bookmarks', 'bookmark' );
341 }
342 add_action( 'add_link', 'delete_get_bookmark_cache' );
343 add_action( 'edit_link', 'delete_get_bookmark_cache' );
344 add_action( 'delete_link', 'delete_get_bookmark_cache' );
345
346 ?>
347
Note: See TracBrowser for help on using the browser.