Feature Enhancement – Control and Monitor of Priming Activities
The existing W3 Total Cache implementation of cache preload function offers no control or monitoring of the background prime activity. This feature enhancement introduces new control functions to start and stop prime background activities on the WordPress cron queue and to maintain a trace file of all prime activities.

w3-total-cache/inc/options/pgcache.php
For the user interface to create the buttons and text in the Cache Preload section of the Page Cache section of the W3TC options.
<?php if (!defined('W3TC')) die(); ?>
<?php include W3TC_INC_DIR . '/options/common/header.php'; ?>
<?php function next_prime_event() {
$crons = _get_cron_array();
foreach ($crons as $timestamp => $hooks) {
foreach ($hooks as $hook => $keys) {
if ($hook == 'w3_pgcache_prime') {
return $timestamp;
}
}
}
return null;
} ?>
.
.
.
<tr>
<th><label for="pgcache_prime_sitemap">Sitemap <acronym title="Uniform Resource Indicator">URL</acronym>:</label></th>
<td>
<input id="pgcache_prime_sitemap" type="text" name="pgcache.prime.sitemap" value="<?php echo $this->_config->get_string('pgcache.prime.sitemap'); ?>" size="100" /><br />
<span class="description">A <a href="http://www.xml-sitemaps.com/validate-xml-sitemap.html" target="_blank">compliant</a> sitemap can be used to specify the pages to maintain in the primed cache. Pages will be cached according to the priorities specified in the <acronym title="Extensible Markup Language">XML</acronym> file. <a href="http://wordpress.org/extend/plugins/google-sitemap-generator/" target="_blank">Google <acronym title="Extensible Markup Language">XML</acronym> Sitemaps</a> is recommended for use with this feature.</span>
</td>
</tr>
<tr>
<th>Status:</th>
<td>
<span class="description">Next prime event <?php echo (next_prime_event()) ? date(DATE_RFC822,next_prime_event()) : 'is unknown'; ?> <br />Current time is <?php echo date(DATE_RFC822,time()); ?><br />View the recorded <a href="/w3tc_primetrace" target="_blank">Cache Prime Trace</a>.</span>
</td>
</tr>
</table>
<p class="submit">
<?php echo $this->nonce_field('w3tc'); ?>
<input type="submit" name="w3tc_save_options" class="w3tc-button-save button-primary" value="Save all settings" />
<input type="submit" name="w3tc_prime_pgcache" value="Prime cache"<?php if (! $pgcache_enabled): ?> disabled="disabled"<?php endif; ?> class="button" /> <input type="submit" name="w3tc_prime_pgcache_stop" value="Stop prime"<?php if (! $pgcache_enabled): ?> disabled="disabled"<?php endif; ?> class="button" /> </p>
<?php echo $this->postbox_footer(); ?>
.
.
.
w3-total-cache/lib/W3/Plugin/TotalCacheAdmin.php
For the user interface action events and message text for the prime control buttons.
.
.
.
$note_messages = array(
'config_save' => 'Plugin configuration successfully updated.',
'prime_pgcache' => 'Page cache prime is successfully scheduled.',
'prime_pgcache_no' => 'Page cache prime is not scheduled.',
'flush_all' => 'All caches successfully emptied.',
'flush_memcached' => 'Memcached cache(s) successfully emptied.',
.
.
.
/**
* Latest widget control
*
* @param integer $widget_id
* @param array $form_inputs
* @return void
*/
function widget_pagespeed_control($widget_id, $form_inputs = array()) {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require_once W3TC_LIB_W3_DIR . '/Request.php';
$this->_config->set('widget.pagespeed.key', W3_Request::get_string('w3tc_widget_pagespeed_key'));
$this->_config->save();
} else {
include W3TC_INC_DIR . '/widget/pagespeed_control.php';
}
}
/**
* Prime page cache action
*
* @return void
*/
function action_prime_pgcache() {
$this->prime_pgcache();
if (wp_next_scheduled('w3_pgcache_prime')) {
$this->redirect(array( 'w3tc_note' => 'prime_pgcache' ), true);
} else {
$this->redirect(array( 'w3tc_note' => 'prime_pgcache_no' ), true);
}
}
/**
* Stop Prime page cache action
*
* @return void
*/
function action_prime_pgcache_stop() {
$this->prime_pgcache_stop();
if (wp_next_scheduled('w3_pgcache_prime')) {
$this->redirect(array( 'w3tc_note' => 'prime_pgcache' ), true);
} else {
$this->redirect(array( 'w3tc_note' => 'prime_pgcache_no' ), true);
}
}
.
.
.
/**
* Save blognames into file
*/
if (w3_is_network() && !w3_is_subdomain_install()) {
w3_save_blognames();
}
return true;
}
return false;
}
/**
* Prime page cache
*
* @return void
*/
function prime_pgcache() {
$w3_plugin_pgcache = & w3_instance('W3_Plugin_PgCacheAdmin');
$w3_plugin_pgcache->schedule_prime();
}
/**
* Stop Prime page cache
*
* @return void
*/
function prime_pgcache_stop() {
$crons = _get_cron_array();
foreach ($crons as $timestamp => $hooks) {
foreach ($hooks as $hook => $keys) {
if ($hook == 'w3_pgcache_prime') {
unset( $crons[$timestamp][$hook] );
if ( empty($crons[$timestamp]) ) {
unset( $crons[$timestamp] );
}
}
}
}
_set_cron_array( $crons );
}
.
.
.
For the implemetation functions to maintain a trace file of all prime activities, see the feature enhancement to preload only uncached pages. The display function is shown below.
w3-total-cache/lib/W3/PgCache.php
To send the ‘w3tc_primetrace’ output if this is the requested page.
.
.
.
/**
* Do page cache logic
*/
if ($this->_debug) {
$this->_time_start = w3_microtime();
}
/**
* Is this cache prime trace output?
*/
if (strpos($this->_request_uri,'w3tc_primetrace') !== false) {
$cache = & $this->_get_cache();
$prime_key = $this->_get_page_key('w3tc_primetrace');
$tracedata = $cache->get($prime_key);
$this->_send_headers(false, time(), 0, false, array());
if ($tracedata) {
echo "<h1>Cache Prime Trace</h1>" . nl2br($tracedata);
} else {
echo "<p>Cache prime trace information does not exist.</p>";
}
exit();
}
.
.
.
Excellent, thanks for the hard but good work !
I was wondering when they will ever fix Fault 1 especially, as it generates an error in my server error logs and filling it up …
Thank you so much, this really improved the website, page speed went from 52 to 65 on desktop, so the improvement was greatly needed.
I had to turn it off though, the lightbox effect didn’t function on the single portfolio pages (using the Angular theme and their premium support is no help, just said to use the W3TC plugin)
W3TC doesn’t work correctly in WP 3.2.2, selecting the minify and page cache options breaks it the same way your version does, but with no speed improvement. also using better WP minify, google libraries and lazy loading, but not much more improvement. this theme doesn’t work with super cache and other plugins haven’t been updated, like smush.it, things would be helpful at this time.
Yours is the only one that really helped, its a real shame. Would you have any idea why it does that? Any help would be greatly appreciated on that. But I wanted to let you know that your efforts made a major improvement and I’m sure would benefit others.
Great work! Thank you