wp/wp-includes/bookmark.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * Link/Bookmark API
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * @subpackage Bookmark
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 * Retrieve Bookmark data
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    14
 * @global wpdb $wpdb WordPress database abstraction object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    15
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    16
 * @param int|stdClass $bookmark
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * @param string $output Optional. Either OBJECT, ARRAY_N, or ARRAY_A constant
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 * @param string $filter Optional, default is 'raw'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 * @return array|object Type returned depends on $output value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
function get_bookmark($bookmark, $output = OBJECT, $filter = 'raw') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
	if ( empty($bookmark) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
		if ( isset($GLOBALS['link']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
			$_bookmark = & $GLOBALS['link'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
			$_bookmark = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
	} elseif ( is_object($bookmark) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
		wp_cache_add($bookmark->link_id, $bookmark, 'bookmark');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
		$_bookmark = $bookmark;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
		if ( isset($GLOBALS['link']) && ($GLOBALS['link']->link_id == $bookmark) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
			$_bookmark = & $GLOBALS['link'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
		} elseif ( ! $_bookmark = wp_cache_get($bookmark, 'bookmark') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
			$_bookmark = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
			if ( $_bookmark ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
				$_bookmark->link_category = array_unique( wp_get_object_terms( $_bookmark->link_id, 'link_category', array( 'fields' => 'ids' ) ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
				wp_cache_add( $_bookmark->link_id, $_bookmark, 'bookmark' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
	if ( ! $_bookmark )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
		return $_bookmark;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
	$_bookmark = sanitize_bookmark($_bookmark, $filter);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
	if ( $output == OBJECT ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
		return $_bookmark;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
	} elseif ( $output == ARRAY_A ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
		return get_object_vars($_bookmark);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
	} elseif ( $output == ARRAY_N ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
		return array_values(get_object_vars($_bookmark));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
		return $_bookmark;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
 * Retrieve single bookmark data item or field.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
 * @param string $field The name of the data field to return
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
 * @param int $bookmark The bookmark ID to get field
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
 * @param string $context Optional. The context of how the field will be used.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
 * @return string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
function get_bookmark_field( $field, $bookmark, $context = 'display' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
	$bookmark = (int) $bookmark;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
	$bookmark = get_bookmark( $bookmark );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
	if ( is_wp_error($bookmark) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		return $bookmark;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
	if ( !is_object($bookmark) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
	if ( !isset($bookmark->$field) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
	return sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
 * Retrieves the list of bookmarks
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
 * Attempts to retrieve from the cache first based on MD5 hash of arguments. If
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
 * that fails, then the query will be built from the arguments and executed. The
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
 * results will be stored to the cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    93
 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    94
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    95
 * @global wpdb $wpdb WordPress database abstraction object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    96
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    97
 * @param string|array $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    98
 *     Optional. String or array of arguments to retrieve bookmarks.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   100
 *     @type string   $orderby        How to order the links by. Accepts post fields. Default 'name'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   101
 *     @type string   $order          Whether to order bookmarks in ascending or descending order.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   102
 *                                    Accepts 'ASC' (ascending) or 'DESC' (descending). Default 'ASC'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   103
 *     @type int      $limit          Amount of bookmarks to display. Accepts 1+ or -1 for all.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   104
 *                                    Default -1.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   105
 *     @type string   $category       Comma-separated list of category ids to include links from.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   106
 *                                    Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   107
 *     @type string   $category_name  Category to retrieve links for by name. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   108
 *     @type int|bool $hide_invisible Whether to show or hide links marked as 'invisible'. Accepts
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   109
 *                                    1|true or 0|false. Default 1|true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   110
 *     @type int|bool $show_updated   Whether to display the time the bookmark was last updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   111
 *                                    Accepts 1|true or 0|false. Default 0|false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   112
 *     @type string   $include        Comma-separated list of bookmark IDs to include. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   113
 *     @type string   $exclude        Comma-separated list of bookmark IDs to exclude. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   114
 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   115
 * @return array List of bookmark row objects.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   117
function get_bookmarks( $args = '' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
	$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
		'orderby' => 'name', 'order' => 'ASC',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
		'limit' => -1, 'category' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
		'category_name' => '', 'hide_invisible' => 1,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
		'show_updated' => 0, 'include' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
		'exclude' => '', 'search' => ''
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
	$r = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
	$key = md5( serialize( $r ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
	if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   132
		if ( is_array( $cache ) && isset( $cache[ $key ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   133
			$bookmarks = $cache[ $key ];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   134
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   135
			 * Filter the returned list of bookmarks.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   136
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   137
			 * The first time the hook is evaluated in this file, it returns the cached
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   138
			 * bookmarks list. The second evaluation returns a cached bookmarks list if the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   139
			 * link category is passed but does not exist. The third evaluation returns
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   140
			 * the full cached results.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   141
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   142
			 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   143
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   144
			 * @see get_bookmarks()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   145
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   146
			 * @param array $bookmarks List of the cached bookmarks.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   147
			 * @param array $r         An array of bookmark query arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   148
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   149
			return apply_filters( 'get_bookmarks', $bookmarks, $r );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   150
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   153
	if ( ! is_array( $cache ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
		$cache = array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   155
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
	$inclusions = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   158
	if ( ! empty( $r['include'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   159
		$r['exclude'] = '';  //ignore exclude, category, and category_name params if using include
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   160
		$r['category'] = '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   161
		$r['category_name'] = '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   162
		$inclinks = preg_split( '/[\s,]+/', $r['include'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   163
		if ( count( $inclinks ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
			foreach ( $inclinks as $inclink ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   165
				if ( empty( $inclusions ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   166
					$inclusions = ' AND ( link_id = ' . intval( $inclink ) . ' ';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   167
				} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   168
					$inclusions .= ' OR link_id = ' . intval( $inclink ) . ' ';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   169
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   173
	if (! empty( $inclusions ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
		$inclusions .= ')';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   175
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
	$exclusions = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   178
	if ( ! empty( $r['exclude'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   179
		$exlinks = preg_split( '/[\s,]+/', $r['exclude'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   180
		if ( count( $exlinks ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
			foreach ( $exlinks as $exlink ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   182
				if ( empty( $exclusions ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   183
					$exclusions = ' AND ( link_id <> ' . intval( $exlink ) . ' ';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   184
				} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   185
					$exclusions .= ' AND link_id <> ' . intval( $exlink ) . ' ';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   186
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   190
	if ( ! empty( $exclusions ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
		$exclusions .= ')';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   192
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   194
	if ( ! empty( $r['category_name'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   195
		if ( $r['category'] = get_term_by('name', $r['category_name'], 'link_category') ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   196
			$r['category'] = $r['category']->term_id;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
			$cache[ $key ] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
			wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   200
			/** This filter is documented in wp-includes/bookmark.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
			return apply_filters( 'get_bookmarks', array(), $r );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   205
	$search = '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   206
	if ( ! empty( $r['search'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   207
		$like = '%' . $wpdb->esc_like( $r['search'] ) . '%';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   208
		$search = $wpdb->prepare(" AND ( (link_url LIKE %s) OR (link_name LIKE %s) OR (link_description LIKE %s) ) ", $like, $like, $like );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
	$category_query = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
	$join = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   213
	if ( ! empty( $r['category'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   214
		$incategories = preg_split( '/[\s,]+/', $r['category'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
		if ( count($incategories) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
			foreach ( $incategories as $incat ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   217
				if ( empty( $category_query ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   218
					$category_query = ' AND ( tt.term_id = ' . intval( $incat ) . ' ';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   219
				} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   220
					$category_query .= ' OR tt.term_id = ' . intval( $incat ) . ' ';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   221
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   225
	if ( ! empty( $category_query ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
		$category_query .= ") AND taxonomy = 'link_category'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   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";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   230
	if ( $r['show_updated'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   231
		$recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated ";
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
		$recently_updated_test = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   236
	$get_updated = ( $r['show_updated'] ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   238
	$orderby = strtolower( $r['orderby'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
	$length = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
	switch ( $orderby ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
		case 'length':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
			$length = ", CHAR_LENGTH(link_name) AS length";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
		case 'rand':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
			$orderby = 'rand()';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
		case 'link_id':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
			$orderby = "$wpdb->links.link_id";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
		default:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
			$orderparams = array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   252
			$keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes', 'link_description' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   253
			foreach ( explode( ',', $orderby ) as $ordparam ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   254
				$ordparam = trim( $ordparam );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   255
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   256
				if ( in_array( 'link_' . $ordparam, $keys ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
					$orderparams[] = 'link_' . $ordparam;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   258
				} elseif ( in_array( $ordparam, $keys ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
					$orderparams[] = $ordparam;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   260
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
			}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   262
			$orderby = implode( ',', $orderparams );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   265
	if ( empty( $orderby ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
		$orderby = 'link_name';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   267
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   269
	$order = strtoupper( $r['order'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   270
	if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
		$order = 'ASC';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   272
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
	$visible = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   275
	if ( $r['hide_invisible'] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
		$visible = "AND link_visible = 'Y'";
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   277
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
	$query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
	$query .= " $exclusions $inclusions $search";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
	$query .= " ORDER BY $orderby $order";
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   282
	if ( $r['limit'] != -1 ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   283
		$query .= ' LIMIT ' . $r['limit'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   284
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   286
	$results = $wpdb->get_results( $query );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
	$cache[ $key ] = $results;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
	wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   291
	/** This filter is documented in wp-includes/bookmark.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   292
	return apply_filters( 'get_bookmarks', $results, $r );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
 * Sanitizes all bookmark fields
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
 * @param object|array $bookmark Bookmark row
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
 * @param string $context Optional, default is 'display'. How to filter the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
 *		fields
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
 * @return object|array Same type as $bookmark but with fields sanitized.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
function sanitize_bookmark($bookmark, $context = 'display') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
	$fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
		'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
		'link_rel', 'link_notes', 'link_rss', );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
	if ( is_object($bookmark) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
		$do_object = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
		$link_id = $bookmark->link_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
		$do_object = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
		$link_id = $bookmark['link_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
	foreach ( $fields as $field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
		if ( $do_object ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
			if ( isset($bookmark->$field) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
				$bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $link_id, $context);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
			if ( isset($bookmark[$field]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
				$bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $link_id, $context);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
	return $bookmark;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
 * Sanitizes a bookmark field
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
 * Sanitizes the bookmark fields based on what the field name is. If the field
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
 * has a strict value set, then it will be tested for that, else a more generic
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
 * filtering is applied. After the more strict filter is applied, if the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
 * $context is 'raw' then the value is immediately return.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
 * Hooks exist for the more generic cases. With the 'edit' context, the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
 * 'edit_$field' filter will be called and passed the $value and $bookmark_id
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
 * respectively. With the 'db' context, the 'pre_$field' filter is called and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
 * passed the value. The 'display' context is the final context and has the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
 * $field has the filter name and is passed the $value, $bookmark_id, and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
 * $context respectively.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
 * @param string $field The bookmark field
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
 * @param mixed $value The bookmark field value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
 * @param int $bookmark_id Bookmark ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
 * @param string $context How to filter the field value. Either 'raw', 'edit',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
 *		'attribute', 'js', 'db', or 'display'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
 * @return mixed The filtered value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
function sanitize_bookmark_field($field, $value, $bookmark_id, $context) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
	switch ( $field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
	case 'link_id' : // ints
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
	case 'link_rating' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
		$value = (int) $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
	case 'link_category' : // array( ints )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
		$value = array_map('absint', (array) $value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
		// We return here so that the categories aren't filtered.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
		// The 'link_category' filter is for the name of a link category, not an array of a link's link categories
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
		return $value;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   366
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
	case 'link_visible' : // bool stored as Y|N
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
		$value = preg_replace('/[^YNyn]/', '', $value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
	case 'link_target' : // "enum"
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
		$targets = array('_top', '_blank');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
		if ( ! in_array($value, $targets) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
			$value = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
	if ( 'raw' == $context )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
		return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
	if ( 'edit' == $context ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   381
		/** This filter is documented in wp-includes/post.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   382
		$value = apply_filters( "edit_$field", $value, $bookmark_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
		if ( 'link_notes' == $field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
			$value = esc_html( $value ); // textarea_escaped
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
			$value = esc_attr($value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
		}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   389
	} elseif ( 'db' == $context ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   390
		/** This filter is documented in wp-includes/post.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   391
		$value = apply_filters( "pre_$field", $value );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
	} else {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   393
		/** This filter is documented in wp-includes/post.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   394
		$value = apply_filters( $field, $value, $bookmark_id, $context );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   396
		if ( 'attribute' == $context ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   397
			$value = esc_attr( $value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   398
		} elseif ( 'js' == $context ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   399
			$value = esc_js( $value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   400
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
	return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
 * Deletes bookmark cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
function clean_bookmark_cache( $bookmark_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
	wp_cache_delete( $bookmark_id, 'bookmark' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
	wp_cache_delete( 'get_bookmarks', 'bookmark' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
	clean_object_term_cache( $bookmark_id, 'link');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
}