From ead46cf4703608f065ddec495999932c9fbc2f0d Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 24 Sep 2009 17:10:28 +0000 Subject: [PATCH] Actions for postmeta updates. Props Demitrious Kelly. see #10750 git-svn-id: http://svn.automattic.com/wordpress/trunk@11968 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/post.php | 14 ++++++++++++-- wp-includes/comment.php | 10 ++++++++-- wp-includes/functions.php | 8 ++++++-- wp-includes/meta.php | 20 +++++++++++++------- wp-includes/post.php | 35 +++++++++++++++++++++++++++++------ 5 files changed, 68 insertions(+), 19 deletions(-) diff --git a/wp-admin/includes/post.php b/wp-admin/includes/post.php index da7628e600..f5b5fa1789 100644 --- a/wp-admin/includes/post.php +++ b/wp-admin/includes/post.php @@ -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; } // diff --git a/wp-includes/comment.php b/wp-includes/comment.php index 8411197c97..49c6e4eb43 100644 --- a/wp-includes/comment.php +++ b/wp-includes/comment.php @@ -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); } diff --git a/wp-includes/functions.php b/wp-includes/functions.php index f42dd607b2..d6d01be6b5 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -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']); } } -?> \ No newline at end of file +?> diff --git a/wp-includes/meta.php b/wp-includes/meta.php index c8fdf78678..2ff8e9b344 100644 --- a/wp-includes/meta.php +++ b/wp-includes/meta.php @@ -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; } -?> \ No newline at end of file +?> diff --git a/wp-includes/post.php b/wp-includes/post.php index b4e9383a74..ed83664456 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -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'); }