Actions for postmeta updates. Props Demitrious Kelly. see #10750

git-svn-id: http://svn.automattic.com/wordpress/trunk@11968 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2009-09-24 17:10:28 +00:00
parent 33836d522b
commit ead46cf470
5 changed files with 68 additions and 19 deletions

View File

@ -588,6 +588,8 @@ function add_meta( $post_ID ) {
wp_cache_delete($post_ID, 'post_meta');
$wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value ) VALUES (%s, %s, %s)", $post_ID, $metakey, $metavalue) );
do_action( 'added_postmeta', $wpdb->insert_id, $post_ID, $metakey, $metavalue );
return $wpdb->insert_id;
}
return false;
@ -608,7 +610,11 @@ function delete_meta( $mid ) {
$post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
wp_cache_delete($post_id, 'post_meta');
return $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
do_action( 'delete_postmeta', $mid );
$rval = $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
do_action( 'deleted_postmeta', $mid );
return $rval;
}
/**
@ -697,7 +703,11 @@ function update_meta( $meta_id, $meta_key, $meta_value ) {
$data = compact( 'meta_key', 'meta_value' );
$where = compact( 'meta_id' );
return $wpdb->update( $wpdb->postmeta, $data, $where );
do_action( 'update_postmeta', $meta_id, $post_id, $meta_key, $meta_value );
$rval = $wpdb->update( $wpdb->postmeta, $data, $where );
do_action( 'updated_postmeta', $meta_id, $post_id, $meta_key, $meta_value );
return $rval;
}
//

View File

@ -1449,13 +1449,19 @@ function do_all_pings() {
// Do pingbacks
while ($ping = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_pingme' LIMIT 1")) {
$wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$ping->ID} AND meta_key = '_pingme';");
$mid = $wpdb->get_var( "SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = {$ping->ID} AND meta_key = '_pingme' LIMIT 1");
do_action( 'delete_postmeta', $mid );
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE meta_id = %d", $mid ) );
do_action( 'deleted_postmeta', $mid );
pingback($ping->post_content, $ping->ID);
}
// Do Enclosures
while ($enclosure = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_encloseme' LIMIT 1")) {
$wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = '_encloseme';", $enclosure->ID) );
$mid = $wpdb->get_var( $wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = '_encloseme'", $enclosure->ID) );
do_action( 'delete_postmeta', $mid );
$wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE meta_id = %d", $mid) );
do_action( 'deleted_postmeta', $mid );
do_enclose($enclosure->post_content, $enclosure->ID);
}

View File

@ -1163,7 +1163,10 @@ function do_enclose( $content, $post_ID ) {
foreach ( $pung as $link_test ) {
if ( !in_array( $link_test, $post_links_temp[0] ) ) { // link no longer in post
$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, $link_test . '%') );
$mid = $wpdb->get_col( $wpdb->prepare("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, $link_test . '%') );
do_action( 'delete_postmeta', $mid );
$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE post_id IN(%s)", implode( ',', $mid ) ) );
do_action( 'deleted_postmeta', $mid );
}
}
@ -1186,6 +1189,7 @@ function do_enclose( $content, $post_ID ) {
if ( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) {
$meta_value = "$url\n$len\n$type\n";
$wpdb->insert($wpdb->postmeta, array('post_id' => $post_ID, 'meta_key' => 'enclosure', 'meta_value' => $meta_value) );
do_action( 'added_postmeta', $wpdb->insert_id, $post_ID, 'enclosure', $meta_value );
}
}
}
@ -3386,4 +3390,4 @@ function wp_scheduled_delete() {
wp_delete_comment($comment['comment_id']);
}
}
?>
?>

View File

@ -57,7 +57,7 @@ function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_v
// expected_slashed ($meta_key)
$meta_key = stripslashes($meta_key);
if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ) )
if ( ! $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ) )
return add_metadata($meta_type, $object_id, $meta_key, $meta_value);
$meta_value = maybe_serialize( stripslashes_deep($meta_value) );
@ -70,12 +70,12 @@ function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_v
$where['meta_value'] = $prev_value;
}
do_action( "update_{$meta_type}_meta", $object_id, $meta_key, $meta_value );
do_action( "update_{$meta_type}_meta", $object_id, $meta_key, $meta_value, $meta_id );
$wpdb->update( $table, $data, $where );
wp_cache_delete($object_id, $meta_type . '_meta');
do_action( "updated_{$meta_type}_meta", $object_id, $meta_key, $meta_value );
do_action( "updated_{$meta_type}_meta", $object_id, $meta_key, $meta_value, $meta_id );
return true;
}
@ -95,10 +95,16 @@ function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '') {
$meta_key = stripslashes($meta_key);
$meta_value = maybe_serialize( stripslashes_deep($meta_value) );
$query = $wpdb->prepare( "DELETE FROM $table WHERE meta_key = %s", $meta_key );
$query = $wpdb->prepare( "SELECT meta_id FROM $table WHERE meta_key = %s", $meta_key );
if ( $meta_value )
$query .= $wpdb->prepare("AND meta_value = %s", $meta_value );
$meta_ids = $wpdb->get_col( $query );
if ( !count( $meta_ids ) )
return false;
$query = "DELETE FROM $table WHERE meta_id IN( " . implode( ',', $meta_ids ) . " )";
$count = $wpdb->query($query);
@ -107,7 +113,7 @@ function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '') {
wp_cache_delete($object_id, $meta_type . '_meta');
do_action( "deleted_{$meta_type}_meta", $object_id, $meta_key, $meta_value );
do_action( "deleted_{$meta_type}_meta", $object_id, $meta_key, $meta_value, $meta_ids );
return true;
}
@ -212,4 +218,4 @@ function _get_meta_table($type) {
return $wpdb->$table_name;
}
?>
?>

View File

@ -601,7 +601,10 @@ function delete_post_meta_by_key($post_meta_key) {
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 ) {
$wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key));
$postmetaids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key ) );
do_action( 'delete_postmeta', $postmetaids );
$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id IN(%s)", implode( ',', $postmetaids) ) );
do_action( 'deleted_postmeta', $postmetaids );
foreach ( $post_ids as $post_id )
wp_cache_delete($post_id, 'post_meta');
return true;
@ -1102,11 +1105,19 @@ function wp_delete_post($postid = 0) {
// Point all attachments to this post up one level
$wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->comments WHERE comment_post_ID = %d", $postid ));
$commentids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $postid ));
do_action( 'delete_comment', $commentids );
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->comments WHERE comment_ID IN(%s)", implode( ',', $commentids ) ));
do_action( 'deleted_comment', $commentids );
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d", $postid ));
$postmetaids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id ));
do_action( 'delete_postmeta', $postmetaids );
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_id IN(%s)", implode( ',', $postmetaids ) ));
do_action( 'deleted_postmeta', $postmetaids );
do_action( 'delete_post', $post_id );
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $postid ));
do_action( 'deleted_post', $post_id );
if ( 'page' == $post->post_type ) {
clean_page_cache($postid);
@ -2584,11 +2595,19 @@ function wp_delete_attachment($post_id) {
/** @todo Delete for pluggable post taxonomies too */
wp_delete_object_term_relationships($post_id, array('category', 'post_tag'));
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ));
$commentids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ));
do_action( 'delete_comment', $commentids );
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->comments WHERE comment_ID IN(%s)", implode( ',', $commentids ) ));
do_action( 'deleted_comment', $commentids );
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d ", $post_id ));
$postmetaids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id ));
do_action( 'delete_postmeta', $postmetaids );
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_id IN(%s)", implode( ',', $postmetaids ) ));
do_action( 'deleted_postmeta', $postmetaids );
do_action( 'delete_post', $post_id );
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $post_id ));
do_action( 'deleted_post', $post_id );
$uploadpath = wp_upload_dir();
@ -3270,9 +3289,13 @@ function _publish_post_hook($post_id) {
return;
$data = array( 'post_id' => $post_id, 'meta_value' => '1' );
if ( get_option('default_pingback_flag') )
if ( get_option('default_pingback_flag') ) {
$wpdb->insert( $wpdb->postmeta, $data + array( 'meta_key' => '_pingme' ) );
do_action( 'added_postmeta', $wpdb->insert_id, $post_id, '_pingme', 1 );
}
$wpdb->insert( $wpdb->postmeta, $data + array( 'meta_key' => '_encloseme' ) );
do_action( 'added_postmeta', $wpdb->insert_id, $post_id, '_encloseme', 1 );
wp_schedule_single_event(time(), 'do_pings');
}