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
|
||||
*/
|
||||
function delete_meta( $mid ) {
|
||||
global $wpdb;
|
||||
$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;
|
||||
return delete_metadata_by_mid( 'post' , $mid );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -742,15 +732,7 @@ function get_meta_keys() {
|
|||
* @return unknown
|
||||
*/
|
||||
function get_post_meta_by_id( $mid ) {
|
||||
global $wpdb;
|
||||
$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;
|
||||
return get_metadata_by_mid( 'post', $mid );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -782,27 +764,10 @@ function has_meta( $postid ) {
|
|||
* @return unknown
|
||||
*/
|
||||
function update_meta( $meta_id, $meta_key, $meta_value ) {
|
||||
global $wpdb;
|
||||
|
||||
$meta_key = stripslashes( $meta_key );
|
||||
$meta_value = stripslashes_deep( $meta_value );
|
||||
|
||||
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;
|
||||
return update_metadata_by_mid( 'post', $meta_id, $meta_value, $meta_key );
|
||||
}
|
||||
|
||||
//
|
||||
|
|
|
@ -150,6 +150,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 );
|
||||
|
||||
if ( 'post' == $meta_type )
|
||||
do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
|
||||
|
||||
$wpdb->update( $table, $data, $where );
|
||||
|
||||
wp_cache_delete($object_id, $meta_type . '_meta');
|
||||
|
@ -159,6 +162,9 @@ function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_v
|
|||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -217,8 +223,14 @@ function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $d
|
|||
if ( !count( $meta_ids ) )
|
||||
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 );
|
||||
|
||||
if ( 'post' == $meta_type )
|
||||
do_action( 'delete_postmeta', $meta_ids );
|
||||
|
||||
$query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . " )";
|
||||
|
||||
$count = $wpdb->query($query);
|
||||
|
@ -226,13 +238,23 @@ function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $d
|
|||
if ( !$count )
|
||||
return false;
|
||||
|
||||
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.
|
||||
if ( 'user' == $meta_type )
|
||||
clean_user_cache($object_id);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -382,6 +404,9 @@ function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key =
|
|||
|
||||
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.
|
||||
$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 );
|
||||
|
||||
if ( 'post' == $meta_type )
|
||||
do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
@ -436,6 +464,9 @@ 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 );
|
||||
|
||||
if ( 'post' == $meta_type )
|
||||
do_action( 'delete_postmeta', $object_id );
|
||||
|
||||
// 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 ) );
|
||||
|
||||
|
@ -448,6 +479,9 @@ function delete_metadata_by_mid( $meta_type, $meta_id ) {
|
|||
|
||||
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;
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
function delete_post_meta_by_key($post_meta_key) {
|
||||
if ( !$post_meta_key )
|
||||
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;
|
||||
return delete_metadata( 'post', null, $post_meta_key, '', true );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue