raw
mp-wp_genesis           1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * Link/Bookmark API
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Bookmark
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 /**
mp-wp_genesis 10 * Retrieve Bookmark data based on ID
mp-wp_genesis 11 *
mp-wp_genesis 12 * @since 2.1.0
mp-wp_genesis 13 * @uses $wpdb Database Object
mp-wp_genesis 14 *
mp-wp_genesis 15 * @param int $bookmark_id
mp-wp_genesis 16 * @param string $output Optional. Either OBJECT, ARRAY_N, or ARRAY_A constant
mp-wp_genesis 17 * @param string $filter Optional, default is 'raw'.
mp-wp_genesis 18 * @return array|object Type returned depends on $output value.
mp-wp_genesis 19 */
mp-wp_genesis 20 function get_bookmark($bookmark, $output = OBJECT, $filter = 'raw') {
mp-wp_genesis 21 global $wpdb;
mp-wp_genesis 22
mp-wp_genesis 23 if ( empty($bookmark) ) {
mp-wp_genesis 24 if ( isset($GLOBALS['link']) )
mp-wp_genesis 25 $_bookmark = & $GLOBALS['link'];
mp-wp_genesis 26 else
mp-wp_genesis 27 $_bookmark = null;
mp-wp_genesis 28 } elseif ( is_object($bookmark) ) {
mp-wp_genesis 29 wp_cache_add($bookmark->link_id, $bookmark, 'bookmark');
mp-wp_genesis 30 $_bookmark = $bookmark;
mp-wp_genesis 31 } else {
mp-wp_genesis 32 if ( isset($GLOBALS['link']) && ($GLOBALS['link']->link_id == $bookmark) ) {
mp-wp_genesis 33 $_bookmark = & $GLOBALS['link'];
mp-wp_genesis 34 } elseif ( ! $_bookmark = wp_cache_get($bookmark, 'bookmark') ) {
mp-wp_genesis 35 $_bookmark = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark));
mp-wp_genesis 36 $_bookmark->link_category = array_unique( wp_get_object_terms($_bookmark->link_id, 'link_category', 'fields=ids') );
mp-wp_genesis 37 wp_cache_add($_bookmark->link_id, $_bookmark, 'bookmark');
mp-wp_genesis 38 }
mp-wp_genesis 39 }
mp-wp_genesis 40
mp-wp_genesis 41 $_bookmark = sanitize_bookmark($_bookmark, $filter);
mp-wp_genesis 42
mp-wp_genesis 43 if ( $output == OBJECT ) {
mp-wp_genesis 44 return $_bookmark;
mp-wp_genesis 45 } elseif ( $output == ARRAY_A ) {
mp-wp_genesis 46 return get_object_vars($_bookmark);
mp-wp_genesis 47 } elseif ( $output == ARRAY_N ) {
mp-wp_genesis 48 return array_values(get_object_vars($_bookmark));
mp-wp_genesis 49 } else {
mp-wp_genesis 50 return $_bookmark;
mp-wp_genesis 51 }
mp-wp_genesis 52 }
mp-wp_genesis 53
mp-wp_genesis 54 /**
mp-wp_genesis 55 * Retrieve single bookmark data item or field.
mp-wp_genesis 56 *
mp-wp_genesis 57 * @since 2.3.0
mp-wp_genesis 58 * @uses get_bookmark() Gets bookmark object using $bookmark as ID
mp-wp_genesis 59 * @uses sanitize_bookmark_field() Sanitizes Bookmark field based on $context.
mp-wp_genesis 60 *
mp-wp_genesis 61 * @param string $field The name of the data field to return
mp-wp_genesis 62 * @param int $bookmark The bookmark ID to get field
mp-wp_genesis 63 * @param string $context Optional. The context of how the field will be used.
mp-wp_genesis 64 * @return string
mp-wp_genesis 65 */
mp-wp_genesis 66 function get_bookmark_field( $field, $bookmark, $context = 'display' ) {
mp-wp_genesis 67 $bookmark = (int) $bookmark;
mp-wp_genesis 68 $bookmark = get_bookmark( $bookmark );
mp-wp_genesis 69
mp-wp_genesis 70 if ( is_wp_error($bookmark) )
mp-wp_genesis 71 return $bookmark;
mp-wp_genesis 72
mp-wp_genesis 73 if ( !is_object($bookmark) )
mp-wp_genesis 74 return '';
mp-wp_genesis 75
mp-wp_genesis 76 if ( !isset($bookmark->$field) )
mp-wp_genesis 77 return '';
mp-wp_genesis 78
mp-wp_genesis 79 return sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
mp-wp_genesis 80 }
mp-wp_genesis 81
mp-wp_genesis 82 /**
mp-wp_genesis 83 * Retrieve bookmark data based on ID.
mp-wp_genesis 84 *
mp-wp_genesis 85 * @since 2.0.0
mp-wp_genesis 86 * @deprecated Use get_bookmark()
mp-wp_genesis 87 * @see get_bookmark()
mp-wp_genesis 88 *
mp-wp_genesis 89 * @param int $bookmark_id ID of link
mp-wp_genesis 90 * @param string $output Either OBJECT, ARRAY_N, or ARRAY_A
mp-wp_genesis 91 * @return object|array
mp-wp_genesis 92 */
mp-wp_genesis 93 function get_link($bookmark_id, $output = OBJECT, $filter = 'raw') {
mp-wp_genesis 94 return get_bookmark($bookmark_id, $output, $filter);
mp-wp_genesis 95 }
mp-wp_genesis 96
mp-wp_genesis 97 /**
mp-wp_genesis 98 * Retrieves the list of bookmarks
mp-wp_genesis 99 *
mp-wp_genesis 100 * Attempts to retrieve from the cache first based on MD5 hash of arguments. If
mp-wp_genesis 101 * that fails, then the query will be built from the arguments and executed. The
mp-wp_genesis 102 * results will be stored to the cache.
mp-wp_genesis 103 *
mp-wp_genesis 104 * List of default arguments are as follows:
mp-wp_genesis 105 * 'orderby' - Default is 'name' (string). How to order the links by. String is
mp-wp_genesis 106 * based off of the bookmark scheme.
mp-wp_genesis 107 * 'order' - Default is 'ASC' (string). Either 'ASC' or 'DESC'. Orders in either
mp-wp_genesis 108 * ascending or descending order.
mp-wp_genesis 109 * 'limit' - Default is -1 (integer) or show all. The amount of bookmarks to
mp-wp_genesis 110 * display.
mp-wp_genesis 111 * 'category' - Default is empty string (string). Include the links in what
mp-wp_genesis 112 * category ID(s).
mp-wp_genesis 113 * 'category_name' - Default is empty string (string). Get links by category
mp-wp_genesis 114 * name.
mp-wp_genesis 115 * 'hide_invisible' - Default is 1 (integer). Whether to show (default) or hide
mp-wp_genesis 116 * links marked as 'invisible'.
mp-wp_genesis 117 * 'show_updated' - Default is 0 (integer). Will show the time of when the
mp-wp_genesis 118 * bookmark was last updated.
mp-wp_genesis 119 * 'include' - Default is empty string (string). Include other categories
mp-wp_genesis 120 * separated by commas.
mp-wp_genesis 121 * 'exclude' - Default is empty string (string). Exclude other categories
mp-wp_genesis 122 * separated by commas.
mp-wp_genesis 123 *
mp-wp_genesis 124 * @since 2.1.0
mp-wp_genesis 125 * @uses $wpdb Database Object
mp-wp_genesis 126 * @link http://codex.wordpress.org/Template_Tags/get_bookmarks
mp-wp_genesis 127 *
mp-wp_genesis 128 * @param string|array $args List of arguments to overwrite the defaults
mp-wp_genesis 129 * @return array List of bookmark row objects
mp-wp_genesis 130 */
mp-wp_genesis 131 function get_bookmarks($args = '') {
mp-wp_genesis 132 global $wpdb;
mp-wp_genesis 133
mp-wp_genesis 134 $defaults = array(
mp-wp_genesis 135 'orderby' => 'name', 'order' => 'ASC',
mp-wp_genesis 136 'limit' => -1, 'category' => '',
mp-wp_genesis 137 'category_name' => '', 'hide_invisible' => 1,
mp-wp_genesis 138 'show_updated' => 0, 'include' => '',
mp-wp_genesis 139 'exclude' => '', 'search' => ''
mp-wp_genesis 140 );
mp-wp_genesis 141
mp-wp_genesis 142 $r = wp_parse_args( $args, $defaults );
mp-wp_genesis 143 extract( $r, EXTR_SKIP );
mp-wp_genesis 144
mp-wp_genesis 145 $key = md5( serialize( $r ) );
mp-wp_genesis 146 if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) )
mp-wp_genesis 147 if ( isset( $cache[ $key ] ) )
mp-wp_genesis 148 return apply_filters('get_bookmarks', $cache[ $key ], $r );
mp-wp_genesis 149
mp-wp_genesis 150 $inclusions = '';
mp-wp_genesis 151 if ( !empty($include) ) {
mp-wp_genesis 152 $exclude = ''; //ignore exclude, category, and category_name params if using include
mp-wp_genesis 153 $category = '';
mp-wp_genesis 154 $category_name = '';
mp-wp_genesis 155 $inclinks = preg_split('/[\s,]+/',$include);
mp-wp_genesis 156 if ( count($inclinks) ) {
mp-wp_genesis 157 foreach ( $inclinks as $inclink ) {
mp-wp_genesis 158 if (empty($inclusions))
mp-wp_genesis 159 $inclusions = ' AND ( link_id = ' . intval($inclink) . ' ';
mp-wp_genesis 160 else
mp-wp_genesis 161 $inclusions .= ' OR link_id = ' . intval($inclink) . ' ';
mp-wp_genesis 162 }
mp-wp_genesis 163 }
mp-wp_genesis 164 }
mp-wp_genesis 165 if (!empty($inclusions))
mp-wp_genesis 166 $inclusions .= ')';
mp-wp_genesis 167
mp-wp_genesis 168 $exclusions = '';
mp-wp_genesis 169 if ( !empty($exclude) ) {
mp-wp_genesis 170 $exlinks = preg_split('/[\s,]+/',$exclude);
mp-wp_genesis 171 if ( count($exlinks) ) {
mp-wp_genesis 172 foreach ( $exlinks as $exlink ) {
mp-wp_genesis 173 if (empty($exclusions))
mp-wp_genesis 174 $exclusions = ' AND ( link_id <> ' . intval($exlink) . ' ';
mp-wp_genesis 175 else
mp-wp_genesis 176 $exclusions .= ' AND link_id <> ' . intval($exlink) . ' ';
mp-wp_genesis 177 }
mp-wp_genesis 178 }
mp-wp_genesis 179 }
mp-wp_genesis 180 if (!empty($exclusions))
mp-wp_genesis 181 $exclusions .= ')';
mp-wp_genesis 182
mp-wp_genesis 183 if ( ! empty($category_name) ) {
mp-wp_genesis 184 if ( $category = get_term_by('name', $category_name, 'link_category') )
mp-wp_genesis 185 $category = $category->term_id;
mp-wp_genesis 186 }
mp-wp_genesis 187
mp-wp_genesis 188 if ( ! empty($search) ) {
mp-wp_genesis 189 $search = like_escape($search);
mp-wp_genesis 190 $search = " AND ( (link_url LIKE '%$search%') OR (link_name LIKE '%$search%') OR (link_description LIKE '%$search%') ) ";
mp-wp_genesis 191 }
mp-wp_genesis 192
mp-wp_genesis 193 $category_query = '';
mp-wp_genesis 194 $join = '';
mp-wp_genesis 195 if ( !empty($category) ) {
mp-wp_genesis 196 $incategories = preg_split('/[\s,]+/',$category);
mp-wp_genesis 197 if ( count($incategories) ) {
mp-wp_genesis 198 foreach ( $incategories as $incat ) {
mp-wp_genesis 199 if (empty($category_query))
mp-wp_genesis 200 $category_query = ' AND ( tt.term_id = ' . intval($incat) . ' ';
mp-wp_genesis 201 else
mp-wp_genesis 202 $category_query .= ' OR tt.term_id = ' . intval($incat) . ' ';
mp-wp_genesis 203 }
mp-wp_genesis 204 }
mp-wp_genesis 205 }
mp-wp_genesis 206 if (!empty($category_query)) {
mp-wp_genesis 207 $category_query .= ") AND taxonomy = 'link_category'";
mp-wp_genesis 208 $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";
mp-wp_genesis 209 }
mp-wp_genesis 210
mp-wp_genesis 211 if (get_option('links_recently_updated_time')) {
mp-wp_genesis 212 $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_option('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
mp-wp_genesis 213 } else {
mp-wp_genesis 214 $recently_updated_test = '';
mp-wp_genesis 215 }
mp-wp_genesis 216
mp-wp_genesis 217 $get_updated = ( $show_updated ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
mp-wp_genesis 218
mp-wp_genesis 219 $orderby = strtolower($orderby);
mp-wp_genesis 220 $length = '';
mp-wp_genesis 221 switch ($orderby) {
mp-wp_genesis 222 case 'length':
mp-wp_genesis 223 $length = ", CHAR_LENGTH(link_name) AS length";
mp-wp_genesis 224 break;
mp-wp_genesis 225 case 'rand':
mp-wp_genesis 226 $orderby = 'rand()';
mp-wp_genesis 227 break;
mp-wp_genesis 228 default:
mp-wp_genesis 229 $orderby = "link_" . $orderby;
mp-wp_genesis 230 }
mp-wp_genesis 231
mp-wp_genesis 232 if ( 'link_id' == $orderby )
mp-wp_genesis 233 $orderby = "$wpdb->links.link_id";
mp-wp_genesis 234
mp-wp_genesis 235 $visible = '';
mp-wp_genesis 236 if ( $hide_invisible )
mp-wp_genesis 237 $visible = "AND link_visible = 'Y'";
mp-wp_genesis 238
mp-wp_genesis 239 $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
mp-wp_genesis 240 $query .= " $exclusions $inclusions $search";
mp-wp_genesis 241 $query .= " ORDER BY $orderby $order";
mp-wp_genesis 242 if ($limit != -1)
mp-wp_genesis 243 $query .= " LIMIT $limit";
mp-wp_genesis 244
mp-wp_genesis 245 $results = $wpdb->get_results($query);
mp-wp_genesis 246
mp-wp_genesis 247 $cache[ $key ] = $results;
mp-wp_genesis 248 wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
mp-wp_genesis 249
mp-wp_genesis 250 return apply_filters('get_bookmarks', $results, $r);
mp-wp_genesis 251 }
mp-wp_genesis 252
mp-wp_genesis 253 /**
mp-wp_genesis 254 * Sanitizes all bookmark fields
mp-wp_genesis 255 *
mp-wp_genesis 256 * @since 2.3.0
mp-wp_genesis 257 *
mp-wp_genesis 258 * @param object|array $bookmark Bookmark row
mp-wp_genesis 259 * @param string $context Optional, default is 'display'. How to filter the
mp-wp_genesis 260 * fields
mp-wp_genesis 261 * @return object|array Same type as $bookmark but with fields sanitized.
mp-wp_genesis 262 */
mp-wp_genesis 263 function sanitize_bookmark($bookmark, $context = 'display') {
mp-wp_genesis 264 $fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category',
mp-wp_genesis 265 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated',
mp-wp_genesis 266 'link_rel', 'link_notes', 'link_rss', );
mp-wp_genesis 267
mp-wp_genesis 268 if ( is_object($bookmark) ) {
mp-wp_genesis 269 $do_object = true;
mp-wp_genesis 270 $link_id = $bookmark->link_id;
mp-wp_genesis 271 } else {
mp-wp_genesis 272 $do_object = false;
mp-wp_genesis 273 $link_id = $bookmark['link_id'];
mp-wp_genesis 274 }
mp-wp_genesis 275
mp-wp_genesis 276 foreach ( $fields as $field ) {
mp-wp_genesis 277 if ( $do_object ) {
mp-wp_genesis 278 if ( isset($bookmark->$field) )
mp-wp_genesis 279 $bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $link_id, $context);
mp-wp_genesis 280 } else {
mp-wp_genesis 281 if ( isset($bookmark[$field]) )
mp-wp_genesis 282 $bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $link_id, $context);
mp-wp_genesis 283 }
mp-wp_genesis 284 }
mp-wp_genesis 285
mp-wp_genesis 286 return $bookmark;
mp-wp_genesis 287 }
mp-wp_genesis 288
mp-wp_genesis 289 /**
mp-wp_genesis 290 * Sanitizes a bookmark field
mp-wp_genesis 291 *
mp-wp_genesis 292 * Sanitizes the bookmark fields based on what the field name is. If the field
mp-wp_genesis 293 * has a strict value set, then it will be tested for that, else a more generic
mp-wp_genesis 294 * filtering is applied. After the more strict filter is applied, if the
mp-wp_genesis 295 * $context is 'raw' then the value is immediately return.
mp-wp_genesis 296 *
mp-wp_genesis 297 * Hooks exist for the more generic cases. With the 'edit' context, the
mp-wp_genesis 298 * 'edit_$field' filter will be called and passed the $value and $bookmark_id
mp-wp_genesis 299 * respectively. With the 'db' context, the 'pre_$field' filter is called and
mp-wp_genesis 300 * passed the value. The 'display' context is the final context and has the
mp-wp_genesis 301 * $field has the filter name and is passed the $value, $bookmark_id, and
mp-wp_genesis 302 * $context respectively.
mp-wp_genesis 303 *
mp-wp_genesis 304 * @since 2.3.0
mp-wp_genesis 305 *
mp-wp_genesis 306 * @param string $field The bookmark field
mp-wp_genesis 307 * @param mixed $value The bookmark field value
mp-wp_genesis 308 * @param int $bookmark_id Bookmark ID
mp-wp_genesis 309 * @param string $context How to filter the field value. Either 'raw', 'edit',
mp-wp_genesis 310 * 'attribute', 'js', 'db', or 'display'
mp-wp_genesis 311 * @return mixed The filtered value
mp-wp_genesis 312 */
mp-wp_genesis 313 function sanitize_bookmark_field($field, $value, $bookmark_id, $context) {
mp-wp_genesis 314 $int_fields = array('link_id', 'link_rating');
mp-wp_genesis 315 if ( in_array($field, $int_fields) )
mp-wp_genesis 316 $value = (int) $value;
mp-wp_genesis 317
mp-wp_genesis 318 $yesno = array('link_visible');
mp-wp_genesis 319 if ( in_array($field, $yesno) )
mp-wp_genesis 320 $value = preg_replace('/[^YNyn]/', '', $value);
mp-wp_genesis 321
mp-wp_genesis 322 if ( 'link_target' == $field ) {
mp-wp_genesis 323 $targets = array('_top', '_blank');
mp-wp_genesis 324 if ( ! in_array($value, $targets) )
mp-wp_genesis 325 $value = '';
mp-wp_genesis 326 }
mp-wp_genesis 327
mp-wp_genesis 328 if ( 'raw' == $context )
mp-wp_genesis 329 return $value;
mp-wp_genesis 330
mp-wp_genesis 331 if ( 'edit' == $context ) {
mp-wp_genesis 332 $format_to_edit = array('link_notes');
mp-wp_genesis 333 $value = apply_filters("edit_$field", $value, $bookmark_id);
mp-wp_genesis 334
mp-wp_genesis 335 if ( in_array($field, $format_to_edit) ) {
mp-wp_genesis 336 $value = format_to_edit($value);
mp-wp_genesis 337 } else {
mp-wp_genesis 338 $value = attribute_escape($value);
mp-wp_genesis 339 }
mp-wp_genesis 340 } else if ( 'db' == $context ) {
mp-wp_genesis 341 $value = apply_filters("pre_$field", $value);
mp-wp_genesis 342 } else {
mp-wp_genesis 343 // Use display filters by default.
mp-wp_genesis 344 $value = apply_filters($field, $value, $bookmark_id, $context);
mp-wp_genesis 345 }
mp-wp_genesis 346
mp-wp_genesis 347 if ( 'attribute' == $context )
mp-wp_genesis 348 $value = attribute_escape($value);
mp-wp_genesis 349 else if ( 'js' == $context )
mp-wp_genesis 350 $value = js_escape($value);
mp-wp_genesis 351
mp-wp_genesis 352 return $value;
mp-wp_genesis 353 }
mp-wp_genesis 354
mp-wp_genesis 355 /**
mp-wp_genesis 356 * Deletes bookmark cache
mp-wp_genesis 357 *
mp-wp_genesis 358 * @since 2.7.0
mp-wp_genesis 359 * @uses wp_cache_delete() Deletes the contents of 'get_bookmarks'
mp-wp_genesis 360 */
mp-wp_genesis 361 function clean_bookmark_cache($bookmark_id) {
mp-wp_genesis 362 wp_cache_delete( $bookmark_id, 'bookmark' );
mp-wp_genesis 363 wp_cache_delete( 'get_bookmarks', 'bookmark' );
mp-wp_genesis 364 }
mp-wp_genesis 365
mp-wp_genesis 366 ?>