Initial commit xmarticaims
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Preload\Controller;
|
||||
|
||||
trait CheckExcludedTrait {
|
||||
|
||||
/**
|
||||
* Add new pattern of excluded uri.
|
||||
*
|
||||
* @param array $regexes regexes used to exclude urls.
|
||||
* @return array
|
||||
*/
|
||||
public function add_cache_reject_uri_to_excluded( array $regexes ): array {
|
||||
$user_added_cache_reject_uri = (array) get_rocket_option( 'cache_reject_uri', [] );
|
||||
|
||||
if ( count( $user_added_cache_reject_uri ) === 0 ) {
|
||||
return $regexes;
|
||||
}
|
||||
|
||||
$altered_user_added_cache_reject_uri = implode( '$|', $user_added_cache_reject_uri );
|
||||
|
||||
$user_added_cache_reject_uri = implode( '|', $user_added_cache_reject_uri );
|
||||
$cache_reject_uri = get_rocket_cache_reject_uri();
|
||||
|
||||
$regexes[] = str_replace( $user_added_cache_reject_uri . '|', $altered_user_added_cache_reject_uri . '$|', $cache_reject_uri );
|
||||
|
||||
return $regexes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the url is excluded by using a filter.
|
||||
*
|
||||
* @param string $url url to check.
|
||||
* @return bool
|
||||
*/
|
||||
protected function is_excluded_by_filter( string $url ): bool {
|
||||
global $wp_rewrite;
|
||||
|
||||
$pagination_regex = "/$wp_rewrite->pagination_base/\d+";
|
||||
$url = strtok( $url, '?' );
|
||||
$url = user_trailingslashit( $url );
|
||||
|
||||
/**
|
||||
* Regex to exclude URI from preload.
|
||||
*
|
||||
* @param string[] $regexes Regexes to check.
|
||||
* @param string $url Current preloading url.
|
||||
*/
|
||||
$regexes = (array) apply_filters( 'rocket_preload_exclude_urls', [ $pagination_regex ], $url );
|
||||
|
||||
if ( empty( $regexes ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$regexes = array_unique( $regexes );
|
||||
|
||||
foreach ( $regexes as $regex ) {
|
||||
if ( ! is_string( $regex ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$regex = user_trailingslashit( $regex );
|
||||
|
||||
if ( preg_match( "@$regex$@m", $url ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Preload\Controller;
|
||||
|
||||
use WP_Rocket\Engine\Preload\Admin\Settings;
|
||||
use WP_Rocket\Engine\Preload\Database\Queries\Cache;
|
||||
|
||||
class CheckFinished {
|
||||
|
||||
/**
|
||||
* Preload settings.
|
||||
*
|
||||
* @var Settings
|
||||
*/
|
||||
protected $settings;
|
||||
|
||||
/**
|
||||
* Preload queue.
|
||||
*
|
||||
* @var Queue
|
||||
*/
|
||||
protected $queue;
|
||||
|
||||
/**
|
||||
* Db query.
|
||||
*
|
||||
* @var Cache
|
||||
*/
|
||||
protected $query;
|
||||
|
||||
/**
|
||||
* Instantiate class.
|
||||
*
|
||||
* @param Settings $settings Preload settings.
|
||||
* @param Cache $cache Db query.
|
||||
* @param Queue $queue Preload queue.
|
||||
*/
|
||||
public function __construct( Settings $settings, Cache $cache, Queue $queue ) {
|
||||
$this->settings = $settings;
|
||||
$this->query = $cache;
|
||||
$this->queue = $queue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the preload is finished.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function check_finished() {
|
||||
if ( ( ! $this->queue->has_remaining_tasks() && ! $this->query->has_pending_jobs() ) || ! $this->settings->is_enabled() ) {
|
||||
delete_transient( 'wpr_preload_running' );
|
||||
return;
|
||||
}
|
||||
|
||||
$this->queue->add_job_preload_job_check_finished_async();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Preload\Controller;
|
||||
|
||||
use WP_Rocket\Engine\Preload\Database\Queries\Cache;
|
||||
|
||||
class ClearCache {
|
||||
use CheckExcludedTrait;
|
||||
|
||||
/**
|
||||
* DB query.
|
||||
*
|
||||
* @var Cache
|
||||
*/
|
||||
protected $query;
|
||||
|
||||
/**
|
||||
* Initialise ClearCache.
|
||||
*
|
||||
* @param Cache $query DB query.
|
||||
*/
|
||||
public function __construct( Cache $query ) {
|
||||
$this->query = $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear urls listed.
|
||||
*
|
||||
* @param array $urls urls to clean.
|
||||
* @return void
|
||||
*/
|
||||
public function partial_clean( array $urls ) {
|
||||
foreach ( $urls as $url ) {
|
||||
|
||||
if ( ! $this->is_excluded_by_filter( $url ) ) {
|
||||
$this->query->create_or_update(
|
||||
[
|
||||
'url' => $url,
|
||||
'status' => 'pending',
|
||||
]
|
||||
);
|
||||
} else {
|
||||
$this->query->delete_by_url( $url );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all urls.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function full_clean() {
|
||||
$this->query->set_all_to_pending();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a URL from the preload.
|
||||
*
|
||||
* @param string $url URL to delete.
|
||||
* @return void
|
||||
*/
|
||||
public function delete_url( string $url ) {
|
||||
$this->query->delete_by_url( $url );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Preload\Controller;
|
||||
|
||||
class CrawlHomepage {
|
||||
|
||||
/**
|
||||
* Crawl the homepage.
|
||||
*
|
||||
* @return array|false
|
||||
*/
|
||||
public function crawl() {
|
||||
$user_agent = 'WP Rocket/Preload';
|
||||
|
||||
/**
|
||||
* Filters the arguments for the partial preload request.
|
||||
*
|
||||
* @param array $args Request arguments.
|
||||
*/
|
||||
$args = apply_filters(
|
||||
'rocket_homepage_preload_url_request_args',
|
||||
[
|
||||
'timeout' => 10,
|
||||
'user-agent' => $user_agent,
|
||||
'sslverify' => apply_filters( 'https_local_ssl_verify', false ), // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
|
||||
]
|
||||
);
|
||||
|
||||
$response = wp_remote_get( esc_url_raw( home_url() ), $args );
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$response_code = wp_remote_retrieve_response_code( $response );
|
||||
|
||||
if ( 200 !== $response_code ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$content = wp_remote_retrieve_body( $response );
|
||||
|
||||
preg_match_all( '/<a\s+(?:[^>]+?[\s"\']|)href\s*=\s*(["\'])(?<href>[^"\']+)\1/imU', $content, $urls );
|
||||
|
||||
$home_url = home_url();
|
||||
|
||||
$urls = array_map(
|
||||
static function ( $url ) use ( $home_url ) {
|
||||
if ( wp_parse_url( $url, PHP_URL_HOST ) || strpos( $url, '#' ) !== false ) {
|
||||
return $url;
|
||||
}
|
||||
return trailingslashit( $home_url ) . ltrim( wp_parse_url( $url, PHP_URL_PATH ), '/' );
|
||||
},
|
||||
$urls['href']
|
||||
);
|
||||
|
||||
$urls = array_filter(
|
||||
$urls,
|
||||
static function ( $url ) use ( $home_url ) {
|
||||
return strpos( $url, $home_url ) !== false && strpos( $url, '#' ) === false;
|
||||
}
|
||||
);
|
||||
|
||||
return array_values( array_unique( $urls ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Preload\Controller;
|
||||
|
||||
use WP_Rocket\Engine\Preload\Database\Queries\Cache;
|
||||
|
||||
/**
|
||||
* Controller to load initial sitemap tasks.
|
||||
*/
|
||||
class LoadInitialSitemap {
|
||||
|
||||
/**
|
||||
* Queue group.
|
||||
*
|
||||
* @var Queue
|
||||
*/
|
||||
protected $queue;
|
||||
|
||||
/**
|
||||
* DB query.
|
||||
*
|
||||
* @var Cache
|
||||
*/
|
||||
protected $query;
|
||||
|
||||
/**
|
||||
* Homepage crawler.
|
||||
*
|
||||
* @var CrawlHomepage
|
||||
*/
|
||||
protected $crawl_homepage;
|
||||
|
||||
/**
|
||||
* Instantiate the class.
|
||||
*
|
||||
* @param Queue $queue Queue group.
|
||||
* @param Cache $query DB query.
|
||||
* @param CrawlHomepage $crawl_homepage Homepage crawler.
|
||||
*/
|
||||
public function __construct( Queue $queue, $query, CrawlHomepage $crawl_homepage ) {
|
||||
$this->queue = $queue;
|
||||
$this->query = $query;
|
||||
$this->crawl_homepage = $crawl_homepage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the initial sitemap to the queue.
|
||||
*/
|
||||
public function load_initial_sitemap() {
|
||||
|
||||
/**
|
||||
* Filter custom preload URL.
|
||||
*
|
||||
* @param array Array of custom preload URL
|
||||
*/
|
||||
$urls = apply_filters( 'rocket_preload_load_custom_urls', [] );
|
||||
$urls [] = home_url();
|
||||
$urls = array_filter( $urls );
|
||||
|
||||
foreach ( $urls as $url ) {
|
||||
$this->query->create_or_nothing(
|
||||
[
|
||||
'url' => $url,
|
||||
]
|
||||
);
|
||||
$this->queue->add_job_preload_job_preload_url_async( $url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter sitemaps URL.
|
||||
*
|
||||
* @param array Array of sitemaps URL
|
||||
*/
|
||||
$sitemaps = (array) apply_filters( 'rocket_sitemap_preload_list', [] );
|
||||
|
||||
if ( count( $sitemaps ) > 0 ) {
|
||||
/**
|
||||
* Filter sitemaps URL that will be preloaded.
|
||||
*
|
||||
* @param array Array of sitemaps URL
|
||||
* @returns array
|
||||
*/
|
||||
$sitemaps = apply_filters( 'rocket_preload_sitemap_before_queue', $sitemaps );
|
||||
$this->add_task_to_queue( $sitemaps );
|
||||
return;
|
||||
}
|
||||
|
||||
$sitemap = $this->load_wordpress_sitemap();
|
||||
|
||||
if ( ! $sitemap ) {
|
||||
$this->add_homepage_urls();
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* Filter sitemaps URL that will be preloaded.
|
||||
*
|
||||
* @param array Array of sitemaps URL
|
||||
* @returns array
|
||||
*/
|
||||
$sitemaps = apply_filters( 'rocket_preload_sitemap_before_queue', [ $sitemap ] );
|
||||
$this->add_task_to_queue( $sitemaps );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add homepage urls to the preload.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function add_homepage_urls() {
|
||||
$urls = $this->crawl_homepage->crawl();
|
||||
|
||||
if ( ! $urls ) {
|
||||
return;
|
||||
}
|
||||
foreach ( $urls as $url ) {
|
||||
$this->query->create_or_nothing(
|
||||
[
|
||||
'url' => $url,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add initial sitemap tasks.
|
||||
*
|
||||
* @param array $sitemaps sitemap used for creating tasks.
|
||||
*/
|
||||
protected function add_task_to_queue( array $sitemaps ) {
|
||||
set_transient( 'wpr_preload_running', true );
|
||||
|
||||
foreach ( $sitemaps as $sitemap ) {
|
||||
$this->queue->add_job_preload_job_parse_sitemap_async( $sitemap );
|
||||
}
|
||||
$this->queue->add_job_preload_job_check_finished_async();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load default WordPress sitemap.
|
||||
*
|
||||
* @return false|string
|
||||
*/
|
||||
protected function load_wordpress_sitemap() {
|
||||
if ( ! $this->sitemaps_enabled() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$sitemaps = wp_sitemaps_get_server();
|
||||
|
||||
if ( ! $sitemaps ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $sitemaps->index->get_index_url();
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel the preloading.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function cancel_preload() {
|
||||
$this->queue->cancel_pending_jobs();
|
||||
$this->query->revert_in_progress();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if sitemap is enabled.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function sitemaps_enabled() {
|
||||
$is_enabled = (bool) get_option( 'blog_public' );
|
||||
|
||||
/**
|
||||
* Filters whether XML Sitemaps are enabled or not.
|
||||
*
|
||||
* When XML Sitemaps are disabled via this filter, rewrite rules are still
|
||||
* in place to ensure a 404 is returned.
|
||||
*
|
||||
* @see WP_Sitemaps::register_rewrites()
|
||||
*
|
||||
* @since 5.5.0
|
||||
*
|
||||
* @param bool $is_enabled Whether XML Sitemaps are enabled or not. Defaults
|
||||
* to true for public sites.
|
||||
*/
|
||||
return (bool) apply_filters( 'wp_sitemaps_enabled', $is_enabled ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,276 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Preload\Controller;
|
||||
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
use WP_Rocket\Engine\Preload\Database\Queries\Cache;
|
||||
use WP_Filesystem_Direct;
|
||||
|
||||
class PreloadUrl {
|
||||
use CheckExcludedTrait;
|
||||
|
||||
/**
|
||||
* Preload queue.
|
||||
*
|
||||
* @var Queue
|
||||
*/
|
||||
protected $queue;
|
||||
|
||||
/**
|
||||
* Preload database query.
|
||||
*
|
||||
* @var Cache
|
||||
*/
|
||||
protected $query;
|
||||
|
||||
/**
|
||||
* Configurations options.
|
||||
*
|
||||
* @var Options_Data
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* Filesystem.
|
||||
*
|
||||
* @var WP_Filesystem_Direct
|
||||
*/
|
||||
protected $filesystem;
|
||||
|
||||
/**
|
||||
* Instantiate preload controller.
|
||||
*
|
||||
* @param Options_Data $options configuration options.
|
||||
* @param Queue $queue preload queue.
|
||||
* @param Cache $rocket_cache preload database query.
|
||||
* @param WP_Filesystem_Direct $filesystem Filesystem.
|
||||
*/
|
||||
public function __construct( Options_Data $options, Queue $queue, Cache $rocket_cache, WP_Filesystem_Direct $filesystem ) {
|
||||
$this->options = $options;
|
||||
$this->query = $rocket_cache;
|
||||
$this->queue = $queue;
|
||||
$this->filesystem = $filesystem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Preload an url.
|
||||
*
|
||||
* @param string $url url to preload.
|
||||
* @return void
|
||||
*/
|
||||
public function preload_url( string $url ) {
|
||||
|
||||
$is_mobile = $this->options->get( 'do_caching_mobile_files', false );
|
||||
if ( $this->is_already_cached( $url ) && ( ! $is_mobile || $this->is_already_cached( $url, true ) ) ) {
|
||||
$this->query->make_status_complete( $url );
|
||||
return;
|
||||
}
|
||||
|
||||
$requests = [
|
||||
[
|
||||
'url' => $url,
|
||||
'is_mobile' => false,
|
||||
'headers' => [
|
||||
'blocking' => false,
|
||||
'timeout' => 0.01,
|
||||
'user-agent' => 'WP Rocket/Preload',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
if ( $is_mobile ) {
|
||||
$requests[] = [
|
||||
'url' => $url,
|
||||
'headers' => [
|
||||
'user-agent' => $this->get_mobile_user_agent_prefix(),
|
||||
],
|
||||
'is_mobile' => true,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters to modify requests done to preload an url.
|
||||
*
|
||||
* @param array $requests Requests that will be done.
|
||||
*/
|
||||
$requests = apply_filters( 'rocket_preload_before_preload_url', $requests );
|
||||
|
||||
if ( ! is_array( $requests ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$requests = array_filter( $requests );
|
||||
|
||||
foreach ( $requests as $request ) {
|
||||
if ( ! isset( $request['url'] ) || ! is_string( $request['url'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$headers = isset( $request['headers'] ) && is_array( $request['headers'] ) ? $request['headers'] : [];
|
||||
|
||||
$headers = array_merge(
|
||||
$headers,
|
||||
[
|
||||
'blocking' => false,
|
||||
'timeout' => 0.01,
|
||||
/**
|
||||
* Filter to activate the verification of SSl.
|
||||
*
|
||||
* @param string $activate is the verification activated.
|
||||
*/
|
||||
'sslverify' => apply_filters( 'https_local_ssl_verify', false ), // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
|
||||
]
|
||||
);
|
||||
|
||||
/**
|
||||
* Filters the arguments for the preload request.
|
||||
*
|
||||
* @param array $headers Request arguments.
|
||||
*/
|
||||
$headers = apply_filters(
|
||||
'rocket_preload_url_request_args',
|
||||
$headers
|
||||
);
|
||||
|
||||
if ( ! is_array( $headers ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
wp_safe_remote_get(
|
||||
user_trailingslashit( $request['url'] ),
|
||||
$headers
|
||||
);
|
||||
/**
|
||||
* Filter the delay between each preload request.
|
||||
*
|
||||
* @param float $delay_between the defined delay.
|
||||
* @returns float
|
||||
*/
|
||||
$delay_between = apply_filters( 'rocket_preload_delay_between_requests', 500000 );
|
||||
|
||||
usleep( $delay_between );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the prefix to prepend to the user agent used for preload to make a HTTP request detected as a mobile device.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_mobile_user_agent_prefix() {
|
||||
$prefix = 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1';
|
||||
|
||||
/**
|
||||
* Filter the prefix to prepend to the user agent used for preload to make a HTTP request detected as a mobile device.
|
||||
*
|
||||
* @param string $prefix The prefix.
|
||||
*/
|
||||
$new_prefix = apply_filters( 'rocket_mobile_preload_user_agent_prefix', $prefix );
|
||||
|
||||
if ( empty( $new_prefix ) || ! is_string( $new_prefix ) ) {
|
||||
return $prefix;
|
||||
}
|
||||
|
||||
return 'WP Rocket/Preload ' . $new_prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process pending jobs inside CRON iteration.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function process_pending_jobs() {
|
||||
|
||||
$pending_actions = $this->queue->get_pending_preload_actions();
|
||||
|
||||
$count = ( (int) apply_filters( 'rocket_preload_cache_pending_jobs_cron_rows_count', 45 ) ) - count( $pending_actions );
|
||||
/**
|
||||
* Set the delay before an in-progress row is considered as outdated.
|
||||
*
|
||||
* @param int $delay delay.
|
||||
* @return int
|
||||
*/
|
||||
$delay = (int) apply_filters(
|
||||
'rocket_preload_outdated',
|
||||
/**
|
||||
* Set the max number of rows in batches.
|
||||
*
|
||||
* @param int $count number of rows in batches.
|
||||
* @return int
|
||||
*/
|
||||
(int) ( $count / 15 )
|
||||
);
|
||||
|
||||
$stuck_rows = $this->query->get_outdated_in_progress_jobs( $delay );
|
||||
|
||||
$stuck_rows = array_filter(
|
||||
$stuck_rows,
|
||||
function ( $row ) use ( $pending_actions ) {
|
||||
foreach ( $pending_actions as $action ) {
|
||||
if ( count( $action->get_args() ) > 0 && $row->url === $action->get_args()[0] ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
);
|
||||
|
||||
foreach ( $stuck_rows as $row ) {
|
||||
$this->query->make_status_failed( $row->id );
|
||||
}
|
||||
$rows = $this->query->get_pending_jobs( $count );
|
||||
foreach ( $rows as $row ) {
|
||||
|
||||
if ( $this->is_excluded_by_filter( $row->url ) ) {
|
||||
$this->query->delete_by_url( $row->url );
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->query->make_status_inprogress( $row->id );
|
||||
$this->queue->add_job_preload_job_preload_url_async( $row->url );
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the cache file for $item already exists.
|
||||
*
|
||||
* @param string $url The URL to preload.
|
||||
* @param bool $is_mobile is mobile text.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_already_cached( string $url, bool $is_mobile = false ) {
|
||||
static $https;
|
||||
|
||||
if ( ! isset( $https ) ) {
|
||||
$https = is_ssl() && $this->options->get( 'cache_ssl' ) ? '-https' : '';
|
||||
}
|
||||
|
||||
$url = get_rocket_parse_url( $url );
|
||||
|
||||
/** This filter is documented in inc/functions/htaccess.php */
|
||||
if ( apply_filters( 'rocket_url_no_dots', false ) ) {
|
||||
$url['host'] = str_replace( '.', '_', $url['host'] );
|
||||
}
|
||||
|
||||
$url['path'] = trailingslashit( $url['path'] );
|
||||
|
||||
if ( '' !== $url['query'] ) {
|
||||
$url['query'] = '#' . $url['query'] . '/';
|
||||
}
|
||||
|
||||
$mobile = $is_mobile ? '-mobile' : '';
|
||||
|
||||
$file_cache_path = rocket_get_constant( 'WP_ROCKET_CACHE_PATH' ) . $url['host'] . strtolower( $url['path'] . $url['query'] ) . 'index' . $mobile . $https . '.html';
|
||||
|
||||
if ( ! $this->options->get( 'cache_webp', false ) ) {
|
||||
return $this->filesystem->exists( $file_cache_path );
|
||||
}
|
||||
|
||||
$webp_path = rocket_get_constant( 'WP_ROCKET_CACHE_PATH' ) . $url['host'] . strtolower( $url['path'] . $url['query'] ) . 'index' . $mobile . $https . '-webp.html';
|
||||
$no_webp_path = rocket_get_constant( 'WP_ROCKET_CACHE_PATH' ) . $url['host'] . strtolower( $url['path'] . $url['query'] ) . '.no-webp';
|
||||
|
||||
return $this->filesystem->exists( $webp_path ) || ( $this->filesystem->exists( $no_webp_path ) && $this->filesystem->exists( $file_cache_path ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Preload\Controller;
|
||||
|
||||
use ActionScheduler_Store;
|
||||
use WP_Rocket\Engine\Common\Queue\AbstractASQueue;
|
||||
|
||||
class Queue extends AbstractASQueue {
|
||||
|
||||
/**
|
||||
* Group from the queue.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'rocket-preload';
|
||||
|
||||
|
||||
/**
|
||||
* Add Async load initial sitemap job.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function add_job_preload_job_load_initial_sitemap_async() {
|
||||
return $this->add_async( 'rocket_preload_job_load_initial_sitemap' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Async parse sitemap job with url.
|
||||
*
|
||||
* @param string $sitemap_url sitemap url.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function add_job_preload_job_parse_sitemap_async( string $sitemap_url ) {
|
||||
return $this->add_async(
|
||||
'rocket_preload_job_parse_sitemap',
|
||||
[
|
||||
$sitemap_url,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Async preload url job with url.
|
||||
*
|
||||
* @param string $url url to preload.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function add_job_preload_job_preload_url_async( string $url ) {
|
||||
return $this->add_async(
|
||||
'rocket_preload_job_preload_url',
|
||||
[
|
||||
$url,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a job that check if the preload is finished.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function add_job_preload_job_check_finished_async() {
|
||||
|
||||
if ( $this->job_preload_job_check_finished_async_exists() ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $this->schedule_single( time() + MINUTE_IN_SECONDS, 'rocket_preload_job_check_finished', [ time() ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a task job_preload_job_check_finished_async_exists already exists.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function job_preload_job_check_finished_async_exists() {
|
||||
if ( ! did_action( 'init' ) || doing_action( 'init' ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$row_found = $this->search(
|
||||
[
|
||||
'hook' => 'rocket_preload_job_check_finished',
|
||||
'status' => ActionScheduler_Store::STATUS_PENDING,
|
||||
],
|
||||
'ids'
|
||||
);
|
||||
|
||||
return count( $row_found ) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if some task is remaining.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has_remaining_tasks() {
|
||||
$parse_sitemap = $this->search(
|
||||
[
|
||||
'hook' => 'rocket_preload_job_parse_sitemap',
|
||||
'status' => ActionScheduler_Store::STATUS_PENDING,
|
||||
],
|
||||
'ids'
|
||||
);
|
||||
$preload_url = $this->search(
|
||||
[
|
||||
'hook' => 'rocket_preload_job_preload_url',
|
||||
'status' => ActionScheduler_Store::STATUS_PENDING,
|
||||
],
|
||||
'ids'
|
||||
);
|
||||
|
||||
return count( $parse_sitemap ) > 0 || count( $preload_url ) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel pending jobs.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function cancel_pending_jobs() {
|
||||
$this->cancel_all( '' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return pending actions inside AS scheduler queue.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_pending_preload_actions(): array {
|
||||
return $this->search(
|
||||
[
|
||||
'hook' => 'rocket_preload_job_preload_url',
|
||||
'status' => ActionScheduler_Store::STATUS_PENDING,
|
||||
'per_page' => -1,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user