12 * @since 2.1.0 |
12 * @since 2.1.0 |
13 * |
13 * |
14 * @global wpdb $wpdb WordPress database abstraction object. |
14 * @global wpdb $wpdb WordPress database abstraction object. |
15 * |
15 * |
16 * @param int|stdClass $bookmark |
16 * @param int|stdClass $bookmark |
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 |
18 * an stdClass object, an associative array, or a numeric array, respectively. Default OBJECT. |
18 * correspond to an stdClass object, an associative array, or a numeric array, |
19 * @param string $filter Optional, default is 'raw'. |
19 * respectively. Default OBJECT. |
|
20 * @param string $filter Optional. How to sanitize bookmark fields. Default 'raw'. |
20 * @return array|object|null Type returned depends on $output value. |
21 * @return array|object|null Type returned depends on $output value. |
21 */ |
22 */ |
22 function get_bookmark( $bookmark, $output = OBJECT, $filter = 'raw' ) { |
23 function get_bookmark( $bookmark, $output = OBJECT, $filter = 'raw' ) { |
23 global $wpdb; |
24 global $wpdb; |
24 |
25 |
32 wp_cache_add( $bookmark->link_id, $bookmark, 'bookmark' ); |
33 wp_cache_add( $bookmark->link_id, $bookmark, 'bookmark' ); |
33 $_bookmark = $bookmark; |
34 $_bookmark = $bookmark; |
34 } else { |
35 } else { |
35 if ( isset( $GLOBALS['link'] ) && ( $GLOBALS['link']->link_id == $bookmark ) ) { |
36 if ( isset( $GLOBALS['link'] ) && ( $GLOBALS['link']->link_id == $bookmark ) ) { |
36 $_bookmark = & $GLOBALS['link']; |
37 $_bookmark = & $GLOBALS['link']; |
37 } elseif ( ! $_bookmark = wp_cache_get( $bookmark, 'bookmark' ) ) { |
38 } else { |
38 $_bookmark = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark ) ); |
39 $_bookmark = wp_cache_get( $bookmark, 'bookmark' ); |
39 if ( $_bookmark ) { |
40 if ( ! $_bookmark ) { |
40 $_bookmark->link_category = array_unique( wp_get_object_terms( $_bookmark->link_id, 'link_category', array( 'fields' => 'ids' ) ) ); |
41 $_bookmark = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark ) ); |
41 wp_cache_add( $_bookmark->link_id, $_bookmark, 'bookmark' ); |
42 if ( $_bookmark ) { |
|
43 $_bookmark->link_category = array_unique( wp_get_object_terms( $_bookmark->link_id, 'link_category', array( 'fields' => 'ids' ) ) ); |
|
44 wp_cache_add( $_bookmark->link_id, $_bookmark, 'bookmark' ); |
|
45 } |
42 } |
46 } |
43 } |
47 } |
44 } |
48 } |
45 |
49 |
46 if ( ! $_bookmark ) { |
50 if ( ! $_bookmark ) { |
47 return $_bookmark; |
51 return $_bookmark; |
48 } |
52 } |
49 |
53 |
50 $_bookmark = sanitize_bookmark( $_bookmark, $filter ); |
54 $_bookmark = sanitize_bookmark( $_bookmark, $filter ); |
51 |
55 |
52 if ( $output == OBJECT ) { |
56 if ( OBJECT == $output ) { |
53 return $_bookmark; |
57 return $_bookmark; |
54 } elseif ( $output == ARRAY_A ) { |
58 } elseif ( ARRAY_A == $output ) { |
55 return get_object_vars( $_bookmark ); |
59 return get_object_vars( $_bookmark ); |
56 } elseif ( $output == ARRAY_N ) { |
60 } elseif ( ARRAY_N == $output ) { |
57 return array_values( get_object_vars( $_bookmark ) ); |
61 return array_values( get_object_vars( $_bookmark ) ); |
58 } else { |
62 } else { |
59 return $_bookmark; |
63 return $_bookmark; |
60 } |
64 } |
61 } |
65 } |
63 /** |
67 /** |
64 * Retrieve single bookmark data item or field. |
68 * Retrieve single bookmark data item or field. |
65 * |
69 * |
66 * @since 2.3.0 |
70 * @since 2.3.0 |
67 * |
71 * |
68 * @param string $field The name of the data field to return |
72 * @param string $field The name of the data field to return. |
69 * @param int $bookmark The bookmark ID to get field |
73 * @param int $bookmark The bookmark ID to get field. |
70 * @param string $context Optional. The context of how the field will be used. |
74 * @param string $context Optional. The context of how the field will be used. |
71 * @return string|WP_Error |
75 * @return string|WP_Error |
72 */ |
76 */ |
73 function get_bookmark_field( $field, $bookmark, $context = 'display' ) { |
77 function get_bookmark_field( $field, $bookmark, $context = 'display' ) { |
74 $bookmark = (int) $bookmark; |
78 $bookmark = (int) $bookmark; |
75 $bookmark = get_bookmark( $bookmark ); |
79 $bookmark = get_bookmark( $bookmark ); |
101 * @global wpdb $wpdb WordPress database abstraction object. |
105 * @global wpdb $wpdb WordPress database abstraction object. |
102 * |
106 * |
103 * @param string|array $args { |
107 * @param string|array $args { |
104 * Optional. String or array of arguments to retrieve bookmarks. |
108 * Optional. String or array of arguments to retrieve bookmarks. |
105 * |
109 * |
106 * @type string $orderby How to order the links by. Accepts post fields. Default 'name'. |
110 * @type string $orderby How to order the links by. Accepts 'id', 'link_id', 'name', 'link_name', |
|
111 * 'url', 'link_url', 'visible', 'link_visible', 'rating', 'link_rating', |
|
112 * 'owner', 'link_owner', 'updated', 'link_updated', 'notes', 'link_notes', |
|
113 * 'description', 'link_description', 'length' and 'rand'. |
|
114 * When `$orderby` is 'length', orders by the character length of |
|
115 * 'link_name'. Default 'name'. |
107 * @type string $order Whether to order bookmarks in ascending or descending order. |
116 * @type string $order Whether to order bookmarks in ascending or descending order. |
108 * Accepts 'ASC' (ascending) or 'DESC' (descending). Default 'ASC'. |
117 * Accepts 'ASC' (ascending) or 'DESC' (descending). Default 'ASC'. |
109 * @type int $limit Amount of bookmarks to display. Accepts 1+ or -1 for all. |
118 * @type int $limit Amount of bookmarks to display. Accepts any positive number or |
110 * Default -1. |
119 * -1 for all. Default -1. |
111 * @type string $category Comma-separated list of category ids to include links from. |
120 * @type string $category Comma-separated list of category IDs to include links from. |
112 * Default empty. |
121 * Default empty. |
113 * @type string $category_name Category to retrieve links for by name. Default empty. |
122 * @type string $category_name Category to retrieve links for by name. Default empty. |
114 * @type int|bool $hide_invisible Whether to show or hide links marked as 'invisible'. Accepts |
123 * @type int|bool $hide_invisible Whether to show or hide links marked as 'invisible'. Accepts |
115 * 1|true or 0|false. Default 1|true. |
124 * 1|true or 0|false. Default 1|true. |
116 * @type int|bool $show_updated Whether to display the time the bookmark was last updated. |
125 * @type int|bool $show_updated Whether to display the time the bookmark was last updated. |
117 * Accepts 1|true or 0|false. Default 0|false. |
126 * Accepts 1|true or 0|false. Default 0|false. |
118 * @type string $include Comma-separated list of bookmark IDs to include. Default empty. |
127 * @type string $include Comma-separated list of bookmark IDs to include. Default empty. |
119 * @type string $exclude Comma-separated list of bookmark IDs to exclude. Default empty. |
128 * @type string $exclude Comma-separated list of bookmark IDs to exclude. Default empty. |
|
129 * @type string $search Search terms. Will be SQL-formatted with wildcards before and after |
|
130 * and searched in 'link_url', 'link_name' and 'link_description'. |
|
131 * Default empty. |
120 * } |
132 * } |
121 * @return array List of bookmark row objects. |
133 * @return object[] List of bookmark row objects. |
122 */ |
134 */ |
123 function get_bookmarks( $args = '' ) { |
135 function get_bookmarks( $args = '' ) { |
124 global $wpdb; |
136 global $wpdb; |
125 |
137 |
126 $defaults = array( |
138 $defaults = array( |
153 * |
166 * |
154 * @since 2.1.0 |
167 * @since 2.1.0 |
155 * |
168 * |
156 * @see get_bookmarks() |
169 * @see get_bookmarks() |
157 * |
170 * |
158 * @param array $bookmarks List of the cached bookmarks. |
171 * @param array $bookmarks List of the cached bookmarks. |
159 * @param array $r An array of bookmark query arguments. |
172 * @param array $parsed_args An array of bookmark query arguments. |
160 */ |
173 */ |
161 return apply_filters( 'get_bookmarks', $bookmarks, $r ); |
174 return apply_filters( 'get_bookmarks', $bookmarks, $parsed_args ); |
162 } |
175 } |
163 } |
176 } |
164 |
177 |
165 if ( ! is_array( $cache ) ) { |
178 if ( ! is_array( $cache ) ) { |
166 $cache = array(); |
179 $cache = array(); |
167 } |
180 } |
168 |
181 |
169 $inclusions = ''; |
182 $inclusions = ''; |
170 if ( ! empty( $r['include'] ) ) { |
183 if ( ! empty( $parsed_args['include'] ) ) { |
171 $r['exclude'] = ''; //ignore exclude, category, and category_name params if using include |
184 $parsed_args['exclude'] = ''; // Ignore exclude, category, and category_name params if using include. |
172 $r['category'] = ''; |
185 $parsed_args['category'] = ''; |
173 $r['category_name'] = ''; |
186 $parsed_args['category_name'] = ''; |
174 $inclinks = wp_parse_id_list( $r['include'] ); |
187 |
|
188 $inclinks = wp_parse_id_list( $parsed_args['include'] ); |
175 if ( count( $inclinks ) ) { |
189 if ( count( $inclinks ) ) { |
176 foreach ( $inclinks as $inclink ) { |
190 foreach ( $inclinks as $inclink ) { |
177 if ( empty( $inclusions ) ) { |
191 if ( empty( $inclusions ) ) { |
178 $inclusions = ' AND ( link_id = ' . $inclink . ' '; |
192 $inclusions = ' AND ( link_id = ' . $inclink . ' '; |
179 } else { |
193 } else { |
201 } |
215 } |
202 if ( ! empty( $exclusions ) ) { |
216 if ( ! empty( $exclusions ) ) { |
203 $exclusions .= ')'; |
217 $exclusions .= ')'; |
204 } |
218 } |
205 |
219 |
206 if ( ! empty( $r['category_name'] ) ) { |
220 if ( ! empty( $parsed_args['category_name'] ) ) { |
207 if ( $r['category'] = get_term_by( 'name', $r['category_name'], 'link_category' ) ) { |
221 $parsed_args['category'] = get_term_by( 'name', $parsed_args['category_name'], 'link_category' ); |
208 $r['category'] = $r['category']->term_id; |
222 if ( $parsed_args['category'] ) { |
|
223 $parsed_args['category'] = $parsed_args['category']->term_id; |
209 } else { |
224 } else { |
210 $cache[ $key ] = array(); |
225 $cache[ $key ] = array(); |
211 wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); |
226 wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); |
212 /** This filter is documented in wp-includes/bookmark.php */ |
227 /** This filter is documented in wp-includes/bookmark.php */ |
213 return apply_filters( 'get_bookmarks', array(), $r ); |
228 return apply_filters( 'get_bookmarks', array(), $parsed_args ); |
214 } |
229 } |
215 } |
230 } |
216 |
231 |
217 $search = ''; |
232 $search = ''; |
218 if ( ! empty( $r['search'] ) ) { |
233 if ( ! empty( $parsed_args['search'] ) ) { |
219 $like = '%' . $wpdb->esc_like( $r['search'] ) . '%'; |
234 $like = '%' . $wpdb->esc_like( $parsed_args['search'] ) . '%'; |
220 $search = $wpdb->prepare( ' AND ( (link_url LIKE %s) OR (link_name LIKE %s) OR (link_description LIKE %s) ) ', $like, $like, $like ); |
235 $search = $wpdb->prepare( ' AND ( (link_url LIKE %s) OR (link_name LIKE %s) OR (link_description LIKE %s) ) ', $like, $like, $like ); |
221 } |
236 } |
222 |
237 |
223 $category_query = ''; |
238 $category_query = ''; |
224 $join = ''; |
239 $join = ''; |
225 if ( ! empty( $r['category'] ) ) { |
240 if ( ! empty( $parsed_args['category'] ) ) { |
226 $incategories = wp_parse_id_list( $r['category'] ); |
241 $incategories = wp_parse_id_list( $parsed_args['category'] ); |
227 if ( count( $incategories ) ) { |
242 if ( count( $incategories ) ) { |
228 foreach ( $incategories as $incat ) { |
243 foreach ( $incategories as $incat ) { |
229 if ( empty( $category_query ) ) { |
244 if ( empty( $category_query ) ) { |
230 $category_query = ' AND ( tt.term_id = ' . $incat . ' '; |
245 $category_query = ' AND ( tt.term_id = ' . $incat . ' '; |
231 } else { |
246 } else { |
237 if ( ! empty( $category_query ) ) { |
252 if ( ! empty( $category_query ) ) { |
238 $category_query .= ") AND taxonomy = 'link_category'"; |
253 $category_query .= ") AND taxonomy = 'link_category'"; |
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"; |
254 $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"; |
240 } |
255 } |
241 |
256 |
242 if ( $r['show_updated'] ) { |
257 if ( $parsed_args['show_updated'] ) { |
243 $recently_updated_test = ', IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated '; |
258 $recently_updated_test = ', IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated '; |
244 } else { |
259 } else { |
245 $recently_updated_test = ''; |
260 $recently_updated_test = ''; |
246 } |
261 } |
247 |
262 |
248 $get_updated = ( $r['show_updated'] ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : ''; |
263 $get_updated = ( $parsed_args['show_updated'] ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : ''; |
249 |
264 |
250 $orderby = strtolower( $r['orderby'] ); |
265 $orderby = strtolower( $parsed_args['orderby'] ); |
251 $length = ''; |
266 $length = ''; |
252 switch ( $orderby ) { |
267 switch ( $orderby ) { |
253 case 'length': |
268 case 'length': |
254 $length = ', CHAR_LENGTH(link_name) AS length'; |
269 $length = ', CHAR_LENGTH(link_name) AS length'; |
255 break; |
270 break; |
263 $orderparams = array(); |
278 $orderparams = array(); |
264 $keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes', 'link_description' ); |
279 $keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes', 'link_description' ); |
265 foreach ( explode( ',', $orderby ) as $ordparam ) { |
280 foreach ( explode( ',', $orderby ) as $ordparam ) { |
266 $ordparam = trim( $ordparam ); |
281 $ordparam = trim( $ordparam ); |
267 |
282 |
268 if ( in_array( 'link_' . $ordparam, $keys ) ) { |
283 if ( in_array( 'link_' . $ordparam, $keys, true ) ) { |
269 $orderparams[] = 'link_' . $ordparam; |
284 $orderparams[] = 'link_' . $ordparam; |
270 } elseif ( in_array( $ordparam, $keys ) ) { |
285 } elseif ( in_array( $ordparam, $keys, true ) ) { |
271 $orderparams[] = $ordparam; |
286 $orderparams[] = $ordparam; |
272 } |
287 } |
273 } |
288 } |
274 $orderby = implode( ',', $orderparams ); |
289 $orderby = implode( ',', $orderparams ); |
275 } |
290 } |
276 |
291 |
277 if ( empty( $orderby ) ) { |
292 if ( empty( $orderby ) ) { |
278 $orderby = 'link_name'; |
293 $orderby = 'link_name'; |
279 } |
294 } |
280 |
295 |
281 $order = strtoupper( $r['order'] ); |
296 $order = strtoupper( $parsed_args['order'] ); |
282 if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) { |
297 if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ), true ) ) { |
283 $order = 'ASC'; |
298 $order = 'ASC'; |
284 } |
299 } |
285 |
300 |
286 $visible = ''; |
301 $visible = ''; |
287 if ( $r['hide_invisible'] ) { |
302 if ( $parsed_args['hide_invisible'] ) { |
288 $visible = "AND link_visible = 'Y'"; |
303 $visible = "AND link_visible = 'Y'"; |
289 } |
304 } |
290 |
305 |
291 $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query"; |
306 $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query"; |
292 $query .= " $exclusions $inclusions $search"; |
307 $query .= " $exclusions $inclusions $search"; |
293 $query .= " ORDER BY $orderby $order"; |
308 $query .= " ORDER BY $orderby $order"; |
294 if ( $r['limit'] != -1 ) { |
309 if ( -1 != $parsed_args['limit'] ) { |
295 $query .= ' LIMIT ' . $r['limit']; |
310 $query .= ' LIMIT ' . $parsed_args['limit']; |
296 } |
311 } |
297 |
312 |
298 $results = $wpdb->get_results( $query ); |
313 $results = $wpdb->get_results( $query ); |
299 |
314 |
300 if ( 'rand()' !== $orderby ) { |
315 if ( 'rand()' !== $orderby ) { |
301 $cache[ $key ] = $results; |
316 $cache[ $key ] = $results; |
302 wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); |
317 wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); |
303 } |
318 } |
304 |
319 |
305 /** This filter is documented in wp-includes/bookmark.php */ |
320 /** This filter is documented in wp-includes/bookmark.php */ |
306 return apply_filters( 'get_bookmarks', $results, $r ); |
321 return apply_filters( 'get_bookmarks', $results, $parsed_args ); |
307 } |
322 } |
308 |
323 |
309 /** |
324 /** |
310 * Sanitizes all bookmark fields |
325 * Sanitizes all bookmark fields. |
311 * |
326 * |
312 * @since 2.3.0 |
327 * @since 2.3.0 |
313 * |
328 * |
314 * @param stdClass|array $bookmark Bookmark row |
329 * @param stdClass|array $bookmark Bookmark row. |
315 * @param string $context Optional, default is 'display'. How to filter the |
330 * @param string $context Optional. How to filter the fields. Default 'display'. |
316 * fields |
|
317 * @return stdClass|array Same type as $bookmark but with fields sanitized. |
331 * @return stdClass|array Same type as $bookmark but with fields sanitized. |
318 */ |
332 */ |
319 function sanitize_bookmark( $bookmark, $context = 'display' ) { |
333 function sanitize_bookmark( $bookmark, $context = 'display' ) { |
320 $fields = array( |
334 $fields = array( |
321 'link_id', |
335 'link_id', |
388 $value = (int) $value; |
402 $value = (int) $value; |
389 break; |
403 break; |
390 case 'link_category': // array( ints ) |
404 case 'link_category': // array( ints ) |
391 $value = array_map( 'absint', (array) $value ); |
405 $value = array_map( 'absint', (array) $value ); |
392 // We return here so that the categories aren't filtered. |
406 // We return here so that the categories aren't filtered. |
393 // The 'link_category' filter is for the name of a link category, not an array of a link's link categories |
407 // The 'link_category' filter is for the name of a link category, not an array of a link's link categories. |
394 return $value; |
408 return $value; |
395 |
409 |
396 case 'link_visible': // bool stored as Y|N |
410 case 'link_visible': // bool stored as Y|N |
397 $value = preg_replace( '/[^YNyn]/', '', $value ); |
411 $value = preg_replace( '/[^YNyn]/', '', $value ); |
398 break; |
412 break; |
399 case 'link_target': // "enum" |
413 case 'link_target': // "enum" |
400 $targets = array( '_top', '_blank' ); |
414 $targets = array( '_top', '_blank' ); |
401 if ( ! in_array( $value, $targets ) ) { |
415 if ( ! in_array( $value, $targets, true ) ) { |
402 $value = ''; |
416 $value = ''; |
403 } |
417 } |
404 break; |
418 break; |
405 } |
419 } |
406 |
420 |
407 if ( 'raw' == $context ) { |
421 if ( 'raw' === $context ) { |
408 return $value; |
422 return $value; |
409 } |
423 } |
410 |
424 |
411 if ( 'edit' == $context ) { |
425 if ( 'edit' === $context ) { |
412 /** This filter is documented in wp-includes/post.php */ |
426 /** This filter is documented in wp-includes/post.php */ |
413 $value = apply_filters( "edit_{$field}", $value, $bookmark_id ); |
427 $value = apply_filters( "edit_{$field}", $value, $bookmark_id ); |
414 |
428 |
415 if ( 'link_notes' == $field ) { |
429 if ( 'link_notes' === $field ) { |
416 $value = esc_html( $value ); // textarea_escaped |
430 $value = esc_html( $value ); // textarea_escaped |
417 } else { |
431 } else { |
418 $value = esc_attr( $value ); |
432 $value = esc_attr( $value ); |
419 } |
433 } |
420 } elseif ( 'db' == $context ) { |
434 } elseif ( 'db' === $context ) { |
421 /** This filter is documented in wp-includes/post.php */ |
435 /** This filter is documented in wp-includes/post.php */ |
422 $value = apply_filters( "pre_{$field}", $value ); |
436 $value = apply_filters( "pre_{$field}", $value ); |
423 } else { |
437 } else { |
424 /** This filter is documented in wp-includes/post.php */ |
438 /** This filter is documented in wp-includes/post.php */ |
425 $value = apply_filters( "{$field}", $value, $bookmark_id, $context ); |
439 $value = apply_filters( "{$field}", $value, $bookmark_id, $context ); |
426 |
440 |
427 if ( 'attribute' == $context ) { |
441 if ( 'attribute' === $context ) { |
428 $value = esc_attr( $value ); |
442 $value = esc_attr( $value ); |
429 } elseif ( 'js' == $context ) { |
443 } elseif ( 'js' === $context ) { |
430 $value = esc_js( $value ); |
444 $value = esc_js( $value ); |
431 } |
445 } |
432 } |
446 } |
433 |
447 |
434 return $value; |
448 return $value; |