<!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>[13142] trunk/wp-includes/functions.php: More cleanups of the options/
 transients APIs.</title>
</head>
<body>

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

<h3>Log Message</h3>
<pre>More cleanups of the options/transients APIs. More inline documentation, better return values (always true on success, false on failure). Only call actions that are after wpdb delete/update operations if the operation was successful. 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 (13141 => 13142)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/functions.php        2010-02-14 07:13:34 UTC (rev 13141)
+++ trunk/wp-includes/functions.php        2010-02-14 07:21:15 UTC (rev 13142)
</span><span class="lines">@@ -289,31 +289,24 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><del>- * Retrieve option value based on setting name.
</del><ins>+ * Retrieve option value based on name of option.
</ins><span class="cx">  *
</span><span class="cx">  * If the option does not exist or does not have a value, then the return value
</span><span class="cx">  * will be false. This is useful to check whether you need to install an option
</span><span class="cx">  * and is commonly used during installation of plugin options and to test
</span><span class="cx">  * whether upgrading is required.
</span><span class="cx">  *
</span><del>- * You can &quot;short-circuit&quot; the retrieval of the option from the database for
- * your plugin or core options that aren't protected. You can do so by hooking
- * into the 'pre_option_$option' with the $option being replaced by the option
- * name. You should not try to override special options, but you will not be
- * prevented from doing so.
</del><ins>+ * If the option was serialized then it will be unserialized when it is returned.
</ins><span class="cx">  *
</span><del>- * There is a second filter called 'option_$option' with the $option being
- * replaced with the option name. This gives the value as the only parameter.
- *
- * If the option was serialized, when the option was added and, or updated, then
- * it will be unserialized, when it is returned.
- *
</del><span class="cx">  * @since 1.5.0
</span><span class="cx">  * @package WordPress
</span><span class="cx">  * @subpackage Option
</span><del>- * @uses apply_filters() Calls 'pre_option_$option' false to allow
- *                overwriting the option value in a plugin.
- * @uses apply_filters() Calls 'option_$option' with the option value.
</del><ins>+ * @uses apply_filters() Calls 'pre_option_$option' before checking the option.
+ *         Any value other than false will &quot;short-circuit&quot; the retrieval of the option
+ *        and return the returned value. You should not try to override special options,
+ *         but you will not be prevented from doing so.
+ * @uses apply_filters() Calls 'option_$option', after checking the option, with
+ *         the option value.
</ins><span class="cx">  *
</span><span class="cx">  * @param string $option Name of option to retrieve. Should already be SQL-escaped
</span><span class="cx">  * @return mixed Value set for the option.
</span><span class="lines">@@ -388,7 +381,7 @@
</span><span class="cx"> function wp_protect_special_option( $option ) {
</span><span class="cx">         $protected = array( 'alloptions', 'notoptions' );
</span><span class="cx">         if ( in_array( $option, $protected ) )
</span><del>-                die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
</del><ins>+                wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -402,7 +395,7 @@
</span><span class="cx">  * @param string $option Option name.
</span><span class="cx">  */
</span><span class="cx"> function form_option( $option ) {
</span><del>-        echo esc_attr(get_option( $option ) );
</del><ins>+        echo esc_attr( get_option( $option ) );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -472,28 +465,22 @@
</span><span class="cx"> /**
</span><span class="cx">  * Update the value of an option that was already added.
</span><span class="cx">  *
</span><del>- * You do not need to serialize values, if the value needs to be serialize, then
</del><ins>+ * You do not need to serialize values. If the value needs to be serialized, then
</ins><span class="cx">  * it will be serialized before it is inserted into the database. Remember,
</span><span class="cx">  * resources can not be serialized or added as an option.
</span><span class="cx">  *
</span><span class="cx">  * If the option does not exist, then the option will be added with the option
</span><span class="cx">  * value, but you will not be able to set whether it is autoloaded. If you want
</span><del>- * to set whether an option autoloaded, then you need to use the add_option().
</del><ins>+ * to set whether an option is autoloaded, then you need to use the add_option().
</ins><span class="cx">  *
</span><del>- * Before the option is updated, then the filter named
- * 'pre_update_option_$option', with the $option as the $option
- * parameter value, will be called. The hook should accept two parameters, the
- * first is the new value and the second is the old value.  Whatever is
- * returned will be used as the new value.
- *
- * After the value has been updated the action named 'update_option_$option'
- * will be called.  This action receives two parameters the first being the old
- * value and the second the new value.
- *
</del><span class="cx">  * @since 1.0.0
</span><span class="cx">  * @package WordPress
</span><span class="cx">  * @subpackage Option
</span><span class="cx">  *
</span><ins>+ * @uses apply_filters() Calls 'pre_update_option_$option' hook to allow overwriting the
+ *         option value to be stored.
+ * @uses do_action() Calls 'update_option_$option' and 'updated_option' hooks on success.
+ *
</ins><span class="cx">  * @param string $option Option name. Expected to not be SQL-escaped
</span><span class="cx">  * @param mixed $newvalue Option value.
</span><span class="cx">  * @return bool False if value was not updated and true if value was updated.
</span><span class="lines">@@ -503,11 +490,9 @@
</span><span class="cx"> 
</span><span class="cx">         wp_protect_special_option( $option );
</span><span class="cx"> 
</span><del>-        $safe_option_name = esc_sql( $option );
</del><ins>+        $safe_option = esc_sql( $option );
</ins><span class="cx">         $newvalue = sanitize_option( $option, $newvalue );
</span><del>-
-        $oldvalue = get_option( $safe_option_name );
-
</del><ins>+        $oldvalue = get_option( $safe_option );
</ins><span class="cx">         $newvalue = apply_filters( 'pre_update_option_' . $option, $newvalue, $oldvalue );
</span><span class="cx"> 
</span><span class="cx">         // If the new and old values are the same, no need to update.
</span><span class="lines">@@ -550,25 +535,23 @@
</span><span class="cx"> /**
</span><span class="cx">  * Add a new option.
</span><span class="cx">  *
</span><del>- * You do not need to serialize values, if the value needs to be serialize, then
</del><ins>+ * You do not need to serialize values. If the value needs to be serialized, then
</ins><span class="cx">  * it will be serialized before it is inserted into the database. Remember,
</span><span class="cx">  * resources can not be serialized or added as an option.
</span><span class="cx">  *
</span><span class="cx">  * You can create options without values and then add values later. Does not
</span><span class="cx">  * check whether the option has already been added, but does check that you
</span><span class="cx">  * aren't adding a protected WordPress option. Care should be taken to not name
</span><del>- * options, the same as the ones which are protected and to not add options
</del><ins>+ * options the same as the ones which are protected and to not add options
</ins><span class="cx">  * that were already added.
</span><span class="cx">  *
</span><del>- * The filter named 'add_option_$option', with the $optionname being
- * replaced with the option's name, will be called. The hook should accept two
- * parameters, the first is the option name, and the second is the value.
- *
</del><span class="cx">  * @package WordPress
</span><span class="cx">  * @subpackage Option
</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_$option' and 'added_option' hooks on success.
+ *
</ins><span class="cx">  * @param string $option Name of option to add. Expects to NOT be SQL escaped.
</span><span class="cx">  * @param mixed $value Optional. Option value, can be anything.
</span><span class="cx">  * @param mixed $deprecated Optional. Description. Not used anymore.
</span><span class="lines">@@ -582,13 +565,13 @@
</span><span class="cx">         global $wpdb;
</span><span class="cx"> 
</span><span class="cx">         wp_protect_special_option( $option );
</span><del>-        $safe_name = esc_sql( $option );
</del><ins>+        $safe_option = esc_sql( $option );
</ins><span class="cx">         $value = sanitize_option( $option, $value );
</span><span class="cx"> 
</span><span class="cx">         // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
</span><span class="cx">         $notoptions = wp_cache_get( 'notoptions', 'options' );
</span><span class="cx">         if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
</span><del>-                if ( false !== get_option( $safe_name ) )
</del><ins>+                if ( false !== get_option( $safe_option ) )
</ins><span class="cx">                         return;
</span><span class="cx"> 
</span><span class="cx">         $value = maybe_serialize( $value );
</span><span class="lines">@@ -613,10 +596,12 @@
</span><span class="cx"> 
</span><span class="cx">         $result = $wpdb-&gt;query( $wpdb-&gt;prepare( &quot;INSERT INTO `$wpdb-&gt;options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)&quot;, $option, $value, $autoload ) );
</span><span class="cx"> 
</span><del>-        do_action( &quot;add_option_{$option}&quot;, $option, $value );
-        do_action( 'added_option', $option, $value );
-
-        return $result;
</del><ins>+        if ( $result ) {
+                do_action( &quot;add_option_{$option}&quot;, $option, $value );
+                do_action( 'added_option', $option, $value );
+                return true;
+        }
+        return false;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -626,6 +611,9 @@
</span><span class="cx">  * @subpackage Option
</span><span class="cx">  * @since 1.2.0
</span><span class="cx">  *
</span><ins>+ * @uses do_action() Calls 'delete_option' hook before option is deleted.
+ * @uses do_action() Calls 'deleted_option' hook on success.
+ *
</ins><span class="cx">  * @param string $option Name of option to remove.
</span><span class="cx">  * @return bool True, if succeed. False, if failure.
</span><span class="cx">  */
</span><span class="lines">@@ -653,8 +641,11 @@
</span><span class="cx">                         wp_cache_delete( $option, 'options' );
</span><span class="cx">                 }
</span><span class="cx">         }
</span><del>-        do_action( 'deleted_option', $option );
-        return true;
</del><ins>+        if ( $result ) {
+                do_action( 'deleted_option', $option );
+                return true;
+        }
+        return false;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -686,6 +677,12 @@
</span><span class="cx">  * If the transient does not exist or does not have a value, then the return value
</span><span class="cx">  * will be false.
</span><span class="cx">  *
</span><ins>+ * @uses apply_filters() Calls 'pre_transient_$transient' hook before checking the transient.
+ *         Any value other than false will &quot;short-circuit&quot; the retrieval of the transient
+ *        and return the returned value.
+ * @uses apply_filters() Calls 'transient_$option' hook, after checking the transient, with
+ *         the transient value.
+ *
</ins><span class="cx">  * @since 2.8.0
</span><span class="cx">  * @package WordPress
</span><span class="cx">  * @subpackage Transient
</span><span class="lines">@@ -734,6 +731,9 @@
</span><span class="cx">  * @package WordPress
</span><span class="cx">  * @subpackage Transient
</span><span class="cx">  *
</span><ins>+ * @uses apply_filters() Calls 'pre_set_transient_$transient' hook to allow overwriting the
+ *         transient value to be stored.
+ *
</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">@@ -3348,6 +3348,12 @@
</span><span class="cx">  * @subpackage Option
</span><span class="cx">  * @since 2.8.0
</span><span class="cx">  *
</span><ins>+ * @uses apply_filters() Calls 'pre_site_option_$option' before checking the option.
+ *         Any value other than false will &quot;short-circuit&quot; the retrieval of the option
+ *        and return the returned value.
+ * @uses apply_filters() Calls 'site_option_$option', after checking the  option, with
+ *         the option value.
+ *
</ins><span class="cx">  * @param string $option Name of option to retrieve. Should already be SQL-escaped
</span><span class="cx">  * @param mixed $default Optional value to return if option doesn't exist. Default false.
</span><span class="cx">  * @param bool $use_cache Whether to use cache. Multisite only. Default true.
</span><span class="lines">@@ -3391,6 +3397,10 @@
</span><span class="cx">  * @subpackage Option
</span><span class="cx">  * @since 2.8.0
</span><span class="cx">  *
</span><ins>+ * @uses apply_filters() Calls 'pre_add_site_option_$option' hook to allow overwriting the
+ *         option value to be stored.
+ * @uses do_action() Calls 'add_site_option_$option' and 'add_site_option' hooks on success.
+ *
</ins><span class="cx">  * @param string $option Name of option to add. Expects to not be SQL escaped.
</span><span class="cx">  * @param mixed $value Optional. Option value, can be anything.
</span><span class="cx">  * @return bool False if option was not added and true if option was added.
</span><span class="lines">@@ -3430,6 +3440,10 @@
</span><span class="cx">  * @subpackage Option
</span><span class="cx">  * @since 2.8.0
</span><span class="cx">  *
</span><ins>+ * @uses do_action() Calls 'pre_delete_site_option_$option' hook before option is deleted.
+ * @uses do_action() Calls 'delete_site_option' and 'delete_site_option_$option'
+ *         hooks on success.
+ *
</ins><span class="cx">  * @param string $option Name of option to remove. Expected to be SQL-escaped.
</span><span class="cx">  * @return bool True, if succeed. False, if failure.
</span><span class="cx">  */
</span><span class="lines">@@ -3465,6 +3479,10 @@
</span><span class="cx">  * @package WordPress
</span><span class="cx">  * @subpackage Option
</span><span class="cx">  *
</span><ins>+ * @uses apply_filters() Calls 'pre_update_site_option_$option' hook to allow overwriting the
+ *         option value to be stored.
+ * @uses do_action() Calls 'update_site_option_$option' and 'update_site_option' hooks on success.
+ *
</ins><span class="cx">  * @param string $option Name of option. Expected to not be SQL-escaped
</span><span class="cx">  * @param mixed $value Option value.
</span><span class="cx">  * @return bool False if value was not updated and true if value was updated.
</span><span class="lines">@@ -3492,9 +3510,12 @@
</span><span class="cx">                 $result = $wpdb-&gt;update( $wpdb-&gt;sitemeta, array( 'meta_value' =&gt; $value ), array( 'site_id' =&gt; $wpdb-&gt;siteid, 'meta_key' =&gt; $option ) );
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        do_action( &quot;update_site_option_{$option}&quot;, $option, $value );
-        do_action( &quot;update_site_option&quot;, $option, $value );
-        return $result;
</del><ins>+        if ( $result ) {
+                do_action( &quot;update_site_option_{$option}&quot;, $option, $value );
+                do_action( &quot;update_site_option&quot;, $option, $value );
+                return true;
+        }
+        return false;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -3530,6 +3551,12 @@
</span><span class="cx">  * @package WordPress
</span><span class="cx">  * @subpackage Transient
</span><span class="cx">  *
</span><ins>+ * @uses apply_filters() Calls 'pre_site_transient_$transient' hook before checking the transient.
+ *         Any value other than false will &quot;short-circuit&quot; the retrieval of the transient
+ *        and return the returned value.
+ * @uses apply_filters() Calls 'site_transient_$option' hook, after checking the transient, with
+ *         the transient value.
+ *
</ins><span class="cx">  * @param string $transient Transient name. Expected to not be SQL-escaped
</span><span class="cx">  * @return mixed Value of transient
</span><span class="cx">  */
</span></span></pre>
</div>
</div>

</body>
</html>