<!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" />
<title>[20435] trunk/wp-includes: Use the metadata API rather than raw queries and direct do_action calls.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { 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 { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#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>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/20435">20435</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2012-04-11 20:02:39 +0000 (Wed, 11 Apr 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Use the metadata API rather than raw queries and direct do_action calls. see <a href="http://core.trac.wordpress.org/ticket/20417">#20417</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<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="trunkwpincludescommentphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/comment.php (20434 => 20435)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/comment.php        2012-04-11 02:58:23 UTC (rev 20434)
+++ trunk/wp-includes/comment.php        2012-04-11 20:02:39 UTC (rev 20435)
</span><span class="lines">@@ -980,13 +980,9 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         // Delete metadata
</span><del>-        $meta_ids = $wpdb-&gt;get_col( $wpdb-&gt;prepare( &quot;SELECT meta_id FROM $wpdb-&gt;commentmeta WHERE comment_id = %d &quot;, $comment_id ) );
-        if ( !empty($meta_ids) ) {
-                do_action( 'delete_commentmeta', $meta_ids );
-                $in_meta_ids = &quot;'&quot; . implode(&quot;', '&quot;, $meta_ids) . &quot;'&quot;;
-                $wpdb-&gt;query( &quot;DELETE FROM $wpdb-&gt;commentmeta WHERE meta_id IN ($in_meta_ids)&quot; );
-                do_action( 'deleted_commentmeta', $meta_ids );
-        }
</del><ins>+        $meta_ids = $wpdb-&gt;get_col( $wpdb-&gt;prepare( &quot;SELECT meta_id FROM $wpdb-&gt;commentmeta WHERE comment_id = %d&quot;, $comment_id ) );
+        foreach ( $meta_ids as $mid )
+                delete_metadata_by_mid( 'comment', $mid );
</ins><span class="cx"> 
</span><span class="cx">         if ( ! $wpdb-&gt;delete( $wpdb-&gt;comments, array( 'comment_ID' =&gt; $comment_id ) ) )
</span><span class="cx">                 return false;
</span></span></pre></div>
<a id="trunkwpincludesfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/functions.php (20434 => 20435)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/functions.php        2012-04-11 02:58:23 UTC (rev 20434)
+++ trunk/wp-includes/functions.php        2012-04-11 20:02:39 UTC (rev 20435)
</span><span class="lines">@@ -426,10 +426,9 @@
</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>-                        $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, like_escape( $link_test ) . '%') );
-                        do_action( 'delete_postmeta', $mid );
-                        $wpdb-&gt;query( $wpdb-&gt;prepare(&quot;DELETE FROM $wpdb-&gt;postmeta WHERE meta_id IN(%s)&quot;, implode( ',', $mid ) ) );
-                        do_action( 'deleted_postmeta', $mid );
</del><ins>+                        $mids = $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, like_escape( $link_test ) . '%') );
+                        foreach ( $mids as $mid )
+                                delete_metadata_by_mid( 'post', $mid );
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -469,9 +468,7 @@
</span><span class="cx">                                 }
</span><span class="cx"> 
</span><span class="cx">                                 if ( in_array( substr( $type, 0, strpos( $type, &quot;/&quot; ) ), $allowed_types ) ) {
</span><del>-                                        $meta_value = &quot;$url\n$len\n$type\n&quot;;
-                                        $wpdb-&gt;insert($wpdb-&gt;postmeta, array('post_id' =&gt; $post_ID, 'meta_key' =&gt; 'enclosure', 'meta_value' =&gt; $meta_value) );
-                                        do_action( 'added_postmeta', $wpdb-&gt;insert_id, $post_ID, 'enclosure', $meta_value );
</del><ins>+                                        add_post_meta( $post_ID, 'enclosure', &quot;$url\n$len\n$mime\n&quot; );
</ins><span class="cx">                                 }
</span><span class="cx">                         }
</span><span class="cx">                 }
</span></span></pre></div>
<a id="trunkwpincludesmetaphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/meta.php (20434 => 20435)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/meta.php        2012-04-11 02:58:23 UTC (rev 20434)
+++ trunk/wp-includes/meta.php        2012-04-11 20:02:39 UTC (rev 20435)
</span><span class="lines">@@ -223,6 +223,7 @@
</span><span class="cx"> 
</span><span class="cx">         do_action( &quot;delete_{$meta_type}_meta&quot;, $meta_ids, $object_id, $meta_key, $_meta_value );
</span><span class="cx"> 
</span><ins>+        // Old-style action.
</ins><span class="cx">         if ( 'post' == $meta_type )
</span><span class="cx">                 do_action( 'delete_postmeta', $meta_ids );
</span><span class="cx"> 
</span><span class="lines">@@ -243,6 +244,7 @@
</span><span class="cx"> 
</span><span class="cx">         do_action( &quot;deleted_{$meta_type}_meta&quot;, $meta_ids, $object_id, $meta_key, $_meta_value );
</span><span class="cx"> 
</span><ins>+        // Old-style action.
</ins><span class="cx">         if ( 'post' == $meta_type )
</span><span class="cx">                 do_action( 'deleted_postmeta', $meta_ids );
</span><span class="cx"> 
</span><span class="lines">@@ -485,8 +487,9 @@
</span><span class="cx"> 
</span><span class="cx">                 do_action( &quot;delete_{$meta_type}_meta&quot;, (array) $meta_id, $object_id, $meta-&gt;meta_key, $meta-&gt;meta_value );
</span><span class="cx"> 
</span><del>-                if ( 'post' == $meta_type )
-                        do_action( 'delete_postmeta', $meta_id );
</del><ins>+                // Old-style action.
+                if ( 'post' == $meta_type || 'comment' == $meta_type )
+                        do_action( &quot;delete_{$meta_type}meta&quot;, $meta_id );
</ins><span class="cx"> 
</span><span class="cx">                 // Run the query, will return true if deleted, false otherwise
</span><span class="cx">                 $result = (bool) $wpdb-&gt;delete( $table, array( $id_column =&gt; $meta_id ) );
</span><span class="lines">@@ -496,8 +499,9 @@
</span><span class="cx"> 
</span><span class="cx">                 do_action( &quot;deleted_{$meta_type}_meta&quot;, (array) $meta_id, $object_id, $meta-&gt;meta_key, $meta-&gt;meta_value );
</span><span class="cx"> 
</span><del>-                if ( 'post' == $meta_type )
-                        do_action( 'deleted_postmeta', $meta_id );
</del><ins>+                // Old-style action.
+                if ( 'post' == $meta_type || 'comment' == $meta_type )
+                        do_action( &quot;deleted_{$meta_type}meta&quot;, $meta_id );
</ins><span class="cx"> 
</span><span class="cx">                 return $result;
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/post.php (20434 => 20435)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/post.php        2012-04-11 02:58:23 UTC (rev 20434)
+++ trunk/wp-includes/post.php        2012-04-11 20:02:39 UTC (rev 20435)
</span><span class="lines">@@ -2034,20 +2034,12 @@
</span><span class="cx">         $wpdb-&gt;update( $wpdb-&gt;posts, $parent_data, $parent_where + array( 'post_type' =&gt; 'attachment' ) );
</span><span class="cx"> 
</span><span class="cx">         $comment_ids = $wpdb-&gt;get_col( $wpdb-&gt;prepare( &quot;SELECT comment_ID FROM $wpdb-&gt;comments WHERE comment_post_ID = %d&quot;, $postid ));
</span><del>-        if ( ! empty($comment_ids) ) {
-                do_action( 'delete_comment', $comment_ids );
-                foreach ( $comment_ids as $comment_id )
-                        wp_delete_comment( $comment_id, true );
-                do_action( 'deleted_comment', $comment_ids );
-        }
</del><ins>+        foreach ( $comment_ids as $comment_id )
+                wp_delete_comment( $comment_id, true );
</ins><span class="cx"> 
</span><span class="cx">         $post_meta_ids = $wpdb-&gt;get_col( $wpdb-&gt;prepare( &quot;SELECT meta_id FROM $wpdb-&gt;postmeta WHERE post_id = %d &quot;, $postid ));
</span><del>-        if ( !empty($post_meta_ids) ) {
-                do_action( 'delete_postmeta', $post_meta_ids );
-                $in_post_meta_ids = &quot;'&quot; . implode(&quot;', '&quot;, $post_meta_ids) . &quot;'&quot;;
-                $wpdb-&gt;query( &quot;DELETE FROM $wpdb-&gt;postmeta WHERE meta_id IN($in_post_meta_ids)&quot; );
-                do_action( 'deleted_postmeta', $post_meta_ids );
-        }
</del><ins>+        foreach ( $post_meta_ids as $mid )
+                delete_metadata_by_mid( 'post', $mid );
</ins><span class="cx"> 
</span><span class="cx">         do_action( 'delete_post', $postid );
</span><span class="cx">         $wpdb-&gt;delete( $wpdb-&gt;posts, array( 'ID' =&gt; $postid ) );
</span><span class="lines">@@ -3800,23 +3792,15 @@
</span><span class="cx">         wp_delete_object_term_relationships($post_id, array('category', 'post_tag'));
</span><span class="cx">         wp_delete_object_term_relationships($post_id, get_object_taxonomies($post-&gt;post_type));
</span><span class="cx"> 
</span><del>-        $wpdb-&gt;delete( $wpdb-&gt;postmeta, array( 'meta_key' =&gt; '_thumbnail_id' , 'meta_value' =&gt; $post_id ) );
</del><ins>+        delete_metadata( 'post', null, '_thumbnail_id', $post_id, true ); // delete all for any posts.
</ins><span class="cx"> 
</span><span class="cx">         $comment_ids = $wpdb-&gt;get_col( $wpdb-&gt;prepare( &quot;SELECT comment_ID FROM $wpdb-&gt;comments WHERE comment_post_ID = %d&quot;, $post_id ));
</span><del>-        if ( ! empty( $comment_ids ) ) {
-                do_action( 'delete_comment', $comment_ids );
-                foreach ( $comment_ids as $comment_id )
-                        wp_delete_comment( $comment_id, true );
-                do_action( 'deleted_comment', $comment_ids );
-        }
</del><ins>+        foreach ( $comment_ids as $comment_id )
+                wp_delete_comment( $comment_id, true );
</ins><span class="cx"> 
</span><span class="cx">         $post_meta_ids = $wpdb-&gt;get_col( $wpdb-&gt;prepare( &quot;SELECT meta_id FROM $wpdb-&gt;postmeta WHERE post_id = %d &quot;, $post_id ));
</span><del>-        if ( !empty($post_meta_ids) ) {
-                do_action( 'delete_postmeta', $post_meta_ids );
-                $in_post_meta_ids = &quot;'&quot; . implode(&quot;', '&quot;, $post_meta_ids) . &quot;'&quot;;
-                $wpdb-&gt;query( &quot;DELETE FROM $wpdb-&gt;postmeta WHERE meta_id IN($in_post_meta_ids)&quot; );
-                do_action( 'deleted_postmeta', $post_meta_ids );
-        }
</del><ins>+        foreach ( $post_meta_ids as $mid )
+                delete_metadata_by_mid( 'post', $mid );
</ins><span class="cx"> 
</span><span class="cx">         do_action( 'delete_post', $post_id );
</span><span class="cx">         $wpdb-&gt;delete( $wpdb-&gt;posts, array( 'ID' =&gt; $post_id ) );
</span><span class="lines">@@ -4563,13 +4547,9 @@
</span><span class="cx">         if ( defined('WP_IMPORTING') )
</span><span class="cx">                 return;
</span><span class="cx"> 
</span><del>-        $data = array( 'post_id' =&gt; $post_id, 'meta_value' =&gt; '1' );
-        if ( get_option('default_pingback_flag') ) {
-                $wpdb-&gt;insert( $wpdb-&gt;postmeta, $data + array( 'meta_key' =&gt; '_pingme' ) );
-                do_action( 'added_postmeta', $wpdb-&gt;insert_id, $post_id, '_pingme', 1 );
-        }
-        $wpdb-&gt;insert( $wpdb-&gt;postmeta, $data + array( 'meta_key' =&gt; '_encloseme' ) );
-        do_action( 'added_postmeta', $wpdb-&gt;insert_id, $post_id, '_encloseme', 1 );
</del><ins>+        if ( get_option('default_pingback_flag') )
+                add_post_meta( $post_id, '_pingme', '1' );
+        add_post_meta( $post_id, '_encloseme', '1' );
</ins><span class="cx"> 
</span><span class="cx">         wp_schedule_single_event(time(), 'do_pings');
</span><span class="cx"> }
</span></span></pre>
</div>
</div>

</body>
</html>