17 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
17 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
18 * an stdClass object, an associative array, or a numeric array, respectively. Default OBJECT. |
18 * an stdClass object, an associative array, or a numeric array, respectively. Default OBJECT. |
19 * @param string $filter Optional, default is 'raw'. |
19 * @param string $filter Optional, default is 'raw'. |
20 * @return array|object|null Type returned depends on $output value. |
20 * @return array|object|null Type returned depends on $output value. |
21 */ |
21 */ |
22 function get_bookmark($bookmark, $output = OBJECT, $filter = 'raw') { |
22 function get_bookmark( $bookmark, $output = OBJECT, $filter = 'raw' ) { |
23 global $wpdb; |
23 global $wpdb; |
24 |
24 |
25 if ( empty($bookmark) ) { |
25 if ( empty( $bookmark ) ) { |
26 if ( isset($GLOBALS['link']) ) |
26 if ( isset( $GLOBALS['link'] ) ) { |
27 $_bookmark = & $GLOBALS['link']; |
27 $_bookmark = & $GLOBALS['link']; |
28 else |
28 } else { |
29 $_bookmark = null; |
29 $_bookmark = null; |
30 } elseif ( is_object($bookmark) ) { |
30 } |
31 wp_cache_add($bookmark->link_id, $bookmark, 'bookmark'); |
31 } elseif ( is_object( $bookmark ) ) { |
|
32 wp_cache_add( $bookmark->link_id, $bookmark, 'bookmark' ); |
32 $_bookmark = $bookmark; |
33 $_bookmark = $bookmark; |
33 } else { |
34 } else { |
34 if ( isset($GLOBALS['link']) && ($GLOBALS['link']->link_id == $bookmark) ) { |
35 if ( isset( $GLOBALS['link'] ) && ( $GLOBALS['link']->link_id == $bookmark ) ) { |
35 $_bookmark = & $GLOBALS['link']; |
36 $_bookmark = & $GLOBALS['link']; |
36 } elseif ( ! $_bookmark = wp_cache_get($bookmark, 'bookmark') ) { |
37 } elseif ( ! $_bookmark = wp_cache_get( $bookmark, 'bookmark' ) ) { |
37 $_bookmark = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark)); |
38 $_bookmark = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark ) ); |
38 if ( $_bookmark ) { |
39 if ( $_bookmark ) { |
39 $_bookmark->link_category = array_unique( wp_get_object_terms( $_bookmark->link_id, 'link_category', array( 'fields' => 'ids' ) ) ); |
40 $_bookmark->link_category = array_unique( wp_get_object_terms( $_bookmark->link_id, 'link_category', array( 'fields' => 'ids' ) ) ); |
40 wp_cache_add( $_bookmark->link_id, $_bookmark, 'bookmark' ); |
41 wp_cache_add( $_bookmark->link_id, $_bookmark, 'bookmark' ); |
41 } |
42 } |
42 } |
43 } |
43 } |
44 } |
44 |
45 |
45 if ( ! $_bookmark ) |
46 if ( ! $_bookmark ) { |
46 return $_bookmark; |
47 return $_bookmark; |
47 |
48 } |
48 $_bookmark = sanitize_bookmark($_bookmark, $filter); |
49 |
|
50 $_bookmark = sanitize_bookmark( $_bookmark, $filter ); |
49 |
51 |
50 if ( $output == OBJECT ) { |
52 if ( $output == OBJECT ) { |
51 return $_bookmark; |
53 return $_bookmark; |
52 } elseif ( $output == ARRAY_A ) { |
54 } elseif ( $output == ARRAY_A ) { |
53 return get_object_vars($_bookmark); |
55 return get_object_vars( $_bookmark ); |
54 } elseif ( $output == ARRAY_N ) { |
56 } elseif ( $output == ARRAY_N ) { |
55 return array_values(get_object_vars($_bookmark)); |
57 return array_values( get_object_vars( $_bookmark ) ); |
56 } else { |
58 } else { |
57 return $_bookmark; |
59 return $_bookmark; |
58 } |
60 } |
59 } |
61 } |
60 |
62 |
70 */ |
72 */ |
71 function get_bookmark_field( $field, $bookmark, $context = 'display' ) { |
73 function get_bookmark_field( $field, $bookmark, $context = 'display' ) { |
72 $bookmark = (int) $bookmark; |
74 $bookmark = (int) $bookmark; |
73 $bookmark = get_bookmark( $bookmark ); |
75 $bookmark = get_bookmark( $bookmark ); |
74 |
76 |
75 if ( is_wp_error($bookmark) ) |
77 if ( is_wp_error( $bookmark ) ) { |
76 return $bookmark; |
78 return $bookmark; |
77 |
79 } |
78 if ( !is_object($bookmark) ) |
80 |
|
81 if ( ! is_object( $bookmark ) ) { |
79 return ''; |
82 return ''; |
80 |
83 } |
81 if ( !isset($bookmark->$field) ) |
84 |
|
85 if ( ! isset( $bookmark->$field ) ) { |
82 return ''; |
86 return ''; |
83 |
87 } |
84 return sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context); |
88 |
|
89 return sanitize_bookmark_field( $field, $bookmark->$field, $bookmark->link_id, $context ); |
85 } |
90 } |
86 |
91 |
87 /** |
92 /** |
88 * Retrieves the list of bookmarks |
93 * Retrieves the list of bookmarks |
89 * |
94 * |
117 */ |
122 */ |
118 function get_bookmarks( $args = '' ) { |
123 function get_bookmarks( $args = '' ) { |
119 global $wpdb; |
124 global $wpdb; |
120 |
125 |
121 $defaults = array( |
126 $defaults = array( |
122 'orderby' => 'name', 'order' => 'ASC', |
127 'orderby' => 'name', |
123 'limit' => -1, 'category' => '', |
128 'order' => 'ASC', |
124 'category_name' => '', 'hide_invisible' => 1, |
129 'limit' => -1, |
125 'show_updated' => 0, 'include' => '', |
130 'category' => '', |
126 'exclude' => '', 'search' => '' |
131 'category_name' => '', |
|
132 'hide_invisible' => 1, |
|
133 'show_updated' => 0, |
|
134 'include' => '', |
|
135 'exclude' => '', |
|
136 'search' => '', |
127 ); |
137 ); |
128 |
138 |
129 $r = wp_parse_args( $args, $defaults ); |
139 $r = wp_parse_args( $args, $defaults ); |
130 |
140 |
131 $key = md5( serialize( $r ) ); |
141 $key = md5( serialize( $r ) ); |
132 $cache = false; |
142 $cache = false; |
133 if ( 'rand' !== $r['orderby'] && $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) { |
143 if ( 'rand' !== $r['orderby'] && $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) { |
134 if ( is_array( $cache ) && isset( $cache[ $key ] ) ) { |
144 if ( is_array( $cache ) && isset( $cache[ $key ] ) ) { |
135 $bookmarks = $cache[ $key ]; |
145 $bookmarks = $cache[ $key ]; |
136 /** |
146 /** |
156 $cache = array(); |
166 $cache = array(); |
157 } |
167 } |
158 |
168 |
159 $inclusions = ''; |
169 $inclusions = ''; |
160 if ( ! empty( $r['include'] ) ) { |
170 if ( ! empty( $r['include'] ) ) { |
161 $r['exclude'] = ''; //ignore exclude, category, and category_name params if using include |
171 $r['exclude'] = ''; //ignore exclude, category, and category_name params if using include |
162 $r['category'] = ''; |
172 $r['category'] = ''; |
163 $r['category_name'] = ''; |
173 $r['category_name'] = ''; |
164 $inclinks = preg_split( '/[\s,]+/', $r['include'] ); |
174 $inclinks = wp_parse_id_list( $r['include'] ); |
165 if ( count( $inclinks ) ) { |
175 if ( count( $inclinks ) ) { |
166 foreach ( $inclinks as $inclink ) { |
176 foreach ( $inclinks as $inclink ) { |
167 if ( empty( $inclusions ) ) { |
177 if ( empty( $inclusions ) ) { |
168 $inclusions = ' AND ( link_id = ' . intval( $inclink ) . ' '; |
178 $inclusions = ' AND ( link_id = ' . $inclink . ' '; |
169 } else { |
179 } else { |
170 $inclusions .= ' OR link_id = ' . intval( $inclink ) . ' '; |
180 $inclusions .= ' OR link_id = ' . $inclink . ' '; |
171 } |
181 } |
172 } |
182 } |
173 } |
183 } |
174 } |
184 } |
175 if (! empty( $inclusions ) ) { |
185 if ( ! empty( $inclusions ) ) { |
176 $inclusions .= ')'; |
186 $inclusions .= ')'; |
177 } |
187 } |
178 |
188 |
179 $exclusions = ''; |
189 $exclusions = ''; |
180 if ( ! empty( $r['exclude'] ) ) { |
190 if ( ! empty( $r['exclude'] ) ) { |
181 $exlinks = preg_split( '/[\s,]+/', $r['exclude'] ); |
191 $exlinks = wp_parse_id_list( $r['exclude'] ); |
182 if ( count( $exlinks ) ) { |
192 if ( count( $exlinks ) ) { |
183 foreach ( $exlinks as $exlink ) { |
193 foreach ( $exlinks as $exlink ) { |
184 if ( empty( $exclusions ) ) { |
194 if ( empty( $exclusions ) ) { |
185 $exclusions = ' AND ( link_id <> ' . intval( $exlink ) . ' '; |
195 $exclusions = ' AND ( link_id <> ' . $exlink . ' '; |
186 } else { |
196 } else { |
187 $exclusions .= ' AND link_id <> ' . intval( $exlink ) . ' '; |
197 $exclusions .= ' AND link_id <> ' . $exlink . ' '; |
188 } |
198 } |
189 } |
199 } |
190 } |
200 } |
191 } |
201 } |
192 if ( ! empty( $exclusions ) ) { |
202 if ( ! empty( $exclusions ) ) { |
193 $exclusions .= ')'; |
203 $exclusions .= ')'; |
194 } |
204 } |
195 |
205 |
196 if ( ! empty( $r['category_name'] ) ) { |
206 if ( ! empty( $r['category_name'] ) ) { |
197 if ( $r['category'] = get_term_by('name', $r['category_name'], 'link_category') ) { |
207 if ( $r['category'] = get_term_by( 'name', $r['category_name'], 'link_category' ) ) { |
198 $r['category'] = $r['category']->term_id; |
208 $r['category'] = $r['category']->term_id; |
199 } else { |
209 } else { |
200 $cache[ $key ] = array(); |
210 $cache[ $key ] = array(); |
201 wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); |
211 wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); |
202 /** This filter is documented in wp-includes/bookmark.php */ |
212 /** This filter is documented in wp-includes/bookmark.php */ |
204 } |
214 } |
205 } |
215 } |
206 |
216 |
207 $search = ''; |
217 $search = ''; |
208 if ( ! empty( $r['search'] ) ) { |
218 if ( ! empty( $r['search'] ) ) { |
209 $like = '%' . $wpdb->esc_like( $r['search'] ) . '%'; |
219 $like = '%' . $wpdb->esc_like( $r['search'] ) . '%'; |
210 $search = $wpdb->prepare(" AND ( (link_url LIKE %s) OR (link_name LIKE %s) OR (link_description LIKE %s) ) ", $like, $like, $like ); |
220 $search = $wpdb->prepare( ' AND ( (link_url LIKE %s) OR (link_name LIKE %s) OR (link_description LIKE %s) ) ', $like, $like, $like ); |
211 } |
221 } |
212 |
222 |
213 $category_query = ''; |
223 $category_query = ''; |
214 $join = ''; |
224 $join = ''; |
215 if ( ! empty( $r['category'] ) ) { |
225 if ( ! empty( $r['category'] ) ) { |
216 $incategories = preg_split( '/[\s,]+/', $r['category'] ); |
226 $incategories = wp_parse_id_list( $r['category'] ); |
217 if ( count($incategories) ) { |
227 if ( count( $incategories ) ) { |
218 foreach ( $incategories as $incat ) { |
228 foreach ( $incategories as $incat ) { |
219 if ( empty( $category_query ) ) { |
229 if ( empty( $category_query ) ) { |
220 $category_query = ' AND ( tt.term_id = ' . intval( $incat ) . ' '; |
230 $category_query = ' AND ( tt.term_id = ' . $incat . ' '; |
221 } else { |
231 } else { |
222 $category_query .= ' OR tt.term_id = ' . intval( $incat ) . ' '; |
232 $category_query .= ' OR tt.term_id = ' . $incat . ' '; |
223 } |
233 } |
224 } |
234 } |
225 } |
235 } |
226 } |
236 } |
227 if ( ! empty( $category_query ) ) { |
237 if ( ! empty( $category_query ) ) { |
228 $category_query .= ") AND taxonomy = 'link_category'"; |
238 $category_query .= ") AND taxonomy = 'link_category'"; |
229 $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"; |
239 $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"; |
230 } |
240 } |
231 |
241 |
232 if ( $r['show_updated'] ) { |
242 if ( $r['show_updated'] ) { |
233 $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated "; |
243 $recently_updated_test = ', IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated '; |
234 } else { |
244 } else { |
235 $recently_updated_test = ''; |
245 $recently_updated_test = ''; |
236 } |
246 } |
237 |
247 |
238 $get_updated = ( $r['show_updated'] ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : ''; |
248 $get_updated = ( $r['show_updated'] ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : ''; |
239 |
249 |
240 $orderby = strtolower( $r['orderby'] ); |
250 $orderby = strtolower( $r['orderby'] ); |
241 $length = ''; |
251 $length = ''; |
242 switch ( $orderby ) { |
252 switch ( $orderby ) { |
243 case 'length': |
253 case 'length': |
244 $length = ", CHAR_LENGTH(link_name) AS length"; |
254 $length = ', CHAR_LENGTH(link_name) AS length'; |
245 break; |
255 break; |
246 case 'rand': |
256 case 'rand': |
247 $orderby = 'rand()'; |
257 $orderby = 'rand()'; |
248 break; |
258 break; |
249 case 'link_id': |
259 case 'link_id': |
250 $orderby = "$wpdb->links.link_id"; |
260 $orderby = "$wpdb->links.link_id"; |
251 break; |
261 break; |
252 default: |
262 default: |
253 $orderparams = array(); |
263 $orderparams = array(); |
254 $keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes', 'link_description' ); |
264 $keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes', 'link_description' ); |
255 foreach ( explode( ',', $orderby ) as $ordparam ) { |
265 foreach ( explode( ',', $orderby ) as $ordparam ) { |
256 $ordparam = trim( $ordparam ); |
266 $ordparam = trim( $ordparam ); |
257 |
267 |
258 if ( in_array( 'link_' . $ordparam, $keys ) ) { |
268 if ( in_array( 'link_' . $ordparam, $keys ) ) { |
259 $orderparams[] = 'link_' . $ordparam; |
269 $orderparams[] = 'link_' . $ordparam; |
301 * |
311 * |
302 * @since 2.3.0 |
312 * @since 2.3.0 |
303 * |
313 * |
304 * @param stdClass|array $bookmark Bookmark row |
314 * @param stdClass|array $bookmark Bookmark row |
305 * @param string $context Optional, default is 'display'. How to filter the |
315 * @param string $context Optional, default is 'display'. How to filter the |
306 * fields |
316 * fields |
307 * @return stdClass|array Same type as $bookmark but with fields sanitized. |
317 * @return stdClass|array Same type as $bookmark but with fields sanitized. |
308 */ |
318 */ |
309 function sanitize_bookmark($bookmark, $context = 'display') { |
319 function sanitize_bookmark( $bookmark, $context = 'display' ) { |
310 $fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category', |
320 $fields = array( |
311 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated', |
321 'link_id', |
312 'link_rel', 'link_notes', 'link_rss', ); |
322 'link_url', |
313 |
323 'link_name', |
314 if ( is_object($bookmark) ) { |
324 'link_image', |
|
325 'link_target', |
|
326 'link_category', |
|
327 'link_description', |
|
328 'link_visible', |
|
329 'link_owner', |
|
330 'link_rating', |
|
331 'link_updated', |
|
332 'link_rel', |
|
333 'link_notes', |
|
334 'link_rss', |
|
335 ); |
|
336 |
|
337 if ( is_object( $bookmark ) ) { |
315 $do_object = true; |
338 $do_object = true; |
316 $link_id = $bookmark->link_id; |
339 $link_id = $bookmark->link_id; |
317 } else { |
340 } else { |
318 $do_object = false; |
341 $do_object = false; |
319 $link_id = $bookmark['link_id']; |
342 $link_id = $bookmark['link_id']; |
320 } |
343 } |
321 |
344 |
322 foreach ( $fields as $field ) { |
345 foreach ( $fields as $field ) { |
323 if ( $do_object ) { |
346 if ( $do_object ) { |
324 if ( isset($bookmark->$field) ) |
347 if ( isset( $bookmark->$field ) ) { |
325 $bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $link_id, $context); |
348 $bookmark->$field = sanitize_bookmark_field( $field, $bookmark->$field, $link_id, $context ); |
|
349 } |
326 } else { |
350 } else { |
327 if ( isset($bookmark[$field]) ) |
351 if ( isset( $bookmark[ $field ] ) ) { |
328 $bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $link_id, $context); |
352 $bookmark[ $field ] = sanitize_bookmark_field( $field, $bookmark[ $field ], $link_id, $context ); |
|
353 } |
329 } |
354 } |
330 } |
355 } |
331 |
356 |
332 return $bookmark; |
357 return $bookmark; |
333 } |
358 } |
356 * 'js', 'db', or 'display' |
381 * 'js', 'db', or 'display' |
357 * @return mixed The filtered value. |
382 * @return mixed The filtered value. |
358 */ |
383 */ |
359 function sanitize_bookmark_field( $field, $value, $bookmark_id, $context ) { |
384 function sanitize_bookmark_field( $field, $value, $bookmark_id, $context ) { |
360 switch ( $field ) { |
385 switch ( $field ) { |
361 case 'link_id' : // ints |
386 case 'link_id': // ints |
362 case 'link_rating' : |
387 case 'link_rating': |
363 $value = (int) $value; |
388 $value = (int) $value; |
364 break; |
389 break; |
365 case 'link_category' : // array( ints ) |
390 case 'link_category': // array( ints ) |
366 $value = array_map('absint', (array) $value); |
391 $value = array_map( 'absint', (array) $value ); |
367 // We return here so that the categories aren't filtered. |
392 // We return here so that the categories aren't filtered. |
368 // The 'link_category' filter is for the name of a link category, not an array of a link's link categories |
393 // The 'link_category' filter is for the name of a link category, not an array of a link's link categories |
|
394 return $value; |
|
395 |
|
396 case 'link_visible': // bool stored as Y|N |
|
397 $value = preg_replace( '/[^YNyn]/', '', $value ); |
|
398 break; |
|
399 case 'link_target': // "enum" |
|
400 $targets = array( '_top', '_blank' ); |
|
401 if ( ! in_array( $value, $targets ) ) { |
|
402 $value = ''; |
|
403 } |
|
404 break; |
|
405 } |
|
406 |
|
407 if ( 'raw' == $context ) { |
369 return $value; |
408 return $value; |
370 |
409 } |
371 case 'link_visible' : // bool stored as Y|N |
|
372 $value = preg_replace('/[^YNyn]/', '', $value); |
|
373 break; |
|
374 case 'link_target' : // "enum" |
|
375 $targets = array('_top', '_blank'); |
|
376 if ( ! in_array($value, $targets) ) |
|
377 $value = ''; |
|
378 break; |
|
379 } |
|
380 |
|
381 if ( 'raw' == $context ) |
|
382 return $value; |
|
383 |
410 |
384 if ( 'edit' == $context ) { |
411 if ( 'edit' == $context ) { |
385 /** This filter is documented in wp-includes/post.php */ |
412 /** This filter is documented in wp-includes/post.php */ |
386 $value = apply_filters( "edit_{$field}", $value, $bookmark_id ); |
413 $value = apply_filters( "edit_{$field}", $value, $bookmark_id ); |
387 |
414 |
388 if ( 'link_notes' == $field ) { |
415 if ( 'link_notes' == $field ) { |
389 $value = esc_html( $value ); // textarea_escaped |
416 $value = esc_html( $value ); // textarea_escaped |
390 } else { |
417 } else { |
391 $value = esc_attr($value); |
418 $value = esc_attr( $value ); |
392 } |
419 } |
393 } elseif ( 'db' == $context ) { |
420 } elseif ( 'db' == $context ) { |
394 /** This filter is documented in wp-includes/post.php */ |
421 /** This filter is documented in wp-includes/post.php */ |
395 $value = apply_filters( "pre_{$field}", $value ); |
422 $value = apply_filters( "pre_{$field}", $value ); |
396 } else { |
423 } else { |