]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/bookmark.php
Wordpress 2.5.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  * get_bookmark() - Get 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  * get_bookmark_field() - Gets 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  * get_link() - Returns 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  * get_bookmarks() - 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 based off of the bookmark scheme.
91  * 'order' - Default is 'ASC' (string). Either 'ASC' or 'DESC'. Orders in either ascending or descending order.
92  * 'limit' - Default is -1 (integer) or show all. The amount of bookmarks to display.
93  * 'category' - Default is empty string (string). Include the links in what category ID(s).
94  * 'category_name' - Default is empty string (string). Get links by category name.
95  * 'hide_invisible' - Default is 1 (integer). Whether to show (default) or hide links marked as 'invisible'.
96  * 'show_updated' - Default is 0 (integer). Will show the time of when the bookmark was last updated.
97  * 'include' - Default is empty string (string). Include other categories separated by commas.
98  * 'exclude' - Default is empty string (string). Exclude other categories separated by commas.
99  *
100  * @since 2.1
101  * @uses $wpdb Database Object
102  *
103  * @param string|array $args List of arguments to overwrite the defaults
104  * @return array List of bookmark row objects
105  */
106 function get_bookmarks($args = '') {
107         global $wpdb;
108
109         $defaults = array(
110                 'orderby' => 'name', 'order' => 'ASC',
111                 'limit' => -1, 'category' => '',
112                 'category_name' => '', 'hide_invisible' => 1,
113                 'show_updated' => 0, 'include' => '',
114                 'exclude' => '', 'search' => ''
115         );
116
117         $r = wp_parse_args( $args, $defaults );
118         extract( $r, EXTR_SKIP );
119
120         $key = md5( serialize( $r ) );
121         if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) )
122                 if ( isset( $cache[ $key ] ) )
123                         return apply_filters('get_bookmarks', $cache[ $key ], $r );
124
125         $inclusions = '';
126         if ( !empty($include) ) {
127                 $exclude = '';  //ignore exclude, category, and category_name params if using include
128                 $category = '';
129                 $category_name = '';
130                 $inclinks = preg_split('/[\s,]+/',$include);
131                 if ( count($inclinks) ) {
132                         foreach ( $inclinks as $inclink ) {
133                                 if (empty($inclusions))
134                                         $inclusions = ' AND ( link_id = ' . intval($inclink) . ' ';
135                                 else
136                                         $inclusions .= ' OR link_id = ' . intval($inclink) . ' ';
137                         }
138                 }
139         }
140         if (!empty($inclusions))
141                 $inclusions .= ')';
142
143         $exclusions = '';
144         if ( !empty($exclude) ) {
145                 $exlinks = preg_split('/[\s,]+/',$exclude);
146                 if ( count($exlinks) ) {
147                         foreach ( $exlinks as $exlink ) {
148                                 if (empty($exclusions))
149                                         $exclusions = ' AND ( link_id <> ' . intval($exlink) . ' ';
150                                 else
151                                         $exclusions .= ' AND link_id <> ' . intval($exlink) . ' ';
152                         }
153                 }
154         }
155         if (!empty($exclusions))
156                 $exclusions .= ')';
157
158         if ( ! empty($category_name) ) {
159                 if ( $category = get_term_by('name', $category_name, 'link_category') )
160                         $category = $category->term_id;
161         }
162
163         if ( ! empty($search) ) {
164                 $search = like_escape($search);
165                 $search = " AND ( (link_url LIKE '%$search%') OR (link_name LIKE '%$search%') OR (link_description LIKE '%$search%') ) ";
166         }
167
168         $category_query = '';
169         $join = '';
170         if ( !empty($category) ) {
171                 $incategories = preg_split('/[\s,]+/',$category);
172                 if ( count($incategories) ) {
173                         foreach ( $incategories as $incat ) {
174                                 if (empty($category_query))
175                                         $category_query = ' AND ( tt.term_id = ' . intval($incat) . ' ';
176                                 else
177                                         $category_query .= ' OR tt.term_id = ' . intval($incat) . ' ';
178                         }
179                 }
180         }
181         if (!empty($category_query)) {
182                 $category_query .= ") AND taxonomy = 'link_category'";
183                 $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";
184         }
185
186         if (get_option('links_recently_updated_time')) {
187                 $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_option('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
188         } else {
189                 $recently_updated_test = '';
190         }
191
192         $get_updated = ( $show_updated ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
193
194         $orderby = strtolower($orderby);
195         $length = '';
196         switch ($orderby) {
197                 case 'length':
198                         $length = ", CHAR_LENGTH(link_name) AS length";
199                         break;
200                 case 'rand':
201                         $orderby = 'rand()';
202                         break;
203                 default:
204                         $orderby = "link_" . $orderby;
205         }
206
207         if ( 'link_id' == $orderby )
208                 $orderby = "$wpdb->links.link_id";
209
210         $visible = '';
211         if ( $hide_invisible )
212                 $visible = "AND link_visible = 'Y'";
213
214         $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
215         $query .= " $exclusions $inclusions $search";
216         $query .= " ORDER BY $orderby $order";
217         if ($limit != -1)
218                 $query .= " LIMIT $limit";
219
220         $results = $wpdb->get_results($query);
221
222         $cache[ $key ] = $results;
223         wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
224
225         return apply_filters('get_bookmarks', $results, $r);
226 }
227
228 /**
229  * sanitize_bookmark() - Sanitizes all bookmark fields
230  *
231  * @since 2.3
232  *
233  * @param object|array $bookmark Bookmark row
234  * @param string $context Optional, default is 'display'. How to filter the fields
235  * @return object|array Same type as $bookmark but with fields sanitized.
236  */
237 function sanitize_bookmark($bookmark, $context = 'display') {
238         $fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category',
239                 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated',
240                 'link_rel', 'link_notes', 'link_rss', );
241
242         $do_object = false;
243         if ( is_object($bookmark) )
244                 $do_object = true;
245
246         foreach ( $fields as $field ) {
247                 if ( $do_object )
248                         $bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
249                 else
250                         $bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $bookmark['link_id'], $context);
251         }
252
253         return $bookmark;
254 }
255
256 /**
257  * sanitize_bookmark_field() - Sanitizes a bookmark field
258  *
259  * Sanitizes the bookmark fields based on what the field name is. If the field has a
260  * strict value set, then it will be tested for that, else a more generic filtering is
261  * applied. After the more strict filter is applied, if the $context is 'raw' then the
262  * value is immediately return.
263  *
264  * Hooks exist for the more generic cases. With the 'edit' context, the 'edit_$field'
265  * filter will be called and passed the $value and $bookmark_id respectively. With the
266  * 'db' context, the 'pre_$field' filter is called and passed the value. The 'display'
267  * context is the final context and has the $field has the filter name and is passed the
268  * $value, $bookmark_id, and $context respectively.
269  *
270  * @since 2.3
271  *
272  * @param string $field The bookmark field
273  * @param mixed $value The bookmark field value
274  * @param int $bookmark_id Bookmark ID
275  * @param string $context How to filter the field value. Either 'raw', 'edit', 'attribute', 'js', 'db', or 'display'
276  * @return mixed The filtered value
277  */
278 function sanitize_bookmark_field($field, $value, $bookmark_id, $context) {
279         $int_fields = array('link_id', 'link_rating');
280         if ( in_array($field, $int_fields) )
281                 $value = (int) $value;
282
283         $yesno = array('link_visible');
284         if ( in_array($field, $yesno) )
285                 $value = preg_replace('/[^YNyn]/', '', $value);
286
287         if ( 'link_target' == $field ) {
288                 $targets = array('_top', '_blank');
289                 if ( ! in_array($value, $targets) )
290                         $value = '';
291         }
292
293         if ( 'raw' == $context )
294                 return $value;
295
296         if ( 'edit' == $context ) {
297                 $format_to_edit = array('link_notes');
298                 $value = apply_filters("edit_$field", $value, $bookmark_id);
299
300                 if ( in_array($field, $format_to_edit) ) {
301                         $value = format_to_edit($value);
302                 } else {
303                         $value = attribute_escape($value);
304                 }
305         } else if ( 'db' == $context ) {
306                 $value = apply_filters("pre_$field", $value);
307         } else {
308                 // Use display filters by default.
309                 $value = apply_filters($field, $value, $bookmark_id, $context);
310         }
311
312         if ( 'attribute' == $context )
313                 $value = attribute_escape($value);
314         else if ( 'js' == $context )
315                 $value = js_escape($value);
316
317         return $value;
318 }
319
320 /**
321  * delete_get_bookmark_cache() - Deletes entire bookmark cache
322  *
323  * @since 2.1
324  * @uses wp_cache_delete() Deletes the contents of 'get_bookmarks'
325  */
326 function delete_get_bookmark_cache() {
327         wp_cache_delete( 'get_bookmarks', 'bookmark' );
328 }
329 add_action( 'add_link', 'delete_get_bookmark_cache' );
330 add_action( 'edit_link', 'delete_get_bookmark_cache' );
331 add_action( 'delete_link', 'delete_get_bookmark_cache' );
332
333 ?>