3 function get_bookmark($bookmark_id, $output = OBJECT, $filter = 'raw') {
6 $bookmark_id = (int) $bookmark_id;
7 $link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$bookmark_id' LIMIT 1");
8 $link->link_category = array_unique( wp_get_object_terms($link_id, 'link_category', 'fields=ids') );
10 $link = sanitize_bookmark($link, $filter);
12 if ( $output == OBJECT ) {
14 } elseif ( $output == ARRAY_A ) {
15 return get_object_vars($link);
16 } elseif ( $output == ARRAY_N ) {
17 return array_values(get_object_vars($link));
23 function get_bookmark_field( $field, $bookmark, $context = 'display' ) {
24 $bookmark = (int) $bookmark;
25 $bookmark = get_bookmark( $bookmark );
27 if ( is_wp_error($bookmark) )
30 if ( !is_object($bookmark) )
33 if ( !isset($bookmark->$field) )
36 return sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
40 function get_link($bookmark_id, $output = OBJECT) {
41 return get_bookmark($bookmark_id, $output);
44 function get_bookmarks($args = '') {
48 'orderby' => 'name', 'order' => 'ASC',
49 'limit' => -1, 'category' => '',
50 'category_name' => '', 'hide_invisible' => 1,
51 'show_updated' => 0, 'include' => '',
55 $r = wp_parse_args( $args, $defaults );
56 extract( $r, EXTR_SKIP );
58 $key = md5( serialize( $r ) );
59 if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) )
60 if ( isset( $cache[ $key ] ) )
61 return apply_filters('get_bookmarks', $cache[ $key ], $r );
64 if ( !empty($include) ) {
65 $exclude = ''; //ignore exclude, category, and category_name params if using include
68 $inclinks = preg_split('/[\s,]+/',$include);
69 if ( count($inclinks) ) {
70 foreach ( $inclinks as $inclink ) {
71 if (empty($inclusions))
72 $inclusions = ' AND ( link_id = ' . intval($inclink) . ' ';
74 $inclusions .= ' OR link_id = ' . intval($inclink) . ' ';
78 if (!empty($inclusions))
82 if ( !empty($exclude) ) {
83 $exlinks = preg_split('/[\s,]+/',$exclude);
84 if ( count($exlinks) ) {
85 foreach ( $exlinks as $exlink ) {
86 if (empty($exclusions))
87 $exclusions = ' AND ( link_id <> ' . intval($exlink) . ' ';
89 $exclusions .= ' AND link_id <> ' . intval($exlink) . ' ';
93 if (!empty($exclusions))
96 if ( ! empty($category_name) ) {
97 if ( $category = get_term_by('name', $category_name, 'link_category') )
98 $category = $category->term_id;
101 $category_query = '';
103 if ( !empty($category) ) {
104 $incategories = preg_split('/[\s,]+/',$category);
105 if ( count($incategories) ) {
106 foreach ( $incategories as $incat ) {
107 if (empty($category_query))
108 $category_query = ' AND ( tt.term_id = ' . intval($incat) . ' ';
110 $category_query .= ' OR tt.term_id = ' . intval($incat) . ' ';
114 if (!empty($category_query)) {
115 $category_query .= ") AND taxonomy = 'link_category'";
116 $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";
119 if (get_option('links_recently_updated_time')) {
120 $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_option('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
122 $recently_updated_test = '';
126 $get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f ";
129 $orderby = strtolower($orderby);
133 $length = ", CHAR_LENGTH(link_name) AS length";
139 $orderby = "link_" . $orderby;
142 if ( 'link_id' == $orderby )
143 $orderby = "$wpdb->links.link_id";
146 if ( $hide_invisible )
147 $visible = "AND link_visible = 'Y'";
149 $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
150 $query .= " $exclusions $inclusions";
151 $query .= " ORDER BY $orderby $order";
153 $query .= " LIMIT $limit";
155 $results = $wpdb->get_results($query);
157 $cache[ $key ] = $results;
158 wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
160 return apply_filters('get_bookmarks', $results, $r);
163 function sanitize_bookmark($bookmark, $context = 'display') {
164 $fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category',
165 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated',
166 'link_rel', 'link_notes', 'link_rss', );
169 if ( is_object($bookmark) )
172 foreach ( $fields as $field ) {
174 $bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
176 $bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $bookmark['link_id'], $context);
182 function sanitize_bookmark_field($field, $value, $bookmark_id, $context) {
183 $int_fields = array('link_id', 'link_rating');
184 if ( in_array($field, $int_fields) )
185 $value = (int) $value;
187 $yesno = array('link_visible');
188 if ( in_array($field, $yesno) )
189 $value = preg_replace('/[^YNyn]/', '', $value);
191 if ( 'link_target' == $field ) {
192 $targets = array('_top', '_blank');
193 if ( ! in_array($value, $targets) )
197 if ( 'raw' == $context )
200 if ( 'edit' == $context ) {
201 $format_to_edit = array('link_notes');
202 $value = apply_filters("edit_$field", $value, $bookmark_id);
204 if ( in_array($field, $format_to_edit) ) {
205 $value = format_to_edit($value);
207 $value = attribute_escape($value);
209 } else if ( 'db' == $context ) {
210 $value = apply_filters("pre_$field", $value);
212 // Use display filters by default.
213 $value = apply_filters($field, $value, $bookmark_id, $context);
216 if ( 'attribute' == $context )
217 $value = attribute_escape($value);
218 else if ( 'js' == $context )
219 $value = js_escape($value);
224 function delete_get_bookmark_cache() {
225 wp_cache_delete( 'get_bookmarks', 'bookmark' );
227 add_action( 'add_link', 'delete_get_bookmark_cache' );
228 add_action( 'edit_link', 'delete_get_bookmark_cache' );
229 add_action( 'delete_link', 'delete_get_bookmark_cache' );