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