-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added WP Rocket support and tests (#38)
- Loading branch information
1 parent
5229280
commit e93e1ad
Showing
10 changed files
with
200 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
warpdrive-plugin/tests/savvii/class-cache-flusher-wprocket-test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |