10 * get_bookmark() - Get 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 * get_bookmark_field() - Gets 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 * get_link() - Returns 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 * get_bookmarks() - 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 based off of the bookmark scheme.
91 * 'order' - Default is 'ASC' (string). Either 'ASC' or 'DESC'. Orders in either ascending or descending order.
92 * 'limit' - Default is -1 (integer) or show all. The amount of bookmarks to display.
93 * 'category' - Default is empty string (string). Include the links in what category ID(s).
94 * 'category_name' - Default is empty string (string). Get links by category name.
95 * 'hide_invisible' - Default is 1 (integer). Whether to show (default) or hide links marked as 'invisible'.
96 * 'show_updated' - Default is 0 (integer). Will show the time of when the bookmark was last updated.
97 * 'include' - Default is empty string (string). Include other categories separated by commas.
98 * 'exclude' - Default is empty string (string). Exclude other categories separated by commas.
101 * @uses $wpdb Database Object
103 * @param string|array $args List of arguments to overwrite the defaults
104 * @return array List of bookmark row objects
106 function get_bookmarks($args = '') {
110 'orderby' => 'name', 'order' => 'ASC',
111 'limit' => -1, 'category' => '',
112 'category_name' => '', 'hide_invisible' => 1,
113 'show_updated' => 0, 'include' => '',
114 'exclude' => '', 'search' => ''
117 $r = wp_parse_args( $args, $defaults );
118 extract( $r, EXTR_SKIP );
120 $key = md5( serialize( $r ) );
121 if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) )
122 if ( isset( $cache[ $key ] ) )
123 return apply_filters('get_bookmarks', $cache[ $key ], $r );
126 if ( !empty($include) ) {
127 $exclude = ''; //ignore exclude, category, and category_name params if using include
130 $inclinks = preg_split('/[\s,]+/',$include);
131 if ( count($inclinks) ) {
132 foreach ( $inclinks as $inclink ) {
133 if (empty($inclusions))
134 $inclusions = ' AND ( link_id = ' . intval($inclink) . ' ';
136 $inclusions .= ' OR link_id = ' . intval($inclink) . ' ';
140 if (!empty($inclusions))
144 if ( !empty($exclude) ) {
145 $exlinks = preg_split('/[\s,]+/',$exclude);
146 if ( count($exlinks) ) {
147 foreach ( $exlinks as $exlink ) {
148 if (empty($exclusions))
149 $exclusions = ' AND ( link_id <> ' . intval($exlink) . ' ';
151 $exclusions .= ' AND link_id <> ' . intval($exlink) . ' ';
155 if (!empty($exclusions))
158 if ( ! empty($category_name) ) {
159 if ( $category = get_term_by('name', $category_name, 'link_category') )
160 $category = $category->term_id;
163 if ( ! empty($search) ) {
164 $search = like_escape($search);
165 $search = " AND ( (link_url LIKE '%$search%') OR (link_name LIKE '%$search%') OR (link_description LIKE '%$search%') ) ";
168 $category_query = '';
170 if ( !empty($category) ) {
171 $incategories = preg_split('/[\s,]+/',$category);
172 if ( count($incategories) ) {
173 foreach ( $incategories as $incat ) {
174 if (empty($category_query))
175 $category_query = ' AND ( tt.term_id = ' . intval($incat) . ' ';
177 $category_query .= ' OR tt.term_id = ' . intval($incat) . ' ';
181 if (!empty($category_query)) {
182 $category_query .= ") AND taxonomy = 'link_category'";
183 $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";
186 if (get_option('links_recently_updated_time')) {
187 $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_option('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
189 $recently_updated_test = '';
192 $get_updated = ( $show_updated ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
194 $orderby = strtolower($orderby);
198 $length = ", CHAR_LENGTH(link_name) AS length";
204 $orderby = "link_" . $orderby;
207 if ( 'link_id' == $orderby )
208 $orderby = "$wpdb->links.link_id";
211 if ( $hide_invisible )
212 $visible = "AND link_visible = 'Y'";
214 $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
215 $query .= " $exclusions $inclusions $search";
216 $query .= " ORDER BY $orderby $order";
218 $query .= " LIMIT $limit";
220 $results = $wpdb->get_results($query);
222 $cache[ $key ] = $results;
223 wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
225 return apply_filters('get_bookmarks', $results, $r);
229 * sanitize_bookmark() - Sanitizes all bookmark fields
233 * @param object|array $bookmark Bookmark row
234 * @param string $context Optional, default is 'display'. How to filter the fields
235 * @return object|array Same type as $bookmark but with fields sanitized.
237 function sanitize_bookmark($bookmark, $context = 'display') {
238 $fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category',
239 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated',
240 'link_rel', 'link_notes', 'link_rss', );
243 if ( is_object($bookmark) )
246 foreach ( $fields as $field ) {
248 $bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
250 $bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $bookmark['link_id'], $context);
257 * sanitize_bookmark_field() - Sanitizes a bookmark field
259 * Sanitizes the bookmark fields based on what the field name is. If the field has a
260 * strict value set, then it will be tested for that, else a more generic filtering is
261 * applied. After the more strict filter is applied, if the $context is 'raw' then the
262 * value is immediately return.
264 * Hooks exist for the more generic cases. With the 'edit' context, the 'edit_$field'
265 * filter will be called and passed the $value and $bookmark_id respectively. With the
266 * 'db' context, the 'pre_$field' filter is called and passed the value. The 'display'
267 * context is the final context and has the $field has the filter name and is passed the
268 * $value, $bookmark_id, and $context respectively.
272 * @param string $field The bookmark field
273 * @param mixed $value The bookmark field value
274 * @param int $bookmark_id Bookmark ID
275 * @param string $context How to filter the field value. Either 'raw', 'edit', 'attribute', 'js', 'db', or 'display'
276 * @return mixed The filtered value
278 function sanitize_bookmark_field($field, $value, $bookmark_id, $context) {
279 $int_fields = array('link_id', 'link_rating');
280 if ( in_array($field, $int_fields) )
281 $value = (int) $value;
283 $yesno = array('link_visible');
284 if ( in_array($field, $yesno) )
285 $value = preg_replace('/[^YNyn]/', '', $value);
287 if ( 'link_target' == $field ) {
288 $targets = array('_top', '_blank');
289 if ( ! in_array($value, $targets) )
293 if ( 'raw' == $context )
296 if ( 'edit' == $context ) {
297 $format_to_edit = array('link_notes');
298 $value = apply_filters("edit_$field", $value, $bookmark_id);
300 if ( in_array($field, $format_to_edit) ) {
301 $value = format_to_edit($value);
303 $value = attribute_escape($value);
305 } else if ( 'db' == $context ) {
306 $value = apply_filters("pre_$field", $value);
308 // Use display filters by default.
309 $value = apply_filters($field, $value, $bookmark_id, $context);
312 if ( 'attribute' == $context )
313 $value = attribute_escape($value);
314 else if ( 'js' == $context )
315 $value = js_escape($value);
321 * delete_get_bookmark_cache() - Deletes entire bookmark cache
324 * @uses wp_cache_delete() Deletes the contents of 'get_bookmarks'
326 function delete_get_bookmark_cache() {
327 wp_cache_delete( 'get_bookmarks', 'bookmark' );
329 add_action( 'add_link', 'delete_get_bookmark_cache' );
330 add_action( 'edit_link', 'delete_get_bookmark_cache' );
331 add_action( 'delete_link', 'delete_get_bookmark_cache' );