wp/wp-includes/bookmark.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
     8 
     8 
     9 /**
     9 /**
    10  * Retrieve Bookmark data
    10  * Retrieve Bookmark data
    11  *
    11  *
    12  * @since 2.1.0
    12  * @since 2.1.0
    13  * @uses $wpdb Database Object
    13  *
    14  *
    14  * @global wpdb $wpdb WordPress database abstraction object.
    15  * @param mixed $bookmark
    15  *
       
    16  * @param int|stdClass $bookmark
    16  * @param string $output Optional. Either OBJECT, ARRAY_N, or ARRAY_A constant
    17  * @param string $output Optional. Either OBJECT, ARRAY_N, or ARRAY_A constant
    17  * @param string $filter Optional, default is 'raw'.
    18  * @param string $filter Optional, default is 'raw'.
    18  * @return array|object Type returned depends on $output value.
    19  * @return array|object Type returned depends on $output value.
    19  */
    20  */
    20 function get_bookmark($bookmark, $output = OBJECT, $filter = 'raw') {
    21 function get_bookmark($bookmark, $output = OBJECT, $filter = 'raw') {
    58 
    59 
    59 /**
    60 /**
    60  * Retrieve single bookmark data item or field.
    61  * Retrieve single bookmark data item or field.
    61  *
    62  *
    62  * @since 2.3.0
    63  * @since 2.3.0
    63  * @uses get_bookmark() Gets bookmark object using $bookmark as ID
       
    64  * @uses sanitize_bookmark_field() Sanitizes Bookmark field based on $context.
       
    65  *
    64  *
    66  * @param string $field The name of the data field to return
    65  * @param string $field The name of the data field to return
    67  * @param int $bookmark The bookmark ID to get field
    66  * @param int $bookmark The bookmark ID to get field
    68  * @param string $context Optional. The context of how the field will be used.
    67  * @param string $context Optional. The context of how the field will be used.
    69  * @return string
    68  * @return string
    89  *
    88  *
    90  * Attempts to retrieve from the cache first based on MD5 hash of arguments. If
    89  * Attempts to retrieve from the cache first based on MD5 hash of arguments. If
    91  * that fails, then the query will be built from the arguments and executed. The
    90  * that fails, then the query will be built from the arguments and executed. The
    92  * results will be stored to the cache.
    91  * results will be stored to the cache.
    93  *
    92  *
    94  * List of default arguments are as follows:
       
    95  * 'orderby' - Default is 'name' (string). How to order the links by. String is
       
    96  *		based off of the bookmark scheme.
       
    97  * 'order' - Default is 'ASC' (string). Either 'ASC' or 'DESC'. Orders in either
       
    98  *		ascending or descending order.
       
    99  * 'limit' - Default is -1 (integer) or show all. The amount of bookmarks to
       
   100  *		display.
       
   101  * 'category' - Default is empty string (string). Include the links in what
       
   102  *		category ID(s).
       
   103  * 'category_name' - Default is empty string (string). Get links by category
       
   104  *		name.
       
   105  * 'hide_invisible' - Default is 1 (integer). Whether to show (default) or hide
       
   106  *		links marked as 'invisible'.
       
   107  * 'show_updated' - Default is 0 (integer). Will show the time of when the
       
   108  *		bookmark was last updated.
       
   109  * 'include' - Default is empty string (string). Include bookmark ID(s)
       
   110  *		separated by commas.
       
   111  * 'exclude' - Default is empty string (string). Exclude bookmark ID(s)
       
   112  *		separated by commas.
       
   113  *
       
   114  * @since 2.1.0
    93  * @since 2.1.0
   115  * @uses $wpdb Database Object
    94  *
   116  * @link http://codex.wordpress.org/Template_Tags/get_bookmarks
    95  * @global wpdb $wpdb WordPress database abstraction object.
   117  *
    96  *
   118  * @param string|array $args List of arguments to overwrite the defaults
    97  * @param string|array $args {
   119  * @return array List of bookmark row objects
    98  *     Optional. String or array of arguments to retrieve bookmarks.
   120  */
    99  *
   121 function get_bookmarks($args = '') {
   100  *     @type string   $orderby        How to order the links by. Accepts post fields. Default 'name'.
       
   101  *     @type string   $order          Whether to order bookmarks in ascending or descending order.
       
   102  *                                    Accepts 'ASC' (ascending) or 'DESC' (descending). Default 'ASC'.
       
   103  *     @type int      $limit          Amount of bookmarks to display. Accepts 1+ or -1 for all.
       
   104  *                                    Default -1.
       
   105  *     @type string   $category       Comma-separated list of category ids to include links from.
       
   106  *                                    Default empty.
       
   107  *     @type string   $category_name  Category to retrieve links for by name. Default empty.
       
   108  *     @type int|bool $hide_invisible Whether to show or hide links marked as 'invisible'. Accepts
       
   109  *                                    1|true or 0|false. Default 1|true.
       
   110  *     @type int|bool $show_updated   Whether to display the time the bookmark was last updated.
       
   111  *                                    Accepts 1|true or 0|false. Default 0|false.
       
   112  *     @type string   $include        Comma-separated list of bookmark IDs to include. Default empty.
       
   113  *     @type string   $exclude        Comma-separated list of bookmark IDs to exclude. Default empty.
       
   114  * }
       
   115  * @return array List of bookmark row objects.
       
   116  */
       
   117 function get_bookmarks( $args = '' ) {
   122 	global $wpdb;
   118 	global $wpdb;
   123 
   119 
   124 	$defaults = array(
   120 	$defaults = array(
   125 		'orderby' => 'name', 'order' => 'ASC',
   121 		'orderby' => 'name', 'order' => 'ASC',
   126 		'limit' => -1, 'category' => '',
   122 		'limit' => -1, 'category' => '',
   128 		'show_updated' => 0, 'include' => '',
   124 		'show_updated' => 0, 'include' => '',
   129 		'exclude' => '', 'search' => ''
   125 		'exclude' => '', 'search' => ''
   130 	);
   126 	);
   131 
   127 
   132 	$r = wp_parse_args( $args, $defaults );
   128 	$r = wp_parse_args( $args, $defaults );
   133 	extract( $r, EXTR_SKIP );
   129 
   134 
       
   135 	$cache = array();
       
   136 	$key = md5( serialize( $r ) );
   130 	$key = md5( serialize( $r ) );
   137 	if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) {
   131 	if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) {
   138 		if ( is_array($cache) && isset( $cache[ $key ] ) )
   132 		if ( is_array( $cache ) && isset( $cache[ $key ] ) ) {
   139 			return apply_filters('get_bookmarks', $cache[ $key ], $r );
   133 			$bookmarks = $cache[ $key ];
   140 	}
   134 			/**
   141 
   135 			 * Filter the returned list of bookmarks.
   142 	if ( !is_array($cache) )
   136 			 *
       
   137 			 * 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
       
   139 			 * link category is passed but does not exist. The third evaluation returns
       
   140 			 * the full cached results.
       
   141 			 *
       
   142 			 * @since 2.1.0
       
   143 			 *
       
   144 			 * @see get_bookmarks()
       
   145 			 *
       
   146 			 * @param array $bookmarks List of the cached bookmarks.
       
   147 			 * @param array $r         An array of bookmark query arguments.
       
   148 			 */
       
   149 			return apply_filters( 'get_bookmarks', $bookmarks, $r );
       
   150 		}
       
   151 	}
       
   152 
       
   153 	if ( ! is_array( $cache ) ) {
   143 		$cache = array();
   154 		$cache = array();
       
   155 	}
   144 
   156 
   145 	$inclusions = '';
   157 	$inclusions = '';
   146 	if ( !empty($include) ) {
   158 	if ( ! empty( $r['include'] ) ) {
   147 		$exclude = '';  //ignore exclude, category, and category_name params if using include
   159 		$r['exclude'] = '';  //ignore exclude, category, and category_name params if using include
   148 		$category = '';
   160 		$r['category'] = '';
   149 		$category_name = '';
   161 		$r['category_name'] = '';
   150 		$inclinks = preg_split('/[\s,]+/',$include);
   162 		$inclinks = preg_split( '/[\s,]+/', $r['include'] );
   151 		if ( count($inclinks) ) {
   163 		if ( count( $inclinks ) ) {
   152 			foreach ( $inclinks as $inclink ) {
   164 			foreach ( $inclinks as $inclink ) {
   153 				if (empty($inclusions))
   165 				if ( empty( $inclusions ) ) {
   154 					$inclusions = ' AND ( link_id = ' . intval($inclink) . ' ';
   166 					$inclusions = ' AND ( link_id = ' . intval( $inclink ) . ' ';
   155 				else
   167 				} else {
   156 					$inclusions .= ' OR link_id = ' . intval($inclink) . ' ';
   168 					$inclusions .= ' OR link_id = ' . intval( $inclink ) . ' ';
       
   169 				}
   157 			}
   170 			}
   158 		}
   171 		}
   159 	}
   172 	}
   160 	if (!empty($inclusions))
   173 	if (! empty( $inclusions ) ) {
   161 		$inclusions .= ')';
   174 		$inclusions .= ')';
       
   175 	}
   162 
   176 
   163 	$exclusions = '';
   177 	$exclusions = '';
   164 	if ( !empty($exclude) ) {
   178 	if ( ! empty( $r['exclude'] ) ) {
   165 		$exlinks = preg_split('/[\s,]+/',$exclude);
   179 		$exlinks = preg_split( '/[\s,]+/', $r['exclude'] );
   166 		if ( count($exlinks) ) {
   180 		if ( count( $exlinks ) ) {
   167 			foreach ( $exlinks as $exlink ) {
   181 			foreach ( $exlinks as $exlink ) {
   168 				if (empty($exclusions))
   182 				if ( empty( $exclusions ) ) {
   169 					$exclusions = ' AND ( link_id <> ' . intval($exlink) . ' ';
   183 					$exclusions = ' AND ( link_id <> ' . intval( $exlink ) . ' ';
   170 				else
   184 				} else {
   171 					$exclusions .= ' AND link_id <> ' . intval($exlink) . ' ';
   185 					$exclusions .= ' AND link_id <> ' . intval( $exlink ) . ' ';
       
   186 				}
   172 			}
   187 			}
   173 		}
   188 		}
   174 	}
   189 	}
   175 	if (!empty($exclusions))
   190 	if ( ! empty( $exclusions ) ) {
   176 		$exclusions .= ')';
   191 		$exclusions .= ')';
   177 
   192 	}
   178 	if ( !empty($category_name) ) {
   193 
   179 		if ( $category = get_term_by('name', $category_name, 'link_category') ) {
   194 	if ( ! empty( $r['category_name'] ) ) {
   180 			$category = $category->term_id;
   195 		if ( $r['category'] = get_term_by('name', $r['category_name'], 'link_category') ) {
       
   196 			$r['category'] = $r['category']->term_id;
   181 		} else {
   197 		} else {
   182 			$cache[ $key ] = array();
   198 			$cache[ $key ] = array();
   183 			wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
   199 			wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
       
   200 			/** This filter is documented in wp-includes/bookmark.php */
   184 			return apply_filters( 'get_bookmarks', array(), $r );
   201 			return apply_filters( 'get_bookmarks', array(), $r );
   185 		}
   202 		}
   186 	}
   203 	}
   187 
   204 
   188 	if ( ! empty($search) ) {
   205 	$search = '';
   189 		$search = esc_sql( like_escape( $search ) );
   206 	if ( ! empty( $r['search'] ) ) {
   190 		$search = " AND ( (link_url LIKE '%$search%') OR (link_name LIKE '%$search%') OR (link_description LIKE '%$search%') ) ";
   207 		$like = '%' . $wpdb->esc_like( $r['search'] ) . '%';
       
   208 		$search = $wpdb->prepare(" AND ( (link_url LIKE %s) OR (link_name LIKE %s) OR (link_description LIKE %s) ) ", $like, $like, $like );
   191 	}
   209 	}
   192 
   210 
   193 	$category_query = '';
   211 	$category_query = '';
   194 	$join = '';
   212 	$join = '';
   195 	if ( !empty($category) ) {
   213 	if ( ! empty( $r['category'] ) ) {
   196 		$incategories = preg_split('/[\s,]+/',$category);
   214 		$incategories = preg_split( '/[\s,]+/', $r['category'] );
   197 		if ( count($incategories) ) {
   215 		if ( count($incategories) ) {
   198 			foreach ( $incategories as $incat ) {
   216 			foreach ( $incategories as $incat ) {
   199 				if (empty($category_query))
   217 				if ( empty( $category_query ) ) {
   200 					$category_query = ' AND ( tt.term_id = ' . intval($incat) . ' ';
   218 					$category_query = ' AND ( tt.term_id = ' . intval( $incat ) . ' ';
   201 				else
   219 				} else {
   202 					$category_query .= ' OR tt.term_id = ' . intval($incat) . ' ';
   220 					$category_query .= ' OR tt.term_id = ' . intval( $incat ) . ' ';
       
   221 				}
   203 			}
   222 			}
   204 		}
   223 		}
   205 	}
   224 	}
   206 	if (!empty($category_query)) {
   225 	if ( ! empty( $category_query ) ) {
   207 		$category_query .= ") AND taxonomy = 'link_category'";
   226 		$category_query .= ") AND taxonomy = 'link_category'";
   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";
   227 		$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";
   209 	}
   228 	}
   210 
   229 
   211 	if ( $show_updated && get_option('links_recently_updated_time') ) {
   230 	if ( $r['show_updated'] ) {
   212 		$recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_option('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
   231 		$recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated ";
   213 	} else {
   232 	} else {
   214 		$recently_updated_test = '';
   233 		$recently_updated_test = '';
   215 	}
   234 	}
   216 
   235 
   217 	$get_updated = ( $show_updated ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
   236 	$get_updated = ( $r['show_updated'] ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
   218 
   237 
   219 	$orderby = strtolower($orderby);
   238 	$orderby = strtolower( $r['orderby'] );
   220 	$length = '';
   239 	$length = '';
   221 	switch ( $orderby ) {
   240 	switch ( $orderby ) {
   222 		case 'length':
   241 		case 'length':
   223 			$length = ", CHAR_LENGTH(link_name) AS length";
   242 			$length = ", CHAR_LENGTH(link_name) AS length";
   224 			break;
   243 			break;
   228 		case 'link_id':
   247 		case 'link_id':
   229 			$orderby = "$wpdb->links.link_id";
   248 			$orderby = "$wpdb->links.link_id";
   230 			break;
   249 			break;
   231 		default:
   250 		default:
   232 			$orderparams = array();
   251 			$orderparams = array();
   233 			foreach ( explode(',', $orderby) as $ordparam ) {
   252 			$keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes', 'link_description' );
   234 				$ordparam = trim($ordparam);
   253 			foreach ( explode( ',', $orderby ) as $ordparam ) {
   235 				$keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes' );
   254 				$ordparam = trim( $ordparam );
   236 				if ( in_array( 'link_' . $ordparam, $keys ) )
   255 
       
   256 				if ( in_array( 'link_' . $ordparam, $keys ) ) {
   237 					$orderparams[] = 'link_' . $ordparam;
   257 					$orderparams[] = 'link_' . $ordparam;
   238 				elseif ( in_array( $ordparam, $keys ) )
   258 				} elseif ( in_array( $ordparam, $keys ) ) {
   239 					$orderparams[] = $ordparam;
   259 					$orderparams[] = $ordparam;
       
   260 				}
   240 			}
   261 			}
   241 			$orderby = implode(',', $orderparams);
   262 			$orderby = implode( ',', $orderparams );
   242 	}
   263 	}
   243 
   264 
   244 	if ( empty( $orderby ) )
   265 	if ( empty( $orderby ) ) {
   245 		$orderby = 'link_name';
   266 		$orderby = 'link_name';
   246 
   267 	}
   247 	$order = strtoupper( $order );
   268 
   248 	if ( '' !== $order && !in_array( $order, array( 'ASC', 'DESC' ) ) )
   269 	$order = strtoupper( $r['order'] );
       
   270 	if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) {
   249 		$order = 'ASC';
   271 		$order = 'ASC';
       
   272 	}
   250 
   273 
   251 	$visible = '';
   274 	$visible = '';
   252 	if ( $hide_invisible )
   275 	if ( $r['hide_invisible'] ) {
   253 		$visible = "AND link_visible = 'Y'";
   276 		$visible = "AND link_visible = 'Y'";
       
   277 	}
   254 
   278 
   255 	$query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
   279 	$query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
   256 	$query .= " $exclusions $inclusions $search";
   280 	$query .= " $exclusions $inclusions $search";
   257 	$query .= " ORDER BY $orderby $order";
   281 	$query .= " ORDER BY $orderby $order";
   258 	if ($limit != -1)
   282 	if ( $r['limit'] != -1 ) {
   259 		$query .= " LIMIT $limit";
   283 		$query .= ' LIMIT ' . $r['limit'];
   260 
   284 	}
   261 	$results = $wpdb->get_results($query);
   285 
       
   286 	$results = $wpdb->get_results( $query );
   262 
   287 
   263 	$cache[ $key ] = $results;
   288 	$cache[ $key ] = $results;
   264 	wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
   289 	wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
   265 
   290 
   266 	return apply_filters('get_bookmarks', $results, $r);
   291 	/** This filter is documented in wp-includes/bookmark.php */
       
   292 	return apply_filters( 'get_bookmarks', $results, $r );
   267 }
   293 }
   268 
   294 
   269 /**
   295 /**
   270  * Sanitizes all bookmark fields
   296  * Sanitizes all bookmark fields
   271  *
   297  *
   335 	case 'link_category' : // array( ints )
   361 	case 'link_category' : // array( ints )
   336 		$value = array_map('absint', (array) $value);
   362 		$value = array_map('absint', (array) $value);
   337 		// We return here so that the categories aren't filtered.
   363 		// We return here so that the categories aren't filtered.
   338 		// The 'link_category' filter is for the name of a link category, not an array of a link's link categories
   364 		// The 'link_category' filter is for the name of a link category, not an array of a link's link categories
   339 		return $value;
   365 		return $value;
   340 		break;
   366 
   341 	case 'link_visible' : // bool stored as Y|N
   367 	case 'link_visible' : // bool stored as Y|N
   342 		$value = preg_replace('/[^YNyn]/', '', $value);
   368 		$value = preg_replace('/[^YNyn]/', '', $value);
   343 		break;
   369 		break;
   344 	case 'link_target' : // "enum"
   370 	case 'link_target' : // "enum"
   345 		$targets = array('_top', '_blank');
   371 		$targets = array('_top', '_blank');
   350 
   376 
   351 	if ( 'raw' == $context )
   377 	if ( 'raw' == $context )
   352 		return $value;
   378 		return $value;
   353 
   379 
   354 	if ( 'edit' == $context ) {
   380 	if ( 'edit' == $context ) {
   355 		$value = apply_filters("edit_$field", $value, $bookmark_id);
   381 		/** This filter is documented in wp-includes/post.php */
       
   382 		$value = apply_filters( "edit_$field", $value, $bookmark_id );
   356 
   383 
   357 		if ( 'link_notes' == $field ) {
   384 		if ( 'link_notes' == $field ) {
   358 			$value = esc_html( $value ); // textarea_escaped
   385 			$value = esc_html( $value ); // textarea_escaped
   359 		} else {
   386 		} else {
   360 			$value = esc_attr($value);
   387 			$value = esc_attr($value);
   361 		}
   388 		}
   362 	} else if ( 'db' == $context ) {
   389 	} elseif ( 'db' == $context ) {
   363 		$value = apply_filters("pre_$field", $value);
   390 		/** This filter is documented in wp-includes/post.php */
       
   391 		$value = apply_filters( "pre_$field", $value );
   364 	} else {
   392 	} else {
   365 		// Use display filters by default.
   393 		/** This filter is documented in wp-includes/post.php */
   366 		$value = apply_filters($field, $value, $bookmark_id, $context);
   394 		$value = apply_filters( $field, $value, $bookmark_id, $context );
   367 
   395 
   368 		if ( 'attribute' == $context )
   396 		if ( 'attribute' == $context ) {
   369 			$value = esc_attr($value);
   397 			$value = esc_attr( $value );
   370 		else if ( 'js' == $context )
   398 		} elseif ( 'js' == $context ) {
   371 			$value = esc_js($value);
   399 			$value = esc_js( $value );
       
   400 		}
   372 	}
   401 	}
   373 
   402 
   374 	return $value;
   403 	return $value;
   375 }
   404 }
   376 
   405 
   377 /**
   406 /**
   378  * Deletes bookmark cache
   407  * Deletes bookmark cache
   379  *
   408  *
   380  * @since 2.7.0
   409  * @since 2.7.0
   381  * @uses wp_cache_delete() Deletes the contents of 'get_bookmarks'
       
   382  */
   410  */
   383 function clean_bookmark_cache( $bookmark_id ) {
   411 function clean_bookmark_cache( $bookmark_id ) {
   384 	wp_cache_delete( $bookmark_id, 'bookmark' );
   412 	wp_cache_delete( $bookmark_id, 'bookmark' );
   385 	wp_cache_delete( 'get_bookmarks', 'bookmark' );
   413 	wp_cache_delete( 'get_bookmarks', 'bookmark' );
   386 	clean_object_term_cache( $bookmark_id, 'link');
   414 	clean_object_term_cache( $bookmark_id, 'link');