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