<!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>[18500] trunk: Turn delete_meta() , get_post_meta_by_id(), update_meta(), delete_post_meta_by_key() into wrappers around the metadata API.</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/18500">18500</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2011-08-03 16:48:37 +0000 (Wed, 03 Aug 2011)</dd>
</dl>

<h3>Log Message</h3>
<pre>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 <a href="http://core.trac.wordpress.org/ticket/18196">#18196</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminincludespostphp">trunk/wp-admin/includes/post.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 (18499 => 18500)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/post.php        2011-08-03 10:36:18 UTC (rev 18499)
+++ trunk/wp-admin/includes/post.php        2011-08-03 16:48:37 UTC (rev 18500)
</span><span class="lines">@@ -701,17 +701,7 @@
</span><span class="cx">  * @return unknown
</span><span class="cx">  */
</span><span class="cx"> function delete_meta( $mid ) {
</span><del>-        global $wpdb;
-        $mid = (int) $mid;
-
-        $post_id = $wpdb-&gt;get_var( $wpdb-&gt;prepare(&quot;SELECT post_id FROM $wpdb-&gt;postmeta WHERE meta_id = %d&quot;, $mid) );
-
-        do_action( 'delete_postmeta', $mid );
-        wp_cache_delete($post_id, 'post_meta');
-        $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;
</del><ins>+        return delete_metadata_by_mid( 'post' , $mid );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -742,15 +732,7 @@
</span><span class="cx">  * @return unknown
</span><span class="cx">  */
</span><span class="cx"> function get_post_meta_by_id( $mid ) {
</span><del>-        global $wpdb;
-        $mid = (int) $mid;
-
-        $meta = $wpdb-&gt;get_row( $wpdb-&gt;prepare(&quot;SELECT * FROM $wpdb-&gt;postmeta WHERE meta_id = %d&quot;, $mid) );
-        if ( empty($meta) )
-                return false;
-        if ( is_serialized_string( $meta-&gt;meta_value ) )
-                $meta-&gt;meta_value = maybe_unserialize( $meta-&gt;meta_value );
-        return $meta;
</del><ins>+        return get_metadata_by_mid( 'post', $mid );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -782,27 +764,10 @@
</span><span class="cx">  * @return unknown
</span><span class="cx">  */
</span><span class="cx"> function update_meta( $meta_id, $meta_key, $meta_value ) {
</span><del>-        global $wpdb;
</del><ins>+        $meta_key = stripslashes( $meta_key );
+        $meta_value = stripslashes_deep( $meta_value );
</ins><span class="cx"> 
</span><del>-        $meta_key = stripslashes($meta_key);
-
-        if ( '' === trim( $meta_value ) )
-                return false;
-
-        $post_id = $wpdb-&gt;get_var( $wpdb-&gt;prepare(&quot;SELECT post_id FROM $wpdb-&gt;postmeta WHERE meta_id = %d&quot;, $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-&gt;update( $wpdb-&gt;postmeta, $data, $where );
-        wp_cache_delete($post_id, 'post_meta');
-        do_action( 'updated_postmeta', $meta_id, $post_id, $meta_key, $meta_value );
-
-        return $rval;
</del><ins>+        return update_metadata_by_mid( 'post', $meta_id, $meta_value, $meta_key );
</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 (18499 => 18500)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/meta.php        2011-08-03 10:36:18 UTC (rev 18499)
+++ trunk/wp-includes/meta.php        2011-08-03 16:48:37 UTC (rev 18500)
</span><span class="lines">@@ -149,6 +149,9 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         do_action( &quot;update_{$meta_type}_meta&quot;, $meta_id, $object_id, $meta_key, $_meta_value );
</span><ins>+        
+        if ( 'post' == $meta_type )
+                do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );        
</ins><span class="cx"> 
</span><span class="cx">         $wpdb-&gt;update( $table, $data, $where );
</span><span class="cx"> 
</span><span class="lines">@@ -158,6 +161,9 @@
</span><span class="cx">                 clean_user_cache($object_id);
</span><span class="cx"> 
</span><span class="cx">         do_action( &quot;updated_{$meta_type}_meta&quot;, $meta_id, $object_id, $meta_key, $_meta_value );
</span><ins>+        
+        if ( 'post' == $meta_type )
+                do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );                
</ins><span class="cx"> 
</span><span class="cx">         return true;
</span><span class="cx"> }
</span><span class="lines">@@ -204,7 +210,7 @@
</span><span class="cx"> 
</span><span class="cx">         $_meta_value = $meta_value;
</span><span class="cx">         $meta_value = maybe_serialize( $meta_value );
</span><del>-
</del><ins>+                
</ins><span class="cx">         $query = $wpdb-&gt;prepare( &quot;SELECT $id_column FROM $table WHERE meta_key = %s&quot;, $meta_key );
</span><span class="cx"> 
</span><span class="cx">         if ( !$delete_all )
</span><span class="lines">@@ -217,7 +223,13 @@
</span><span class="cx">         if ( !count( $meta_ids ) )
</span><span class="cx">                 return false;
</span><span class="cx"> 
</span><ins>+        if ( $delete_all )
+                $object_ids = $wpdb-&gt;get_col( $wpdb-&gt;prepare( &quot;SELECT $type_column FROM $table WHERE meta_key = %s&quot;, $meta_key ) );
+
</ins><span class="cx">         do_action( &quot;delete_{$meta_type}_meta&quot;, $meta_ids, $object_id, $meta_key, $_meta_value );
</span><ins>+        
+        if ( 'post' == $meta_type )
+                do_action( 'delete_postmeta', $meta_ids );
</ins><span class="cx"> 
</span><span class="cx">         $query = &quot;DELETE FROM $table WHERE $id_column IN( &quot; . implode( ',', $meta_ids ) . &quot; )&quot;;
</span><span class="cx"> 
</span><span class="lines">@@ -226,12 +238,22 @@
</span><span class="cx">         if ( !$count )
</span><span class="cx">                 return false;
</span><span class="cx"> 
</span><del>-        wp_cache_delete($object_id, $meta_type . '_meta');
</del><ins>+        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');
+        }
+
</ins><span class="cx">         // users cache stores usermeta that must be cleared.
</span><span class="cx">         if ( 'user' == $meta_type )
</span><span class="cx">                 clean_user_cache($object_id);
</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><ins>+        
+        if ( 'post' == $meta_type )
+                do_action( 'deleted_postmeta', $meta_ids );
</ins><span class="cx"> 
</span><span class="cx">         return true;
</span><span class="cx"> }
</span><span class="lines">@@ -381,6 +403,9 @@
</span><span class="cx">                 $where[$id_column] = $meta_id;
</span><span class="cx"> 
</span><span class="cx">                 do_action( &quot;update_{$meta_type}_meta&quot;, $meta_id, $object_id, $meta_key, $_meta_value );
</span><ins>+
+                if ( 'post' == $meta_type )
+                        do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
</ins><span class="cx">                 
</span><span class="cx">                 // Run the update query, all fields in $data are %s, $where is a %d.
</span><span class="cx">                 $result = (bool) $wpdb-&gt;update( $table, $data, $where, '%s', '%d' );
</span><span class="lines">@@ -394,6 +419,9 @@
</span><span class="cx"> 
</span><span class="cx">                 do_action( &quot;updated_{$meta_type}_meta&quot;, $meta_id, $object_id, $meta_key, $_meta_value );
</span><span class="cx"> 
</span><ins>+                if ( 'post' == $meta_type )
+                        do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
+
</ins><span class="cx">                 return $result;
</span><span class="cx">         }
</span><span class="cx">         
</span><span class="lines">@@ -436,18 +464,24 @@
</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><ins>+                if ( 'post' == $meta_type )
+                        do_action( 'delete_postmeta', $object_id );
+
</ins><span class="cx">                 // Run the query, will return true if deleted, false otherwise
</span><span class="cx">                 $result = (bool) $wpdb-&gt;query( $wpdb-&gt;prepare( &quot;DELETE FROM $table WHERE $id_column = %d LIMIT 1;&quot;, $meta_id ) );
</span><span class="cx"> 
</span><span class="cx">                 // Clear the caches.
</span><span class="cx">                 wp_cache_delete($object_id, $meta_type . '_meta');
</span><del>-                
</del><ins>+
</ins><span class="cx">                 // Users cache stores usermeta that must be cleared.
</span><span class="cx">                 if ( 'user' == $meta_type )
</span><span class="cx">                         clean_user_cache($object_id);
</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><ins>+                if ( 'post' == $meta_type )
+                        do_action( 'delete_postmeta', $object_id );
+
</ins><span class="cx">                 return $result;
</span><span class="cx"> 
</span><span class="cx">         }
</span></span></pre></div>
<a id="trunkwpincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/post.php (18499 => 18500)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/post.php        2011-08-03 10:36:18 UTC (rev 18499)
+++ trunk/wp-includes/post.php        2011-08-03 16:48:37 UTC (rev 18500)
</span><span class="lines">@@ -1497,22 +1497,7 @@
</span><span class="cx">  * @return bool Whether the post meta key was deleted from the database
</span><span class="cx">  */
</span><span class="cx"> function delete_post_meta_by_key($post_meta_key) {
</span><del>-        if ( !$post_meta_key )
-                return false;
-
-        global $wpdb;
-        $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));
-        if ( $post_ids ) {
-                $postmetaids = $wpdb-&gt;get_col( $wpdb-&gt;prepare( &quot;SELECT meta_id FROM $wpdb-&gt;postmeta WHERE meta_key = %s&quot;, $post_meta_key ) );
-                $in = implode( ',', array_fill(1, count($postmetaids), '%d'));
-                do_action( 'delete_postmeta', $postmetaids );
-                $wpdb-&gt;query( $wpdb-&gt;prepare(&quot;DELETE FROM $wpdb-&gt;postmeta WHERE meta_id IN($in)&quot;, $postmetaids ));
-                do_action( 'deleted_postmeta', $postmetaids );
-                foreach ( $post_ids as $post_id )
-                        wp_cache_delete($post_id, 'post_meta');
-                return true;
-        }
-        return false;
</del><ins>+        return delete_metadata( 'post', null, $post_meta_key, '', true );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span></span></pre>
</div>
</div>

</body>
</html>