web/wp-includes/meta.php
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
   121 
   121 
   122 	$check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value );
   122 	$check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value );
   123 	if ( null !== $check )
   123 	if ( null !== $check )
   124 		return (bool) $check;
   124 		return (bool) $check;
   125 
   125 
   126 	if ( ! $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ) )
       
   127 		return add_metadata($meta_type, $object_id, $meta_key, $passed_value);
       
   128 
       
   129 	// Compare existing value to new value if no prev value given and the key exists only once.
   126 	// Compare existing value to new value if no prev value given and the key exists only once.
   130 	if ( empty($prev_value) ) {
   127 	if ( empty($prev_value) ) {
   131 		$old_value = get_metadata($meta_type, $object_id, $meta_key);
   128 		$old_value = get_metadata($meta_type, $object_id, $meta_key);
   132 		if ( count($old_value) == 1 ) {
   129 		if ( count($old_value) == 1 ) {
   133 			if ( $old_value[0] === $meta_value )
   130 			if ( $old_value[0] === $meta_value )
   134 				return false;
   131 				return false;
   135 		}
   132 		}
   136 	}
   133 	}
       
   134 
       
   135 	if ( ! $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ) )
       
   136 		return add_metadata($meta_type, $object_id, $meta_key, $passed_value);
   137 
   137 
   138 	$_meta_value = $meta_value;
   138 	$_meta_value = $meta_value;
   139 	$meta_value = maybe_serialize( $meta_value );
   139 	$meta_value = maybe_serialize( $meta_value );
   140 
   140 
   141 	$data  = compact( 'meta_value' );
   141 	$data  = compact( 'meta_value' );
   709 		$meta_id_column = esc_sql( $type . '_id' );
   709 		$meta_id_column = esc_sql( $type . '_id' );
   710 
   710 
   711 		$join = array();
   711 		$join = array();
   712 		$where = array();
   712 		$where = array();
   713 
   713 
   714 		foreach ( $this->queries as $k => $q ) {
   714 		$key_only_queries = array();
       
   715 		$queries = array();
       
   716 
       
   717 		// Split out the meta_key only queries (we can only do this for OR)
       
   718 		if ( 'OR' == $this->relation ) {
       
   719 			foreach ( $this->queries as $k => $q ) {
       
   720 				if ( ! isset( $q['value'] ) && ! empty( $q['key'] ) )
       
   721 					$key_only_queries[$k] = $q;
       
   722 				else
       
   723 					$queries[$k] = $q;
       
   724 			}
       
   725 		} else {
       
   726 			$queries = $this->queries;
       
   727 		}
       
   728 
       
   729 		// Specify all the meta_key only queries in one go
       
   730 		if ( $key_only_queries ) {
       
   731 			$join[]  = "INNER JOIN $meta_table ON $primary_table.$primary_id_column = $meta_table.$meta_id_column";
       
   732 
       
   733 			foreach ( $key_only_queries as $key => $q )
       
   734 				$where["key-only-$key"] = $wpdb->prepare( "$meta_table.meta_key = %s", trim( $q['key'] ) );
       
   735 		}
       
   736 
       
   737 		foreach ( $queries as $k => $q ) {
   715 			$meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
   738 			$meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
   716 			$meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
   739 			$meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
   717 
   740 
   718 			if ( 'NUMERIC' == $meta_type )
   741 			if ( 'NUMERIC' == $meta_type )
   719 				$meta_type = 'SIGNED';
   742 				$meta_type = 'SIGNED';
   720 			elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
   743 			elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
   721 				$meta_type = 'CHAR';
   744 				$meta_type = 'CHAR';
   722 
   745 
       
   746 			$meta_value = isset( $q['value'] ) ? $q['value'] : null;
       
   747 
       
   748 			if ( isset( $q['compare'] ) )
       
   749 				$meta_compare = strtoupper( $q['compare'] );
       
   750 			else
       
   751 				$meta_compare = is_array( $meta_value ) ? 'IN' : '=';
       
   752 
       
   753 			if ( ! in_array( $meta_compare, array(
       
   754 				'=', '!=', '>', '>=', '<', '<=',
       
   755 				'LIKE', 'NOT LIKE',
       
   756 				'IN', 'NOT IN',
       
   757 				'BETWEEN', 'NOT BETWEEN',
       
   758 				'NOT EXISTS'
       
   759 			) ) )
       
   760 				$meta_compare = '=';
       
   761 
   723 			$i = count( $join );
   762 			$i = count( $join );
   724 			$alias = $i ? 'mt' . $i : $meta_table;
   763 			$alias = $i ? 'mt' . $i : $meta_table;
   725 
   764 
   726 			// Set JOIN
   765 			if ( 'NOT EXISTS' == $meta_compare ) {
       
   766 				$join[$i]  = "LEFT JOIN $meta_table";
       
   767 				$join[$i] .= $i ? " AS $alias" : '';
       
   768 				$join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column AND $alias.meta_key = '$meta_key')";
       
   769 
       
   770 				$where[$k] = ' ' . $alias . '.' . $meta_id_column . ' IS NULL';
       
   771 
       
   772 				continue;
       
   773 			}
       
   774 
   727 			$join[$i]  = "INNER JOIN $meta_table";
   775 			$join[$i]  = "INNER JOIN $meta_table";
   728 			$join[$i] .= $i ? " AS $alias" : '';
   776 			$join[$i] .= $i ? " AS $alias" : '';
   729 			$join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
   777 			$join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
   730 
   778 
   731 			$where[$k] = '';
   779 			$where[$k] = '';
   732 			if ( !empty( $meta_key ) )
   780 			if ( !empty( $meta_key ) )
   733 				$where[$k] = $wpdb->prepare( "$alias.meta_key = %s", $meta_key );
   781 				$where[$k] = $wpdb->prepare( "$alias.meta_key = %s", $meta_key );
   734 
   782 
   735 			if ( !isset( $q['value'] ) ) {
   783 			if ( is_null( $meta_value ) ) {
   736 				if ( empty( $where[$k] ) )
   784 				if ( empty( $where[$k] ) )
   737 					unset( $join[$i] );
   785 					unset( $join[$i] );
   738 				continue;
   786 				continue;
   739 			}
   787 			}
   740 
       
   741 			$meta_value = $q['value'];
       
   742 
       
   743 			$meta_compare = is_array( $meta_value ) ? 'IN' : '=';
       
   744 			if ( isset( $q['compare'] ) )
       
   745 				$meta_compare = strtoupper( $q['compare'] );
       
   746 
       
   747 			if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
       
   748 				$meta_compare = '=';
       
   749 
   788 
   750 			if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
   789 			if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
   751 				if ( ! is_array( $meta_value ) )
   790 				if ( ! is_array( $meta_value ) )
   752 					$meta_value = preg_split( '/[,\s]+/', $meta_value );
   791 					$meta_value = preg_split( '/[,\s]+/', $meta_value );
   753 
   792