<!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 { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { 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>[13148] trunk/wp-includes/functions.php:
  Add hooks across options and transients APIs.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/13148">13148</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2010-02-14 10:39:46 +0000 (Sun, 14 Feb 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add hooks across options and transients APIs. Focuses on site transients/options functions. Attempts to standardize where possible. See <a href="http://trac.wordpress.org/ticket/10788">#10788</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesfunctionsphp">trunk/wp-includes/functions.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/functions.php (13147 => 13148)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/functions.php        2010-02-14 10:02:38 UTC (rev 13147)
+++ trunk/wp-includes/functions.php        2010-02-14 10:39:46 UTC (rev 13148)
</span><span class="lines">@@ -345,7 +345,8 @@
</span><span class="cx">                         if ( defined( 'WP_INSTALLING' ) )
</span><span class="cx">                                 $wpdb-&gt;suppress_errors( $suppress );
</span><span class="cx"> 
</span><del>-                        if ( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
</del><ins>+                        // Has to be get_row instead of get_var because of funkiness with 0, false, null values
+                        if ( is_object( $row ) ) {
</ins><span class="cx">                                 $value = $row-&gt;option_value;
</span><span class="cx">                                 wp_cache_add( $option, $value, 'options' );
</span><span class="cx">                         } else { // option does not exist, so we must cache its non-existence
</span><span class="lines">@@ -479,6 +480,7 @@
</span><span class="cx">  *
</span><span class="cx">  * @uses apply_filters() Calls 'pre_update_option_$option' hook to allow overwriting the
</span><span class="cx">  *         option value to be stored.
</span><ins>+ * @uses do_action() Calls 'update_option' hook before updating the option.
</ins><span class="cx">  * @uses do_action() Calls 'update_option_$option' and 'updated_option' hooks on success.
</span><span class="cx">  *
</span><span class="cx">  * @param string $option Option name. Expected to not be SQL-escaped
</span><span class="lines">@@ -511,7 +513,7 @@
</span><span class="cx">         $_newvalue = $newvalue;
</span><span class="cx">         $newvalue = maybe_serialize( $newvalue );
</span><span class="cx"> 
</span><del>-        do_action( 'update_option', $option, $oldvalue, $newvalue );
</del><ins>+        do_action( 'update_option', $option, $oldvalue, $newvalue ); // $newvalue may be serialized.
</ins><span class="cx">         if ( ! defined( 'WP_INSTALLING' ) ) {
</span><span class="cx">                 $alloptions = wp_load_alloptions();
</span><span class="cx">                 if ( isset( $alloptions[$option] ) ) {
</span><span class="lines">@@ -550,6 +552,7 @@
</span><span class="cx">  * @since 1.0.0
</span><span class="cx">  * @link http://alex.vort-x.net/blog/ Thanks Alex Stapleton
</span><span class="cx">  *
</span><ins>+ * @uses do_action() Calls 'add_option' hook before adding the option.
</ins><span class="cx">  * @uses do_action() Calls 'add_option_$option' and 'added_option' hooks on success.
</span><span class="cx">  *
</span><span class="cx">  * @param string $option Name of option to add. Expects to NOT be SQL escaped.
</span><span class="lines">@@ -608,14 +611,14 @@
</span><span class="cx">  * Removes option by name. Prevents removal of protected WordPress options.
</span><span class="cx">  *
</span><span class="cx">  * @package WordPress
</span><del>- * @subpackage Option
</del><ins>+ * @subpackage Option 
</ins><span class="cx">  * @since 1.2.0
</span><span class="cx">  *
</span><span class="cx">  * @uses do_action() Calls 'delete_option' hook before option is deleted.
</span><del>- * @uses do_action() Calls 'deleted_option' hook on success.
</del><ins>+ * @uses do_action() Calls 'deleted_option' and 'delete_option_$option' hooks on success.
</ins><span class="cx">  *
</span><span class="cx">  * @param string $option Name of option to remove.
</span><del>- * @return bool True, if succeed. False, if failure.
</del><ins>+ * @return bool True, if option is successfully deleted. False on failure.
</ins><span class="cx">  */
</span><span class="cx"> function delete_option( $option ) {
</span><span class="cx">         global $wpdb;
</span><span class="lines">@@ -642,6 +645,7 @@
</span><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">         if ( $result ) {
</span><ins>+                do_action( &quot;delete_option_$option&quot;, $option );
</ins><span class="cx">                 do_action( 'deleted_option', $option );
</span><span class="cx">                 return true;
</span><span class="cx">         }
</span><span class="lines">@@ -655,20 +659,27 @@
</span><span class="cx">  * @package WordPress
</span><span class="cx">  * @subpackage Transient
</span><span class="cx">  *
</span><ins>+ * @uses do_action() Calls 'delete_transient_$transient' hook before transient is deleted.
+ * @uses do_action() Calls 'deleted_transient' hook on success.
+ *
</ins><span class="cx">  * @param string $transient Transient name. Expected to not be SQL-escaped
</span><span class="cx">  * @return bool true if successful, false otherwise
</span><span class="cx">  */
</span><span class="cx"> function delete_transient( $transient ) {
</span><span class="cx">         global $_wp_using_ext_object_cache;
</span><span class="cx"> 
</span><del>-    do_action( 'delete_transient_' . $transient );
</del><ins>+    do_action( 'delete_transient_' . $transient, $transient );
</ins><span class="cx"> 
</span><span class="cx">         if ( $_wp_using_ext_object_cache ) {
</span><del>-                return wp_cache_delete( $transient, 'transient' );
</del><ins>+                $result = wp_cache_delete( $transient, 'transient' );
</ins><span class="cx">         } else {
</span><del>-                $transient = '_transient_' . esc_sql( $transient );
-                return delete_option( $transient );
</del><ins>+                $option = '_transient_' . esc_sql( $transient );
+                $result = delete_option( $option );
</ins><span class="cx">         }
</span><ins>+
+        if ( $result )
+                do_action( 'deleted_transient', $transient );
+        return $result;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -733,6 +744,7 @@
</span><span class="cx">  *
</span><span class="cx">  * @uses apply_filters() Calls 'pre_set_transient_$transient' hook to allow overwriting the
</span><span class="cx">  *         transient value to be stored.
</span><ins>+ * @uses do_action() Calls 'set_transient_$transient' and 'setted_transient' hooks on success.
</ins><span class="cx">  *
</span><span class="cx">  * @param string $transient Transient name. Expected to not be SQL-escaped
</span><span class="cx">  * @param mixed $value Transient value.
</span><span class="lines">@@ -745,7 +757,7 @@
</span><span class="cx">     $value = apply_filters( 'pre_set_transient_' . $transient, $value );
</span><span class="cx"> 
</span><span class="cx">         if ( $_wp_using_ext_object_cache ) {
</span><del>-                return wp_cache_set( $transient, $value, 'transient', $expiration );
</del><ins>+                $result = wp_cache_set( $transient, $value, 'transient', $expiration );
</ins><span class="cx">         } else {
</span><span class="cx">                 $transient_timeout = '_transient_timeout_' . $transient;
</span><span class="cx">                 $transient = '_transient_' . $transient;
</span><span class="lines">@@ -754,15 +766,20 @@
</span><span class="cx">                         $autoload = 'yes';
</span><span class="cx">                         if ( $expiration ) {
</span><span class="cx">                                 $autoload = 'no';
</span><del>-                                add_option($transient_timeout, time() + $expiration, '', 'no');
</del><ins>+                                add_option( $transient_timeout, time() + $expiration, '', 'no' );
</ins><span class="cx">                         }
</span><del>-                        return add_option($transient, $value, '', $autoload);
</del><ins>+                        $result = add_option( $transient, $value, '', $autoload );
</ins><span class="cx">                 } else {
</span><span class="cx">                         if ( $expiration )
</span><span class="cx">                                 update_option( $transient_timeout, time() + $expiration );
</span><del>-                        return update_option($transient, $value);
</del><ins>+                        $result = update_option( $transient, $value );
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><ins>+        if ( $result ) {
+                do_action( 'set_transient_' . $transient );
+                do_action( 'setted_transient', $transient );
+        }
+        return $result;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -3466,9 +3483,12 @@
</span><span class="cx">                 $result = $wpdb-&gt;query( $wpdb-&gt;prepare( &quot;DELETE FROM {$wpdb-&gt;sitemeta} WHERE meta_key = %s AND site_id = %d&quot;, $option, $wpdb-&gt;siteid ) );
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        do_action( &quot;delete_site_option_{$option}&quot;, $option );
-        do_action( &quot;delete_site_option&quot;, $option );
-        return $result;
</del><ins>+        if ( $result ) {
+                do_action( &quot;delete_site_option_{$option}&quot;, $option );
+                do_action( &quot;delete_site_option&quot;, $option );
+                return true;
+        }
+        return false;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -3525,18 +3545,24 @@
</span><span class="cx">  * @package WordPress
</span><span class="cx">  * @subpackage Transient
</span><span class="cx">  *
</span><ins>+ * @uses do_action() Calls 'delete_site_transient_$transient' hook before transient is deleted.
+ * @uses do_action() Calls 'deleted_site_transient' hook on success.
+ *
</ins><span class="cx">  * @param string $transient Transient name. Expected to not be SQL-escaped
</span><span class="cx">  * @return bool True if successful, false otherwise
</span><span class="cx">  */
</span><span class="cx"> function delete_site_transient( $transient ) {
</span><span class="cx">         global $_wp_using_ext_object_cache;
</span><span class="cx"> 
</span><ins>+        do_action( 'delete_site_transient_' . $transient, $transient );
</ins><span class="cx">         if ( $_wp_using_ext_object_cache ) {
</span><span class="cx">                 $result = wp_cache_delete( $transient, 'site-transient' );
</span><span class="cx">         } else {
</span><del>-                $transient = '_site_transient_' . esc_sql( $transient );
-                $result = delete_site_option( $transient );
</del><ins>+                $option = '_site_transient_' . esc_sql( $transient );
+                $result = delete_site_option( $option );
</ins><span class="cx">         }
</span><ins>+        if ( $result )
+                do_action( 'deleted_site_transient', $transient );
</ins><span class="cx">         return $result;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -3600,6 +3626,10 @@
</span><span class="cx">  * @package WordPress
</span><span class="cx">  * @subpackage Transient
</span><span class="cx">  *
</span><ins>+ * @uses apply_filters() Calls 'pre_set_site_transient_$transient' hook to allow overwriting the
+ *         transient value to be stored.
+ * @uses do_action() Calls 'set_site_transient_$transient' and 'setted_site_transient' hooks on success.
+ *
</ins><span class="cx">  * @param string $transient Transient name. Expected to not be SQL-escaped
</span><span class="cx">  * @param mixed $value Transient value.
</span><span class="cx">  * @param int $expiration Time until expiration in seconds, default 0
</span><span class="lines">@@ -3608,6 +3638,8 @@
</span><span class="cx"> function set_site_transient( $transient, $value, $expiration = 0 ) {
</span><span class="cx">         global $_wp_using_ext_object_cache;
</span><span class="cx"> 
</span><ins>+    $value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
+
</ins><span class="cx">         if ( $_wp_using_ext_object_cache ) {
</span><span class="cx">                 $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
</span><span class="cx">         } else {
</span><span class="lines">@@ -3624,6 +3656,10 @@
</span><span class="cx">                         $result = update_site_option( $transient, $value );
</span><span class="cx">                 }
</span><span class="cx">         }
</span><ins>+        if ( $result ) {
+                do_action( 'set_site_transient_' . $transient );
+                do_action( 'setted_site_transient', $transient );
+        }
</ins><span class="cx">         return $result;
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>