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