wp/wp-includes/bookmark.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
    12  * @since 2.1.0
    12  * @since 2.1.0
    13  *
    13  *
    14  * @global wpdb $wpdb WordPress database abstraction object.
    14  * @global wpdb $wpdb WordPress database abstraction object.
    15  *
    15  *
    16  * @param int|stdClass $bookmark
    16  * @param int|stdClass $bookmark
    17  * @param string $output Optional. Either OBJECT, ARRAY_N, or ARRAY_A constant
    17  * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
       
    18  *                       an stdClass object, an associative array, or a numeric array, respectively. Default OBJECT.
    18  * @param string $filter Optional, default is 'raw'.
    19  * @param string $filter Optional, default is 'raw'.
    19  * @return array|object Type returned depends on $output value.
    20  * @return array|object|null Type returned depends on $output value.
    20  */
    21  */
    21 function get_bookmark($bookmark, $output = OBJECT, $filter = 'raw') {
    22 function get_bookmark($bookmark, $output = OBJECT, $filter = 'raw') {
    22 	global $wpdb;
    23 	global $wpdb;
    23 
    24 
    24 	if ( empty($bookmark) ) {
    25 	if ( empty($bookmark) ) {
    63  * @since 2.3.0
    64  * @since 2.3.0
    64  *
    65  *
    65  * @param string $field The name of the data field to return
    66  * @param string $field The name of the data field to return
    66  * @param int $bookmark The bookmark ID to get field
    67  * @param int $bookmark The bookmark ID to get field
    67  * @param string $context Optional. The context of how the field will be used.
    68  * @param string $context Optional. The context of how the field will be used.
    68  * @return string
    69  * @return string|WP_Error
    69  */
    70  */
    70 function get_bookmark_field( $field, $bookmark, $context = 'display' ) {
    71 function get_bookmark_field( $field, $bookmark, $context = 'display' ) {
    71 	$bookmark = (int) $bookmark;
    72 	$bookmark = (int) $bookmark;
    72 	$bookmark = get_bookmark( $bookmark );
    73 	$bookmark = get_bookmark( $bookmark );
    73 
    74 
   126 	);
   127 	);
   127 
   128 
   128 	$r = wp_parse_args( $args, $defaults );
   129 	$r = wp_parse_args( $args, $defaults );
   129 
   130 
   130 	$key = md5( serialize( $r ) );
   131 	$key = md5( serialize( $r ) );
   131 	if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) {
   132 	$cache = false;
       
   133 	if ( 'rand' !== $r['orderby'] && $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) {
   132 		if ( is_array( $cache ) && isset( $cache[ $key ] ) ) {
   134 		if ( is_array( $cache ) && isset( $cache[ $key ] ) ) {
   133 			$bookmarks = $cache[ $key ];
   135 			$bookmarks = $cache[ $key ];
   134 			/**
   136 			/**
   135 			 * Filter the returned list of bookmarks.
   137 			 * Filters the returned list of bookmarks.
   136 			 *
   138 			 *
   137 			 * The first time the hook is evaluated in this file, it returns the cached
   139 			 * The first time the hook is evaluated in this file, it returns the cached
   138 			 * bookmarks list. The second evaluation returns a cached bookmarks list if the
   140 			 * bookmarks list. The second evaluation returns a cached bookmarks list if the
   139 			 * link category is passed but does not exist. The third evaluation returns
   141 			 * link category is passed but does not exist. The third evaluation returns
   140 			 * the full cached results.
   142 			 * the full cached results.
   283 		$query .= ' LIMIT ' . $r['limit'];
   285 		$query .= ' LIMIT ' . $r['limit'];
   284 	}
   286 	}
   285 
   287 
   286 	$results = $wpdb->get_results( $query );
   288 	$results = $wpdb->get_results( $query );
   287 
   289 
   288 	$cache[ $key ] = $results;
   290 	if ( 'rand()' !== $orderby ) {
   289 	wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
   291 		$cache[ $key ] = $results;
       
   292 		wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
       
   293 	}
   290 
   294 
   291 	/** This filter is documented in wp-includes/bookmark.php */
   295 	/** This filter is documented in wp-includes/bookmark.php */
   292 	return apply_filters( 'get_bookmarks', $results, $r );
   296 	return apply_filters( 'get_bookmarks', $results, $r );
   293 }
   297 }
   294 
   298 
   295 /**
   299 /**
   296  * Sanitizes all bookmark fields
   300  * Sanitizes all bookmark fields
   297  *
   301  *
   298  * @since 2.3.0
   302  * @since 2.3.0
   299  *
   303  *
   300  * @param object|array $bookmark Bookmark row
   304  * @param stdClass|array $bookmark Bookmark row
   301  * @param string $context Optional, default is 'display'. How to filter the
   305  * @param string $context Optional, default is 'display'. How to filter the
   302  *		fields
   306  *		fields
   303  * @return object|array Same type as $bookmark but with fields sanitized.
   307  * @return stdClass|array Same type as $bookmark but with fields sanitized.
   304  */
   308  */
   305 function sanitize_bookmark($bookmark, $context = 'display') {
   309 function sanitize_bookmark($bookmark, $context = 'display') {
   306 	$fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category',
   310 	$fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category',
   307 		'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated',
   311 		'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated',
   308 		'link_rel', 'link_notes', 'link_rss', );
   312 		'link_rel', 'link_notes', 'link_rss', );
   327 
   331 
   328 	return $bookmark;
   332 	return $bookmark;
   329 }
   333 }
   330 
   334 
   331 /**
   335 /**
   332  * Sanitizes a bookmark field
   336  * Sanitizes a bookmark field.
   333  *
   337  *
   334  * Sanitizes the bookmark fields based on what the field name is. If the field
   338  * Sanitizes the bookmark fields based on what the field name is. If the field
   335  * has a strict value set, then it will be tested for that, else a more generic
   339  * has a strict value set, then it will be tested for that, else a more generic
   336  * filtering is applied. After the more strict filter is applied, if the
   340  * filtering is applied. After the more strict filter is applied, if the `$context`
   337  * $context is 'raw' then the value is immediately return.
   341  * is 'raw' then the value is immediately return.
   338  *
   342  *
   339  * Hooks exist for the more generic cases. With the 'edit' context, the
   343  * Hooks exist for the more generic cases. With the 'edit' context, the {@see 'edit_$field'}
   340  * 'edit_$field' filter will be called and passed the $value and $bookmark_id
   344  * filter will be called and passed the `$value` and `$bookmark_id` respectively.
   341  * respectively. With the 'db' context, the 'pre_$field' filter is called and
   345  *
   342  * passed the value. The 'display' context is the final context and has the
   346  * With the 'db' context, the {@see 'pre_$field'} filter is called and passed the value.
   343  * $field has the filter name and is passed the $value, $bookmark_id, and
   347  * The 'display' context is the final context and has the `$field` has the filter name
   344  * $context respectively.
   348  * and is passed the `$value`, `$bookmark_id`, and `$context`, respectively.
   345  *
   349  *
   346  * @since 2.3.0
   350  * @since 2.3.0
   347  *
   351  *
   348  * @param string $field The bookmark field
   352  * @param string $field       The bookmark field.
   349  * @param mixed $value The bookmark field value
   353  * @param mixed  $value       The bookmark field value.
   350  * @param int $bookmark_id Bookmark ID
   354  * @param int    $bookmark_id Bookmark ID.
   351  * @param string $context How to filter the field value. Either 'raw', 'edit',
   355  * @param string $context     How to filter the field value. Accepts 'raw', 'edit', 'attribute',
   352  *		'attribute', 'js', 'db', or 'display'
   356  *                            'js', 'db', or 'display'
   353  * @return mixed The filtered value
   357  * @return mixed The filtered value.
   354  */
   358  */
   355 function sanitize_bookmark_field($field, $value, $bookmark_id, $context) {
   359 function sanitize_bookmark_field( $field, $value, $bookmark_id, $context ) {
   356 	switch ( $field ) {
   360 	switch ( $field ) {
   357 	case 'link_id' : // ints
   361 	case 'link_id' : // ints
   358 	case 'link_rating' :
   362 	case 'link_rating' :
   359 		$value = (int) $value;
   363 		$value = (int) $value;
   360 		break;
   364 		break;
   377 	if ( 'raw' == $context )
   381 	if ( 'raw' == $context )
   378 		return $value;
   382 		return $value;
   379 
   383 
   380 	if ( 'edit' == $context ) {
   384 	if ( 'edit' == $context ) {
   381 		/** This filter is documented in wp-includes/post.php */
   385 		/** This filter is documented in wp-includes/post.php */
   382 		$value = apply_filters( "edit_$field", $value, $bookmark_id );
   386 		$value = apply_filters( "edit_{$field}", $value, $bookmark_id );
   383 
   387 
   384 		if ( 'link_notes' == $field ) {
   388 		if ( 'link_notes' == $field ) {
   385 			$value = esc_html( $value ); // textarea_escaped
   389 			$value = esc_html( $value ); // textarea_escaped
   386 		} else {
   390 		} else {
   387 			$value = esc_attr($value);
   391 			$value = esc_attr($value);
   388 		}
   392 		}
   389 	} elseif ( 'db' == $context ) {
   393 	} elseif ( 'db' == $context ) {
   390 		/** This filter is documented in wp-includes/post.php */
   394 		/** This filter is documented in wp-includes/post.php */
   391 		$value = apply_filters( "pre_$field", $value );
   395 		$value = apply_filters( "pre_{$field}", $value );
   392 	} else {
   396 	} else {
   393 		/** This filter is documented in wp-includes/post.php */
   397 		/** This filter is documented in wp-includes/post.php */
   394 		$value = apply_filters( $field, $value, $bookmark_id, $context );
   398 		$value = apply_filters( "{$field}", $value, $bookmark_id, $context );
   395 
   399 
   396 		if ( 'attribute' == $context ) {
   400 		if ( 'attribute' == $context ) {
   397 			$value = esc_attr( $value );
   401 			$value = esc_attr( $value );
   398 		} elseif ( 'js' == $context ) {
   402 		} elseif ( 'js' == $context ) {
   399 			$value = esc_js( $value );
   403 			$value = esc_js( $value );
   402 
   406 
   403 	return $value;
   407 	return $value;
   404 }
   408 }
   405 
   409 
   406 /**
   410 /**
   407  * Deletes bookmark cache
   411  * Deletes the bookmark cache.
   408  *
   412  *
   409  * @since 2.7.0
   413  * @since 2.7.0
       
   414  *
       
   415  * @param int $bookmark_id Bookmark ID.
   410  */
   416  */
   411 function clean_bookmark_cache( $bookmark_id ) {
   417 function clean_bookmark_cache( $bookmark_id ) {
   412 	wp_cache_delete( $bookmark_id, 'bookmark' );
   418 	wp_cache_delete( $bookmark_id, 'bookmark' );
   413 	wp_cache_delete( 'get_bookmarks', 'bookmark' );
   419 	wp_cache_delete( 'get_bookmarks', 'bookmark' );
   414 	clean_object_term_cache( $bookmark_id, 'link');
   420 	clean_object_term_cache( $bookmark_id, 'link');