<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre, #msg p { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[11968] trunk: Actions for postmeta updates.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/11968">11968</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2009-09-24 17:10:28 +0000 (Thu, 24 Sep 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>Actions for postmeta updates. Props Demitrious Kelly. see <a href="http://trac.wordpress.org/ticket/10750">#10750</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminincludespostphp">trunk/wp-admin/includes/post.php</a></li>
<li><a href="#trunkwpincludescommentphp">trunk/wp-includes/comment.php</a></li>
<li><a href="#trunkwpincludesfunctionsphp">trunk/wp-includes/functions.php</a></li>
<li><a href="#trunkwpincludesmetaphp">trunk/wp-includes/meta.php</a></li>
<li><a href="#trunkwpincludespostphp">trunk/wp-includes/post.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/post.php (11967 => 11968)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/post.php        2009-09-24 08:47:33 UTC (rev 11967)
+++ trunk/wp-admin/includes/post.php        2009-09-24 17:10:28 UTC (rev 11968)
</span><span class="lines">@@ -588,6 +588,8 @@
</span><span class="cx">                 wp_cache_delete($post_ID, 'post_meta');
</span><span class="cx"> 
</span><span class="cx">                 $wpdb-&gt;query( $wpdb-&gt;prepare(&quot;INSERT INTO $wpdb-&gt;postmeta (post_id,meta_key,meta_value ) VALUES (%s, %s, %s)&quot;, $post_ID, $metakey, $metavalue) );
</span><ins>+                do_action( 'added_postmeta', $wpdb-&gt;insert_id, $post_ID, $metakey, $metavalue );
+                
</ins><span class="cx">                 return $wpdb-&gt;insert_id;
</span><span class="cx">         }
</span><span class="cx">         return false;
</span><span class="lines">@@ -608,7 +610,11 @@
</span><span class="cx">         $post_id = $wpdb-&gt;get_var( $wpdb-&gt;prepare(&quot;SELECT post_id FROM $wpdb-&gt;postmeta WHERE meta_id = %d&quot;, $mid) );
</span><span class="cx">         wp_cache_delete($post_id, 'post_meta');
</span><span class="cx"> 
</span><del>-        return $wpdb-&gt;query( $wpdb-&gt;prepare(&quot;DELETE FROM $wpdb-&gt;postmeta WHERE meta_id = %d&quot;, $mid) );
</del><ins>+        do_action( 'delete_postmeta', $mid );
+        $rval = $wpdb-&gt;query( $wpdb-&gt;prepare(&quot;DELETE FROM $wpdb-&gt;postmeta WHERE meta_id = %d&quot;, $mid) );
+        do_action( 'deleted_postmeta', $mid );
+
+        return $rval;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -697,7 +703,11 @@
</span><span class="cx">         $data  = compact( 'meta_key', 'meta_value' );
</span><span class="cx">         $where = compact( 'meta_id' );
</span><span class="cx"> 
</span><del>-        return $wpdb-&gt;update( $wpdb-&gt;postmeta, $data, $where );
</del><ins>+        do_action( 'update_postmeta', $meta_id, $post_id, $meta_key, $meta_value );
+        $rval = $wpdb-&gt;update( $wpdb-&gt;postmeta, $data, $where );
+        do_action( 'updated_postmeta', $meta_id, $post_id, $meta_key, $meta_value );
+
+        return $rval;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> //
</span></span></pre></div>
<a id="trunkwpincludescommentphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/comment.php (11967 => 11968)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/comment.php        2009-09-24 08:47:33 UTC (rev 11967)
+++ trunk/wp-includes/comment.php        2009-09-24 17:10:28 UTC (rev 11968)
</span><span class="lines">@@ -1449,13 +1449,19 @@
</span><span class="cx"> 
</span><span class="cx">         // Do pingbacks
</span><span class="cx">         while ($ping = $wpdb-&gt;get_row(&quot;SELECT * FROM {$wpdb-&gt;posts}, {$wpdb-&gt;postmeta} WHERE {$wpdb-&gt;posts}.ID = {$wpdb-&gt;postmeta}.post_id AND {$wpdb-&gt;postmeta}.meta_key = '_pingme' LIMIT 1&quot;)) {
</span><del>-                $wpdb-&gt;query(&quot;DELETE FROM {$wpdb-&gt;postmeta} WHERE post_id = {$ping-&gt;ID} AND meta_key = '_pingme';&quot;);
</del><ins>+                $mid = $wpdb-&gt;get_var( &quot;SELECT meta_id FROM {$wpdb-&gt;postmeta} WHERE post_id = {$ping-&gt;ID} AND meta_key = '_pingme' LIMIT 1&quot;);
+                do_action( 'delete_postmeta', $mid );
+                $wpdb-&gt;query( $wpdb-&gt;prepare( &quot;DELETE FROM {$wpdb-&gt;postmeta} WHERE meta_id = %d&quot;, $mid ) );
+                do_action( 'deleted_postmeta', $mid );
</ins><span class="cx">                 pingback($ping-&gt;post_content, $ping-&gt;ID);
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         // Do Enclosures
</span><span class="cx">         while ($enclosure = $wpdb-&gt;get_row(&quot;SELECT * FROM {$wpdb-&gt;posts}, {$wpdb-&gt;postmeta} WHERE {$wpdb-&gt;posts}.ID = {$wpdb-&gt;postmeta}.post_id AND {$wpdb-&gt;postmeta}.meta_key = '_encloseme' LIMIT 1&quot;)) {
</span><del>-                $wpdb-&gt;query( $wpdb-&gt;prepare(&quot;DELETE FROM {$wpdb-&gt;postmeta} WHERE post_id = %d AND meta_key = '_encloseme';&quot;, $enclosure-&gt;ID) );
</del><ins>+                $mid = $wpdb-&gt;get_var( $wpdb-&gt;prepare(&quot;SELECT meta_id FROM {$wpdb-&gt;postmeta} WHERE post_id = %d AND meta_key = '_encloseme'&quot;, $enclosure-&gt;ID) );
+                do_action( 'delete_postmeta', $mid );
+                $wpdb-&gt;query( $wpdb-&gt;prepare(&quot;DELETE FROM {$wpdb-&gt;postmeta} WHERE meta_id =  %d&quot;, $mid) );
+                do_action( 'deleted_postmeta', $mid );
</ins><span class="cx">                 do_enclose($enclosure-&gt;post_content, $enclosure-&gt;ID);
</span><span class="cx">         }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpincludesfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/functions.php (11967 => 11968)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/functions.php        2009-09-24 08:47:33 UTC (rev 11967)
+++ trunk/wp-includes/functions.php        2009-09-24 17:10:28 UTC (rev 11968)
</span><span class="lines">@@ -1163,7 +1163,10 @@
</span><span class="cx"> 
</span><span class="cx">         foreach ( $pung as $link_test ) {
</span><span class="cx">                 if ( !in_array( $link_test, $post_links_temp[0] ) ) { // link no longer in post
</span><del>-                        $wpdb-&gt;query( $wpdb-&gt;prepare(&quot;DELETE FROM $wpdb-&gt;postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)&quot;, $post_ID, $link_test . '%') );
</del><ins>+                        $mid = $wpdb-&gt;get_col( $wpdb-&gt;prepare(&quot;SELECT meta_id FROM $wpdb-&gt;postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)&quot;, $post_ID, $link_test . '%') );
+                        do_action( 'delete_postmeta', $mid );
+                        $wpdb-&gt;query( $wpdb-&gt;prepare(&quot;DELETE FROM $wpdb-&gt;postmeta WHERE post_id IN(%s)&quot;, implode( ',', $mid ) ) );
+                        do_action( 'deleted_postmeta', $mid );
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -1186,6 +1189,7 @@
</span><span class="cx">                                 if ( in_array( substr( $type, 0, strpos( $type, &quot;/&quot; ) ), $allowed_types ) ) {
</span><span class="cx">                                         $meta_value = &quot;$url\n$len\n$type\n&quot;;
</span><span class="cx">                                         $wpdb-&gt;insert($wpdb-&gt;postmeta, array('post_id' =&gt; $post_ID, 'meta_key' =&gt; 'enclosure', 'meta_value' =&gt; $meta_value) );
</span><ins>+                                        do_action( 'added_postmeta', $wpdb-&gt;insert_id, $post_ID, 'enclosure', $meta_value );
</ins><span class="cx">                                 }
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="lines">@@ -3386,4 +3390,4 @@
</span><span class="cx">                 wp_delete_comment($comment['comment_id']);
</span><span class="cx">         }
</span><span class="cx"> }
</span><del>-?&gt;
</del><span class="cx">\ No newline at end of file
</span><ins>+?&gt;
</ins></span></pre></div>
<a id="trunkwpincludesmetaphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/meta.php (11967 => 11968)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/meta.php        2009-09-24 08:47:33 UTC (rev 11967)
+++ trunk/wp-includes/meta.php        2009-09-24 17:10:28 UTC (rev 11968)
</span><span class="lines">@@ -57,7 +57,7 @@
</span><span class="cx">         // expected_slashed ($meta_key)
</span><span class="cx">         $meta_key = stripslashes($meta_key);
</span><span class="cx"> 
</span><del>-        if ( ! $wpdb-&gt;get_var( $wpdb-&gt;prepare( &quot;SELECT meta_key FROM $table WHERE meta_key = %s AND $column = %d&quot;, $meta_key, $object_id ) ) )
</del><ins>+        if ( ! $meta_id = $wpdb-&gt;get_var( $wpdb-&gt;prepare( &quot;SELECT meta_id FROM $table WHERE meta_key = %s AND $column = %d&quot;, $meta_key, $object_id ) ) )
</ins><span class="cx">                 return add_metadata($meta_type, $object_id, $meta_key, $meta_value);
</span><span class="cx"> 
</span><span class="cx">         $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
</span><span class="lines">@@ -70,12 +70,12 @@
</span><span class="cx">                 $where['meta_value'] = $prev_value;
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        do_action( &quot;update_{$meta_type}_meta&quot;, $object_id, $meta_key, $meta_value );
</del><ins>+        do_action( &quot;update_{$meta_type}_meta&quot;, $object_id, $meta_key, $meta_value, $meta_id );
</ins><span class="cx"> 
</span><span class="cx">         $wpdb-&gt;update( $table, $data, $where );
</span><span class="cx">         wp_cache_delete($object_id, $meta_type . '_meta');
</span><span class="cx"> 
</span><del>-        do_action( &quot;updated_{$meta_type}_meta&quot;, $object_id, $meta_key, $meta_value );
</del><ins>+        do_action( &quot;updated_{$meta_type}_meta&quot;, $object_id, $meta_key, $meta_value, $meta_id );
</ins><span class="cx"> 
</span><span class="cx">         return true;
</span><span class="cx"> }
</span><span class="lines">@@ -95,10 +95,16 @@
</span><span class="cx">         $meta_key = stripslashes($meta_key);
</span><span class="cx">         $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
</span><span class="cx"> 
</span><del>-        $query = $wpdb-&gt;prepare( &quot;DELETE FROM $table WHERE meta_key = %s&quot;, $meta_key );
-
</del><ins>+        $query = $wpdb-&gt;prepare( &quot;SELECT meta_id FROM $table WHERE meta_key = %s&quot;, $meta_key );
+        
</ins><span class="cx">         if ( $meta_value )
</span><span class="cx">                 $query .= $wpdb-&gt;prepare(&quot;AND meta_value = %s&quot;, $meta_value );
</span><ins>+
+        $meta_ids = $wpdb-&gt;get_col( $query );
+        if ( !count( $meta_ids ) )
+                return false;
+
+        $query = &quot;DELETE FROM $table WHERE meta_id IN( &quot; . implode( ',', $meta_ids ) . &quot; )&quot;;
</ins><span class="cx">         
</span><span class="cx">         $count = $wpdb-&gt;query($query);
</span><span class="cx">         
</span><span class="lines">@@ -107,7 +113,7 @@
</span><span class="cx"> 
</span><span class="cx">         wp_cache_delete($object_id, $meta_type . '_meta');
</span><span class="cx"> 
</span><del>-        do_action( &quot;deleted_{$meta_type}_meta&quot;, $object_id, $meta_key, $meta_value );
</del><ins>+        do_action( &quot;deleted_{$meta_type}_meta&quot;, $object_id, $meta_key, $meta_value, $meta_ids );
</ins><span class="cx"> 
</span><span class="cx">         return true;
</span><span class="cx"> }
</span><span class="lines">@@ -212,4 +218,4 @@
</span><span class="cx"> 
</span><span class="cx">         return $wpdb-&gt;$table_name;
</span><span class="cx"> }
</span><del>-?&gt;
</del><span class="cx">\ No newline at end of file
</span><ins>+?&gt;
</ins></span></pre></div>
<a id="trunkwpincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/post.php (11967 => 11968)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/post.php        2009-09-24 08:47:33 UTC (rev 11967)
+++ trunk/wp-includes/post.php        2009-09-24 17:10:28 UTC (rev 11968)
</span><span class="lines">@@ -601,7 +601,10 @@
</span><span class="cx">         global $wpdb;
</span><span class="cx">         $post_ids = $wpdb-&gt;get_col($wpdb-&gt;prepare(&quot;SELECT DISTINCT post_id FROM $wpdb-&gt;postmeta WHERE meta_key = %s&quot;, $post_meta_key));
</span><span class="cx">         if ( $post_ids ) {
</span><del>-                $wpdb-&gt;query($wpdb-&gt;prepare(&quot;DELETE FROM $wpdb-&gt;postmeta WHERE meta_key = %s&quot;, $post_meta_key));
</del><ins>+                $postmetaids = $wpdb-&gt;get_col( $wpdb-&gt;prepare( &quot;SELECT meta_id FROM $wpdb-&gt;postmeta WHERE meta_key = %s&quot;, $post_meta_key ) );
+                do_action( 'delete_postmeta', $postmetaids );
+                $wpdb-&gt;query( $wpdb-&gt;prepare(&quot;DELETE FROM $wpdb-&gt;postmeta WHERE meta_id IN(%s)&quot;, implode( ',', $postmetaids) ) );
+                do_action( 'deleted_postmeta', $postmetaids );
</ins><span class="cx">                 foreach ( $post_ids as $post_id )
</span><span class="cx">                         wp_cache_delete($post_id, 'post_meta');
</span><span class="cx">                 return true;
</span><span class="lines">@@ -1102,11 +1105,19 @@
</span><span class="cx">         // Point all attachments to this post up one level
</span><span class="cx">         $wpdb-&gt;update( $wpdb-&gt;posts, $parent_data, $parent_where + array( 'post_type' =&gt; 'attachment' ) );
</span><span class="cx"> 
</span><del>-        $wpdb-&gt;query( $wpdb-&gt;prepare( &quot;DELETE FROM $wpdb-&gt;comments WHERE comment_post_ID = %d&quot;, $postid ));
</del><ins>+        $commentids = $wpdb-&gt;get_col( $wpdb-&gt;prepare( &quot;SELECT comment_ID FROM $wpdb-&gt;comments WHERE comment_post_ID = %d&quot;, $postid ));
+        do_action( 'delete_comment', $commentids );
+        $wpdb-&gt;query( $wpdb-&gt;prepare( &quot;DELETE FROM $wpdb-&gt;comments WHERE comment_ID IN(%s)&quot;, implode( ',', $commentids ) ));
+        do_action( 'deleted_comment', $commentids );
</ins><span class="cx"> 
</span><del>-        $wpdb-&gt;query( $wpdb-&gt;prepare( &quot;DELETE FROM $wpdb-&gt;postmeta WHERE post_id = %d&quot;, $postid ));
</del><ins>+        $postmetaids = $wpdb-&gt;get_col( $wpdb-&gt;prepare( &quot;SELECT meta_id FROM $wpdb-&gt;postmeta WHERE post_id = %d &quot;, $post_id ));
+        do_action( 'delete_postmeta', $postmetaids );
+        $wpdb-&gt;query( $wpdb-&gt;prepare( &quot;DELETE FROM $wpdb-&gt;postmeta WHERE meta_id IN(%s)&quot;, implode( ',', $postmetaids ) ));
+        do_action( 'deleted_postmeta', $postmetaids );
</ins><span class="cx"> 
</span><ins>+        do_action( 'delete_post', $post_id );
</ins><span class="cx">         $wpdb-&gt;query( $wpdb-&gt;prepare( &quot;DELETE FROM $wpdb-&gt;posts WHERE ID = %d&quot;, $postid ));
</span><ins>+        do_action( 'deleted_post', $post_id );
</ins><span class="cx"> 
</span><span class="cx">         if ( 'page' == $post-&gt;post_type ) {
</span><span class="cx">                 clean_page_cache($postid);
</span><span class="lines">@@ -2584,11 +2595,19 @@
</span><span class="cx">         /** @todo Delete for pluggable post taxonomies too */
</span><span class="cx">         wp_delete_object_term_relationships($post_id, array('category', 'post_tag'));
</span><span class="cx"> 
</span><del>-        $wpdb-&gt;query( $wpdb-&gt;prepare( &quot;DELETE FROM $wpdb-&gt;comments WHERE comment_post_ID = %d&quot;, $post_id ));
</del><ins>+        $commentids = $wpdb-&gt;get_col( $wpdb-&gt;prepare( &quot;SELECT comment_ID FROM $wpdb-&gt;comments WHERE comment_post_ID = %d&quot;, $post_id ));
+        do_action( 'delete_comment', $commentids );
+        $wpdb-&gt;query( $wpdb-&gt;prepare( &quot;DELETE FROM $wpdb-&gt;comments WHERE comment_ID IN(%s)&quot;, implode( ',', $commentids ) ));
+        do_action( 'deleted_comment', $commentids );
</ins><span class="cx"> 
</span><del>-        $wpdb-&gt;query( $wpdb-&gt;prepare( &quot;DELETE FROM $wpdb-&gt;postmeta WHERE post_id = %d &quot;, $post_id ));
</del><ins>+        $postmetaids = $wpdb-&gt;get_col( $wpdb-&gt;prepare( &quot;SELECT meta_id FROM $wpdb-&gt;postmeta WHERE post_id = %d &quot;, $post_id ));
+        do_action( 'delete_postmeta', $postmetaids );
+        $wpdb-&gt;query( $wpdb-&gt;prepare( &quot;DELETE FROM $wpdb-&gt;postmeta WHERE meta_id IN(%s)&quot;, implode( ',', $postmetaids ) ));
+        do_action( 'deleted_postmeta', $postmetaids );
</ins><span class="cx"> 
</span><ins>+        do_action( 'delete_post', $post_id );
</ins><span class="cx">         $wpdb-&gt;query( $wpdb-&gt;prepare( &quot;DELETE FROM $wpdb-&gt;posts WHERE ID = %d&quot;, $post_id ));
</span><ins>+        do_action( 'deleted_post', $post_id );
</ins><span class="cx"> 
</span><span class="cx">         $uploadpath = wp_upload_dir();
</span><span class="cx"> 
</span><span class="lines">@@ -3270,9 +3289,13 @@
</span><span class="cx">                 return;
</span><span class="cx"> 
</span><span class="cx">         $data = array( 'post_id' =&gt; $post_id, 'meta_value' =&gt; '1' );
</span><del>-        if ( get_option('default_pingback_flag') )
</del><ins>+        if ( get_option('default_pingback_flag') ) {
</ins><span class="cx">                 $wpdb-&gt;insert( $wpdb-&gt;postmeta, $data + array( 'meta_key' =&gt; '_pingme' ) );
</span><ins>+                do_action( 'added_postmeta', $wpdb-&gt;insert_id, $post_id, '_pingme', 1 );
+        }
</ins><span class="cx">         $wpdb-&gt;insert( $wpdb-&gt;postmeta, $data + array( 'meta_key' =&gt; '_encloseme' ) );
</span><ins>+        do_action( 'added_postmeta', $wpdb-&gt;insert_id, $post_id, '_encloseme', 1 );
+
</ins><span class="cx">         wp_schedule_single_event(time(), 'do_pings');
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>