| 1 |
<?php |
| 2 |
/** |
| 3 |
* Background process for cache purging |
| 4 |
* |
| 5 |
* @package PoweredCache |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace PoweredCache\Async; |
| 9 |
|
| 10 |
use \Powered_Cache_WP_Background_Process as Powered_Cache_WP_Background_Process; |
| 11 |
use function PoweredCache\Utils\clean_page_cache_dir; |
| 12 |
use function PoweredCache\Utils\clean_site_cache_dir; |
| 13 |
use function PoweredCache\Utils\delete_page_cache; |
| 14 |
use function PoweredCache\Utils\powered_cache_flush; |
| 15 |
|
| 16 |
/** |
| 17 |
* Class CachePurger |
| 18 |
*/ |
| 19 |
class CachePurger extends Powered_Cache_WP_Background_Process { |
| 20 |
|
| 21 |
/** |
| 22 |
* Plugin settings |
| 23 |
* |
| 24 |
* @var $settings |
| 25 |
*/ |
| 26 |
protected $settings; |
| 27 |
|
| 28 |
/** |
| 29 |
* string |
| 30 |
* |
| 31 |
* @var $action |
| 32 |
*/ |
| 33 |
protected $action = 'powered_cache_purger'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Task |
| 37 |
* |
| 38 |
* Perform Preload Tasks |
| 39 |
* |
| 40 |
* @param mixed $item Queue item to iterate over |
| 41 |
* |
| 42 |
* @return mixed |
| 43 |
*/ |
| 44 |
protected function task( $item ) { |
| 45 |
$this->settings = \PoweredCache\Utils\get_settings(); |
| 46 |
|
| 47 |
if ( empty( $item['call'] ) ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
\PoweredCache\Utils\log( sprintf( 'Call: %s', $item['call'] ) ); |
| 52 |
|
| 53 |
switch ( $item['call'] ) { |
| 54 |
case 'powered_cache_flush': |
| 55 |
powered_cache_flush(); |
| 56 |
break; |
| 57 |
case 'clean_page_cache_dir': |
| 58 |
clean_page_cache_dir(); |
| 59 |
break; |
| 60 |
case 'clean_site_cache_dir': |
| 61 |
clean_site_cache_dir(); |
| 62 |
break; |
| 63 |
case 'clean_site_cache_for_language': |
| 64 |
if ( function_exists( '\PoweredCache\Compat\WPML\clean_site_cache_for_language' ) ) { |
| 65 |
\PoweredCache\Compat\WPML\clean_site_cache_for_language( $item['lang_code'] ); |
| 66 |
} |
| 67 |
|
| 68 |
break; |
| 69 |
case 'delete_page_cache': |
| 70 |
$urls = $item['urls']; |
| 71 |
if ( ! empty( $urls ) ) { |
| 72 |
foreach ( $urls as $url ) { |
| 73 |
\PoweredCache\Utils\log( sprintf( 'delete_page_cache - URL: %s', $url ) ); |
| 74 |
delete_page_cache( $url ); |
| 75 |
} |
| 76 |
} |
| 77 |
break; |
| 78 |
} |
| 79 |
|
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
/** |
| 85 |
* Complete |
| 86 |
* |
| 87 |
* Override if applicable, but ensure that the below actions are |
| 88 |
* performed, or, call parent::complete(). |
| 89 |
*/ |
| 90 |
protected function complete() { // phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found |
| 91 |
\PoweredCache\Utils\log( 'Async cache purge has been completed!' ); |
| 92 |
parent::complete(); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Sometimes canceling a process is glitchy |
| 97 |
* Try to cancel all items in the queue up to $max_attempt |
| 98 |
*/ |
| 99 |
public function cancel_process() { |
| 100 |
$max_attempt = 5; |
| 101 |
$cancelled = 0; |
| 102 |
while ( ! parent::is_queue_empty() ) { |
| 103 |
if ( $cancelled >= $max_attempt ) { |
| 104 |
break; |
| 105 |
} |
| 106 |
parent::cancel(); |
| 107 |
$cancelled ++; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Whether the process running or not |
| 113 |
* |
| 114 |
* @return bool |
| 115 |
*/ |
| 116 |
public function is_process_running() { // phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found |
| 117 |
return parent::is_processing(); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Return an instance of the current class |
| 122 |
* |
| 123 |
* @return CachePurger |
| 124 |
* @since 2.3 |
| 125 |
*/ |
| 126 |
public static function factory() { |
| 127 |
|
| 128 |
static $instance; |
| 129 |
|
| 130 |
if ( ! $instance ) { |
| 131 |
$instance = new self(); |
| 132 |
} |
| 133 |
|
| 134 |
return $instance; |
| 135 |
} |
| 136 |
|
| 137 |
} |
| 138 |
|