10 * Retrieve Bookmark data based on ID
13 * @uses $wpdb Database Object
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.
20 function get_bookmark($bookmark_id, $output = OBJECT, $filter = 'raw') {
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') );
26 $link = sanitize_bookmark($link, $filter);
28 if ( $output == OBJECT ) {
30 } elseif ( $output == ARRAY_A ) {
31 return get_object_vars($link);
32 } elseif ( $output == ARRAY_N ) {
33 return array_values(get_object_vars($link));
40 * Retrieve single bookmark data item or field.
43 * @uses get_bookmark() Gets bookmark object using $bookmark as ID
44 * @uses sanitize_bookmark_field() Sanitizes Bookmark field based on $context.
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.
51 function get_bookmark_field( $field, $bookmark, $context = 'display' ) {
52 $bookmark = (int) $bookmark;
53 $bookmark = get_bookmark( $bookmark );
55 if ( is_wp_error($bookmark) )
58 if ( !is_object($bookmark) )
61 if ( !isset($bookmark->$field) )
64 return sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
68 * Retrieve bookmark data based on ID.
71 * @deprecated Use get_bookmark()
74 * @param int $bookmark_id ID of link
75 * @param string $output Either OBJECT, ARRAY_N, or ARRAY_A
76 * @return object|array
78 function get_link($bookmark_id, $output = OBJECT, $filter = 'raw') {
79 return get_bookmark($bookmark_id, $output, $filter);
83 * Retrieves the list of bookmarks
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.
89 * List of default arguments are as follows:
90 * 'orderby' - Default is 'name' (string). How to order the links by. String is
91 * based off of the bookmark scheme.
92 * 'order' - Default is 'ASC' (string). Either 'ASC' or 'DESC'. Orders in either
93 * ascending or descending order.
94 * 'limit' - Default is -1 (integer) or show all. The amount of bookmarks to
96 * 'category' - Default is empty string (string). Include the links in what
98 * 'category_name' - Default is empty string (string). Get links by category
100 * 'hide_invisible' - Default is 1 (integer). Whether to show (default) or hide
101 * links marked as 'invisible'.
102 * 'show_updated' - Default is 0 (integer). Will show the time of when the
103 * bookmark was last updated.
104 * 'include' - Default is empty string (string). Include other categories
105 * separated by commas.
106 * 'exclude' - Default is empty string (string). Exclude other categories
107 * separated by commas.
110 * @uses $wpdb Database Object
111 * @link http://codex.wordpress.org/Template_Tags/get_bookmarks
113 * @param string|array $args List of arguments to overwrite the defaults
114 * @return array List of bookmark row objects
116 function get_bookmarks($args = '') {
120 'orderby' => 'name', 'order' => 'ASC',
121 'limit' => -1, 'category' => '',
122 'category_name' => '', 'hide_invisible' => 1,
123 'show_updated' => 0, 'include' => '',
124 'exclude' => '', 'search' => ''
127 $r = wp_parse_args( $args, $defaults );
128 extract( $r, EXTR_SKIP );
130 $key = md5( serialize( $r ) );
131 if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) )
132 if ( isset( $cache[ $key ] ) )
133 return apply_filters('get_bookmarks', $cache[ $key ], $r );
136 if ( !empty($include) ) {
137 $exclude = ''; //ignore exclude, category, and category_name params if using include
140 $inclinks = preg_split('/[\s,]+/',$include);
141 if ( count($inclinks) ) {
142 foreach ( $inclinks as $inclink ) {
143 if (empty($inclusions))
144 $inclusions = ' AND ( link_id = ' . intval($inclink) . ' ';
146 $inclusions .= ' OR link_id = ' . intval($inclink) . ' ';
150 if (!empty($inclusions))
154 if ( !empty($exclude) ) {
155 $exlinks = preg_split('/[\s,]+/',$exclude);
156 if ( count($exlinks) ) {
157 foreach ( $exlinks as $exlink ) {
158 if (empty($exclusions))
159 $exclusions = ' AND ( link_id <> ' . intval($exlink) . ' ';
161 $exclusions .= ' AND link_id <> ' . intval($exlink) . ' ';
165 if (!empty($exclusions))
168 if ( ! empty($category_name) ) {
169 if ( $category = get_term_by('name', $category_name, 'link_category') )
170 $category = $category->term_id;
173 if ( ! empty($search) ) {
174 $search = like_escape($search);
175 $search = " AND ( (link_url LIKE '%$search%') OR (link_name LIKE '%$search%') OR (link_description LIKE '%$search%') ) ";
178 $category_query = '';
180 if ( !empty($category) ) {
181 $incategories = preg_split('/[\s,]+/',$category);
182 if ( count($incategories) ) {
183 foreach ( $incategories as $incat ) {
184 if (empty($category_query))
185 $category_query = ' AND ( tt.term_id = ' . intval($incat) . ' ';
187 $category_query .= ' OR tt.term_id = ' . intval($incat) . ' ';
191 if (!empty($category_query)) {
192 $category_query .= ") AND taxonomy = 'link_category'";
193 $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";
196 if (get_option('links_recently_updated_time')) {
197 $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_option('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
199 $recently_updated_test = '';
202 $get_updated = ( $show_updated ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
204 $orderby = strtolower($orderby);
208 $length = ", CHAR_LENGTH(link_name) AS length";
214 $orderby = "link_" . $orderby;
217 if ( 'link_id' == $orderby )
218 $orderby = "$wpdb->links.link_id";
221 if ( $hide_invisible )
222 $visible = "AND link_visible = 'Y'";
224 $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
225 $query .= " $exclusions $inclusions $search";
226 $query .= " ORDER BY $orderby $order";
228 $query .= " LIMIT $limit";
230 $results = $wpdb->get_results($query);
232 $cache[ $key ] = $results;
233 wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
235 return apply_filters('get_bookmarks', $results, $r);
239 * Sanitizes all bookmark fields
243 * @param object|array $bookmark Bookmark row
244 * @param string $context Optional, default is 'display'. How to filter the
246 * @return object|array Same type as $bookmark but with fields sanitized.
248 function sanitize_bookmark($bookmark, $context = 'display') {
249 $fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category',
250 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated',
251 'link_rel', 'link_notes', 'link_rss', );
254 if ( is_object($bookmark) )
257 foreach ( $fields as $field ) {
259 $bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
261 $bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $bookmark['link_id'], $context);
268 * Sanitizes a bookmark field
270 * Sanitizes the bookmark fields based on what the field name is. If the field
271 * has a strict value set, then it will be tested for that, else a more generic
272 * filtering is applied. After the more strict filter is applied, if the
273 * $context is 'raw' then the value is immediately return.
275 * Hooks exist for the more generic cases. With the 'edit' context, the
276 * 'edit_$field' filter will be called and passed the $value and $bookmark_id
277 * respectively. With the 'db' context, the 'pre_$field' filter is called and
278 * passed the value. The 'display' context is the final context and has the
279 * $field has the filter name and is passed the $value, $bookmark_id, and
280 * $context respectively.
284 * @param string $field The bookmark field
285 * @param mixed $value The bookmark field value
286 * @param int $bookmark_id Bookmark ID
287 * @param string $context How to filter the field value. Either 'raw', 'edit',
288 * 'attribute', 'js', 'db', or 'display'
289 * @return mixed The filtered value
291 function sanitize_bookmark_field($field, $value, $bookmark_id, $context) {
292 $int_fields = array('link_id', 'link_rating');
293 if ( in_array($field, $int_fields) )
294 $value = (int) $value;
296 $yesno = array('link_visible');
297 if ( in_array($field, $yesno) )
298 $value = preg_replace('/[^YNyn]/', '', $value);
300 if ( 'link_target' == $field ) {
301 $targets = array('_top', '_blank');
302 if ( ! in_array($value, $targets) )
306 if ( 'raw' == $context )
309 if ( 'edit' == $context ) {
310 $format_to_edit = array('link_notes');
311 $value = apply_filters("edit_$field", $value, $bookmark_id);
313 if ( in_array($field, $format_to_edit) ) {
314 $value = format_to_edit($value);
316 $value = attribute_escape($value);
318 } else if ( 'db' == $context ) {
319 $value = apply_filters("pre_$field", $value);
321 // Use display filters by default.
322 $value = apply_filters($field, $value, $bookmark_id, $context);
325 if ( 'attribute' == $context )
326 $value = attribute_escape($value);
327 else if ( 'js' == $context )
328 $value = js_escape($value);
334 * Deletes entire bookmark cache
337 * @uses wp_cache_delete() Deletes the contents of 'get_bookmarks'
339 function delete_get_bookmark_cache() {
340 wp_cache_delete( 'get_bookmarks', 'bookmark' );
342 add_action( 'add_link', 'delete_get_bookmark_cache' );
343 add_action( 'edit_link', 'delete_get_bookmark_cache' );
344 add_action( 'delete_link', 'delete_get_bookmark_cache' );