10 * Retrieve Bookmark data
14 * @global wpdb $wpdb WordPress database abstraction object.
16 * @param int|stdClass $bookmark
17 * @param string $output Optional. Either OBJECT, ARRAY_N, or ARRAY_A constant
18 * @param string $filter Optional, default is 'raw'.
19 * @return array|object|null Type returned depends on $output value.
21 function get_bookmark($bookmark, $output = OBJECT, $filter = 'raw') {
24 if ( empty($bookmark) ) {
25 if ( isset($GLOBALS['link']) )
26 $_bookmark = & $GLOBALS['link'];
29 } elseif ( is_object($bookmark) ) {
30 wp_cache_add($bookmark->link_id, $bookmark, 'bookmark');
31 $_bookmark = $bookmark;
33 if ( isset($GLOBALS['link']) && ($GLOBALS['link']->link_id == $bookmark) ) {
34 $_bookmark = & $GLOBALS['link'];
35 } elseif ( ! $_bookmark = wp_cache_get($bookmark, 'bookmark') ) {
36 $_bookmark = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark));
38 $_bookmark->link_category = array_unique( wp_get_object_terms( $_bookmark->link_id, 'link_category', array( 'fields' => 'ids' ) ) );
39 wp_cache_add( $_bookmark->link_id, $_bookmark, 'bookmark' );
47 $_bookmark = sanitize_bookmark($_bookmark, $filter);
49 if ( $output == OBJECT ) {
51 } elseif ( $output == ARRAY_A ) {
52 return get_object_vars($_bookmark);
53 } elseif ( $output == ARRAY_N ) {
54 return array_values(get_object_vars($_bookmark));
61 * Retrieve single bookmark data item or field.
65 * @param string $field The name of the data field to return
66 * @param int $bookmark The bookmark ID to get field
67 * @param string $context Optional. The context of how the field will be used.
68 * @return string|WP_Error
70 function get_bookmark_field( $field, $bookmark, $context = 'display' ) {
71 $bookmark = (int) $bookmark;
72 $bookmark = get_bookmark( $bookmark );
74 if ( is_wp_error($bookmark) )
77 if ( !is_object($bookmark) )
80 if ( !isset($bookmark->$field) )
83 return sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
87 * Retrieves the list of bookmarks
89 * Attempts to retrieve from the cache first based on MD5 hash of arguments. If
90 * that fails, then the query will be built from the arguments and executed. The
91 * results will be stored to the cache.
95 * @global wpdb $wpdb WordPress database abstraction object.
97 * @param string|array $args {
98 * Optional. String or array of arguments to retrieve bookmarks.
100 * @type string $orderby How to order the links by. Accepts post fields. Default 'name'.
101 * @type string $order Whether to order bookmarks in ascending or descending order.
102 * Accepts 'ASC' (ascending) or 'DESC' (descending). Default 'ASC'.
103 * @type int $limit Amount of bookmarks to display. Accepts 1+ or -1 for all.
105 * @type string $category Comma-separated list of category ids to include links from.
107 * @type string $category_name Category to retrieve links for by name. Default empty.
108 * @type int|bool $hide_invisible Whether to show or hide links marked as 'invisible'. Accepts
109 * 1|true or 0|false. Default 1|true.
110 * @type int|bool $show_updated Whether to display the time the bookmark was last updated.
111 * Accepts 1|true or 0|false. Default 0|false.
112 * @type string $include Comma-separated list of bookmark IDs to include. Default empty.
113 * @type string $exclude Comma-separated list of bookmark IDs to exclude. Default empty.
115 * @return array List of bookmark row objects.
117 function get_bookmarks( $args = '' ) {
121 'orderby' => 'name', 'order' => 'ASC',
122 'limit' => -1, 'category' => '',
123 'category_name' => '', 'hide_invisible' => 1,
124 'show_updated' => 0, 'include' => '',
125 'exclude' => '', 'search' => ''
128 $r = wp_parse_args( $args, $defaults );
130 $key = md5( serialize( $r ) );
132 if ( 'rand' !== $r['orderby'] && $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) {
133 if ( is_array( $cache ) && isset( $cache[ $key ] ) ) {
134 $bookmarks = $cache[ $key ];
136 * Filters the returned list of bookmarks.
138 * The first time the hook is evaluated in this file, it returns the cached
139 * bookmarks list. The second evaluation returns a cached bookmarks list if the
140 * link category is passed but does not exist. The third evaluation returns
141 * the full cached results.
145 * @see get_bookmarks()
147 * @param array $bookmarks List of the cached bookmarks.
148 * @param array $r An array of bookmark query arguments.
150 return apply_filters( 'get_bookmarks', $bookmarks, $r );
154 if ( ! is_array( $cache ) ) {
159 if ( ! empty( $r['include'] ) ) {
160 $r['exclude'] = ''; //ignore exclude, category, and category_name params if using include
162 $r['category_name'] = '';
163 $inclinks = preg_split( '/[\s,]+/', $r['include'] );
164 if ( count( $inclinks ) ) {
165 foreach ( $inclinks as $inclink ) {
166 if ( empty( $inclusions ) ) {
167 $inclusions = ' AND ( link_id = ' . intval( $inclink ) . ' ';
169 $inclusions .= ' OR link_id = ' . intval( $inclink ) . ' ';
174 if (! empty( $inclusions ) ) {
179 if ( ! empty( $r['exclude'] ) ) {
180 $exlinks = preg_split( '/[\s,]+/', $r['exclude'] );
181 if ( count( $exlinks ) ) {
182 foreach ( $exlinks as $exlink ) {
183 if ( empty( $exclusions ) ) {
184 $exclusions = ' AND ( link_id <> ' . intval( $exlink ) . ' ';
186 $exclusions .= ' AND link_id <> ' . intval( $exlink ) . ' ';
191 if ( ! empty( $exclusions ) ) {
195 if ( ! empty( $r['category_name'] ) ) {
196 if ( $r['category'] = get_term_by('name', $r['category_name'], 'link_category') ) {
197 $r['category'] = $r['category']->term_id;
199 $cache[ $key ] = array();
200 wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
201 /** This filter is documented in wp-includes/bookmark.php */
202 return apply_filters( 'get_bookmarks', array(), $r );
207 if ( ! empty( $r['search'] ) ) {
208 $like = '%' . $wpdb->esc_like( $r['search'] ) . '%';
209 $search = $wpdb->prepare(" AND ( (link_url LIKE %s) OR (link_name LIKE %s) OR (link_description LIKE %s) ) ", $like, $like, $like );
212 $category_query = '';
214 if ( ! empty( $r['category'] ) ) {
215 $incategories = preg_split( '/[\s,]+/', $r['category'] );
216 if ( count($incategories) ) {
217 foreach ( $incategories as $incat ) {
218 if ( empty( $category_query ) ) {
219 $category_query = ' AND ( tt.term_id = ' . intval( $incat ) . ' ';
221 $category_query .= ' OR tt.term_id = ' . intval( $incat ) . ' ';
226 if ( ! empty( $category_query ) ) {
227 $category_query .= ") AND taxonomy = 'link_category'";
228 $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";
231 if ( $r['show_updated'] ) {
232 $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated ";
234 $recently_updated_test = '';
237 $get_updated = ( $r['show_updated'] ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
239 $orderby = strtolower( $r['orderby'] );
241 switch ( $orderby ) {
243 $length = ", CHAR_LENGTH(link_name) AS length";
249 $orderby = "$wpdb->links.link_id";
252 $orderparams = array();
253 $keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes', 'link_description' );
254 foreach ( explode( ',', $orderby ) as $ordparam ) {
255 $ordparam = trim( $ordparam );
257 if ( in_array( 'link_' . $ordparam, $keys ) ) {
258 $orderparams[] = 'link_' . $ordparam;
259 } elseif ( in_array( $ordparam, $keys ) ) {
260 $orderparams[] = $ordparam;
263 $orderby = implode( ',', $orderparams );
266 if ( empty( $orderby ) ) {
267 $orderby = 'link_name';
270 $order = strtoupper( $r['order'] );
271 if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) {
276 if ( $r['hide_invisible'] ) {
277 $visible = "AND link_visible = 'Y'";
280 $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
281 $query .= " $exclusions $inclusions $search";
282 $query .= " ORDER BY $orderby $order";
283 if ( $r['limit'] != -1 ) {
284 $query .= ' LIMIT ' . $r['limit'];
287 $results = $wpdb->get_results( $query );
289 if ( 'rand()' !== $orderby ) {
290 $cache[ $key ] = $results;
291 wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
294 /** This filter is documented in wp-includes/bookmark.php */
295 return apply_filters( 'get_bookmarks', $results, $r );
299 * Sanitizes all bookmark fields
303 * @param object|array $bookmark Bookmark row
304 * @param string $context Optional, default is 'display'. How to filter the
306 * @return object|array Same type as $bookmark but with fields sanitized.
308 function sanitize_bookmark($bookmark, $context = 'display') {
309 $fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category',
310 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated',
311 'link_rel', 'link_notes', 'link_rss', );
313 if ( is_object($bookmark) ) {
315 $link_id = $bookmark->link_id;
318 $link_id = $bookmark['link_id'];
321 foreach ( $fields as $field ) {
323 if ( isset($bookmark->$field) )
324 $bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $link_id, $context);
326 if ( isset($bookmark[$field]) )
327 $bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $link_id, $context);
335 * Sanitizes a bookmark field.
337 * Sanitizes the bookmark fields based on what the field name is. If the field
338 * has a strict value set, then it will be tested for that, else a more generic
339 * filtering is applied. After the more strict filter is applied, if the `$context`
340 * is 'raw' then the value is immediately return.
342 * Hooks exist for the more generic cases. With the 'edit' context, the {@see 'edit_$field'}
343 * filter will be called and passed the `$value` and `$bookmark_id` respectively.
345 * With the 'db' context, the {@see 'pre_$field'} filter is called and passed the value.
346 * The 'display' context is the final context and has the `$field` has the filter name
347 * and is passed the `$value`, `$bookmark_id`, and `$context`, respectively.
351 * @param string $field The bookmark field.
352 * @param mixed $value The bookmark field value.
353 * @param int $bookmark_id Bookmark ID.
354 * @param string $context How to filter the field value. Accepts 'raw', 'edit', 'attribute',
355 * 'js', 'db', or 'display'
356 * @return mixed The filtered value.
358 function sanitize_bookmark_field( $field, $value, $bookmark_id, $context ) {
360 case 'link_id' : // ints
362 $value = (int) $value;
364 case 'link_category' : // array( ints )
365 $value = array_map('absint', (array) $value);
366 // We return here so that the categories aren't filtered.
367 // The 'link_category' filter is for the name of a link category, not an array of a link's link categories
370 case 'link_visible' : // bool stored as Y|N
371 $value = preg_replace('/[^YNyn]/', '', $value);
373 case 'link_target' : // "enum"
374 $targets = array('_top', '_blank');
375 if ( ! in_array($value, $targets) )
380 if ( 'raw' == $context )
383 if ( 'edit' == $context ) {
384 /** This filter is documented in wp-includes/post.php */
385 $value = apply_filters( "edit_$field", $value, $bookmark_id );
387 if ( 'link_notes' == $field ) {
388 $value = esc_html( $value ); // textarea_escaped
390 $value = esc_attr($value);
392 } elseif ( 'db' == $context ) {
393 /** This filter is documented in wp-includes/post.php */
394 $value = apply_filters( "pre_$field", $value );
396 /** This filter is documented in wp-includes/post.php */
397 $value = apply_filters( $field, $value, $bookmark_id, $context );
399 if ( 'attribute' == $context ) {
400 $value = esc_attr( $value );
401 } elseif ( 'js' == $context ) {
402 $value = esc_js( $value );
410 * Deletes the bookmark cache.
414 * @param int $bookmark_id Bookmark ID.
416 function clean_bookmark_cache( $bookmark_id ) {
417 wp_cache_delete( $bookmark_id, 'bookmark' );
418 wp_cache_delete( 'get_bookmarks', 'bookmark' );
419 clean_object_term_cache( $bookmark_id, 'link');