Skip to content

Commit

Permalink
Added WP Rocket support and tests (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
joverthegrey authored Nov 21, 2023
1 parent 5229280 commit e93e1ad
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .warden/wordpress/wp-config.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
define( 'WP_CACHE', true ); // Added by WP Rocket

/**
* The base configuration for WordPress
*
Expand Down
7 changes: 6 additions & 1 deletion warpdrive-plugin/src/class-cache-flusher-memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
namespace Savvii;


/**
* Class CacheFlusherMemcached
*/
class CacheFlusherMemcached implements CacheFlusherInterface
{

const CACHENAME='Memcached';

/**
* @var false|resource
*/
Expand Down Expand Up @@ -97,4 +102,4 @@ public function is_enabled()

return true;
}
}
}
7 changes: 6 additions & 1 deletion warpdrive-plugin/src/class-cache-flusher-opcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
namespace Savvii;


/**
* Class CacheFlusherOpcache
*/
class CacheFlusherOpcache implements CacheFlusherInterface
{
const CACHENAME='OPcache';

/**
* Used to override the behaviour in the flush_opcache() method during unittests
* (workaround, 'cause we can't disable and enable opcache on the fly
Expand Down Expand Up @@ -89,4 +94,4 @@ public function is_enabled()
$opcache_status['opcache_enabled']
);
}
}
}
4 changes: 3 additions & 1 deletion warpdrive-plugin/src/class-cache-flusher-sucuri.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace Savvii;

/**
* Class SavviiCacheSucuri
* Class CacheFlusherSucuri
* Sends Sucuri cache flush request to the API
*/
class CacheFlusherSucuri implements CacheFlusherInterface {

const CACHENAME='Sucuri';

/**
* Are we in a test
*
Expand Down
4 changes: 3 additions & 1 deletion warpdrive-plugin/src/class-cache-flusher-varnish.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace Savvii;

/**
* Class SavviiCacheFlusher
* Class CacheFlushVarnish
* Sends cache flush request to the API
*/
class CacheFlusherVarnish implements CacheFlusherInterface {

const CACHENAME='Varnish';

/**
* Are we in a test
*
Expand Down
101 changes: 101 additions & 0 deletions warpdrive-plugin/src/class-cache-flusher-wprocket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Savvii;

/**
* Class SavviiCacheWPRocket
* Trigger WP Rockets plugin
*/
class CacheFlusherWPRocket implements CacheFlusherInterface {

const CACHENAME='WP Rocket';

/**
* Are we in a test
*
* @var bool
*/
protected $inTest = false;

/**
* Return value of flush_opcache() when overridden
* @var bool
*/
protected $inTestResult = true;

/**
* Return value of is_enabled() when overridden
* @var bool
*/
protected $inTestEnabled = true;


/**
* Flush cache
* @return bool True on success
*/
public function flush() {
// early exit when in phpunittest
if ($this->inTest) return $this->inTestResult;

// Early return if not enabled
if (!$this->is_enabled()) return true;

$language = get_bloginfo('language');

// clean base domain
if (function_exists('rocket_clean_domain')) {
rocket_clean_domain($language);
}

// clean minified CSS and JS
if (function_exists('rocket_clean_minify')) {
rocket_clean_minify($language);
}

return true;
}

/**
* Flush cache for a specific domain
* @param null $domain
* @return bool True on success
*/
public function flush_domain($domain = null) {
// early exit when in phpunittest
if ($this->inTest) return $this->inTestEnabled;

// Early return if not enabled
if (!$this->is_enabled()) return true;

// Early return if no domain specified
if (is_null($domain) || empty($domain)) {
return true;
}

if (function_exists('rocket_clean_files')) {
$language = get_bloginfo('language');
$paths = [
'https://' . $domain . '/',
'http://' . $domain . '/'
];
rocket_clean_files($language);
}

return true;
}

/**
* Check if the WP Rocket plugin is enabled
*
* @return bool
*/
public function is_enabled()
{
// early exit when in phpunittest
if ($this->inTest) return $this->inTestEnabled;

// See if the plugin exists / is activated by checking if its functions exists
return function_exists('get_rocket_cdn_url');
}
}
3 changes: 2 additions & 1 deletion warpdrive-plugin/src/class-database-size-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ function readableBytes($bytes) {
$i = floor(log($bytes) / log(1024));
$sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');

return sprintf('%.02F', $bytes / pow(1024, $i)) * 1 . ' ' . $sizes[$i];
$pow = ($p = pow(1024, $i)) == 0 ? 1: $p;
return sprintf('%.02F', $bytes / $pow) * 1 . ' ' . $sizes[$i];
}
}
2 changes: 1 addition & 1 deletion warpdrive-plugin/src/class-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Options {
const REPO_LOCATION = 'https://github.com/Savvii/warpdrive';
const REPO_RELEASES_LOCATION = 'https://api.github.com/repos/Savvii/warpdrive/releases/latest';
const REPO_RELEASES_LATEST_ZIPBALL = 'https://github.com/Savvii/warpdrive/releases/latest/download/package.zip';
const AVAILABLE_CACHES = ['memcached', 'opcache', 'varnish', 'sucuri'];
const AVAILABLE_CACHES = ['memcached', 'opcache', 'varnish', 'sucuri', 'wprocket' ];

/**************************************************
* Groups consts
Expand Down
2 changes: 1 addition & 1 deletion warpdrive-plugin/src/class-savvii-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function warpdrive_dashboard() {
<?php $className = '\Savvii\CacheFlusher' . ucfirst($cache) ?>
<?php $cacheClass = new $className() ?>
<?php if ($cacheClass->is_enabled()) : ?>
<li><?php echo esc_attr(ucfirst($cache)) ?></li>
<li><?php echo esc_attr($className::CACHENAME) ?></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php




class CacheFlusherWPRocketTest extends \Warpdrive_UnitTestCase
{

protected $cache;

/**
* testcase setUp(), create an testing enabled CacheFlusherOpcache class
*/
public function setUp():void
{
parent::setUp();
$this->cache = new \Savvii\CacheFlusherWPRocket();
$this->setProtectedProperty($this->cache, 'inTest', true);
}

/**
* Should return true
*/
public function test_successfull_flush()
{
$this->assertTrue($this->cache->flush());
}

/**
* Should return true
*/
public function test_successfull_flush_domain()
{
$this->assertTrue($this->cache->flush_domain('example.com'));
}

/**
* Flush should fail
*/
public function test_unsuccessfull_flush()
{
// let the flush fail
$this->setProtectedProperty($this->cache, 'inTestResult', false);

$this->assertFalse($this->cache->flush());
}

/**
* Flush_domain() should always succeed
*/
public function test_unsuccessfull_but_successfull_flush_domain()
{
// let the 'normal' flush fail
$this->setProtectedProperty($this->cache, 'inTestResult', false);

$this->assertTrue($this->cache->flush_domain('example.com'));
}

/**
* Check successfull is_enabled()
*/
public function test_success_is_enabled()
{
$this->assertTrue($this->cache->is_enabled());
}

/**
* Check unsuccessfull is_enabled()
*/
public function test_unsuccessfull_is_enabled()
{
$this->setProtectedProperty($this->cache, 'inTestEnabled', false);
$this->assertFalse($this->cache->is_enabled());
}
}

0 comments on commit e93e1ad

Please sign in to comment.