| Server IP : 103.88.176.108 / Your IP : 216.73.216.211 Web Server : Apache/2.4.41 (Ubuntu) System : Linux webserver 5.4.0-42-generic #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020 x86_64 User : www-data ( 33) PHP Version : 7.4.3-4ubuntu2.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/html/wp-content/plugins/wp-cloudflare-page-cache/libs/ |
Upload File : |
<?php
use SPC\Constants;
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
class SWCFPC_Preloader_Process {
/**
* The main plugin class.
*
* @var \SW_CLOUDFLARE_PAGECACHE
*/
private $main_instance = null;
/**
* Logger instance
*
* @var SWCFPC_Logs
*/
private $logger = null;
/**
* Job items.
*
* @var items
*/
private $items = array();
/**
* Action name.
*
* @var string
*/
protected $action = 'swcfpc_cache_preloader_background_process';
function __construct( $main_instance ) {
$this->main_instance = $main_instance;
$this->logger = $this->main_instance->get_logger();
add_action( 'spc_preloader_job', array( $this, 'preloader_jobs' ) );
add_action( 'spc_preloader_completed', array( $this, 'preloader_completed' ) );
}
public function push_to_queue( $item ) {
$this->items[] = $item;
}
public function save() {
foreach ( $this->items as $item ) {
as_enqueue_async_action('spc_preloader_job', $item, Constants::ACTION_SCHEDULER_GROUP);
}
as_schedule_single_action(time() + 60, 'spc_preloader_completed', array(), Constants::ACTION_SCHEDULER_GROUP);
}
public function preloader_jobs( $item ) {
if ( empty( $item ) ) {
$this->logger->add_log( 'preloader::preloader_jobs', 'Unable to find a valid URL to preload. Exit.' );
return false;
}
$this->logger->add_log( 'preloader::preloader_jobs', 'Preloading URL ' . esc_url_raw( $item ) );
$args = [
'timeout' => defined( 'SWCFPC_CURL_TIMEOUT' ) ? SWCFPC_CURL_TIMEOUT : 10,
'blocking' => true,
'user-agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0',
'sslverify' => false,
'headers' => [
'Accept' => 'text/html',
],
];
$response = wp_remote_get( esc_url_raw( $item ), $args );
$status = wp_remote_retrieve_response_code( $response );
if ( $status !== 200 ) {
$this->logger->add_log( 'preloader::preloader_jobs', 'Error preloading URL ' . esc_url_raw( $item ) . '. Status: ' . $status );
} else {
$this->logger->add_log( 'preloader::preloader_jobs', 'URL ' . esc_url_raw( $item ) . ' preloaded successfully.' );
}
// Sleep 2 seconds before to remove the item from queue and preload next url
sleep( 2 );
// Return false to remove item from the queue. If not, the process enter in loop
return false;
}
public function preloader_completed() {
// Unlock preloader
$this->main_instance->get_cache_controller()->unlock_preloader();
// Log preloading complete
$this->logger->add_log( 'preloader::preloader_completed', 'Preloading complete' );
}
}