<!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>[25289] trunk/src: Introduce `wp_using_ext_object_cache()` - mimic `wp_suspend_cache_invalidation()` and discourage direct access to `$_wp_using_ext_object_cache`, cleaning up importing of globals in functions and provides function to modify that global.</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 { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { 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/25289">25289</a></dd>
<dt>Author</dt> <dd>wonderboymusic</dd>
<dt>Date</dt> <dd>2013-09-06 18:09:24 +0000 (Fri, 06 Sep 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Introduce `wp_using_ext_object_cache()` - mimic `wp_suspend_cache_invalidation()` and discourage direct access to `$_wp_using_ext_object_cache`, cleaning up importing of globals in functions and provides function to modify that global.  Loads the packaged object cache when an external cache hasn't been loaded or doesn't contain `wp_cache_init()`.

Fixes <a href="http://core.trac.wordpress.org/ticket/21401">#21401</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpcronphp">trunk/src/wp-cron.php</a></li>
<li><a href="#trunksrcwpincludesloadphp">trunk/src/wp-includes/load.php</a></li>
<li><a href="#trunksrcwpincludesnavmenuphp">trunk/src/wp-includes/nav-menu.php</a></li>
<li><a href="#trunksrcwpincludesoptionphp">trunk/src/wp-includes/option.php</a></li>
<li><a href="#trunksrcwpincludesqueryphp">trunk/src/wp-includes/query.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpcronphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-cron.php (25288 => 25289)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-cron.php    2013-09-06 17:37:15 UTC (rev 25288)
+++ trunk/src/wp-cron.php       2013-09-06 18:09:24 UTC (rev 25289)
</span><span class="lines">@@ -28,10 +28,10 @@
</span><span class="cx"> 
</span><span class="cx"> // Uncached doing_cron transient fetch
</span><span class="cx"> function _get_cron_lock() {
</span><del>-       global $_wp_using_ext_object_cache, $wpdb;
</del><ins>+        global $wpdb;
</ins><span class="cx"> 
</span><span class="cx">  $value = 0;
</span><del>-       if ( $_wp_using_ext_object_cache ) {
</del><ins>+        if ( wp_using_ext_object_cache() ) {
</ins><span class="cx">           // Skip local cache and force refetch of doing_cron transient in case
</span><span class="cx">          // another processs updated the cache
</span><span class="cx">          $value = wp_cache_get( 'doing_cron', 'transient', true );
</span></span></pre></div>
<a id="trunksrcwpincludesloadphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/load.php (25288 => 25289)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/load.php   2013-09-06 17:37:15 UTC (rev 25288)
+++ trunk/src/wp-includes/load.php      2013-09-06 18:09:24 UTC (rev 25289)
</span><span class="lines">@@ -370,6 +370,24 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><ins>+ * Access/Modify private global variable $_wp_using_ext_object_cache
+ *
+ * Toggle $_wp_using_ext_object_cache on and off without directly touching global
+ *
+ * @since 3.7.0
+ *
+ * @param bool $using Whether external object cache is being used
+ * @return bool The current 'using' setting
+ */
+function wp_using_ext_object_cache( $using = null ) {
+       global $_wp_using_ext_object_cache;
+       $current_using = $_wp_using_ext_object_cache;
+       if ( null !== $using )
+               $_wp_using_ext_object_cache = $using;
+       return $current_using;
+}
+
+/**
</ins><span class="cx">  * Starts the WordPress object cache.
</span><span class="cx">  *
</span><span class="cx">  * If an object-cache.php file exists in the wp-content directory,
</span><span class="lines">@@ -379,31 +397,33 @@
</span><span class="cx">  * @since 3.0.0
</span><span class="cx">  */
</span><span class="cx"> function wp_start_object_cache() {
</span><del>-       global $_wp_using_ext_object_cache, $blog_id;
</del><ins>+        global $blog_id;
</ins><span class="cx"> 
</span><span class="cx">  $first_init = false;
</span><span class="cx">          if ( ! function_exists( 'wp_cache_init' ) ) {
</span><span class="cx">          if ( file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
</span><span class="cx">                  require_once ( WP_CONTENT_DIR . '/object-cache.php' );
</span><del>-                       $_wp_using_ext_object_cache = true;
-               } else {
-                       require_once ( ABSPATH . WPINC . '/cache.php' );
-                       $_wp_using_ext_object_cache = false;
</del><ins>+                        if ( function_exists( 'wp_cache_init' ) )
+                               wp_using_ext_object_cache( true );
</ins><span class="cx">           }
</span><ins>+
</ins><span class="cx">           $first_init = true;
</span><del>-       } else if ( !$_wp_using_ext_object_cache && file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
</del><ins>+        } else if ( ! wp_using_ext_object_cache() && file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
</ins><span class="cx">           // Sometimes advanced-cache.php can load object-cache.php before it is loaded here.
</span><span class="cx">          // This breaks the function_exists check above and can result in $_wp_using_ext_object_cache
</span><span class="cx">          // being set incorrectly. Double check if an external cache exists.
</span><del>-               $_wp_using_ext_object_cache = true;
</del><ins>+                wp_using_ext_object_cache( true );
</ins><span class="cx">   }
</span><span class="cx"> 
</span><ins>+       if ( ! wp_using_ext_object_cache() )
+               require_once ( ABSPATH . WPINC . '/cache.php' );
+
</ins><span class="cx">   // If cache supports reset, reset instead of init if already initialized.
</span><span class="cx">  // Reset signals to the cache that global IDs have changed and it may need to update keys
</span><span class="cx">  // and cleanup caches.
</span><span class="cx">  if ( ! $first_init && function_exists( 'wp_cache_switch_to_blog' ) )
</span><span class="cx">          wp_cache_switch_to_blog( $blog_id );
</span><del>-       else
</del><ins>+        elseif ( function_exists( 'wp_cache_init' ) )
</ins><span class="cx">           wp_cache_init();
</span><span class="cx"> 
</span><span class="cx">  if ( function_exists( 'wp_cache_add_global_groups' ) ) {
</span></span></pre></div>
<a id="trunksrcwpincludesnavmenuphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/nav-menu.php (25288 => 25289)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/nav-menu.php       2013-09-06 17:37:15 UTC (rev 25288)
+++ trunk/src/wp-includes/nav-menu.php  2013-09-06 18:09:24 UTC (rev 25289)
</span><span class="lines">@@ -476,8 +476,6 @@
</span><span class="cx">  * @return mixed $items array of menu items, else false.
</span><span class="cx">  */
</span><span class="cx"> function wp_get_nav_menu_items( $menu, $args = array() ) {
</span><del>-       global $_wp_using_ext_object_cache;
-
</del><span class="cx">   $menu = wp_get_nav_menu_object( $menu );
</span><span class="cx"> 
</span><span class="cx">  if ( ! $menu )
</span><span class="lines">@@ -504,7 +502,7 @@
</span><span class="cx">          return false;
</span><span class="cx"> 
</span><span class="cx">  // Get all posts and terms at once to prime the caches
</span><del>-       if ( empty( $fetched[$menu->term_id] ) || $_wp_using_ext_object_cache ) {
</del><ins>+        if ( empty( $fetched[$menu->term_id] ) || wp_using_ext_object_cache() ) {
</ins><span class="cx">           $fetched[$menu->term_id] = true;
</span><span class="cx">          $posts = array();
</span><span class="cx">          $terms = array();
</span></span></pre></div>
<a id="trunksrcwpincludesoptionphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/option.php (25288 => 25289)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/option.php 2013-09-06 17:37:15 UTC (rev 25288)
+++ trunk/src/wp-includes/option.php    2013-09-06 18:09:24 UTC (rev 25289)
</span><span class="lines">@@ -165,9 +165,9 @@
</span><span class="cx">  * @param int $site_id Optional site ID for which to query the options. Defaults to the current site.
</span><span class="cx">  */
</span><span class="cx"> function wp_load_core_site_options( $site_id = null ) {
</span><del>-       global $wpdb, $_wp_using_ext_object_cache;
</del><ins>+        global $wpdb;
</ins><span class="cx"> 
</span><del>-       if ( !is_multisite() || $_wp_using_ext_object_cache || defined( 'WP_INSTALLING' ) )
</del><ins>+        if ( !is_multisite() || wp_using_ext_object_cache() || defined( 'WP_INSTALLING' ) )
</ins><span class="cx">           return;
</span><span class="cx"> 
</span><span class="cx">  if ( empty($site_id) )
</span><span class="lines">@@ -404,11 +404,9 @@
</span><span class="cx">  * @return bool true if successful, false otherwise
</span><span class="cx">  */
</span><span class="cx"> function delete_transient( $transient ) {
</span><del>-       global $_wp_using_ext_object_cache;
-
</del><span class="cx">   do_action( 'delete_transient_' . $transient, $transient );
</span><span class="cx"> 
</span><del>-       if ( $_wp_using_ext_object_cache ) {
</del><ins>+        if ( wp_using_ext_object_cache() ) {
</ins><span class="cx">           $result = wp_cache_delete( $transient, 'transient' );
</span><span class="cx">  } else {
</span><span class="cx">          $option_timeout = '_transient_timeout_' . $transient;
</span><span class="lines">@@ -443,13 +441,11 @@
</span><span class="cx">  * @return mixed Value of transient
</span><span class="cx">  */
</span><span class="cx"> function get_transient( $transient ) {
</span><del>-       global $_wp_using_ext_object_cache;
-
</del><span class="cx">   $pre = apply_filters( 'pre_transient_' . $transient, false );
</span><span class="cx">  if ( false !== $pre )
</span><span class="cx">          return $pre;
</span><span class="cx"> 
</span><del>-       if ( $_wp_using_ext_object_cache ) {
</del><ins>+        if ( wp_using_ext_object_cache() ) {
</ins><span class="cx">           $value = wp_cache_get( $transient, 'transient' );
</span><span class="cx">  } else {
</span><span class="cx">          $transient_option = '_transient_' . $transient;
</span><span class="lines">@@ -493,11 +489,9 @@
</span><span class="cx">  * @return bool False if value was not set and true if value was set.
</span><span class="cx">  */
</span><span class="cx"> function set_transient( $transient, $value, $expiration = 0 ) {
</span><del>-       global $_wp_using_ext_object_cache;
-
</del><span class="cx">   $value = apply_filters( 'pre_set_transient_' . $transient, $value );
</span><span class="cx"> 
</span><del>-       if ( $_wp_using_ext_object_cache ) {
</del><ins>+        if ( wp_using_ext_object_cache() ) {
</ins><span class="cx">           $result = wp_cache_set( $transient, $value, 'transient', $expiration );
</span><span class="cx">  } else {
</span><span class="cx">          $transient_timeout = '_transient_timeout_' . $transient;
</span><span class="lines">@@ -970,10 +964,8 @@
</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><del>-       global $_wp_using_ext_object_cache;
-
</del><span class="cx">   do_action( 'delete_site_transient_' . $transient, $transient );
</span><del>-       if ( $_wp_using_ext_object_cache ) {
</del><ins>+        if ( wp_using_ext_object_cache() ) {
</ins><span class="cx">           $result = wp_cache_delete( $transient, 'site-transient' );
</span><span class="cx">  } else {
</span><span class="cx">          $option_timeout = '_site_transient_timeout_' . $transient;
</span><span class="lines">@@ -1008,13 +1000,11 @@
</span><span class="cx">  * @return mixed Value of transient
</span><span class="cx">  */
</span><span class="cx"> function get_site_transient( $transient ) {
</span><del>-       global $_wp_using_ext_object_cache;
-
</del><span class="cx">   $pre = apply_filters( 'pre_site_transient_' . $transient, false );
</span><span class="cx">  if ( false !== $pre )
</span><span class="cx">          return $pre;
</span><span class="cx"> 
</span><del>-       if ( $_wp_using_ext_object_cache ) {
</del><ins>+        if ( wp_using_ext_object_cache() ) {
</ins><span class="cx">           $value = wp_cache_get( $transient, 'site-transient' );
</span><span class="cx">  } else {
</span><span class="cx">          // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
</span><span class="lines">@@ -1058,11 +1048,9 @@
</span><span class="cx">  * @return bool False if value was not set and true if value was set.
</span><span class="cx">  */
</span><span class="cx"> function set_site_transient( $transient, $value, $expiration = 0 ) {
</span><del>-       global $_wp_using_ext_object_cache;
-
</del><span class="cx">   $value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
</span><span class="cx"> 
</span><del>-       if ( $_wp_using_ext_object_cache ) {
</del><ins>+        if ( wp_using_ext_object_cache() ) {
</ins><span class="cx">           $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
</span><span class="cx">  } else {
</span><span class="cx">          $transient_timeout = '_site_transient_timeout_' . $transient;
</span></span></pre></div>
<a id="trunksrcwpincludesqueryphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/query.php (25288 => 25289)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/query.php  2013-09-06 17:37:15 UTC (rev 25288)
+++ trunk/src/wp-includes/query.php     2013-09-06 18:09:24 UTC (rev 25289)
</span><span class="lines">@@ -1946,7 +1946,7 @@
</span><span class="cx">   * @return array List of posts.
</span><span class="cx">   */
</span><span class="cx">  function get_posts() {
</span><del>-               global $wpdb, $user_ID, $_wp_using_ext_object_cache;
</del><ins>+                global $wpdb, $user_ID;
</ins><span class="cx"> 
</span><span class="cx">          $this->parse_query();
</span><span class="cx"> 
</span><span class="lines">@@ -1996,7 +1996,7 @@
</span><span class="cx">                  $q['suppress_filters'] = false;
</span><span class="cx"> 
</span><span class="cx">          if ( !isset($q['cache_results']) ) {
</span><del>-                       if ( $_wp_using_ext_object_cache )
</del><ins>+                        if ( wp_using_ext_object_cache() )
</ins><span class="cx">                           $q['cache_results'] = false;
</span><span class="cx">                  else
</span><span class="cx">                          $q['cache_results'] = true;
</span></span></pre>
</div>
</div>

</body>
</html>