-
+ 7CC871DC7100DFFB2484FD11C763280368633E021752C6B506F5CDF797816F9801D6E316FD7FADE96372227F8E1F872CDA42187DAEF1F661DB3C10704920EA69
mp-wp/wp-includes/bookmark.php
(0 . 0)(1 . 366)
72017 <?php
72018 /**
72019 * Link/Bookmark API
72020 *
72021 * @package WordPress
72022 * @subpackage Bookmark
72023 */
72024
72025 /**
72026 * Retrieve Bookmark data based on ID
72027 *
72028 * @since 2.1.0
72029 * @uses $wpdb Database Object
72030 *
72031 * @param int $bookmark_id
72032 * @param string $output Optional. Either OBJECT, ARRAY_N, or ARRAY_A constant
72033 * @param string $filter Optional, default is 'raw'.
72034 * @return array|object Type returned depends on $output value.
72035 */
72036 function get_bookmark($bookmark, $output = OBJECT, $filter = 'raw') {
72037 global $wpdb;
72038
72039 if ( empty($bookmark) ) {
72040 if ( isset($GLOBALS['link']) )
72041 $_bookmark = & $GLOBALS['link'];
72042 else
72043 $_bookmark = null;
72044 } elseif ( is_object($bookmark) ) {
72045 wp_cache_add($bookmark->link_id, $bookmark, 'bookmark');
72046 $_bookmark = $bookmark;
72047 } else {
72048 if ( isset($GLOBALS['link']) && ($GLOBALS['link']->link_id == $bookmark) ) {
72049 $_bookmark = & $GLOBALS['link'];
72050 } elseif ( ! $_bookmark = wp_cache_get($bookmark, 'bookmark') ) {
72051 $_bookmark = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark));
72052 $_bookmark->link_category = array_unique( wp_get_object_terms($_bookmark->link_id, 'link_category', 'fields=ids') );
72053 wp_cache_add($_bookmark->link_id, $_bookmark, 'bookmark');
72054 }
72055 }
72056
72057 $_bookmark = sanitize_bookmark($_bookmark, $filter);
72058
72059 if ( $output == OBJECT ) {
72060 return $_bookmark;
72061 } elseif ( $output == ARRAY_A ) {
72062 return get_object_vars($_bookmark);
72063 } elseif ( $output == ARRAY_N ) {
72064 return array_values(get_object_vars($_bookmark));
72065 } else {
72066 return $_bookmark;
72067 }
72068 }
72069
72070 /**
72071 * Retrieve single bookmark data item or field.
72072 *
72073 * @since 2.3.0
72074 * @uses get_bookmark() Gets bookmark object using $bookmark as ID
72075 * @uses sanitize_bookmark_field() Sanitizes Bookmark field based on $context.
72076 *
72077 * @param string $field The name of the data field to return
72078 * @param int $bookmark The bookmark ID to get field
72079 * @param string $context Optional. The context of how the field will be used.
72080 * @return string
72081 */
72082 function get_bookmark_field( $field, $bookmark, $context = 'display' ) {
72083 $bookmark = (int) $bookmark;
72084 $bookmark = get_bookmark( $bookmark );
72085
72086 if ( is_wp_error($bookmark) )
72087 return $bookmark;
72088
72089 if ( !is_object($bookmark) )
72090 return '';
72091
72092 if ( !isset($bookmark->$field) )
72093 return '';
72094
72095 return sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
72096 }
72097
72098 /**
72099 * Retrieve bookmark data based on ID.
72100 *
72101 * @since 2.0.0
72102 * @deprecated Use get_bookmark()
72103 * @see get_bookmark()
72104 *
72105 * @param int $bookmark_id ID of link
72106 * @param string $output Either OBJECT, ARRAY_N, or ARRAY_A
72107 * @return object|array
72108 */
72109 function get_link($bookmark_id, $output = OBJECT, $filter = 'raw') {
72110 return get_bookmark($bookmark_id, $output, $filter);
72111 }
72112
72113 /**
72114 * Retrieves the list of bookmarks
72115 *
72116 * Attempts to retrieve from the cache first based on MD5 hash of arguments. If
72117 * that fails, then the query will be built from the arguments and executed. The
72118 * results will be stored to the cache.
72119 *
72120 * List of default arguments are as follows:
72121 * 'orderby' - Default is 'name' (string). How to order the links by. String is
72122 * based off of the bookmark scheme.
72123 * 'order' - Default is 'ASC' (string). Either 'ASC' or 'DESC'. Orders in either
72124 * ascending or descending order.
72125 * 'limit' - Default is -1 (integer) or show all. The amount of bookmarks to
72126 * display.
72127 * 'category' - Default is empty string (string). Include the links in what
72128 * category ID(s).
72129 * 'category_name' - Default is empty string (string). Get links by category
72130 * name.
72131 * 'hide_invisible' - Default is 1 (integer). Whether to show (default) or hide
72132 * links marked as 'invisible'.
72133 * 'show_updated' - Default is 0 (integer). Will show the time of when the
72134 * bookmark was last updated.
72135 * 'include' - Default is empty string (string). Include other categories
72136 * separated by commas.
72137 * 'exclude' - Default is empty string (string). Exclude other categories
72138 * separated by commas.
72139 *
72140 * @since 2.1.0
72141 * @uses $wpdb Database Object
72142 * @link http://codex.wordpress.org/Template_Tags/get_bookmarks
72143 *
72144 * @param string|array $args List of arguments to overwrite the defaults
72145 * @return array List of bookmark row objects
72146 */
72147 function get_bookmarks($args = '') {
72148 global $wpdb;
72149
72150 $defaults = array(
72151 'orderby' => 'name', 'order' => 'ASC',
72152 'limit' => -1, 'category' => '',
72153 'category_name' => '', 'hide_invisible' => 1,
72154 'show_updated' => 0, 'include' => '',
72155 'exclude' => '', 'search' => ''
72156 );
72157
72158 $r = wp_parse_args( $args, $defaults );
72159 extract( $r, EXTR_SKIP );
72160
72161 $key = md5( serialize( $r ) );
72162 if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) )
72163 if ( isset( $cache[ $key ] ) )
72164 return apply_filters('get_bookmarks', $cache[ $key ], $r );
72165
72166 $inclusions = '';
72167 if ( !empty($include) ) {
72168 $exclude = ''; //ignore exclude, category, and category_name params if using include
72169 $category = '';
72170 $category_name = '';
72171 $inclinks = preg_split('/[\s,]+/',$include);
72172 if ( count($inclinks) ) {
72173 foreach ( $inclinks as $inclink ) {
72174 if (empty($inclusions))
72175 $inclusions = ' AND ( link_id = ' . intval($inclink) . ' ';
72176 else
72177 $inclusions .= ' OR link_id = ' . intval($inclink) . ' ';
72178 }
72179 }
72180 }
72181 if (!empty($inclusions))
72182 $inclusions .= ')';
72183
72184 $exclusions = '';
72185 if ( !empty($exclude) ) {
72186 $exlinks = preg_split('/[\s,]+/',$exclude);
72187 if ( count($exlinks) ) {
72188 foreach ( $exlinks as $exlink ) {
72189 if (empty($exclusions))
72190 $exclusions = ' AND ( link_id <> ' . intval($exlink) . ' ';
72191 else
72192 $exclusions .= ' AND link_id <> ' . intval($exlink) . ' ';
72193 }
72194 }
72195 }
72196 if (!empty($exclusions))
72197 $exclusions .= ')';
72198
72199 if ( ! empty($category_name) ) {
72200 if ( $category = get_term_by('name', $category_name, 'link_category') )
72201 $category = $category->term_id;
72202 }
72203
72204 if ( ! empty($search) ) {
72205 $search = like_escape($search);
72206 $search = " AND ( (link_url LIKE '%$search%') OR (link_name LIKE '%$search%') OR (link_description LIKE '%$search%') ) ";
72207 }
72208
72209 $category_query = '';
72210 $join = '';
72211 if ( !empty($category) ) {
72212 $incategories = preg_split('/[\s,]+/',$category);
72213 if ( count($incategories) ) {
72214 foreach ( $incategories as $incat ) {
72215 if (empty($category_query))
72216 $category_query = ' AND ( tt.term_id = ' . intval($incat) . ' ';
72217 else
72218 $category_query .= ' OR tt.term_id = ' . intval($incat) . ' ';
72219 }
72220 }
72221 }
72222 if (!empty($category_query)) {
72223 $category_query .= ") AND taxonomy = 'link_category'";
72224 $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";
72225 }
72226
72227 if (get_option('links_recently_updated_time')) {
72228 $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_option('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
72229 } else {
72230 $recently_updated_test = '';
72231 }
72232
72233 $get_updated = ( $show_updated ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
72234
72235 $orderby = strtolower($orderby);
72236 $length = '';
72237 switch ($orderby) {
72238 case 'length':
72239 $length = ", CHAR_LENGTH(link_name) AS length";
72240 break;
72241 case 'rand':
72242 $orderby = 'rand()';
72243 break;
72244 default:
72245 $orderby = "link_" . $orderby;
72246 }
72247
72248 if ( 'link_id' == $orderby )
72249 $orderby = "$wpdb->links.link_id";
72250
72251 $visible = '';
72252 if ( $hide_invisible )
72253 $visible = "AND link_visible = 'Y'";
72254
72255 $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
72256 $query .= " $exclusions $inclusions $search";
72257 $query .= " ORDER BY $orderby $order";
72258 if ($limit != -1)
72259 $query .= " LIMIT $limit";
72260
72261 $results = $wpdb->get_results($query);
72262
72263 $cache[ $key ] = $results;
72264 wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
72265
72266 return apply_filters('get_bookmarks', $results, $r);
72267 }
72268
72269 /**
72270 * Sanitizes all bookmark fields
72271 *
72272 * @since 2.3.0
72273 *
72274 * @param object|array $bookmark Bookmark row
72275 * @param string $context Optional, default is 'display'. How to filter the
72276 * fields
72277 * @return object|array Same type as $bookmark but with fields sanitized.
72278 */
72279 function sanitize_bookmark($bookmark, $context = 'display') {
72280 $fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category',
72281 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated',
72282 'link_rel', 'link_notes', 'link_rss', );
72283
72284 if ( is_object($bookmark) ) {
72285 $do_object = true;
72286 $link_id = $bookmark->link_id;
72287 } else {
72288 $do_object = false;
72289 $link_id = $bookmark['link_id'];
72290 }
72291
72292 foreach ( $fields as $field ) {
72293 if ( $do_object ) {
72294 if ( isset($bookmark->$field) )
72295 $bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $link_id, $context);
72296 } else {
72297 if ( isset($bookmark[$field]) )
72298 $bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $link_id, $context);
72299 }
72300 }
72301
72302 return $bookmark;
72303 }
72304
72305 /**
72306 * Sanitizes a bookmark field
72307 *
72308 * Sanitizes the bookmark fields based on what the field name is. If the field
72309 * has a strict value set, then it will be tested for that, else a more generic
72310 * filtering is applied. After the more strict filter is applied, if the
72311 * $context is 'raw' then the value is immediately return.
72312 *
72313 * Hooks exist for the more generic cases. With the 'edit' context, the
72314 * 'edit_$field' filter will be called and passed the $value and $bookmark_id
72315 * respectively. With the 'db' context, the 'pre_$field' filter is called and
72316 * passed the value. The 'display' context is the final context and has the
72317 * $field has the filter name and is passed the $value, $bookmark_id, and
72318 * $context respectively.
72319 *
72320 * @since 2.3.0
72321 *
72322 * @param string $field The bookmark field
72323 * @param mixed $value The bookmark field value
72324 * @param int $bookmark_id Bookmark ID
72325 * @param string $context How to filter the field value. Either 'raw', 'edit',
72326 * 'attribute', 'js', 'db', or 'display'
72327 * @return mixed The filtered value
72328 */
72329 function sanitize_bookmark_field($field, $value, $bookmark_id, $context) {
72330 $int_fields = array('link_id', 'link_rating');
72331 if ( in_array($field, $int_fields) )
72332 $value = (int) $value;
72333
72334 $yesno = array('link_visible');
72335 if ( in_array($field, $yesno) )
72336 $value = preg_replace('/[^YNyn]/', '', $value);
72337
72338 if ( 'link_target' == $field ) {
72339 $targets = array('_top', '_blank');
72340 if ( ! in_array($value, $targets) )
72341 $value = '';
72342 }
72343
72344 if ( 'raw' == $context )
72345 return $value;
72346
72347 if ( 'edit' == $context ) {
72348 $format_to_edit = array('link_notes');
72349 $value = apply_filters("edit_$field", $value, $bookmark_id);
72350
72351 if ( in_array($field, $format_to_edit) ) {
72352 $value = format_to_edit($value);
72353 } else {
72354 $value = attribute_escape($value);
72355 }
72356 } else if ( 'db' == $context ) {
72357 $value = apply_filters("pre_$field", $value);
72358 } else {
72359 // Use display filters by default.
72360 $value = apply_filters($field, $value, $bookmark_id, $context);
72361 }
72362
72363 if ( 'attribute' == $context )
72364 $value = attribute_escape($value);
72365 else if ( 'js' == $context )
72366 $value = js_escape($value);
72367
72368 return $value;
72369 }
72370
72371 /**
72372 * Deletes bookmark cache
72373 *
72374 * @since 2.7.0
72375 * @uses wp_cache_delete() Deletes the contents of 'get_bookmarks'
72376 */
72377 function clean_bookmark_cache($bookmark_id) {
72378 wp_cache_delete( $bookmark_id, 'bookmark' );
72379 wp_cache_delete( 'get_bookmarks', 'bookmark' );
72380 }
72381
72382 ?>