10 * Retrieve Bookmark data
14 * @global wpdb $wpdb WordPress database abstraction object.
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
18 * an stdClass object, an associative array, or a numeric array, respectively. Default OBJECT.
19 * @param string $filter Optional, default is 'raw'.
20 * @return array|object|null Type returned depends on $output value.
22 function get_bookmark($bookmark, $output = OBJECT, $filter = 'raw') {
25 if ( empty($bookmark) ) {
26 if ( isset($GLOBALS['link']) )
27 $_bookmark = & $GLOBALS['link'];
30 } elseif ( is_object($bookmark) ) {
31 wp_cache_add($bookmark->link_id, $bookmark, 'bookmark');
32 $_bookmark = $bookmark;
34 if ( isset($GLOBALS['link']) && ($GLOBALS['link']->link_id == $bookmark) ) {
35 $_bookmark = & $GLOBALS['link'];
36 } 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));
39 $_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' );
48 $_bookmark = sanitize_bookmark($_bookmark, $filter);
50 if ( $output == OBJECT ) {
52 } elseif ( $output == ARRAY_A ) {
53 return get_object_vars($_bookmark);
54 } elseif ( $output == ARRAY_N ) {
55 return array_values(get_object_vars($_bookmark));
62 * Retrieve single bookmark data item or field.
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|WP_Error
71 function get_bookmark_field( $field, $bookmark, $context = 'display' ) {
72 $bookmark = (int) $bookmark;
73 $bookmark = get_bookmark( $bookmark );
75 if ( is_wp_error($bookmark) )
78 if ( !is_object($bookmark) )
81 if ( !isset($bookmark->$field) )
84 return sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
88 * Retrieves the list of bookmarks
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.
96 * @global wpdb $wpdb WordPress database abstraction object.
98 * @param string|array $args {
99 * Optional. String or array of arguments to retrieve bookmarks.
101 * @type string $orderby How to order the links by. Accepts post fields. Default 'name'.
102 * @type string $order Whether to order bookmarks in ascending or descending order.
103 * Accepts 'ASC' (ascending) or 'DESC' (descending). Default 'ASC'.
104 * @type int $limit Amount of bookmarks to display. Accepts 1+ or -1 for all.
106 * @type string $category Comma-separated list of category ids to include links from.
108 * @type string $category_name Category to retrieve links for by name. Default empty.
109 * @type int|bool $hide_invisible Whether to show or hide links marked as 'invisible'. Accepts
110 * 1|true or 0|false. Default 1|true.
111 * @type int|bool $show_updated Whether to display the time the bookmark was last updated.
112 * Accepts 1|true or 0|false. Default 0|false.
113 * @type string $include Comma-separated list of bookmark IDs to include. Default empty.
114 * @type string $exclude Comma-separated list of bookmark IDs to exclude. Default empty.
116 * @return array List of bookmark row objects.
118 function get_bookmarks( $args = '' ) {
122 'orderby' => 'name', 'order' => 'ASC',
123 'limit' => -1, 'category' => '',
124 'category_name' => '', 'hide_invisible' => 1,
125 'show_updated' => 0, 'include' => '',
126 'exclude' => '', 'search' => ''
129 $r = wp_parse_args( $args, $defaults );
131 $key = md5( serialize( $r ) );
133 if ( 'rand' !== $r['orderby'] && $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) {
134 if ( is_array( $cache ) && isset( $cache[ $key ] ) ) {
135 $bookmarks = $cache[ $key ];
137 * Filters the returned list of bookmarks.
139 * The first time the hook is evaluated in this file, it returns the cached
140 * bookmarks list. The second evaluation returns a cached bookmarks list if the
141 * link category is passed but does not exist. The third evaluation returns
142 * the full cached results.
146 * @see get_bookmarks()
148 * @param array $bookmarks List of the cached bookmarks.
149 * @param array $r An array of bookmark query arguments.
151 return apply_filters( 'get_bookmarks', $bookmarks, $r );
155 if ( ! is_array( $cache ) ) {
160 if ( ! empty( $r['include'] ) ) {
161 $r['exclude'] = ''; //ignore exclude, category, and category_name params if using include
163 $r['category_name'] = '';
164 $inclinks = preg_split( '/[\s,]+/', $r['include'] );
165 if ( count( $inclinks ) ) {
166 foreach ( $inclinks as $inclink ) {
167 if ( empty( $inclusions ) ) {
168 $inclusions = ' AND ( link_id = ' . intval( $inclink ) . ' ';
170 $inclusions .= ' OR link_id = ' . intval( $inclink ) . ' ';
175 if (! empty( $inclusions ) ) {
180 if ( ! empty( $r['exclude'] ) ) {
181 $exlinks = preg_split( '/[\s,]+/', $r['exclude'] );
182 if ( count( $exlinks ) ) {
183 foreach ( $exlinks as $exlink ) {
184 if ( empty( $exclusions ) ) {
185 $exclusions = ' AND ( link_id <> ' . intval( $exlink ) . ' ';
187 $exclusions .= ' AND link_id <> ' . intval( $exlink ) . ' ';
192 if ( ! empty( $exclusions ) ) {
196 if ( ! empty( $r['category_name'] ) ) {
197 if ( $r['category'] = get_term_by('name', $r['category_name'], 'link_category') ) {
198 $r['category'] = $r['category']->term_id;
200 $cache[ $key ] = array();
201 wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
202 /** This filter is documented in wp-includes/bookmark.php */
203 return apply_filters( 'get_bookmarks', array(), $r );
208 if ( ! empty( $r['search'] ) ) {
209 $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 );
213 $category_query = '';
215 if ( ! empty( $r['category'] ) ) {
216 $incategories = preg_split( '/[\s,]+/', $r['category'] );
217 if ( count($incategories) ) {
218 foreach ( $incategories as $incat ) {
219 if ( empty( $category_query ) ) {
220 $category_query = ' AND ( tt.term_id = ' . intval( $incat ) . ' ';
222 $category_query .= ' OR tt.term_id = ' . intval( $incat ) . ' ';
227 if ( ! empty( $category_query ) ) {
228 $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";
232 if ( $r['show_updated'] ) {
233 $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated ";
235 $recently_updated_test = '';
238 $get_updated = ( $r['show_updated'] ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
240 $orderby = strtolower( $r['orderby'] );
242 switch ( $orderby ) {
244 $length = ", CHAR_LENGTH(link_name) AS length";
250 $orderby = "$wpdb->links.link_id";
253 $orderparams = array();
254 $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 ) {
256 $ordparam = trim( $ordparam );
258 if ( in_array( 'link_' . $ordparam, $keys ) ) {
259 $orderparams[] = 'link_' . $ordparam;
260 } elseif ( in_array( $ordparam, $keys ) ) {
261 $orderparams[] = $ordparam;
264 $orderby = implode( ',', $orderparams );
267 if ( empty( $orderby ) ) {
268 $orderby = 'link_name';
271 $order = strtoupper( $r['order'] );
272 if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) {
277 if ( $r['hide_invisible'] ) {
278 $visible = "AND link_visible = 'Y'";
281 $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
282 $query .= " $exclusions $inclusions $search";
283 $query .= " ORDER BY $orderby $order";
284 if ( $r['limit'] != -1 ) {
285 $query .= ' LIMIT ' . $r['limit'];
288 $results = $wpdb->get_results( $query );
290 if ( 'rand()' !== $orderby ) {
291 $cache[ $key ] = $results;
292 wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
295 /** This filter is documented in wp-includes/bookmark.php */
296 return apply_filters( 'get_bookmarks', $results, $r );
300 * Sanitizes all bookmark fields
304 * @param stdClass|array $bookmark Bookmark row
305 * @param string $context Optional, default is 'display'. How to filter the
307 * @return stdClass|array Same type as $bookmark but with fields sanitized.
309 function sanitize_bookmark($bookmark, $context = 'display') {
310 $fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category',
311 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated',
312 'link_rel', 'link_notes', 'link_rss', );
314 if ( is_object($bookmark) ) {
316 $link_id = $bookmark->link_id;
319 $link_id = $bookmark['link_id'];
322 foreach ( $fields as $field ) {
324 if ( isset($bookmark->$field) )
325 $bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $link_id, $context);
327 if ( isset($bookmark[$field]) )
328 $bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $link_id, $context);
336 * Sanitizes a bookmark field.
338 * Sanitizes the bookmark fields based on what the field name is. If the field
339 * has a strict value set, then it will be tested for that, else a more generic
340 * filtering is applied. After the more strict filter is applied, if the `$context`
341 * is 'raw' then the value is immediately return.
343 * Hooks exist for the more generic cases. With the 'edit' context, the {@see 'edit_$field'}
344 * filter will be called and passed the `$value` and `$bookmark_id` respectively.
346 * With the 'db' context, the {@see 'pre_$field'} filter is called and passed the value.
347 * The 'display' context is the final context and has the `$field` has the filter name
348 * and is passed the `$value`, `$bookmark_id`, and `$context`, respectively.
352 * @param string $field The bookmark field.
353 * @param mixed $value The bookmark field value.
354 * @param int $bookmark_id Bookmark ID.
355 * @param string $context How to filter the field value. Accepts 'raw', 'edit', 'attribute',
356 * 'js', 'db', or 'display'
357 * @return mixed The filtered value.
359 function sanitize_bookmark_field( $field, $value, $bookmark_id, $context ) {
361 case 'link_id' : // ints
363 $value = (int) $value;
365 case 'link_category' : // array( ints )
366 $value = array_map('absint', (array) $value);
367 // 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
371 case 'link_visible' : // bool stored as Y|N
372 $value = preg_replace('/[^YNyn]/', '', $value);
374 case 'link_target' : // "enum"
375 $targets = array('_top', '_blank');
376 if ( ! in_array($value, $targets) )
381 if ( 'raw' == $context )
384 if ( 'edit' == $context ) {
385 /** This filter is documented in wp-includes/post.php */
386 $value = apply_filters( "edit_$field", $value, $bookmark_id );
388 if ( 'link_notes' == $field ) {
389 $value = esc_html( $value ); // textarea_escaped
391 $value = esc_attr($value);
393 } elseif ( 'db' == $context ) {
394 /** This filter is documented in wp-includes/post.php */
395 $value = apply_filters( "pre_$field", $value );
397 /** This filter is documented in wp-includes/post.php */
398 $value = apply_filters( $field, $value, $bookmark_id, $context );
400 if ( 'attribute' == $context ) {
401 $value = esc_attr( $value );
402 } elseif ( 'js' == $context ) {
403 $value = esc_js( $value );
411 * Deletes the bookmark cache.
415 * @param int $bookmark_id Bookmark ID.
417 function clean_bookmark_cache( $bookmark_id ) {
418 wp_cache_delete( $bookmark_id, 'bookmark' );
419 wp_cache_delete( 'get_bookmarks', 'bookmark' );
420 clean_object_term_cache( $bookmark_id, 'link');