Turn delete_meta() , get_post_meta_by_id(), update_meta(), delete_post_meta_by_key() into wrappers around the metadata API. Add back compat *_postmeta actions to metadata API. Props jgadbois. see #18196
git-svn-id: http://svn.automattic.com/wordpress/trunk@18500 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
3713abe855
commit
24d480d088
|
@ -701,17 +701,7 @@ function add_meta( $post_ID ) {
|
||||||
* @return unknown
|
* @return unknown
|
||||||
*/
|
*/
|
||||||
function delete_meta( $mid ) {
|
function delete_meta( $mid ) {
|
||||||
global $wpdb;
|
return delete_metadata_by_mid( 'post' , $mid );
|
||||||
$mid = (int) $mid;
|
|
||||||
|
|
||||||
$post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
|
|
||||||
|
|
||||||
do_action( 'delete_postmeta', $mid );
|
|
||||||
wp_cache_delete($post_id, 'post_meta');
|
|
||||||
$rval = $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
|
|
||||||
do_action( 'deleted_postmeta', $mid );
|
|
||||||
|
|
||||||
return $rval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -742,15 +732,7 @@ function get_meta_keys() {
|
||||||
* @return unknown
|
* @return unknown
|
||||||
*/
|
*/
|
||||||
function get_post_meta_by_id( $mid ) {
|
function get_post_meta_by_id( $mid ) {
|
||||||
global $wpdb;
|
return get_metadata_by_mid( 'post', $mid );
|
||||||
$mid = (int) $mid;
|
|
||||||
|
|
||||||
$meta = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
|
|
||||||
if ( empty($meta) )
|
|
||||||
return false;
|
|
||||||
if ( is_serialized_string( $meta->meta_value ) )
|
|
||||||
$meta->meta_value = maybe_unserialize( $meta->meta_value );
|
|
||||||
return $meta;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -782,27 +764,10 @@ function has_meta( $postid ) {
|
||||||
* @return unknown
|
* @return unknown
|
||||||
*/
|
*/
|
||||||
function update_meta( $meta_id, $meta_key, $meta_value ) {
|
function update_meta( $meta_id, $meta_key, $meta_value ) {
|
||||||
global $wpdb;
|
$meta_key = stripslashes( $meta_key );
|
||||||
|
$meta_value = stripslashes_deep( $meta_value );
|
||||||
|
|
||||||
$meta_key = stripslashes($meta_key);
|
return update_metadata_by_mid( 'post', $meta_id, $meta_value, $meta_key );
|
||||||
|
|
||||||
if ( '' === trim( $meta_value ) )
|
|
||||||
return false;
|
|
||||||
|
|
||||||
$post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = %d", $meta_id) );
|
|
||||||
|
|
||||||
$meta_value = maybe_serialize( stripslashes_deep( $meta_value ) );
|
|
||||||
$meta_id = (int) $meta_id;
|
|
||||||
|
|
||||||
$data = compact( 'meta_key', 'meta_value' );
|
|
||||||
$where = compact( 'meta_id' );
|
|
||||||
|
|
||||||
do_action( 'update_postmeta', $meta_id, $post_id, $meta_key, $meta_value );
|
|
||||||
$rval = $wpdb->update( $wpdb->postmeta, $data, $where );
|
|
||||||
wp_cache_delete($post_id, 'post_meta');
|
|
||||||
do_action( 'updated_postmeta', $meta_id, $post_id, $meta_key, $meta_value );
|
|
||||||
|
|
||||||
return $rval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
@ -149,6 +149,9 @@ function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_v
|
||||||
}
|
}
|
||||||
|
|
||||||
do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
|
do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
|
||||||
|
|
||||||
|
if ( 'post' == $meta_type )
|
||||||
|
do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
|
||||||
|
|
||||||
$wpdb->update( $table, $data, $where );
|
$wpdb->update( $table, $data, $where );
|
||||||
|
|
||||||
|
@ -158,6 +161,9 @@ function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_v
|
||||||
clean_user_cache($object_id);
|
clean_user_cache($object_id);
|
||||||
|
|
||||||
do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
|
do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
|
||||||
|
|
||||||
|
if ( 'post' == $meta_type )
|
||||||
|
do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -204,7 +210,7 @@ function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $d
|
||||||
|
|
||||||
$_meta_value = $meta_value;
|
$_meta_value = $meta_value;
|
||||||
$meta_value = maybe_serialize( $meta_value );
|
$meta_value = maybe_serialize( $meta_value );
|
||||||
|
|
||||||
$query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key );
|
$query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key );
|
||||||
|
|
||||||
if ( !$delete_all )
|
if ( !$delete_all )
|
||||||
|
@ -217,7 +223,13 @@ function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $d
|
||||||
if ( !count( $meta_ids ) )
|
if ( !count( $meta_ids ) )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
if ( $delete_all )
|
||||||
|
$object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) );
|
||||||
|
|
||||||
do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
|
do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
|
||||||
|
|
||||||
|
if ( 'post' == $meta_type )
|
||||||
|
do_action( 'delete_postmeta', $meta_ids );
|
||||||
|
|
||||||
$query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . " )";
|
$query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . " )";
|
||||||
|
|
||||||
|
@ -226,12 +238,22 @@ function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $d
|
||||||
if ( !$count )
|
if ( !$count )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
wp_cache_delete($object_id, $meta_type . '_meta');
|
if ( $delete_all ) {
|
||||||
|
foreach ( (array) $object_ids as $o_id ) {
|
||||||
|
wp_cache_delete($o_id, $meta_type . '_meta');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
wp_cache_delete($object_id, $meta_type . '_meta');
|
||||||
|
}
|
||||||
|
|
||||||
// users cache stores usermeta that must be cleared.
|
// users cache stores usermeta that must be cleared.
|
||||||
if ( 'user' == $meta_type )
|
if ( 'user' == $meta_type )
|
||||||
clean_user_cache($object_id);
|
clean_user_cache($object_id);
|
||||||
|
|
||||||
do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
|
do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
|
||||||
|
|
||||||
|
if ( 'post' == $meta_type )
|
||||||
|
do_action( 'deleted_postmeta', $meta_ids );
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -381,6 +403,9 @@ function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key =
|
||||||
$where[$id_column] = $meta_id;
|
$where[$id_column] = $meta_id;
|
||||||
|
|
||||||
do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
|
do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
|
||||||
|
|
||||||
|
if ( 'post' == $meta_type )
|
||||||
|
do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
|
||||||
|
|
||||||
// Run the update query, all fields in $data are %s, $where is a %d.
|
// Run the update query, all fields in $data are %s, $where is a %d.
|
||||||
$result = (bool) $wpdb->update( $table, $data, $where, '%s', '%d' );
|
$result = (bool) $wpdb->update( $table, $data, $where, '%s', '%d' );
|
||||||
|
@ -394,6 +419,9 @@ function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key =
|
||||||
|
|
||||||
do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
|
do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
|
||||||
|
|
||||||
|
if ( 'post' == $meta_type )
|
||||||
|
do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -436,18 +464,24 @@ function delete_metadata_by_mid( $meta_type, $meta_id ) {
|
||||||
|
|
||||||
do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
|
do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
|
||||||
|
|
||||||
|
if ( 'post' == $meta_type )
|
||||||
|
do_action( 'delete_postmeta', $object_id );
|
||||||
|
|
||||||
// Run the query, will return true if deleted, false otherwise
|
// Run the query, will return true if deleted, false otherwise
|
||||||
$result = (bool) $wpdb->query( $wpdb->prepare( "DELETE FROM $table WHERE $id_column = %d LIMIT 1;", $meta_id ) );
|
$result = (bool) $wpdb->query( $wpdb->prepare( "DELETE FROM $table WHERE $id_column = %d LIMIT 1;", $meta_id ) );
|
||||||
|
|
||||||
// Clear the caches.
|
// Clear the caches.
|
||||||
wp_cache_delete($object_id, $meta_type . '_meta');
|
wp_cache_delete($object_id, $meta_type . '_meta');
|
||||||
|
|
||||||
// Users cache stores usermeta that must be cleared.
|
// Users cache stores usermeta that must be cleared.
|
||||||
if ( 'user' == $meta_type )
|
if ( 'user' == $meta_type )
|
||||||
clean_user_cache($object_id);
|
clean_user_cache($object_id);
|
||||||
|
|
||||||
do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
|
do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
|
||||||
|
|
||||||
|
if ( 'post' == $meta_type )
|
||||||
|
do_action( 'delete_postmeta', $object_id );
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1497,22 +1497,7 @@ function update_post_meta($post_id, $meta_key, $meta_value, $prev_value = '') {
|
||||||
* @return bool Whether the post meta key was deleted from the database
|
* @return bool Whether the post meta key was deleted from the database
|
||||||
*/
|
*/
|
||||||
function delete_post_meta_by_key($post_meta_key) {
|
function delete_post_meta_by_key($post_meta_key) {
|
||||||
if ( !$post_meta_key )
|
return delete_metadata( 'post', null, $post_meta_key, '', true );
|
||||||
return false;
|
|
||||||
|
|
||||||
global $wpdb;
|
|
||||||
$post_ids = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key));
|
|
||||||
if ( $post_ids ) {
|
|
||||||
$postmetaids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key ) );
|
|
||||||
$in = implode( ',', array_fill(1, count($postmetaids), '%d'));
|
|
||||||
do_action( 'delete_postmeta', $postmetaids );
|
|
||||||
$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id IN($in)", $postmetaids ));
|
|
||||||
do_action( 'deleted_postmeta', $postmetaids );
|
|
||||||
foreach ( $post_ids as $post_id )
|
|
||||||
wp_cache_delete($post_id, 'post_meta');
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue