wp/wp-includes/meta.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
 * Metadata 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
 * Functions for retrieving and manipulating metadata of various WordPress object types. Metadata
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * for an object is a represented by a simple key-value pair. Objects may contain multiple
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * metadata entries that share the same key and differ only in their value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 * @subpackage Meta
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 * Add metadata for the specified object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * @since 2.9.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    18
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    19
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 * @param int $object_id ID of the object metadata is for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
 * @param string $meta_key Metadata key
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
 * @param bool $unique Optional, default is false. Whether the specified metadata key should be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
 * 		unique for the object. If true, and the object already has a value for the specified
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
 * 		metadata key, no change will be made
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    28
 * @return int|bool The meta ID on success, false on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
function add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique = false) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    31
	global $wpdb;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    33
	if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    35
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    37
	$object_id = absint( $object_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    38
	if ( ! $object_id ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    40
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    42
	$table = _get_meta_table( $meta_type );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    43
	if ( ! $table ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    44
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    45
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
	$column = sanitize_key($meta_type . '_id');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
	// expected_slashed ($meta_key)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
	$meta_key = wp_unslash($meta_key);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
	$meta_value = wp_unslash($meta_value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
	$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    54
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    55
	 * Filter whether to add metadata of a specific type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    56
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    57
	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    58
	 * object type (comment, post, or user). Returning a non-null value
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    59
	 * will effectively short-circuit the function.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    60
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    61
	 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    62
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    63
	 * @param null|bool $check      Whether to allow adding metadata for the given type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    64
	 * @param int       $object_id  Object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    65
	 * @param string    $meta_key   Meta key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    66
	 * @param mixed     $meta_value Meta value. Must be serializable if non-scalar.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    67
	 * @param bool      $unique     Whether the specified meta key should be unique
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    68
	 *                              for the object. Optional. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    69
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
	$check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
	if ( null !== $check )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
		return $check;
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 ( $unique && $wpdb->get_var( $wpdb->prepare(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		"SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d",
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
		$meta_key, $object_id ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
	$_meta_value = $meta_value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
	$meta_value = maybe_serialize( $meta_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    82
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    83
	 * Fires immediately before meta of a specific type is added.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    84
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    85
	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    86
	 * object type (comment, post, or user).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    87
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    88
	 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    89
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    90
	 * @param int    $object_id  Object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    91
	 * @param string $meta_key   Meta key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    92
	 * @param mixed  $meta_value Meta value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    93
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
	do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
	$result = $wpdb->insert( $table, array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
		$column => $object_id,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
		'meta_key' => $meta_key,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
		'meta_value' => $meta_value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
	if ( ! $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
	$mid = (int) $wpdb->insert_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
	wp_cache_delete($object_id, $meta_type . '_meta');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   109
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   110
	 * Fires immediately after meta of a specific type is added.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   111
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   112
	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   113
	 * object type (comment, post, or user).
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
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   116
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   117
	 * @param int    $mid        The meta ID after successful update.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   118
	 * @param int    $object_id  Object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   119
	 * @param string $meta_key   Meta key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   120
	 * @param mixed  $meta_value Meta value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   121
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
	do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
	return $mid;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
}
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
 * Update metadata for the specified object. If no value already exists for the specified object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
 * ID and metadata key, the metadata will be added.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
 * @since 2.9.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   132
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   133
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
 * @param int $object_id ID of the object metadata is for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
 * @param string $meta_key Metadata key
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
 * @param mixed $prev_value Optional. If specified, only update existing metadata entries with
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
 * 		the specified value. Otherwise, update all entries.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   141
 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value = '') {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   144
	global $wpdb;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   146
	if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   148
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   150
	$object_id = absint( $object_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   151
	if ( ! $object_id ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   153
	}
0
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
	$table = _get_meta_table( $meta_type );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   156
	if ( ! $table ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   157
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   158
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
	$column = sanitize_key($meta_type . '_id');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
	$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
	// expected_slashed ($meta_key)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
	$meta_key = wp_unslash($meta_key);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
	$passed_value = $meta_value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
	$meta_value = wp_unslash($meta_value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
	$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   169
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   170
	 * Filter whether to update metadata of a specific type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   171
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   172
	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   173
	 * object type (comment, post, or user). Returning a non-null value
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   174
	 * will effectively short-circuit the function.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   175
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   176
	 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   177
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   178
	 * @param null|bool $check      Whether to allow updating metadata for the given type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   179
	 * @param int       $object_id  Object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   180
	 * @param string    $meta_key   Meta key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   181
	 * @param mixed     $meta_value Meta value. Must be serializable if non-scalar.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   182
	 * @param mixed     $prev_value Optional. If specified, only update existing
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   183
	 *                              metadata entries with the specified value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   184
	 *                              Otherwise, update all entries.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   185
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
	$check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
	if ( null !== $check )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
		return (bool) $check;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
	// Compare existing value to new value if no prev value given and the key exists only once.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
	if ( empty($prev_value) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
		$old_value = get_metadata($meta_type, $object_id, $meta_key);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
		if ( count($old_value) == 1 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
			if ( $old_value[0] === $meta_value )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   199
	$meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   200
	if ( empty( $meta_ids ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
		return add_metadata($meta_type, $object_id, $meta_key, $passed_value);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   202
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
	$_meta_value = $meta_value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
	$meta_value = maybe_serialize( $meta_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
	$data  = compact( 'meta_value' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
	$where = array( $column => $object_id, 'meta_key' => $meta_key );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
	if ( !empty( $prev_value ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
		$prev_value = maybe_serialize($prev_value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
		$where['meta_value'] = $prev_value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   215
	foreach ( $meta_ids as $meta_id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   216
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   217
		 * Fires immediately before updating metadata of a specific type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   218
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   219
		 * The dynamic portion of the hook, `$meta_type`, refers to the meta
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   220
		 * object type (comment, post, or user).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   221
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   222
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   223
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   224
		 * @param int    $meta_id    ID of the metadata entry to update.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   225
		 * @param int    $object_id  Object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   226
		 * @param string $meta_key   Meta key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   227
		 * @param mixed  $meta_value Meta value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   228
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   229
		do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   230
	}
0
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 ( 'post' == $meta_type ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   233
		foreach ( $meta_ids as $meta_id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   234
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   235
			 * Fires immediately before updating a post's metadata.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   236
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   237
			 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   238
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   239
			 * @param int    $meta_id    ID of metadata entry to update.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   240
			 * @param int    $object_id  Object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   241
			 * @param string $meta_key   Meta key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   242
			 * @param mixed  $meta_value Meta value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   243
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   244
			do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   245
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   246
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
	$result = $wpdb->update( $table, $data, $where );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
	if ( ! $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
	wp_cache_delete($object_id, $meta_type . '_meta');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   254
	foreach ( $meta_ids as $meta_id ) {
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
		 * Fires immediately after updating metadata of a specific type.
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
		 * The dynamic portion of the hook, `$meta_type`, refers to the meta
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   259
		 * object type (comment, post, or user).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   260
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   261
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   262
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   263
		 * @param int    $meta_id    ID of updated metadata entry.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   264
		 * @param int    $object_id  Object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   265
		 * @param string $meta_key   Meta key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   266
		 * @param mixed  $meta_value Meta value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   267
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   268
		do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
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
	if ( 'post' == $meta_type ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   272
		foreach ( $meta_ids as $meta_id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   273
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   274
			 * Fires immediately after updating a post's metadata.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   275
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   276
			 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   277
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   278
			 * @param int    $meta_id    ID of updated metadata entry.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   279
			 * @param int    $object_id  Object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   280
			 * @param string $meta_key   Meta key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   281
			 * @param mixed  $meta_value Meta value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   282
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   283
			do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   284
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   285
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
 * Delete metadata for the specified object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
 * @since 2.9.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   294
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   295
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
 * @param int $object_id ID of the object metadata is for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
 * @param string $meta_key Metadata key
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
 * @param mixed $meta_value Optional. Metadata value. Must be serializable if non-scalar. If specified, only delete metadata entries
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
 * 		with this value. Otherwise, delete all entries with the specified meta_key.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
 * @param bool $delete_all Optional, default is false. If true, delete matching metadata entries
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
 * 		for all objects, ignoring the specified object_id. Otherwise, only delete matching
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
 * 		metadata entries for the specified object_id.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
 * @return bool True on successful delete, false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   308
	global $wpdb;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   310
	if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) && ! $delete_all ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   312
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   314
	$object_id = absint( $object_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   315
	if ( ! $object_id && ! $delete_all ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   317
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   319
	$table = _get_meta_table( $meta_type );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   320
	if ( ! $table ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   321
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   322
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
	$type_column = sanitize_key($meta_type . '_id');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
	$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
	// expected_slashed ($meta_key)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
	$meta_key = wp_unslash($meta_key);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
	$meta_value = wp_unslash($meta_value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   330
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   331
	 * Filter whether to delete metadata of a specific type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   332
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   333
	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   334
	 * object type (comment, post, or user). Returning a non-null value
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   335
	 * will effectively short-circuit the function.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   336
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   337
	 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   338
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   339
	 * @param null|bool $delete     Whether to allow metadata deletion of the given type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   340
	 * @param int       $object_id  Object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   341
	 * @param string    $meta_key   Meta key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   342
	 * @param mixed     $meta_value Meta value. Must be serializable if non-scalar.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   343
	 * @param bool      $delete_all Whether to delete the matching metadata entries
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   344
	 *                              for all objects, ignoring the specified $object_id.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   345
	 *                              Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   346
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
	$check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
	if ( null !== $check )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
		return (bool) $check;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
	$_meta_value = $meta_value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
	$meta_value = maybe_serialize( $meta_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
	$query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
	if ( !$delete_all )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
		$query .= $wpdb->prepare(" AND $type_column = %d", $object_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
	if ( $meta_value )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
		$query .= $wpdb->prepare(" AND meta_value = %s", $meta_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
	$meta_ids = $wpdb->get_col( $query );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
	if ( !count( $meta_ids ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
	if ( $delete_all )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
		$object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   369
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   370
	 * Fires immediately before deleting metadata of a specific type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   371
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   372
	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   373
	 * object type (comment, post, or user).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   374
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   375
	 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   376
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   377
	 * @param array  $meta_ids   An array of metadata entry IDs to delete.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   378
	 * @param int    $object_id  Object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   379
	 * @param string $meta_key   Meta key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   380
	 * @param mixed  $meta_value Meta value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   381
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
	do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_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
	// Old-style action.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   385
	if ( 'post' == $meta_type ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   386
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   387
		 * Fires immediately before deleting metadata for a post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   388
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   389
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   390
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   391
		 * @param array $meta_ids An array of post metadata entry IDs to delete.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   392
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
		do_action( 'delete_postmeta', $meta_ids );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   394
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
	$query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . " )";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
	$count = $wpdb->query($query);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
	if ( !$count )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
	if ( $delete_all ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
		foreach ( (array) $object_ids as $o_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
			wp_cache_delete($o_id, $meta_type . '_meta');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
		wp_cache_delete($object_id, $meta_type . '_meta');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   411
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   412
	 * Fires immediately after deleting metadata of a specific type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   413
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   414
	 * The dynamic portion of the hook name, `$meta_type`, refers to the meta
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   415
	 * object type (comment, post, or user).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   416
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   417
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   418
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   419
	 * @param array  $meta_ids   An array of deleted metadata entry IDs.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   420
	 * @param int    $object_id  Object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   421
	 * @param string $meta_key   Meta key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   422
	 * @param mixed  $meta_value Meta value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   423
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
	do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
	// Old-style action.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   427
	if ( 'post' == $meta_type ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   428
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   429
		 * Fires immediately after deleting metadata for a post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   430
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   431
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   432
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   433
		 * @param array $meta_ids An array of deleted post metadata entry IDs.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   434
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
		do_action( 'deleted_postmeta', $meta_ids );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   436
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
 * Retrieve metadata for the specified object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
 * @param int $object_id ID of the object metadata is for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
 * 		the specified object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
 * @param bool $single Optional, default is false. If true, return only the first value of the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
 * 		specified meta_key. This parameter has no effect if meta_key is not specified.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   452
 * @return mixed Single metadata value, or array of values
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   455
	if ( ! $meta_type || ! is_numeric( $object_id ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   457
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   458
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   459
	$object_id = absint( $object_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   460
	if ( ! $object_id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   461
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   462
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   464
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   465
	 * Filter whether to retrieve metadata of a specific type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   466
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   467
	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   468
	 * object type (comment, post, or user). Returning a non-null value
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   469
	 * will effectively short-circuit the function.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   470
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   471
	 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   472
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   473
	 * @param null|array|string $value     The value get_metadata() should
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   474
	 *                                     return - a single metadata value,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   475
	 *                                     or an array of values.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   476
	 * @param int               $object_id Object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   477
	 * @param string            $meta_key  Meta key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   478
	 * @param string|array      $single    Meta value, or an array of values.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   479
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
	$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
	if ( null !== $check ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
		if ( $single && is_array( $check ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
			return $check[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
			return $check;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
	$meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
	if ( !$meta_cache ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
		$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
		$meta_cache = $meta_cache[$object_id];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   495
	if ( ! $meta_key ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
		return $meta_cache;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   497
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
	if ( isset($meta_cache[$meta_key]) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
		if ( $single )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
			return maybe_unserialize( $meta_cache[$meta_key][0] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
			return array_map('maybe_unserialize', $meta_cache[$meta_key]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
	if ($single)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
 * Determine if a meta key is set for a given object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
 * @since 3.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
 * @param int $object_id ID of the object metadata is for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
 * @param string $meta_key Metadata key.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
 * @return boolean true of the key is set, false if not.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
function metadata_exists( $meta_type, $object_id, $meta_key ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   523
	if ( ! $meta_type || ! is_numeric( $object_id ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   525
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   527
	$object_id = absint( $object_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   528
	if ( ! $object_id ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   530
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   532
	/** This filter is documented in wp-includes/meta.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
	$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
	if ( null !== $check )
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   535
		return (bool) $check;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
	$meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
	if ( !$meta_cache ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
		$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
		$meta_cache = $meta_cache[$object_id];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
	if ( isset( $meta_cache[ $meta_key ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
 * Get meta data by meta ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
 * @since 3.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
 * @param int $meta_id ID for a specific meta row
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
 * @return object Meta object or false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   559
function get_metadata_by_mid( $meta_type, $meta_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   562
	if ( ! $meta_type || ! is_numeric( $meta_id ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   564
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   566
	$meta_id = absint( $meta_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   567
	if ( ! $meta_id ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   569
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   571
	$table = _get_meta_table( $meta_type );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   572
	if ( ! $table ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   574
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
	$id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
	$meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
	if ( empty( $meta ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
	if ( isset( $meta->meta_value ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
		$meta->meta_value = maybe_unserialize( $meta->meta_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
	return $meta;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   588
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
 * Update meta data by meta ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   591
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
 * @since 3.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
 * @param int $meta_id ID for a specific meta row
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
 * @param string $meta_value Metadata value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
 * @param string $meta_key Optional, you can provide a meta key to update it
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
 * @return bool True on successful update, false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
	// Make sure everything is valid.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   604
	if ( ! $meta_type || ! is_numeric( $meta_id ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   606
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   608
	$meta_id = absint( $meta_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   609
	if ( ! $meta_id ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   611
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   613
	$table = _get_meta_table( $meta_type );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   614
	if ( ! $table ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   616
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
	$column = sanitize_key($meta_type . '_id');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
	$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
	// Fetch the meta and go on if it's found.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
	if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
		$original_key = $meta->meta_key;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
		$object_id = $meta->{$column};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
		// If a new meta_key (last parameter) was specified, change the meta key,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
		// otherwise use the original key in the update statement.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   628
		if ( false === $meta_key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
			$meta_key = $original_key;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   630
		} elseif ( ! is_string( $meta_key ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
		// Sanitize the meta
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
		$_meta_value = $meta_value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
		$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
		$meta_value = maybe_serialize( $meta_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
		// Format the data query arguments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   640
		$data = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
			'meta_key' => $meta_key,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
			'meta_value' => $meta_value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   643
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   645
		// Format the where query arguments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
		$where = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
		$where[$id_column] = $meta_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   649
		/** This action is documented in wp-includes/meta.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
		do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   652
		if ( 'post' == $meta_type ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   653
			/** This action is documented in wp-includes/meta.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
			do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   655
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   657
		// Run the update query, all fields in $data are %s, $where is a %d.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   658
		$result = $wpdb->update( $table, $data, $where, '%s', '%d' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
		if ( ! $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   660
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
		// Clear the caches.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
		wp_cache_delete($object_id, $meta_type . '_meta');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   665
		/** This action is documented in wp-includes/meta.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   666
		do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   667
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   668
		if ( 'post' == $meta_type ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   669
			/** This action is documented in wp-includes/meta.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
			do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   671
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   672
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   674
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   676
	// And if the meta was not found.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   677
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   678
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   679
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   680
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   681
 * Delete meta data by meta ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   682
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   683
 * @since 3.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   684
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   685
 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   686
 * @param int $meta_id ID for a specific meta row
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   687
 * @return bool True on successful delete, false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   688
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   689
function delete_metadata_by_mid( $meta_type, $meta_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   690
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   691
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   692
	// Make sure everything is valid.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   693
	if ( ! $meta_type || ! is_numeric( $meta_id ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   694
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   695
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   696
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   697
	$meta_id = absint( $meta_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   698
	if ( ! $meta_id ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   700
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   702
	$table = _get_meta_table( $meta_type );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   703
	if ( ! $table ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   704
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   705
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   706
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   707
	// object and id columns
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   708
	$column = sanitize_key($meta_type . '_id');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   709
	$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   710
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   711
	// Fetch the meta and go on if it's found.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   712
	if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   713
		$object_id = $meta->{$column};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   714
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   715
		/** This action is documented in wp-includes/meta.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   716
		do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   717
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   718
		// Old-style action.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   719
		if ( 'post' == $meta_type || 'comment' == $meta_type ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   720
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   721
			 * Fires immediately before deleting post or comment metadata of a specific type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   722
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   723
			 * The dynamic portion of the hook, `$meta_type`, refers to the meta
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   724
			 * object type (post or comment).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   725
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   726
			 * @since 3.4.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   727
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   728
			 * @param int $meta_id ID of the metadata entry to delete.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   729
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   730
			do_action( "delete_{$meta_type}meta", $meta_id );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   731
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   733
		// Run the query, will return true if deleted, false otherwise
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   734
		$result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   735
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   736
		// Clear the caches.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   737
		wp_cache_delete($object_id, $meta_type . '_meta');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   738
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   739
		/** This action is documented in wp-includes/meta.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   740
		do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   741
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   742
		// Old-style action.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   743
		if ( 'post' == $meta_type || 'comment' == $meta_type ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   744
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   745
			 * Fires immediately after deleting post or comment metadata of a specific type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   746
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   747
			 * The dynamic portion of the hook, `$meta_type`, refers to the meta
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   748
			 * object type (post or comment).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   749
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   750
			 * @since 3.4.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   751
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   752
			 * @param int $meta_ids Deleted metadata entry ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   753
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   754
			do_action( "deleted_{$meta_type}meta", $meta_id );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   755
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   756
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   757
		return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   758
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   759
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   760
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   761
	// Meta id was not found.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   762
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   763
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   764
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   765
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   766
 * Update the metadata cache for the specified objects.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   767
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   768
 * @since 2.9.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   769
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   770
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   771
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   772
 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   773
 * @param int|array $object_ids array or comma delimited list of object IDs to update cache for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   774
 * @return mixed Metadata cache for the specified objects, or false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   775
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   776
function update_meta_cache($meta_type, $object_ids) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   777
	global $wpdb;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   778
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   779
	if ( ! $meta_type || ! $object_ids ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   781
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   782
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   783
	$table = _get_meta_table( $meta_type );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   784
	if ( ! $table ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   785
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   786
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   787
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   788
	$column = sanitize_key($meta_type . '_id');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
	if ( !is_array($object_ids) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
		$object_ids = preg_replace('|[^0-9,]|', '', $object_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   792
		$object_ids = explode(',', $object_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   795
	$object_ids = array_map('intval', $object_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
	$cache_key = $meta_type . '_meta';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
	$ids = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
	$cache = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
	foreach ( $object_ids as $id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
		$cached_object = wp_cache_get( $id, $cache_key );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   802
		if ( false === $cached_object )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   803
			$ids[] = $id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
			$cache[$id] = $cached_object;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   806
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   807
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   808
	if ( empty( $ids ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
		return $cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   810
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   811
	// Get meta info
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   812
	$id_list = join( ',', $ids );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   813
	$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   814
	$meta_list = $wpdb->get_results( "SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC", ARRAY_A );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   816
	if ( !empty($meta_list) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   817
		foreach ( $meta_list as $metarow) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   818
			$mpid = intval($metarow[$column]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   819
			$mkey = $metarow['meta_key'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   820
			$mval = $metarow['meta_value'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   821
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   822
			// Force subkeys to be array type:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   823
			if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   824
				$cache[$mpid] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   825
			if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   826
				$cache[$mpid][$mkey] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   827
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   828
			// Add a value to the current pid/key:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   829
			$cache[$mpid][$mkey][] = $mval;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   830
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   831
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   832
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
	foreach ( $ids as $id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   834
		if ( ! isset($cache[$id]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   835
			$cache[$id] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   836
		wp_cache_add( $id, $cache[$id], $cache_key );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   837
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   838
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   839
	return $cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   840
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   841
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   842
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   843
 * Given a meta query, generates SQL clauses to be appended to a main query.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   844
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   845
 * @since 3.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   846
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   847
 * @see WP_Meta_Query
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   848
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   849
 * @param array $meta_query         A meta query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   850
 * @param string $type              Type of meta.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   851
 * @param string $primary_table     Primary database table name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   852
 * @param string $primary_id_column Primary ID column name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   853
 * @param object $context           Optional. The main query object
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   854
 * @return array Associative array of `JOIN` and `WHERE` SQL.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   855
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   856
function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   857
	$meta_query_obj = new WP_Meta_Query( $meta_query );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
	return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   861
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   862
 * Class for generating SQL clauses that filter a primary query according to metadata keys and values.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   863
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   864
 * `WP_Meta_Query` is a helper that allows primary query classes, such as {@see WP_Query} and {@see WP_User_Query},
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   865
 * to filter their results by object metadata, by generating `JOIN` and `WHERE` subclauses to be attached
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   866
 * to the primary SQL query string.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
 * @since 3.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   870
class WP_Meta_Query {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   871
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   872
	 * Array of metadata queries.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   873
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   874
	 * See {@see WP_Meta_Query::__construct()} for information on meta query arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   875
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   876
	 * @since 3.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   877
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   878
	 * @var array
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   879
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   880
	public $queries = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   881
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   882
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   883
	 * The relation between the queries. Can be one of 'AND' or 'OR'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   884
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   885
	 * @since 3.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   886
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   887
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   888
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   889
	public $relation;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   890
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   891
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   892
	 * Database table to query for the metadata.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   893
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   894
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   895
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   896
	 * @var string
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   897
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   898
	public $meta_table;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   899
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   900
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   901
	 * Column in meta_table that represents the ID of the object the metadata belongs to.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   902
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   903
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   904
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   905
	 * @var string
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   906
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   907
	public $meta_id_column;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   908
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   909
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   910
	 * Database table that where the metadata's objects are stored (eg $wpdb->users).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   911
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   912
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   913
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   914
	 * @var string
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   915
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   916
	public $primary_table;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   917
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   918
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   919
	 * Column in primary_table that represents the ID of the object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   920
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   921
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   922
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   923
	 * @var string
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   924
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   925
	public $primary_id_column;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   926
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   927
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   928
	 * A flat list of table aliases used in JOIN clauses.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   929
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   930
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   931
	 * @access protected
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   932
	 * @var array
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   933
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   934
	protected $table_aliases = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   935
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   936
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   937
	 * A flat list of clauses, keyed by clause 'name'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   938
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   939
	 * @since 4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   940
	 * @access protected
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   941
	 * @var array
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   942
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   943
	protected $clauses = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   944
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   945
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   946
	 * Constructor.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   947
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   948
	 * @since 3.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   949
	 * @since 4.2.0 Introduced support for naming query clauses by associative array keys.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   950
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   951
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   952
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   953
	 * @param array $meta_query {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   954
	 *     Array of meta query clauses. When first-order clauses use strings as their array keys, they may be
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   955
	 *     referenced in the 'orderby' parameter of the parent query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   956
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   957
	 *     @type string $relation Optional. The MySQL keyword used to join
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   958
	 *                            the clauses of the query. Accepts 'AND', or 'OR'. Default 'AND'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   959
	 *     @type array {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   960
	 *         Optional. An array of first-order clause parameters, or another fully-formed meta query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   961
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   962
	 *         @type string $key     Meta key to filter by.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   963
	 *         @type string $value   Meta value to filter by.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   964
	 *         @type string $compare MySQL operator used for comparing the $value. Accepts '=',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   965
	 *                               '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   966
	 *                               'BETWEEN', 'NOT BETWEEN', 'REGEXP', 'NOT REGEXP', or 'RLIKE'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   967
	 *                               Default is 'IN' when `$value` is an array, '=' otherwise.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   968
	 *         @type string $type    MySQL data type that the meta_value column will be CAST to for
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   969
	 *                               comparisons. Accepts 'NUMERIC', 'BINARY', 'CHAR', 'DATE',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   970
	 *                               'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', or 'UNSIGNED'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   971
	 *                               Default is 'CHAR'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   972
	 *     }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   973
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   974
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   975
	public function __construct( $meta_query = false ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   976
		if ( !$meta_query )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   977
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   978
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   979
		if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   980
			$this->relation = 'OR';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   981
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   982
			$this->relation = 'AND';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   983
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   984
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   985
		$this->queries = $this->sanitize_query( $meta_query );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   986
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   987
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   988
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   989
	 * Ensure the 'meta_query' argument passed to the class constructor is well-formed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   990
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   991
	 * Eliminates empty items and ensures that a 'relation' is set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   992
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   993
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   994
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   995
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   996
	 * @param array $queries Array of query clauses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   997
	 * @return array Sanitized array of query clauses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   998
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   999
	public function sanitize_query( $queries ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1000
		$clean_queries = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1001
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1002
		if ( ! is_array( $queries ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1003
			return $clean_queries;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1004
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1005
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1006
		foreach ( $queries as $key => $query ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1007
			if ( 'relation' === $key ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1008
				$relation = $query;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1009
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1010
			} elseif ( ! is_array( $query ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1011
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1012
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1013
			// First-order clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1014
			} elseif ( $this->is_first_order_clause( $query ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1015
				if ( isset( $query['value'] ) && array() === $query['value'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1016
					unset( $query['value'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1017
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1018
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1019
				$clean_queries[ $key ] = $query;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1020
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1021
			// Otherwise, it's a nested query, so we recurse.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1022
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1023
				$cleaned_query = $this->sanitize_query( $query );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1024
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1025
				if ( ! empty( $cleaned_query ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1026
					$clean_queries[ $key ] = $cleaned_query;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1027
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1028
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1029
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1030
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1031
		if ( empty( $clean_queries ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1032
			return $clean_queries;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1033
		}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1034
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1035
		// Sanitize the 'relation' key provided in the query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1036
		if ( isset( $relation ) && 'OR' === strtoupper( $relation ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1037
			$clean_queries['relation'] = 'OR';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1038
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1039
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1040
		 * If there is only a single clause, call the relation 'OR'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1041
		 * This value will not actually be used to join clauses, but it
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1042
		 * simplifies the logic around combining key-only queries.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1043
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1044
		} elseif ( 1 === count( $clean_queries ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1045
			$clean_queries['relation'] = 'OR';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1046
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1047
		// Default to AND.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1048
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1049
			$clean_queries['relation'] = 'AND';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1050
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1051
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1052
		return $clean_queries;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1053
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1054
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1055
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1056
	 * Determine whether a query clause is first-order.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1057
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1058
	 * A first-order meta query clause is one that has either a 'key' or
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1059
	 * a 'value' array key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1060
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1061
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1062
	 * @access protected
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1063
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1064
	 * @param array $query Meta query arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1065
	 * @return bool Whether the query clause is a first-order clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1066
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1067
	protected function is_first_order_clause( $query ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1068
		return isset( $query['key'] ) || isset( $query['value'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1069
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1070
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1072
	 * Constructs a meta query based on 'meta_*' query vars
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1073
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1074
	 * @since 3.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1075
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1076
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1077
	 * @param array $qv The query variables
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1078
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1079
	public function parse_query_vars( $qv ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1080
		$meta_query = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1081
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1082
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1083
		 * For orderby=meta_value to work correctly, simple query needs to be
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1084
		 * first (so that its table join is against an unaliased meta table) and
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1085
		 * needs to be its own clause (so it doesn't interfere with the logic of
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1086
		 * the rest of the meta_query).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1087
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1088
		$primary_meta_query = array();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1089
		foreach ( array( 'key', 'compare', 'type' ) as $key ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1090
			if ( ! empty( $qv[ "meta_$key" ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1091
				$primary_meta_query[ $key ] = $qv[ "meta_$key" ];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1092
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1093
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1094
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1095
		// WP_Query sets 'meta_value' = '' by default.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1096
		if ( isset( $qv['meta_value'] ) && '' !== $qv['meta_value'] && ( ! is_array( $qv['meta_value'] ) || $qv['meta_value'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1097
			$primary_meta_query['value'] = $qv['meta_value'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1098
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1099
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1100
		$existing_meta_query = isset( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ? $qv['meta_query'] : array();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1101
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1102
		if ( ! empty( $primary_meta_query ) && ! empty( $existing_meta_query ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1103
			$meta_query = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1104
				'relation' => 'AND',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1105
				$primary_meta_query,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1106
				$existing_meta_query,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1107
			);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1108
		} elseif ( ! empty( $primary_meta_query ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1109
			$meta_query = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1110
				$primary_meta_query,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1111
			);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1112
		} elseif ( ! empty( $existing_meta_query ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1113
			$meta_query = $existing_meta_query;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1114
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1115
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1116
		$this->__construct( $meta_query );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1117
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1118
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1119
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1120
	 * Return the appropriate alias for the given meta type if applicable.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1121
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1122
	 * @since 3.7.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1123
	 * @access public
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1124
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1125
	 * @param string $type MySQL type to cast meta_value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1126
	 * @return string MySQL type.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1127
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1128
	public function get_cast_for_type( $type = '' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1129
		if ( empty( $type ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1130
			return 'CHAR';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1131
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1132
		$meta_type = strtoupper( $type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1133
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1134
		if ( ! preg_match( '/^(?:BINARY|CHAR|DATE|DATETIME|SIGNED|UNSIGNED|TIME|NUMERIC(?:\(\d+(?:,\s?\d+)?\))?|DECIMAL(?:\(\d+(?:,\s?\d+)?\))?)$/', $meta_type ) )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1135
			return 'CHAR';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1136
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1137
		if ( 'NUMERIC' == $meta_type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1138
			$meta_type = 'SIGNED';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1139
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1140
		return $meta_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1141
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1142
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1143
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1144
	 * Generates SQL clauses to be appended to a main query.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1145
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1146
	 * @since 3.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1147
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1148
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1149
	 * @param string $type              Type of meta, eg 'user', 'post'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1150
	 * @param string $primary_table     Database table where the object being filtered is stored (eg wp_users).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1151
	 * @param string $primary_id_column ID column for the filtered object in $primary_table.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1152
	 * @param object $context           Optional. The main query object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1153
	 * @return array {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1154
	 *     Array containing JOIN and WHERE SQL clauses to append to the main query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1155
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1156
	 *     @type string $join  SQL fragment to append to the main JOIN clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1157
	 *     @type string $where SQL fragment to append to the main WHERE clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1158
	 * }
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1159
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1160
	public function get_sql( $type, $primary_table, $primary_id_column, $context = null ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1161
		if ( ! $meta_table = _get_meta_table( $type ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1162
			return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1163
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1164
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1165
		$this->meta_table     = $meta_table;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1166
		$this->meta_id_column = sanitize_key( $type . '_id' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1167
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1168
		$this->primary_table     = $primary_table;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1169
		$this->primary_id_column = $primary_id_column;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1170
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1171
		$sql = $this->get_sql_clauses();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1172
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1173
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1174
		 * If any JOINs are LEFT JOINs (as in the case of NOT EXISTS), then all JOINs should
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1175
		 * be LEFT. Otherwise posts with no metadata will be excluded from results.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1176
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1177
		if ( false !== strpos( $sql['join'], 'LEFT JOIN' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1178
			$sql['join'] = str_replace( 'INNER JOIN', 'LEFT JOIN', $sql['join'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1179
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1180
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1181
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1182
		 * Filter the meta query's generated SQL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1183
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1184
		 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1185
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1186
		 * @param array $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1187
		 *     An array of meta query SQL arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1188
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1189
		 *     @type array  $clauses           Array containing the query's JOIN and WHERE clauses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1190
		 *     @type array  $queries           Array of meta queries.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1191
		 *     @type string $type              Type of meta.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1192
		 *     @type string $primary_table     Primary table.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1193
		 *     @type string $primary_id_column Primary column ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1194
		 *     @type object $context           The main query object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1195
		 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1196
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1197
		return apply_filters_ref_array( 'get_meta_sql', array( $sql, $this->queries, $type, $primary_table, $primary_id_column, $context ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1198
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1199
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1200
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1201
	 * Generate SQL clauses to be appended to a main query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1202
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1203
	 * Called by the public {@see WP_Meta_Query::get_sql()}, this method
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1204
	 * is abstracted out to maintain parity with the other Query classes.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1205
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1206
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1207
	 * @access protected
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1208
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1209
	 * @return array {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1210
	 *     Array containing JOIN and WHERE SQL clauses to append to the main query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1211
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1212
	 *     @type string $join  SQL fragment to append to the main JOIN clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1213
	 *     @type string $where SQL fragment to append to the main WHERE clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1214
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1215
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1216
	protected function get_sql_clauses() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1217
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1218
		 * $queries are passed by reference to get_sql_for_query() for recursion.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1219
		 * To keep $this->queries unaltered, pass a copy.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1220
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1221
		$queries = $this->queries;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1222
		$sql = $this->get_sql_for_query( $queries );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1223
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1224
		if ( ! empty( $sql['where'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1225
			$sql['where'] = ' AND ' . $sql['where'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1226
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1227
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1228
		return $sql;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1229
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1230
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1231
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1232
	 * Generate SQL clauses for a single query array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1233
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1234
	 * If nested subqueries are found, this method recurses the tree to
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1235
	 * produce the properly nested SQL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1236
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1237
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1238
	 * @access protected
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1239
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1240
	 * @param array $query Query to parse, passed by reference.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1241
	 * @param int   $depth Optional. Number of tree levels deep we currently are.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1242
	 *                     Used to calculate indentation. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1243
	 * @return array {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1244
	 *     Array containing JOIN and WHERE SQL clauses to append to a single query array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1245
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1246
	 *     @type string $join  SQL fragment to append to the main JOIN clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1247
	 *     @type string $where SQL fragment to append to the main WHERE clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1248
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1249
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1250
	protected function get_sql_for_query( &$query, $depth = 0 ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1251
		$sql_chunks = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1252
			'join'  => array(),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1253
			'where' => array(),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1254
		);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1255
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1256
		$sql = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1257
			'join'  => '',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1258
			'where' => '',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1259
		);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1260
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1261
		$indent = '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1262
		for ( $i = 0; $i < $depth; $i++ ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1263
			$indent .= "  ";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1264
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1265
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1266
		foreach ( $query as $key => &$clause ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1267
			if ( 'relation' === $key ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1268
				$relation = $query['relation'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1269
			} elseif ( is_array( $clause ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1270
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1271
				// This is a first-order clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1272
				if ( $this->is_first_order_clause( $clause ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1273
					$clause_sql = $this->get_sql_for_clause( $clause, $query, $key );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1274
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1275
					$where_count = count( $clause_sql['where'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1276
					if ( ! $where_count ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1277
						$sql_chunks['where'][] = '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1278
					} elseif ( 1 === $where_count ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1279
						$sql_chunks['where'][] = $clause_sql['where'][0];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1280
					} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1281
						$sql_chunks['where'][] = '( ' . implode( ' AND ', $clause_sql['where'] ) . ' )';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1282
					}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1283
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1284
					$sql_chunks['join'] = array_merge( $sql_chunks['join'], $clause_sql['join'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1285
				// This is a subquery, so we recurse.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1286
				} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1287
					$clause_sql = $this->get_sql_for_query( $clause, $depth + 1 );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1288
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1289
					$sql_chunks['where'][] = $clause_sql['where'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1290
					$sql_chunks['join'][]  = $clause_sql['join'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1291
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1292
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1293
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1294
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1295
		// Filter to remove empties.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1296
		$sql_chunks['join']  = array_filter( $sql_chunks['join'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1297
		$sql_chunks['where'] = array_filter( $sql_chunks['where'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1298
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1299
		if ( empty( $relation ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1300
			$relation = 'AND';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1301
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1302
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1303
		// Filter duplicate JOIN clauses and combine into a single string.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1304
		if ( ! empty( $sql_chunks['join'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1305
			$sql['join'] = implode( ' ', array_unique( $sql_chunks['join'] ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1306
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1307
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1308
		// Generate a single WHERE clause with proper brackets and indentation.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1309
		if ( ! empty( $sql_chunks['where'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1310
			$sql['where'] = '( ' . "\n  " . $indent . implode( ' ' . "\n  " . $indent . $relation . ' ' . "\n  " . $indent, $sql_chunks['where'] ) . "\n" . $indent . ')';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1311
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1312
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1313
		return $sql;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1314
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1315
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1316
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1317
	 * Generate SQL JOIN and WHERE clauses for a first-order query clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1318
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1319
	 * "First-order" means that it's an array with a 'key' or 'value'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1320
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1321
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1322
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1323
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1324
	 * @param array  $clause       Query clause, passed by reference.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1325
	 * @param array  $parent_query Parent query array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1326
	 * @param string $clause_key   Optional. The array key used to name the clause in the original `$meta_query`
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1327
	 *                             parameters. If not provided, a key will be generated automatically.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1328
	 * @return array {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1329
	 *     Array containing JOIN and WHERE SQL clauses to append to a first-order query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1330
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1331
	 *     @type string $join  SQL fragment to append to the main JOIN clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1332
	 *     @type string $where SQL fragment to append to the main WHERE clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1333
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1334
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1335
	public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1336
		global $wpdb;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1337
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1338
		$sql_chunks = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1339
			'where' => array(),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1340
			'join' => array(),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1341
		);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1342
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1343
		if ( isset( $clause['compare'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1344
			$clause['compare'] = strtoupper( $clause['compare'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1345
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1346
			$clause['compare'] = isset( $clause['value'] ) && is_array( $clause['value'] ) ? 'IN' : '=';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1347
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1348
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1349
		if ( ! in_array( $clause['compare'], array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1350
			'=', '!=', '>', '>=', '<', '<=',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1351
			'LIKE', 'NOT LIKE',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1352
			'IN', 'NOT IN',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1353
			'BETWEEN', 'NOT BETWEEN',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1354
			'EXISTS', 'NOT EXISTS',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1355
			'REGEXP', 'NOT REGEXP', 'RLIKE'
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1356
		) ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1357
			$clause['compare'] = '=';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1358
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1359
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1360
		$meta_compare = $clause['compare'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1361
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1362
		// First build the JOIN clause, if one is required.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1363
		$join = '';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1364
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1365
		// We prefer to avoid joins if possible. Look for an existing join compatible with this clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1366
		$alias = $this->find_compatible_table_alias( $clause, $parent_query );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1367
		if ( false === $alias ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1368
			$i = count( $this->table_aliases );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1369
			$alias = $i ? 'mt' . $i : $this->meta_table;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1370
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1371
			// JOIN clauses for NOT EXISTS have their own syntax.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1372
			if ( 'NOT EXISTS' === $meta_compare ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1373
				$join .= " LEFT JOIN $this->meta_table";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1374
				$join .= $i ? " AS $alias" : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1375
				$join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1376
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1377
			// All other JOIN clauses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1378
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1379
				$join .= " INNER JOIN $this->meta_table";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1380
				$join .= $i ? " AS $alias" : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1381
				$join .= " ON ( $this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column )";
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1382
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1383
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1384
			$this->table_aliases[] = $alias;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1385
			$sql_chunks['join'][] = $join;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1386
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1387
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1388
		// Save the alias to this clause, for future siblings to find.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1389
		$clause['alias'] = $alias;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1390
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1391
		// Determine the data type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1392
		$_meta_type = isset( $clause['type'] ) ? $clause['type'] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1393
		$meta_type  = $this->get_cast_for_type( $_meta_type );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1394
		$clause['cast'] = $meta_type;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1395
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1396
		// Fallback for clause keys is the table alias.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1397
		if ( ! $clause_key ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1398
			$clause_key = $clause['alias'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1399
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1400
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1401
		// Ensure unique clause keys, so none are overwritten.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1402
		$iterator = 1;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1403
		$clause_key_base = $clause_key;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1404
		while ( isset( $this->clauses[ $clause_key ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1405
			$clause_key = $clause_key_base . '-' . $iterator;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1406
			$iterator++;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1407
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1408
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1409
		// Store the clause in our flat array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1410
		$this->clauses[ $clause_key ] =& $clause;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1411
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1412
		// Next, build the WHERE clause.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1413
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1414
		// meta_key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1415
		if ( array_key_exists( 'key', $clause ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1416
			if ( 'NOT EXISTS' === $meta_compare ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1417
				$sql_chunks['where'][] = $alias . '.' . $this->meta_id_column . ' IS NULL';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1418
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1419
				$sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1420
			}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1421
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1422
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1423
		// meta_value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1424
		if ( array_key_exists( 'value', $clause ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1425
			$meta_value = $clause['value'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1426
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1427
			if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1428
				if ( ! is_array( $meta_value ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1429
					$meta_value = preg_split( '/[,\s]+/', $meta_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1430
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1431
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1432
				$meta_value = trim( $meta_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1433
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1434
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1435
			switch ( $meta_compare ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1436
				case 'IN' :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1437
				case 'NOT IN' :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1438
					$meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1439
					$where = $wpdb->prepare( $meta_compare_string, $meta_value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1440
					break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1441
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1442
				case 'BETWEEN' :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1443
				case 'NOT BETWEEN' :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1444
					$meta_value = array_slice( $meta_value, 0, 2 );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1445
					$where = $wpdb->prepare( '%s AND %s', $meta_value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1446
					break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1447
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1448
				case 'LIKE' :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1449
				case 'NOT LIKE' :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1450
					$meta_value = '%' . $wpdb->esc_like( $meta_value ) . '%';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1451
					$where = $wpdb->prepare( '%s', $meta_value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1452
					break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1453
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1454
				// EXISTS with a value is interpreted as '='.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1455
				case 'EXISTS' :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1456
					$meta_compare = '=';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1457
					$where = $wpdb->prepare( '%s', $meta_value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1458
					break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1459
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1460
				// 'value' is ignored for NOT EXISTS.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1461
				case 'NOT EXISTS' :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1462
					$where = '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1463
					break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1464
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1465
				default :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1466
					$where = $wpdb->prepare( '%s', $meta_value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1467
					break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1468
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1469
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1470
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1471
			if ( $where ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1472
				$sql_chunks['where'][] = "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$where}";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1473
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1474
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1475
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1476
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1477
		 * Multiple WHERE clauses (for meta_key and meta_value) should
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1478
		 * be joined in parentheses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1479
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1480
		if ( 1 < count( $sql_chunks['where'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1481
			$sql_chunks['where'] = array( '( ' . implode( ' AND ', $sql_chunks['where'] ) . ' )' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1482
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1483
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1484
		return $sql_chunks;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1485
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1486
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1487
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1488
	 * Get a flattened list of sanitized meta clauses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1489
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1490
	 * This array should be used for clause lookup, as when the table alias and CAST type must be determined for
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1491
	 * a value of 'orderby' corresponding to a meta clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1492
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1493
	 * @since 4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1494
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1495
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1496
	 * @return array Meta clauses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1497
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1498
	public function get_clauses() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1499
		return $this->clauses;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1500
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1501
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1502
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1503
	 * Identify an existing table alias that is compatible with the current
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1504
	 * query clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1505
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1506
	 * We avoid unnecessary table joins by allowing each clause to look for
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1507
	 * an existing table alias that is compatible with the query that it
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1508
	 * needs to perform.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1509
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1510
	 * An existing alias is compatible if (a) it is a sibling of `$clause`
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1511
	 * (ie, it's under the scope of the same relation), and (b) the combination
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1512
	 * of operator and relation between the clauses allows for a shared table join.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1513
	 * In the case of {@see WP_Meta_Query}, this only applies to 'IN' clauses that
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1514
	 * are connected by the relation 'OR'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1515
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1516
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1517
	 * @access protected
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1518
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1519
	 * @param  array       $clause       Query clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1520
	 * @param  array       $parent_query Parent query of $clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1521
	 * @return string|bool Table alias if found, otherwise false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1522
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1523
	protected function find_compatible_table_alias( $clause, $parent_query ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1524
		$alias = false;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1525
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1526
		foreach ( $parent_query as $sibling ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1527
			// If the sibling has no alias yet, there's nothing to check.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1528
			if ( empty( $sibling['alias'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1529
				continue;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1530
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1531
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1532
			// We're only interested in siblings that are first-order clauses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1533
			if ( ! is_array( $sibling ) || ! $this->is_first_order_clause( $sibling ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1534
				continue;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1535
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1536
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1537
			$compatible_compares = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1538
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1539
			// Clauses connected by OR can share joins as long as they have "positive" operators.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1540
			if ( 'OR' === $parent_query['relation'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1541
				$compatible_compares = array( '=', 'IN', 'BETWEEN', 'LIKE', 'REGEXP', 'RLIKE', '>', '>=', '<', '<=' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1542
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1543
			// Clauses joined by AND with "negative" operators share a join only if they also share a key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1544
			} elseif ( isset( $sibling['key'] ) && isset( $clause['key'] ) && $sibling['key'] === $clause['key'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1545
				$compatible_compares = array( '!=', 'NOT IN', 'NOT LIKE' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1546
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1547
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1548
			$clause_compare  = strtoupper( $clause['compare'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1549
			$sibling_compare = strtoupper( $sibling['compare'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1550
			if ( in_array( $clause_compare, $compatible_compares ) && in_array( $sibling_compare, $compatible_compares ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1551
				$alias = $sibling['alias'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1552
				break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1553
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1554
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1555
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1556
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1557
		 * Filter the table alias identified as compatible with the current clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1558
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1559
		 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1560
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1561
		 * @param string|bool $alias        Table alias, or false if none was found.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1562
		 * @param array       $clause       First-order query clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1563
		 * @param array       $parent_query Parent of $clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1564
		 * @param object      $this         WP_Meta_Query object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1565
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1566
		return apply_filters( 'meta_query_find_compatible_table_alias', $alias, $clause, $parent_query, $this ) ;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1567
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1568
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1569
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1570
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1571
 * Retrieve the name of the metadata table for the specified object type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1572
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1573
 * @since 2.9.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1574
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1575
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1576
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1577
 * @param string $type Type of object to get metadata table for (e.g., comment, post, or user)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1578
 * @return mixed Metadata table name, or false if no metadata table exists
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1579
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1580
function _get_meta_table($type) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1581
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1582
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1583
	$table_name = $type . 'meta';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1584
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1585
	if ( empty($wpdb->$table_name) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1586
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1587
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1588
	return $wpdb->$table_name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1589
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1590
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1591
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1592
 * Determine whether a meta key is protected.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1593
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1594
 * @since 3.1.3
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1595
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1596
 * @param string      $meta_key Meta key
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1597
 * @param string|null $meta_type
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1598
 * @return bool True if the key is protected, false otherwise.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1599
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1600
function is_protected_meta( $meta_key, $meta_type = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1601
	$protected = ( '_' == $meta_key[0] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1602
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1603
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1604
	 * Filter whether a meta key is protected.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1605
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1606
	 * @since 3.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1607
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1608
	 * @param bool   $protected Whether the key is protected. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1609
	 * @param string $meta_key  Meta key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1610
	 * @param string $meta_type Meta type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1611
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1612
	return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1613
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1614
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1615
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1616
 * Sanitize meta value.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1617
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1618
 * @since 3.1.3
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1619
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1620
 * @param string $meta_key Meta key
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1621
 * @param mixed $meta_value Meta value to sanitize
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1622
 * @param string $meta_type Type of meta
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1623
 * @return mixed Sanitized $meta_value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1624
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1625
function sanitize_meta( $meta_key, $meta_value, $meta_type ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1626
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1627
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1628
	 * Filter the sanitization of a specific meta key of a specific meta type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1629
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1630
	 * The dynamic portions of the hook name, `$meta_type`, and `$meta_key`,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1631
	 * refer to the metadata object type (comment, post, or user) and the meta
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1632
	 * key value,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1633
	 * respectively.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1634
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1635
	 * @since 3.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1636
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1637
	 * @param mixed  $meta_value Meta value to sanitize.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1638
	 * @param string $meta_key   Meta key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1639
	 * @param string $meta_type  Meta type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1640
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1641
	return apply_filters( "sanitize_{$meta_type}_meta_{$meta_key}", $meta_value, $meta_key, $meta_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1642
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1643
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1644
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1645
 * Register meta key
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1646
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1647
 * @since 3.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1648
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1649
 * @param string $meta_type Type of meta
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1650
 * @param string $meta_key Meta key
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1651
 * @param string|array $sanitize_callback A function or method to call when sanitizing the value of $meta_key.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1652
 * @param string|array $auth_callback Optional. A function or method to call when performing edit_post_meta, add_post_meta, and delete_post_meta capability checks.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1653
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1654
function register_meta( $meta_type, $meta_key, $sanitize_callback, $auth_callback = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1655
	if ( is_callable( $sanitize_callback ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1656
		add_filter( "sanitize_{$meta_type}_meta_{$meta_key}", $sanitize_callback, 10, 3 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1657
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1658
	if ( empty( $auth_callback ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1659
		if ( is_protected_meta( $meta_key, $meta_type ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1660
			$auth_callback = '__return_false';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1661
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1662
			$auth_callback = '__return_true';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1663
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1664
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1665
	if ( is_callable( $auth_callback ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1666
		add_filter( "auth_{$meta_type}_meta_{$meta_key}", $auth_callback, 10, 6 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1667
}