web/wp-includes/bookmark.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
     5  * @package WordPress
     5  * @package WordPress
     6  * @subpackage Bookmark
     6  * @subpackage Bookmark
     7  */
     7  */
     8 
     8 
     9 /**
     9 /**
    10  * Retrieve Bookmark data based on ID
    10  * Retrieve Bookmark data
    11  *
    11  *
    12  * @since 2.1.0
    12  * @since 2.1.0
    13  * @uses $wpdb Database Object
    13  * @uses $wpdb Database Object
    14  *
    14  *
    15  * @param int $bookmark_id
    15  * @param mixed $bookmark
    16  * @param string $output Optional. Either OBJECT, ARRAY_N, or ARRAY_A constant
    16  * @param string $output Optional. Either OBJECT, ARRAY_N, or ARRAY_A constant
    17  * @param string $filter Optional, default is 'raw'.
    17  * @param string $filter Optional, default is 'raw'.
    18  * @return array|object Type returned depends on $output value.
    18  * @return array|object Type returned depends on $output value.
    19  */
    19  */
    20 function get_bookmark($bookmark, $output = OBJECT, $filter = 'raw') {
    20 function get_bookmark($bookmark, $output = OBJECT, $filter = 'raw') {
    31 	} else {
    31 	} else {
    32 		if ( isset($GLOBALS['link']) && ($GLOBALS['link']->link_id == $bookmark) ) {
    32 		if ( isset($GLOBALS['link']) && ($GLOBALS['link']->link_id == $bookmark) ) {
    33 			$_bookmark = & $GLOBALS['link'];
    33 			$_bookmark = & $GLOBALS['link'];
    34 		} elseif ( ! $_bookmark = wp_cache_get($bookmark, 'bookmark') ) {
    34 		} elseif ( ! $_bookmark = wp_cache_get($bookmark, 'bookmark') ) {
    35 			$_bookmark = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark));
    35 			$_bookmark = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark));
    36 			$_bookmark->link_category = array_unique( wp_get_object_terms($_bookmark->link_id, 'link_category', 'fields=ids') );
    36 			$_bookmark->link_category = array_unique( wp_get_object_terms($_bookmark->link_id, 'link_category', array('fields' => 'ids')) );
    37 			wp_cache_add($_bookmark->link_id, $_bookmark, 'bookmark');
    37 			wp_cache_add($_bookmark->link_id, $_bookmark, 'bookmark');
    38 		}
    38 		}
    39 	}
    39 	}
    40 
    40 
    41 	$_bookmark = sanitize_bookmark($_bookmark, $filter);
    41 	$_bookmark = sanitize_bookmark($_bookmark, $filter);
    75 
    75 
    76 	if ( !isset($bookmark->$field) )
    76 	if ( !isset($bookmark->$field) )
    77 		return '';
    77 		return '';
    78 
    78 
    79 	return sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
    79 	return sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
    80 }
       
    81 
       
    82 /**
       
    83  * Retrieve bookmark data based on ID.
       
    84  *
       
    85  * @since 2.0.0
       
    86  * @deprecated Use get_bookmark()
       
    87  * @see get_bookmark()
       
    88  *
       
    89  * @param int $bookmark_id ID of link
       
    90  * @param string $output Either OBJECT, ARRAY_N, or ARRAY_A
       
    91  * @return object|array
       
    92  */
       
    93 function get_link($bookmark_id, $output = OBJECT, $filter = 'raw') {
       
    94 	return get_bookmark($bookmark_id, $output, $filter);
       
    95 }
    80 }
    96 
    81 
    97 /**
    82 /**
    98  * Retrieves the list of bookmarks
    83  * Retrieves the list of bookmarks
    99  *
    84  *
   114  *		name.
    99  *		name.
   115  * 'hide_invisible' - Default is 1 (integer). Whether to show (default) or hide
   100  * 'hide_invisible' - Default is 1 (integer). Whether to show (default) or hide
   116  *		links marked as 'invisible'.
   101  *		links marked as 'invisible'.
   117  * 'show_updated' - Default is 0 (integer). Will show the time of when the
   102  * 'show_updated' - Default is 0 (integer). Will show the time of when the
   118  *		bookmark was last updated.
   103  *		bookmark was last updated.
   119  * 'include' - Default is empty string (string). Include other categories
   104  * 'include' - Default is empty string (string). Include bookmark ID(s)
   120  *		separated by commas.
   105  *		separated by commas.
   121  * 'exclude' - Default is empty string (string). Exclude other categories
   106  * 'exclude' - Default is empty string (string). Exclude bookmark ID(s)
   122  *		separated by commas.
   107  *		separated by commas.
   123  *
   108  *
   124  * @since 2.1.0
   109  * @since 2.1.0
   125  * @uses $wpdb Database Object
   110  * @uses $wpdb Database Object
   126  * @link http://codex.wordpress.org/Template_Tags/get_bookmarks
   111  * @link http://codex.wordpress.org/Template_Tags/get_bookmarks
   226 
   211 
   227 	$get_updated = ( $show_updated ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
   212 	$get_updated = ( $show_updated ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
   228 
   213 
   229 	$orderby = strtolower($orderby);
   214 	$orderby = strtolower($orderby);
   230 	$length = '';
   215 	$length = '';
   231 	switch ($orderby) {
   216 	switch ( $orderby ) {
   232 		case 'length':
   217 		case 'length':
   233 			$length = ", CHAR_LENGTH(link_name) AS length";
   218 			$length = ", CHAR_LENGTH(link_name) AS length";
   234 			break;
   219 			break;
   235 		case 'rand':
   220 		case 'rand':
   236 			$orderby = 'rand()';
   221 			$orderby = 'rand()';
   237 			break;
   222 			break;
       
   223 		case 'link_id':
       
   224 			$orderby = "$wpdb->links.link_id";
       
   225 			break;
   238 		default:
   226 		default:
   239 			$orderby = "link_" . $orderby;
   227 			$orderparams = array();
   240 	}
   228 			foreach ( explode(',', $orderby) as $ordparam ) {
   241 
   229 				$ordparam = trim($ordparam);
   242 	if ( 'link_id' == $orderby )
   230 				$keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes' );
   243 		$orderby = "$wpdb->links.link_id";
   231 				if ( in_array( 'link_' . $ordparam, $keys ) )
       
   232 					$orderparams[] = 'link_' . $ordparam;
       
   233 				elseif ( in_array( $ordparam, $keys ) )
       
   234 					$orderparams[] = $ordparam;
       
   235 			}
       
   236 			$orderby = implode(',', $orderparams);
       
   237 	}
       
   238 
       
   239 	if ( empty( $orderby ) )
       
   240 		$orderby = 'link_name';
       
   241 
       
   242 	$order = strtoupper( $order );
       
   243 	if ( '' !== $order && !in_array( $order, array( 'ASC', 'DESC' ) ) )
       
   244 		$order = 'ASC';
   244 
   245 
   245 	$visible = '';
   246 	$visible = '';
   246 	if ( $hide_invisible )
   247 	if ( $hide_invisible )
   247 		$visible = "AND link_visible = 'Y'";
   248 		$visible = "AND link_visible = 'Y'";
   248 
   249 
   319  * @param string $context How to filter the field value. Either 'raw', 'edit',
   320  * @param string $context How to filter the field value. Either 'raw', 'edit',
   320  *		'attribute', 'js', 'db', or 'display'
   321  *		'attribute', 'js', 'db', or 'display'
   321  * @return mixed The filtered value
   322  * @return mixed The filtered value
   322  */
   323  */
   323 function sanitize_bookmark_field($field, $value, $bookmark_id, $context) {
   324 function sanitize_bookmark_field($field, $value, $bookmark_id, $context) {
   324 	$int_fields = array('link_id', 'link_rating');
   325 	switch ( $field ) {
   325 	if ( in_array($field, $int_fields) )
   326 	case 'link_id' : // ints
       
   327 	case 'link_rating' :
   326 		$value = (int) $value;
   328 		$value = (int) $value;
   327 
   329 		break;
   328 	$yesno = array('link_visible');
   330 	case 'link_category' : // array( ints )
   329 	if ( in_array($field, $yesno) )
   331 		$value = array_map('absint', (array) $value);
       
   332 		// We return here so that the categories aren't filtered.
       
   333 		// The 'link_category' filter is for the name of a link category, not an array of a link's link categories
       
   334 		return $value;
       
   335 		break;
       
   336 	case 'link_visible' : // bool stored as Y|N
   330 		$value = preg_replace('/[^YNyn]/', '', $value);
   337 		$value = preg_replace('/[^YNyn]/', '', $value);
   331 
   338 		break;
   332 	if ( 'link_target' == $field ) {
   339 	case 'link_target' : // "enum"
   333 		$targets = array('_top', '_blank');
   340 		$targets = array('_top', '_blank');
   334 		if ( ! in_array($value, $targets) )
   341 		if ( ! in_array($value, $targets) )
   335 			$value = '';
   342 			$value = '';
       
   343 		break;
   336 	}
   344 	}
   337 
   345 
   338 	if ( 'raw' == $context )
   346 	if ( 'raw' == $context )
   339 		return $value;
   347 		return $value;
   340 
   348 
   341 	if ( 'edit' == $context ) {
   349 	if ( 'edit' == $context ) {
   342 		$format_to_edit = array('link_notes');
       
   343 		$value = apply_filters("edit_$field", $value, $bookmark_id);
   350 		$value = apply_filters("edit_$field", $value, $bookmark_id);
   344 
   351 
   345 		if ( in_array($field, $format_to_edit) ) {
   352 		if ( 'link_notes' == $field ) {
   346 			$value = format_to_edit($value);
   353 			$value = esc_html( $value ); // textarea_escaped
   347 		} else {
   354 		} else {
   348 			$value = esc_attr($value);
   355 			$value = esc_attr($value);
   349 		}
   356 		}
   350 	} else if ( 'db' == $context ) {
   357 	} else if ( 'db' == $context ) {
   351 		$value = apply_filters("pre_$field", $value);
   358 		$value = apply_filters("pre_$field", $value);
   352 	} else {
   359 	} else {
   353 		// Use display filters by default.
   360 		// Use display filters by default.
   354 		$value = apply_filters($field, $value, $bookmark_id, $context);
   361 		$value = apply_filters($field, $value, $bookmark_id, $context);
   355 	}
   362 
   356 
   363 		if ( 'attribute' == $context )
   357 	if ( 'attribute' == $context )
   364 			$value = esc_attr($value);
   358 		$value = esc_attr($value);
   365 		else if ( 'js' == $context )
   359 	else if ( 'js' == $context )
   366 			$value = esc_js($value);
   360 		$value = esc_js($value);
   367 	}
   361 
   368 
   362 	return $value;
   369 	return $value;
   363 }
   370 }
   364 
   371 
   365 /**
   372 /**
   366  * Deletes bookmark cache
   373  * Deletes bookmark cache
   367  *
   374  *
   368  * @since 2.7.0
   375  * @since 2.7.0
   369  * @uses wp_cache_delete() Deletes the contents of 'get_bookmarks'
   376  * @uses wp_cache_delete() Deletes the contents of 'get_bookmarks'
   370  */
   377  */
   371 function clean_bookmark_cache($bookmark_id) {
   378 function clean_bookmark_cache( $bookmark_id ) {
   372 	wp_cache_delete( $bookmark_id, 'bookmark' );
   379 	wp_cache_delete( $bookmark_id, 'bookmark' );
   373 	wp_cache_delete( 'get_bookmarks', 'bookmark' );
   380 	wp_cache_delete( 'get_bookmarks', 'bookmark' );
   374 }
   381 	clean_object_term_cache( $bookmark_id, 'link');
   375 
   382 }
   376 ?>