Commit inicial powerservice

This commit is contained in:
Gabriel
2026-10-19 12:45:45 -06:00
commit ad85486fe2
10158 changed files with 2736804 additions and 0 deletions
@@ -0,0 +1,135 @@
<?php
declare(strict_types=1);
namespace WP_Rocket\Engine\Preload\Activation;
use WP_Rocket\Engine\Activation\ActivationInterface;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Preload\Controller\{PreloadUrl, Queue};
use WP_Rocket\Engine\Preload\Database\Queries\Cache;
class Activation implements ActivationInterface {
/**
* Preload controller
*
* @var PreloadUrl
*/
private $preload_url;
/**
* Preload queue.
*
* @var Queue
*/
protected $queue;
/**
* DB query.
*
* @var Cache
*/
protected $query;
/**
* Options.
*
* @var Options_Data
*/
protected $options;
/**
* Instantiate class.
*
* @param PreloadUrl $preload_url PreloadUrl instance.
* @param Queue $queue Preload queue.
* @param Cache $query DB query.
* @param Options_Data $options Options.
*/
public function __construct( PreloadUrl $preload_url, Queue $queue, Cache $query, Options_Data $options ) {
$this->preload_url = $preload_url;
$this->queue = $queue;
$this->query = $query;
$this->options = $options;
}
/**
* Launch preload on activation.
*/
public function activate() {
add_action( 'rocket_activation', [ $this, 'preload_activation' ], 15 );
add_action( 'rocket_after_activation', [ $this, 'preload_homepage' ] );
}
/**
* Run actions on activation.
*
* @return void
*/
public function preload_activation() {
if ( ! $this->options->get( 'manual_preload', true ) ) {
return;
}
/**
* Action that fires before the preload does.
*/
do_action( 'rocket_preload_activation' );
$this->queue->add_job_preload_job_load_initial_sitemap_async();
}
/**
* Preloads the homepage on activation
*
* @return void
*/
public function preload_homepage() {
$this->preload_url->preload_url( home_url() );
}
/**
* Disable cron and jobs on update.
*
* @param string $new_version new version from the plugin.
* @param string $old_version old version from the plugin.
* @return void
*/
public function clean_on_update( $new_version, $old_version ) {
if ( version_compare( $old_version, '3.12.0', '>=' ) ) {
return;
}
$this->query->remove_all();
$this->queue->cancel_pending_jobs();
if ( ! wp_next_scheduled( 'rocket_preload_process_pending' ) ) {
return;
}
wp_clear_scheduled_hook( 'rocket_preload_process_pending' );
}
/**
* Reload sitemap on update.
*
* @param string $new_version new version from the plugin.
* @param string $old_version old version from the plugin.
* @return void
*/
public function refresh_on_update( $new_version, $old_version ) {
if ( version_compare( $old_version, '3.12.0.2', '>' ) ) {
return;
}
$this->queue->add_job_preload_job_load_initial_sitemap_async();
}
/**
* Clear preload on deactivation.
*/
public function deactivation() {
wp_clear_scheduled_hook( 'rocket_preload_clean_rows_time_event' );
wp_clear_scheduled_hook( 'rocket_preload_process_pending' );
wp_clear_scheduled_hook( 'rocket_preload_revert_old_failed_rows' );
}
}
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace WP_Rocket\Engine\Preload\Activation;
use WP_Filesystem_Direct;
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\Preload\Controller\{PreloadUrl, Queue};
use WP_Rocket\Engine\Preload\Database\Queries\Cache as CacheQuery;
use WP_Rocket\Engine\Preload\Database\Tables\Cache as CacheTable;
use WP_Rocket\Logger\Logger;
class ServiceProvider extends AbstractServiceProvider {
/**
* The provides array is a way to let the container
* know that a service is provided by this service
* provider. Every service that is registered via
* this service provider must have an alias added
* to this array or it will be ignored.
*
* @var array
*/
protected $provides = [
'preload_cache_table',
'preload_caches_query',
'preload_url_controller',
'preload_queue',
'preload_activation',
];
/**
* Registers the subscribers in the container
*
* @since 3.3
*
* @return void
*/
public function register() {
$options = $this->getContainer()->get( 'options' );
$this->getContainer()->add( 'preload_cache_table', CacheTable::class );
$this->getContainer()->get( 'preload_cache_table' );
$this->getContainer()->add( 'wp_direct_filesystem', WP_Filesystem_Direct::class )
->addArgument( [] );
$this->getContainer()->add( 'preload_cache_query', CacheQuery::class )
->addArgument( new Logger() );
$this->getContainer()->add( 'preload_queue', Queue::class );
$cache_query = $this->getContainer()->get( 'preload_cache_query' );
$queue = $this->getContainer()->get( 'preload_queue' );
$this->getContainer()->add( 'preload_url_controller', PreloadUrl::class )
->addArgument( $options )
->addArgument( $queue )
->addArgument( $cache_query )
->addArgument( $this->getContainer()->get( 'wp_direct_filesystem' ) );
$this->getContainer()->add( 'preload_activation', Activation::class )
->addArgument( $this->getContainer()->get( 'preload_url_controller' ) )
->addArgument( $queue )
->addArgument( $cache_query )
->addArgument( $options );
}
}
@@ -0,0 +1,112 @@
<?php
namespace WP_Rocket\Engine\Preload\Admin;
use WP_Rocket\Engine\Preload\Controller\PreloadUrl;
use WP_Rocket\Admin\Options_Data;
class Settings {
/**
* Instance of options handler.
*
* @var Options_Data
*/
protected $options;
/**
* PreloadUrl instance
*
* @var PreloadUrl
*/
private $preload_url;
/**
* Instantiate the class
*
* @param Options_Data $options Instance of options handler.
* @param PreloadUrl $preload_url PreloadUrl instance.
*/
public function __construct( Options_Data $options, PreloadUrl $preload_url ) {
$this->options = $options;
$this->preload_url = $preload_url;
}
/**
* Maybe display the preload notice.
*
* @return void
*/
public function maybe_display_preload_notice() {
if ( ! $this->can_display_notice() ) {
return;
}
if ( false === get_transient( 'wpr_preload_running' ) ) {
return;
}
$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true );
if ( in_array( 'preload_notice', (array) $boxes, true ) ) {
return;
}
$message = sprintf(
// translators: %1$s = plugin name.
__( '%1$s: The preload service is now active. After the initial preload it will continue to cache all your pages whenever they are purged. No further action is needed.', 'rocket' ),
'<strong>WP Rocket</strong>'
);
rocket_dismiss_box( 'preload_notice' );
rocket_notice_html(
[
'status' => 'info',
'message' => $message,
'id' => 'rocket-notice-preload-processing',
]
);
}
/**
* Checks if we can display the Preload notices.
*
* @return bool
*/
private function can_display_notice(): bool {
$screen = get_current_screen();
if (
isset( $screen->id )
&&
'settings_page_wprocket' !== $screen->id
) {
return false;
}
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return false;
}
return $this->is_enabled();
}
/**
* Determines if Preload option is enabled.
*
* @return boolean
*/
public function is_enabled(): bool {
return (bool) $this->options->get( 'manual_preload', 0 );
}
/**
* Preload the homepage
*
* @return void
*/
public function preload_homepage() {
$this->preload_url->preload_url( home_url() );
}
}
@@ -0,0 +1,109 @@
<?php
namespace WP_Rocket\Engine\Preload\Admin;
use stdClass;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Preload\Controller\ClearCache;
use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Rocket\Logger\Logger;
use WP_Rocket\Engine\Admin\Settings\Settings as AdminSettings;
class Subscriber implements Subscriber_Interface {
/**
* Options instance.
*
* @var Options_Data
*/
protected $options;
/**
* Settings instance.
*
* @var Settings
*/
protected $settings;
/**
* Creates an instance of the class.
*
* @param Options_Data $options Options instance.
* @param Settings $settings Settings instance.
*/
public function __construct( Options_Data $options, Settings $settings ) {
$this->options = $options;
$this->settings = $settings;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
return [
'admin_notices' => [
[ 'maybe_display_preload_notice' ],
],
'rocket_options_changed' => 'preload_homepage',
'switch_theme' => 'preload_homepage',
'rocket_after_clean_used_css' => 'preload_homepage',
'rocket_domain_options_changed' => 'preload_homepage',
'rocket_input_sanitize' => 'sanitize_options',
'wp_rocket_upgrade' => [ 'maybe_clean_cron', 15, 2 ],
];
}
/**
* Maybe display the preload notice.
*
* @return void
*/
public function maybe_display_preload_notice() {
$this->settings->maybe_display_preload_notice();
}
/**
* Preload the homepage after changing the settings
*
* @return void
*/
public function preload_homepage() {
$this->settings->preload_homepage();
}
/**
* Sanitizes Preload Excluded URI option when saving the settings
*
* @param array $input Array of values submitted from the form.
*
* @return array
*/
public function sanitize_options( $input ): array {
if ( empty( $input['preload_excluded_uri'] ) ) {
$input['preload_excluded_uri'] = [];
return $input;
}
$input['preload_excluded_uri'] = rocket_sanitize_textarea_field( 'preload_excluded_uri', $input['preload_excluded_uri'] );
return $input;
}
/**
* Unlock all preload URL on update.
*
* @param string $wp_rocket_version Latest WP Rocket version.
* @param string $actual_version Installed WP Rocket version.
*/
public function maybe_clean_cron( $wp_rocket_version, $actual_version ) {
if ( version_compare( $actual_version, '3.12.5', '<' ) ) {
return;
}
wp_clear_scheduled_hook( 'rocket_preload_revert_old_in_progress_rows' );
}
}
@@ -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,
]
);
}
}
@@ -0,0 +1,265 @@
<?php
namespace WP_Rocket\Engine\Preload\Cron;
use WP_Rocket\Engine\Common\Queue\Cleaner;
use WP_Rocket\Engine\Preload\Admin\Settings;
use WP_Rocket\Engine\Preload\Controller\PreloadUrl;
use WP_Rocket\Engine\Preload\Database\Queries\Cache;
use WP_Rocket\Event_Management\Subscriber_Interface;
class Subscriber implements Subscriber_Interface {
/**
* Preload settings.
*
* @var Settings
*/
protected $settings;
/**
* Db query.
*
* @var Cache
*/
protected $query;
/**
* Preload url controller.
*
* @var PreloadUrl
*/
protected $preload_controller;
/**
* Creates an instance of the class.
*
* @param Settings $settings Preload settings.
* @param Cache $query Db query.
* @param PreloadUrl $preload_controller Preload url controller.
*/
public function __construct( Settings $settings, Cache $query, PreloadUrl $preload_controller ) {
$this->settings = $settings;
$this->query = $query;
$this->preload_controller = $preload_controller;
}
/**
* Return an array of events that this subscriber listens to.
*
* @return array
*/
public static function get_subscribed_events() {
return [
'rocket_preload_clean_rows_time_event' => 'remove_old_rows',
'rocket_preload_process_pending' => [
[ 'process_pending_urls' ],
[ 'clean_preload_jobs' ],
],
'rocket_preload_revert_old_failed_rows' => 'revert_old_failed_rows',
'cron_schedules' => [
[ 'add_interval' ],
[ 'add_revert_old_failed_interval' ],
],
'init' => [
[ 'schedule_clean_not_commonly_used_rows' ],
[ 'schedule_pending_jobs' ],
[ 'schedule_revert_old_failed_rows' ],
],
];
}
/**
* Schedule clean from removing of old urls.
*
* @return void
*/
public function schedule_clean_not_commonly_used_rows() {
/**
* Delay before the not accessed row is deleted.
*
* @param string $delay delay before the not accessed row is deleted.
*/
$delay = (string) apply_filters( 'rocket_preload_delay_delete_non_accessed', '1 month' );
if ( '' === $delay || wp_next_scheduled( 'rocket_preload_clean_rows_time_event' ) ) {
return;
}
wp_schedule_event( time() + 10 * MINUTE_IN_SECONDS, 'weekly', 'rocket_preload_clean_rows_time_event' );
}
/**
* Preload Url jobs.
*
* @return void
*/
public function process_pending_urls() {
if ( ! $this->settings->is_enabled() ) {
return;
}
$this->preload_controller->process_pending_jobs();
}
/**
* Clean Action Scheduler jobs for preload.
*
* @return void
*/
public function clean_preload_jobs() {
$clean_batch_size = (int) apply_filters( 'rocket_action_scheduler_clean_batch_size', 100, 'rocket-preload' );
$cleaner = new Cleaner( null, $clean_batch_size, 'rocket-preload' );
$cleaner->clean();
}
/**
* Add the interval for the cron.
*
* @param array $schedules Cron schedules.
* @return mixed
*/
public function add_interval( $schedules ) {
if ( ! $this->settings->is_enabled() ) {
return $schedules;
}
/**
* Filters the cron interval.
*
* @since 3.11
*
* @param int $interval Interval in seconds.
*/
$interval = apply_filters( 'rocket_preload_pending_jobs_cron_interval', 1 * rocket_get_constant( 'MINUTE_IN_SECONDS', 60 ) );
$schedules['rocket_preload_process_pending'] = [
'interval' => $interval,
'display' => esc_html__( 'WP Rocket Preload pending jobs', 'rocket' ),
];
return $schedules;
}
/**
* Add the interval for the cron.
*
* @param array $schedules Cron schedules.
* @return mixed
*/
public function add_revert_old_failed_interval( $schedules ) {
if ( ! $this->settings->is_enabled() ) {
return $schedules;
}
/**
* Filters the cron interval.
*
* @since 3.11
*
* @param int $interval Interval in seconds.
*/
$interval = apply_filters( 'rocket_preload_revert_old_failed_rows_cron_interval', 12 * rocket_get_constant( 'HOUR_IN_SECONDS', 60 * 60 ) );
$schedules['rocket_revert_old_failed_rows'] = [
'interval' => $interval,
'display' => esc_html__( 'WP Rocket Preload revert stuck failed jobs', 'rocket' ),
];
return $schedules;
}
/**
* Schedule pending preload urls.
*
* @return void
*/
public function schedule_pending_jobs() {
if (
! $this->settings->is_enabled()
&&
wp_next_scheduled( 'rocket_preload_process_pending' )
) {
wp_clear_scheduled_hook( 'rocket_preload_process_pending' );
return;
}
if ( ! $this->settings->is_enabled() ) {
return;
}
if ( wp_next_scheduled( 'rocket_preload_process_pending' ) ) {
return;
}
wp_schedule_event( time() + MINUTE_IN_SECONDS, 'rocket_preload_process_pending', 'rocket_preload_process_pending' );
}
/**
* Schedule revert stuck failed row cron.
*
* @return void
*/
public function schedule_revert_old_failed_rows() {
if (
! $this->settings->is_enabled()
&&
wp_next_scheduled( 'rocket_preload_revert_old_failed_rows' )
) {
wp_clear_scheduled_hook( 'rocket_preload_revert_old_failed_rows' );
return;
}
if ( ! $this->settings->is_enabled() ) {
return;
}
if ( wp_next_scheduled( 'rocket_preload_revert_old_failed_rows' ) ) {
return;
}
wp_schedule_event( time() + MINUTE_IN_SECONDS, 'rocket_revert_old_failed_rows', 'rocket_preload_revert_old_failed_rows' );
}
/**
* Remove old urls.
*
* @return void
*/
public function remove_old_rows() {
/**
* Delay before the not accessed row is deleted.
*
* @param string $delay delay before the not accessed row is deleted.
*/
$delay = (string) apply_filters( 'rocket_preload_delay_delete_non_accessed', '1 month' );
$parts = explode( ' ', $delay );
if ( '' === $delay || '0' === $delay ) {
return;
}
$value = 1;
$unit = 'month';
if ( count( $parts ) === 2 && $parts[0] >= 0 ) {
$value = $parts[0];
$unit = $parts[1];
}
$this->query->remove_all_not_accessed_rows( $value, $unit );
}
/**
* Remove old failed urls.
*
* @return void
*/
public function revert_old_failed_rows() {
$this->query->revert_old_failed();
}
}
@@ -0,0 +1,695 @@
<?php
namespace WP_Rocket\Engine\Preload\Database\Queries;
use WP_Rocket\Logger\Logger;
use WP_Rocket\Dependencies\Database\Query;
use WP_Rocket\Engine\Preload\Database\Rows\CacheRow;
use WP_Rocket\Engine\Preload\Database\Schemas\Cache as Schema;
class Cache extends Query {
/**
* Logger instance.
*
* @var Logger
*/
protected $logger;
/**
* Name of the database table to query.
*
* @var string
*/
protected $table_name = 'wpr_rocket_cache';
/**
* String used to alias the database table in MySQL statement.
*
* Keep this short, but descriptive. I.E. "tr" for term relationships.
*
* This is used to avoid collisions with JOINs.
*
* @var string
*/
protected $table_alias = 'wpr_cache';
/**
* Name of class used to setup the database schema.
*
* @var string
*/
protected $table_schema = Schema::class;
/** Item ******************************************************************/
/**
* Name for a single item.
*
* Use underscores between words. I.E. "term_relationship"
*
* This is used to automatically generate action hooks.
*
* @var string
*/
protected $item_name = 'cache';
/**
* Plural version for a group of items.
*
* Use underscores between words. I.E. "term_relationships"
*
* This is used to automatically generate action hooks.
*
* @var string
*/
protected $item_name_plural = 'caches';
/**
* Name of class used to turn IDs into first-class objects.
*
* This is used when looping through return values to guarantee their shape.
*
* @var mixed
*/
protected $item_shape = CacheRow::class;
/**
* Instantiate query.
*
* @param Logger $logger logger instance.
*
* @param string|array $query {
* Optional. Array or query string of item query parameters.
* Default empty.
*
* @type string $fields Site fields to return. Accepts 'ids' (returns an array of item IDs)
* or empty (returns an array of complete item objects). Default empty.
* To do a date query against a field, append the field name with _query
* @type bool $count Whether to return a item count (true) or array of item objects.
* Default false.
* @type int $number Limit number of items to retrieve. Use 0 for no limit.
* Default 100.
* @type int $offset Number of items to offset the query. Used to build LIMIT clause.
* Default 0.
* @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query.
* Default true.
* @type string|array $orderby Accepts false, an empty array, or 'none' to disable `ORDER BY` clause.
* Default 'id'.
* @type string $item How to item retrieved items. Accepts 'ASC', 'DESC'.
* Default 'DESC'.
* @type string $search Search term(s) to retrieve matching items for.
* Default empty.
* @type array $search_columns Array of column names to be searched.
* Default empty array.
* @type bool $update_item_cache Whether to prime the cache for found items.
* Default false.
* @type bool $update_meta_cache Whether to prime the meta cache for found items.
* Default false.
* }
*/
public function __construct( Logger $logger, $query = [] ) {
parent::__construct( $query );
$this->logger = $logger;
}
/**
* Create new resource row or update its contents if not created before.
*
* @since 3.9
*
* @param array $resource Resource array.
*
* @return bool
*/
public function create_or_update( array $resource ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.resourceFound
/**
* Format the url.
*
* @param string $url url to format.
* @return string
*/
$url = apply_filters( 'rocket_preload_format_url', $resource['url'] );
$url = untrailingslashit( strtok( $url, '?' ) );
if ( $this->is_rejected( $resource['url'] ) || get_transient( 'wp_rocket_updating' ) ) {
return false;
}
// check the database if those resources added before.
$rows = $this->query(
[
'url' => $url,
],
false
);
if ( count( $rows ) === 0 ) {
// Create this new row in DB.
$resource_id = $this->add_item(
[
'url' => $url,
'status' => key_exists( 'status', $resource ) ? $resource['status'] : 'pending',
'is_locked' => key_exists( 'is_locked', $resource ) ? $resource['is_locked'] : false,
'last_accessed' => current_time( 'mysql', true ),
]
);
if ( $resource_id ) {
return $resource_id;
}
$this->logger->error( "Cannot insert {$resource['url']} into {$this->table_name}" );
return false;
}
$db_row = array_pop( $rows );
$data = [
'url' => $url,
'status' => key_exists( 'status', $resource ) ? $resource['status'] : $db_row->status,
'is_locked' => key_exists( 'is_locked', $resource ) ? $resource['is_locked'] : $db_row->is_locked,
'modified' => current_time( 'mysql', true ),
];
if ( key_exists( 'last_accessed', $resource ) && (bool) $resource['last_accessed'] ) {
$data['last_accessed'] = current_time( 'mysql', true );
}
// Update this row with the new content.
$this->update_item(
$db_row->id,
$data
);
return $db_row->id;
}
/**
* Create new resource row or update its contents if not created before.
*
* @since 3.9
*
* @param array $resource Resource array.
*
* @return bool
*/
public function create_or_nothing( array $resource ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.resourceFound
if ( $this->is_rejected( $resource['url'] ) ) {
return false;
}
/**
* Format the url.
*
* @param string $url url to format.
* @return string
*/
$url = apply_filters( 'rocket_preload_format_url', $resource['url'] );
$url = strtok( $url, '?' );
// check the database if those resources added before.
$rows = $this->query(
[
'url' => untrailingslashit( $url ),
],
false
);
if ( count( $rows ) > 0 ) {
return false;
}
// Create this new row in DB.
$resource_id = $this->add_item(
[
'url' => untrailingslashit( $url ),
'status' => key_exists( 'status', $resource ) ? $resource['status'] : 'pending',
'is_locked' => key_exists( 'is_locked', $resource ) ? $resource['is_locked'] : false,
'last_accessed' => current_time( 'mysql', true ),
]
);
if ( $resource_id ) {
return $resource_id;
}
$this->logger->error( "Cannot insert {$resource['url']} into {$this->table_name}" );
return false;
}
/**
* Get all rows with the same url (desktop and mobile versions).
*
* @param string $url Page url.
*
* @return array|false
*/
public function get_rows_by_url( string $url ) {
$url = strtok( $url, '?' );
$query = $this->query(
[
'url' => untrailingslashit( $url ),
]
);
if ( empty( $query ) ) {
return false;
}
return $query;
}
/**
* Delete DB row by url.
*
* @param string $url Page url to be deleted.
*
* @return bool
*/
public function delete_by_url( string $url ) {
$items = $this->get_rows_by_url( $url );
if ( ! $items ) {
return false;
}
$deleted = true;
foreach ( $items as $item ) {
if ( ! is_bool( $item ) ) {
$deleted = $deleted && $this->delete_item( $item->id );
}
}
return $deleted;
}
/**
* Get all preload caches which were not accessed in the last month.
*
* @param float $delay delay before the not accessed row is deleted.
* @param string $unit unit from the delay.
* @return array
*/
public function get_old_cache( float $delay = 1, string $unit = 'month' ): array {
// Get the database interface.
$db = $this->get_db();
// Bail if no database interface is available.
if ( empty( $db ) ) {
return [];
}
$prefixed_table_name = $db->prefix . $this->table_name;
$query = "SELECT id FROM `$prefixed_table_name` WHERE `last_accessed` <= date_sub(now(), interval $delay $unit)";
$rows_affected = $db->get_results( $query );
return $rows_affected;
}
/**
* Remove all completed rows one by one.
*
* @param float $delay delay before the not accessed row is deleted.
* @param string $unit unit from the delay.
* @return void
*/
public function remove_all_not_accessed_rows( float $delay = 1, string $unit = 'month' ) {
$rows = $this->get_old_cache( $delay, $unit );
foreach ( $rows as $row ) {
if ( ! is_bool( $row ) ) {
$this->delete_item( $row->id );
}
}
}
/**
* Fetch pending jobs.
*
* @param int $total total of jobs to fetch.
* @return array
*/
public function get_pending_jobs( int $total = 45 ) {
if ( $total <= 0 ) {
return [];
}
$orderby = 'modified';
/**
* Filter order for preloading pending urls.
*
* @param bool $orderby order for preloading pending urls.
*
* @returns bool
*/
if ( apply_filters( 'rocket_preload_order', false ) ) {
$orderby = 'id';
}
return $this->query(
[
'number' => $total,
'status' => 'pending',
'fields' => [
'id',
'url',
],
'job_id__not_in' => [
'not_in' => '',
],
'is_locked' => false,
'orderby' => $orderby,
'order' => 'asc',
]
);
}
/**
* Change the status from the task to inprogress.
*
* @param int $id id from the task.
* @return bool
*/
public function make_status_inprogress( int $id ) {
return $this->update_item(
$id,
[
'status' => 'in-progress',
'modified' => current_time( 'mysql', true ),
]
);
}
/**
* Make the status from the task to complete.
*
* @param string $url url from the task.
* @return bool
*/
public function make_status_complete( string $url ) {
$tasks = $this->query(
[
'url' => $url,
]
);
if ( count( $tasks ) === 0 ) {
return false;
}
$task = array_pop( $tasks );
return $this->update_item(
$task->id,
[
'status' => 'completed',
'modified' => current_time( 'mysql', true ),
]
);
}
/**
* Check if pending jobs are remaining.
*
* @return bool
*/
public function has_pending_jobs() {
$pending_count = $this->query(
[
'count' => true,
'status' => 'pending',
]
);
return 0 !== $pending_count;
}
/**
* Revert in-progress urls.
*
* @return void
*/
public function revert_in_progress() {
$in_progress_list = $this->query(
[
'status' => 'in-progress',
]
);
foreach ( $in_progress_list as $in_progress ) {
$this->update_item(
$in_progress->id,
[
'status' => 'pending',
'modified' => current_time( 'mysql', true ),
]
);
}
}
/**
* Revert old in-progress rows
*
* @depecated
*/
public function revert_old_in_progress() {
// Get the database interface.
$db = $this->get_db();
// Bail if no database interface is available.
if ( empty( $db ) ) {
return false;
}
$prefixed_table_name = $db->prefix . $this->table_name;
$db->query( "UPDATE `$prefixed_table_name` SET status = 'pending', modified = '" . current_time( 'mysql', true ) . "' WHERE status = 'in-progress' AND `modified` <= date_sub(now(), interval 12 hour)" );
}
/**
* Revert old failed rows.
*/
public function revert_old_failed() {
// Get the database interface.
$db = $this->get_db();
// Bail if no database interface is available.
if ( empty( $db ) ) {
return false;
}
$prefixed_table_name = $db->prefix . $this->table_name;
return $db->query( "UPDATE `$prefixed_table_name` SET status = 'pending', modified = '" . current_time( 'mysql', true ) . "' WHERE status = 'failed' AND `modified` <= date_sub(now(), interval 12 hour)" );
}
/**
* Set all rows to pending.
*/
public function set_all_to_pending() {
// Get the database interface.
$db = $this->get_db();
// Bail if no database interface is available.
if ( empty( $db ) ) {
return false;
}
$prefixed_table_name = $db->prefix . $this->table_name;
/**
* Filter condition for cleaning URLS in the database.
*
* @param string $condition condition for cleaning URLS in the database.
* @returns string
*/
$condition = apply_filters( 'rocket_preload_all_to_pending_condition', ' WHERE 1 = 1' );
$db->query( "UPDATE `$prefixed_table_name` SET status = 'pending', modified = '" . current_time( 'mysql', true ) . "'$condition" );
}
/**
* Check if the page is preloaded.
*
* @param string $url url from the page to check.
* @return bool
*/
public function is_preloaded( string $url ): bool {
$pending_count = $this->query(
[
'count' => true,
'status' => 'in-progress',
'url' => untrailingslashit( $url ),
]
);
return 0 !== $pending_count;
}
/**
* Check if the page is pending.
*
* @param string $url url from the page to check.
* @return bool
*/
public function is_pending( string $url ): bool {
$pending_count = $this->query(
[
'count' => true,
'status' => 'pending',
'url' => untrailingslashit( $url ),
]
);
return 0 !== $pending_count;
}
/**
* Remove all entries from the table.
*
* @return false|void
*/
public function remove_all() {
// Get the database interface.
$db = $this->get_db();
// Bail if no database interface is available.
if ( empty( $db ) ) {
return false;
}
$prefixed_table_name = $db->prefix . $this->table_name;
$db->query( "DELETE FROM `$prefixed_table_name` WHERE 1 = 1" );
}
/**
* Lock a URL.
*
* @param string $url URL to lock.
*
* @return void
*/
public function lock( string $url ) {
$this->create_or_update(
[
'url' => $url,
'is_locked' => true,
]
);
}
/**
* Unlock all URLs.
*
* @return false|void
*/
public function unlock_all() {
// Get the database interface.
$db = $this->get_db();
// Bail if no database interface is available.
if ( empty( $db ) ) {
return false;
}
$prefixed_table_name = $db->prefix . $this->table_name;
$db->query( "UPDATE `$prefixed_table_name` SET is_locked = false;" );
}
/**
* Unlock a URL.
*
* @param string $url URL to unlock.
*
* @return void
*/
public function unlock( string $url ) {
$this->create_or_update(
[
'url' => $url,
'is_locked' => false,
]
);
}
/**
* Check if the url is rejected.
*
* @param string $url url to check.
* @return bool
*/
protected function is_rejected( string $url ): bool {
$extensions = [
'php' => 1,
'xml' => 1,
'xsl' => 1,
'kml' => 1,
];
$extension = pathinfo( $url, PATHINFO_EXTENSION );
return $extension && isset( $extensions[ $extension ] );
}
/**
* Make the status from the task to failed.
*
* @param int $id id from the task.
* @return bool
*/
public function make_status_failed( int $id ) {
return $this->update_item(
$id,
[
'status' => 'failed',
'modified' => current_time( 'mysql', true ),
]
);
}
/**
* Update last accessed from the row.
*
* @param int $id id from the row.
* @return bool
*/
public function update_last_access( int $id ) {
return $this->update_item(
$id,
[
'last_accessed' => current_time( 'mysql', true ),
]
);
}
/**
* Return outdated in-progress jobs.
*
* @param int $delay delay to delete.
* @return array|int
*/
public function get_outdated_in_progress_jobs( int $delay = 3 ) {
return $this->query(
[
'status' => 'in-progress',
'is_locked' => false,
'date_query' => [
[
'column' => 'modified',
'before' => "$delay minute ago",
],
],
],
false
);
}
}
@@ -0,0 +1,71 @@
<?php
namespace WP_Rocket\Engine\Preload\Database\Rows;
use WP_Rocket\Dependencies\Database\Row;
class CacheRow extends Row {
/**
* Row ID
*
* @var int
*/
public $id;
/**
* URL
*
* @var string
*/
public $url;
/**
* Status
*
* @var string
*/
public $status;
/**
* Last modified time
*
* @var int
*/
public $modified;
/**
* Last accessed time
*
* @var int
*/
public $last_accessed;
/**
* Is row locked
*
* @var bool
*/
public $is_locked;
/**
* Unused variable
*
* @var bool
*/
public $is_mobile;
/**
* CacheRow constructor.
*
* @param object $item Current row details.
*/
public function __construct( $item ) {
parent::__construct( $item );
$this->id = (int) $this->id;
$this->url = (string) $this->url;
$this->status = (string) $this->status;
$this->modified = empty( $this->modified ) ? 0 : strtotime( $this->modified );
$this->last_accessed = empty( $this->last_accessed ) ? 0 : strtotime( $this->last_accessed );
$this->is_locked = (bool) $this->is_locked;
}
}
@@ -0,0 +1,82 @@
<?php
namespace WP_Rocket\Engine\Preload\Database\Schemas;
use WP_Rocket\Dependencies\Database\Schema;
class Cache extends Schema {
/**
* Array of database column objects
*
* @var array
*/
public $columns = [
// ID column.
[
'name' => 'id',
'type' => 'bigint',
'length' => '20',
'unsigned' => true,
'extra' => 'auto_increment',
'primary' => true,
'sortable' => true,
],
// URL column.
[
'name' => 'url',
'type' => 'varchar',
'length' => '2000',
'default' => null,
'cache_key' => true,
'searchable' => true,
'sortable' => false,
],
// STATUS column.
[
'name' => 'status',
'type' => 'varchar',
'length' => '255',
'default' => null,
'cache_key' => true,
'searchable' => true,
'sortable' => false,
],
// MODIFIED column.
[
'name' => 'modified',
'type' => 'timestamp',
'default' => '0000-00-00 00:00:00',
'created' => true,
'date_query' => true,
'sortable' => true,
],
// LAST_ACCESSED column.
[
'name' => 'last_accessed',
'type' => 'timestamp',
'default' => '0000-00-00 00:00:00',
'created' => true,
'date_query' => true,
'sortable' => true,
],
// IS_LOCKED column.
[
'name' => 'is_locked',
'type' => 'tinyint',
'length' => '1',
'default' => 0,
'cache_key' => true,
'searchable' => true,
'sortable' => true,
],
];
}
@@ -0,0 +1,99 @@
<?php
namespace WP_Rocket\Engine\Preload\Database\Tables;
use WP_Rocket\Dependencies\Database\Table;
class Cache extends Table {
/**
* Hook into queries, admin screens, and more!
*
* @since 1.0.0
*/
public function __construct() {
parent::__construct();
add_action( 'rocket_preload_activation', [ $this, 'maybe_upgrade' ] );
add_action( 'init', [ $this, 'maybe_upgrade' ] );
add_action( 'admin_init', [ $this, 'maybe_trigger_recreate_table' ], 9 );
}
/**
* Table name
*
* @var string
*/
protected $name = 'wpr_rocket_cache';
/**
* Database version key (saved in _options or _sitemeta)
*
* @var string
*/
protected $db_version_key = 'wpr_rocket_cache_version';
/**
* Database version
*
* @var int
*/
protected $version = 20220927;
/**
* Key => value array of versions => methods.
*
* @var array
*/
protected $upgrades = [
20220927 => 'add_is_locked_column',
];
/**
* Setup the database schema
*
* @return void
*/
protected function set_schema() {
$this->schema = "
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
url varchar(2000) NOT NULL default '',
status varchar(255) NOT NULL default '',
modified timestamp NOT NULL default '0000-00-00 00:00:00',
last_accessed timestamp NOT NULL default '0000-00-00 00:00:00',
is_locked tinyint(1) NOT NULL default 0,
PRIMARY KEY (id),
KEY url (url(191)),
KEY modified (modified),
KEY last_accessed (last_accessed)";
}
/**
* Trigger recreation of cache table if not exist.
*
* @return void
*/
public function maybe_trigger_recreate_table() {
if ( $this->exists() ) {
return;
}
delete_option( $this->db_version_key );
}
/**
* Add is_locked column.
*
* @return bool
*/
public function add_is_locked_column() {
$hash_column_exists = $this->column_exists( 'is_locked' );
$created = true;
if ( ! $hash_column_exists ) {
$created &= $this->get_db()->query( "ALTER TABLE {$this->table_name} ADD COLUMN is_locked tinyint(1) NOT NULL default 0 AFTER last_accessed" );
}
return $this->is_success( $created );
}
}
@@ -0,0 +1,169 @@
<?php
namespace WP_Rocket\Engine\Preload;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\CDN\CDN;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Fonts preload.
*
* @since 3.6
*/
class Fonts implements Subscriber_Interface {
/**
* WP Rocket Options instance.
*
* @since 3.6
*
* @var Options_Data
*/
private $options;
/**
* WP Rocket CDN instance.
*
* @since 3.6
*
* @var CDN
*/
private $cdn;
/**
* Font formats allowed to be preloaded.
*
* @since 3.6
* @see $this->sanitize_font()
*
* @var string|bool
*/
private $font_formats = [
'otf',
'ttf',
'svg',
'woff',
'woff2',
];
/**
* Return an array of events that this subscriber wants to listen to.
*
* @since 3.6
*
* @param Options_Data $options WP Rocket Options instance.
* @param CDN $cdn WP Rocket CDN instance.
*/
public function __construct( Options_Data $options, CDN $cdn ) {
$this->options = $options;
$this->cdn = $cdn;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @since 3.6
*
* @return array
*/
public static function get_subscribed_events() {
return [
'rocket_buffer' => [ 'preload_fonts', 20 ],
];
}
/**
* Add the required <link/> tags used to preload fonts.
*
* @param string $html html buffer.
* @return string
*
* @since 3.6
*/
public function preload_fonts( $html ): string {
if ( ! $this->is_allowed() ) {
return $html;
}
$fonts = $this->options->get( 'preload_fonts', [] );
/**
* Filters the list of fonts to preload
*
* @since 3.6
*
* @param array $fonts Array of fonts paths.
*/
$fonts = (array) apply_filters( 'rocket_preload_fonts', $fonts );
$fonts = array_map( [ $this, 'sanitize_font' ], $fonts );
$fonts = array_filter( $fonts );
if ( empty( $fonts ) ) {
return $html;
}
$base_url = get_rocket_parse_url( home_url() );
$base_url = "{$base_url['scheme']}://{$base_url['host']}";
$preloads = '</title>';
foreach ( array_unique( $fonts ) as $font ) {
$preloads .= sprintf(
"\n<link rel=\"preload\" as=\"font\" href=\"%s\" crossorigin>",
esc_url( $this->cdn->rewrite_url( $base_url . $font ) )
);
}
return preg_replace( '#</title>#', $preloads, $html, 1 );
}
/**
* Sanitize a font file path.
*
* @since 3.6
*
* @param string $file Filepath to sanitize.
* @return string|bool Sanitized filepath. False if not a font file.
*/
private function sanitize_font( $file ) {
if ( ! is_string( $file ) ) {
return false;
}
$file = trim( $file );
if ( empty( $file ) ) {
return false;
}
$ext = strtolower( pathinfo( wp_parse_url( $file, PHP_URL_PATH ), PATHINFO_EXTENSION ) );
if ( ! in_array( $ext, $this->font_formats, true ) ) {
return false;
}
return $file;
}
/**
* Checks if we should add the preload links
*
* @since 3.8.8
*
* @return bool
*/
private function is_allowed(): bool {
if ( rocket_bypass() ) {
return false;
}
/**
* Filters the preload fonts links addition
*
* @since 3.8.8
*
* @param bool $disable_preload_fonts True to disable, false otherwise.
*/
return ! apply_filters( 'rocket_disable_preload_fonts', false );
}
}
@@ -0,0 +1,82 @@
<?php
namespace WP_Rocket\Engine\Preload\Frontend;
use WP_Rocket\Engine\Preload\Controller\CheckExcludedTrait;
use WP_Rocket\Engine\Preload\Controller\Queue;
use WP_Rocket\Engine\Preload\Database\Queries\Cache;
class FetchSitemap {
use CheckExcludedTrait;
/**
* Parse controller.
*
* @var SitemapParser
*/
protected $sitemap_parser;
/**
* Queue instance.
*
* @var Queue
*/
protected $queue;
/**
* DB query.
*
* @var Cache
*/
protected $query;
/**
* Instantiate the class.
*
* @param SitemapParser $sitemap_parser Parse controller.
* @param Queue $queue Queue instance.
* @param Cache $rocket_cache DB query.
*/
public function __construct( SitemapParser $sitemap_parser, Queue $queue, Cache $rocket_cache ) {
$this->sitemap_parser = $sitemap_parser;
$this->queue = $queue;
$this->query = $rocket_cache;
}
/**
* Parse a sitemap.
*
* @param string $url url from the sitemap.
*/
public function parse_sitemap( string $url ) {
$response = wp_safe_remote_get( $url );
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
return;
}
$data = wp_remote_retrieve_body( $response );
if ( ! $data ) {
return;
}
$this->sitemap_parser->set_content( $data );
$links = $this->sitemap_parser->get_links();
foreach ( $links as $link ) {
if ( ! $this->is_excluded_by_filter( $link ) ) {
$this->query->create_or_nothing(
[
'url' => $link,
]
);
}
}
$children = $this->sitemap_parser->get_children();
foreach ( $children as $child ) {
$this->queue->add_job_preload_job_parse_sitemap_async( $child );
}
}
}
@@ -0,0 +1,76 @@
<?php
namespace WP_Rocket\Engine\Preload\Frontend;
use SimpleXMLElement;
class SitemapParser {
/**
* XML document to parse.
*
* @var SimpleXMLElement|false
*/
protected $xml;
/**
* Set the content from the sitemap to parse.
*
* @param string $content content from the sitemap to parse.
*/
public function set_content( string $content ) {
libxml_use_internal_errors( true );
$this->xml = simplexml_load_string( $content );
}
/**
* Get links to sitemaps.
*
* @return array
*/
public function get_links(): array {
$links = [];
if ( false === $this->xml ) {
return [];
}
$url_count = count( $this->xml->url );
for ( $i = 0; $i < $url_count; $i++ ) {
$url = (string) $this->xml->url[ $i ]->loc;
if ( ! $url ) {
continue;
}
$links [] = $url;
}
return $links;
}
/**
* Get children sitemaps.
*
* @return array
*/
public function get_children(): array {
$children = [];
if ( false === $this->xml ) {
return [];
}
$sitemap_children = count( $this->xml->sitemap );
for ( $i = 0; $i < $sitemap_children; $i++ ) {
$url = (string) $this->xml->sitemap[ $i ]->loc;
if ( ! $url ) {
continue;
}
$children [] = $url;
}
return $children;
}
}
@@ -0,0 +1,106 @@
<?php
namespace WP_Rocket\Engine\Preload\Frontend;
use WP_Rocket\Engine\Preload\Controller\CheckFinished;
use WP_Rocket\Engine\Preload\Controller\LoadInitialSitemap;
use WP_Rocket\Engine\Preload\Controller\PreloadUrl;
use WP_Rocket\Event_Management\Subscriber_Interface;
class Subscriber implements Subscriber_Interface {
/**
* Controller fetching the sitemap.
*
* @var FetchSitemap
*/
protected $fetch_sitemap;
/**
* Controller preloading urls.
*
* @var PreloadUrl
*/
protected $preload_controller;
/**
* Controller checking if the preload is finished.
*
* @var CheckFinished
*/
protected $check_finished;
/**
* Controller loading the initial sitemap.
*
* @var LoadInitialSitemap
*/
protected $initial_sitemap;
/**
* Creates an instance of the class.
*
* @param FetchSitemap $fetch_sitemap controller fetching the sitemap.
* @param PreloadUrl $preload_controller controller preloading urls.
* @param CheckFinished $check_finished controller checking if the preload is finished.
* @param LoadInitialSitemap $initial_sitemap Controller loading the initial sitemap.
*/
public function __construct( FetchSitemap $fetch_sitemap, PreloadUrl $preload_controller, CheckFinished $check_finished, LoadInitialSitemap $initial_sitemap ) {
$this->fetch_sitemap = $fetch_sitemap;
$this->preload_controller = $preload_controller;
$this->check_finished = $check_finished;
$this->initial_sitemap = $initial_sitemap;
}
/**
* Return an array of events that this subscriber listens to.
*
* @return array
*/
public static function get_subscribed_events() {
return [
'rocket_preload_job_parse_sitemap' => 'parse_sitemap',
'rocket_preload_job_preload_url' => 'preload_url',
'rocket_preload_job_check_finished' => 'check_finished',
'rocket_preload_job_load_initial_sitemap' => 'load_initial_sitemap',
];
}
/**
* Parse the sitemap.
*
* @param string $url url to parse.
* @return void
*/
public function parse_sitemap( string $url ) {
$this->fetch_sitemap->parse_sitemap( $url );
}
/**
* Preload url.
*
* @param string $url url to preload.
* @return void
*/
public function preload_url( string $url ) {
$this->preload_controller->preload_url( $url );
}
/**
* Check if the preload is finished.
*
* @return void
*/
public function check_finished() {
$this->check_finished->check_finished();
}
/**
* Load the initial sitemap.
*
* @return void
*/
public function load_initial_sitemap() {
$this->initial_sitemap->load_initial_sitemap();
}
}
@@ -0,0 +1,72 @@
<?php
namespace WP_Rocket\Engine\Preload\Links;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;
class AdminSubscriber implements Subscriber_Interface {
/**
* Options Data instance
*
* @var Options_Data
*/
private $options;
/**
* Instantiate the class.
*
* @param Options_Data $options Options Data instance.
*/
public function __construct( Options_Data $options ) {
$this->options = $options;
}
/**
* Events this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
return [
'rocket_first_install_options' => 'add_option',
'rocket_plugins_to_deactivate' => 'add_incompatible_plugins',
];
}
/**
* Adds the option key & value to the WP Rocket options array
*
* @since 3.7
*
* @param array $options WP Rocket options array.
* @return array
*/
public function add_option( $options ) {
$options = (array) $options;
$options['preload_links'] = 1;
return $options;
}
/**
* Adds plugins incompatible with preload links to our notice.
*
* @since 3.7
*
* @param array $plugins Array of incompatible plugins.
* @return array
*/
public function add_incompatible_plugins( $plugins ) {
if ( ! (bool) $this->options->get( 'preload_links', 0 ) ) {
return $plugins;
}
$plugins['flying-pages'] = 'flying-pages/flying-pages.php';
$plugins['instant-page'] = 'instant-page/instantpage.php';
$plugins['quicklink'] = 'quicklink/quicklink.php';
return $plugins;
}
}
@@ -0,0 +1,40 @@
<?php
namespace WP_Rocket\Engine\Preload\Links;
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
/**
* Service provider for WP Rocket preload links.
*/
class ServiceProvider extends AbstractServiceProvider {
/**
* The provides array is a way to let the container
* know that a service is provided by this service
* provider. Every service that is registered via
* this service provider must have an alias added
* to this array or it will be ignored.
*
* @var array
*/
protected $provides = [
'preload_links_admin_subscriber',
'preload_links_subscriber',
];
/**
* Registers the subscribers in the container
*
* @return void
*/
public function register() {
$options = $this->getContainer()->get( 'options' );
$this->getContainer()->share( 'preload_links_admin_subscriber', AdminSubscriber::class )
->addArgument( $options )
->addTag( 'common_subscriber' );
$this->getContainer()->share( 'preload_links_subscriber', Subscriber::class )
->addArgument( $options )
->addArgument( rocket_direct_filesystem() )
->addTag( 'common_subscriber' );
}
}
@@ -0,0 +1,247 @@
<?php
namespace WP_Rocket\Engine\Preload\Links;
use WP_Filesystem_Direct;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;
class Subscriber implements Subscriber_Interface {
/**
* Options Data instance
*
* @var Options_Data
*/
private $options;
/**
* WP_Filesystem_Direct instance.
*
* @var WP_Filesystem_Direct
*/
private $filesystem;
/**
* Script enqueued status.
*
* @var bool
*/
private $is_enqueued = false;
/**
* Instantiate the class.
*
* @param Options_Data $options Options Data instance.
* @param WP_Filesystem_Direct $filesystem The Filesystem object.
*/
public function __construct( Options_Data $options, $filesystem ) {
$this->options = $options;
$this->filesystem = $filesystem;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
return [
'wp_enqueue_scripts' => 'add_preload_script',
];
}
/**
* Adds the inline script to the footer when the option is enabled
*
* @since 3.7
*
* @return void
*/
public function add_preload_script() {
/**
* Bail out if user is logged in
* Don't add preload link script
*/
if ( is_user_logged_in() ) {
return;
}
if ( $this->is_enqueued ) {
return;
}
if ( ! (bool) $this->options->get( 'preload_links', 0 ) || rocket_bypass() ) {
return;
}
$js_assets_path = rocket_get_constant( 'WP_ROCKET_PATH' ) . 'assets/js/';
if ( ! wp_script_is( 'rocket-browser-checker' ) ) {
$checker_filename = rocket_get_constant( 'SCRIPT_DEBUG' ) ? 'browser-checker.js' : 'browser-checker.min.js';
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion
wp_register_script(
'rocket-browser-checker',
'',
[],
rocket_get_constant( WP_ROCKET_VERSION, '' ),
true
);
wp_enqueue_script( 'rocket-browser-checker' );
wp_add_inline_script(
'rocket-browser-checker',
$this->filesystem->get_contents( "{$js_assets_path}{$checker_filename}" )
);
}
$preload_filename = rocket_get_constant( 'SCRIPT_DEBUG' ) ? 'preload-links.js' : 'preload-links.min.js';
// Register handle with no src to add the inline script after.
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion
wp_register_script(
'rocket-preload-links',
'',
[
'rocket-browser-checker',
],
rocket_get_constant( WP_ROCKET_VERSION, '' ),
true
);
wp_enqueue_script( 'rocket-preload-links' );
wp_add_inline_script(
'rocket-preload-links',
$this->filesystem->get_contents( "{$js_assets_path}{$preload_filename}" )
);
wp_localize_script(
'rocket-preload-links',
'RocketPreloadLinksConfig',
$this->get_preload_links_config()
);
$this->is_enqueued = true;
}
/**
* Gets the Preload Links script configuration parameters.
*
* @since 3.7
*
* @return string[] Preload Links script configuration parameters.
*/
private function get_preload_links_config() {
$use_trailing_slash = $this->use_trailing_slash();
$images_ext = 'jpg|jpeg|gif|png|tiff|bmp|webp|avif|pdf|doc|docx|xls|xlsx|php';
$config = [
'excludeUris' => $this->get_uris_to_exclude( $use_trailing_slash ),
'usesTrailingSlash' => $use_trailing_slash,
'imageExt' => $images_ext,
'fileExt' => $images_ext . '|html|htm',
'siteUrl' => home_url(),
'onHoverDelay' => 100, // milliseconds. -1 disables the "on hover" feature.
'rateThrottle' => 3, // on hover: limits the number of links preloaded per second.
];
/**
* Preload Links script configuration parameters.
*
* This array of parameters are passed as RocketPreloadLinksConfig object and used by the
* `preload-links.min.js` script to configure the behavior of the Preload Links feature.
*
* @since 3.7
*
* @param string[] $config Preload Links script configuration parameters.
*/
$filtered_config = apply_filters( 'rocket_preload_links_config', $config );
if ( ! is_array( $filtered_config ) ) {
return $config;
}
return array_merge( $config, $filtered_config );
}
/**
* Gets the URIs to exclude.
*
* @since 3.7
*
* @param bool $use_trailing_slash When true, uses trailing slash.
*
* @return string
*/
private function get_uris_to_exclude( $use_trailing_slash ) {
$site_url = site_url();
$uris = get_rocket_cache_reject_uri( false, false );
$uris = str_replace( [ '/(.*)|', '/(.*)/|' ], '/|', $uris );
$default = [
'/refer/',
'/go/',
'/recommend/',
'/recommends/',
];
$excluded = $default;
/**
* Filters the patterns excluded from links preload
*
* @since 3.10.8
*
* @param string[] $excluded Array of excluded patterns.
* @param string[] $default Array of default excluded patterns.
*/
$excluded = apply_filters( 'rocket_preload_links_exclusions', $excluded, $default );
if ( ! is_array( $excluded ) ) {
$excluded = (array) $excluded;
}
$excluded = array_filter( $excluded );
$login_url = wp_login_url(); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
$login_uri = str_replace( home_url(), '', $login_url );
$excluded = array_filter(
$excluded,
function ( $uri ) use ( $login_uri ) {
return ! str_contains( $login_uri, $uri );
}
);
$excluded_patterns = '';
if ( ! empty( $excluded ) ) {
$excluded_patterns = '|' . implode( '|', $excluded );
}
return $uris . $excluded_patterns;
}
/**
* Checks if the given URL has a trailing slash.
*
* @since 3.7
*
* @param string $url URL to check.
*
* @return bool
*/
private function has_trailing_slash( $url ) {
return substr( $url, -1 ) === '/';
}
/**
* Indicates if the site uses a trailing slash in the permalink structure.
*
* @since 3.7
*
* @return bool when true, uses `/`; else, no.
*/
private function use_trailing_slash() {
return $this->has_trailing_slash( get_permalink() );
}
}
@@ -0,0 +1,164 @@
<?php
declare(strict_types=1);
namespace WP_Rocket\Engine\Preload;
use WP_Filesystem_Direct;
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\Preload\Activation\Activation;
use WP_Rocket\Engine\Preload\Admin\Settings;
use WP_Rocket\Engine\Preload\Admin\Subscriber as AdminSubscriber;
use WP_Rocket\Engine\Preload\Controller\{CheckFinished, ClearCache, CrawlHomepage, LoadInitialSitemap, PreloadUrl, Queue};
use WP_Rocket\Engine\Preload\Cron\Subscriber as CronSubscriber;
use WP_Rocket\Engine\Preload\Database\Queries\Cache as CacheQuery;
use WP_Rocket\Engine\Preload\Database\Tables\Cache as CacheTable;
use WP_Rocket\Engine\Preload\Frontend\FetchSitemap;
use WP_Rocket\Engine\Preload\Frontend\SitemapParser;
use WP_Rocket\Engine\Preload\Frontend\Subscriber as FrontEndSubscriber;
use WP_Rocket\Logger\Logger;
use WP_Rocket_Mobile_Detect;
/**
* Service provider for the WP Rocket preload.
*
* @since 3.3
*/
class ServiceProvider extends AbstractServiceProvider {
/**
* The provides array is a way to let the container
* know that a service is provided by this service
* provider. Every service that is registered via
* this service provider must have an alias added
* to this array or it will be ignored.
*
* @var array
*/
protected $provides = [
'preload_queue',
'sitemap_parser',
'fetch_sitemap_controller',
'check_finished_controller',
'load_initial_sitemap_controller',
'preload_url_controller',
'preload_caches_table',
'preload_caches_query',
'preload_admin_subscriber',
'preload_clean_controller',
'preload_subscriber',
'preload_front_subscriber',
'preload_cron_subscriber',
'fonts_preload_subscriber',
'preload_activation',
];
/**
* Registers the subscribers in the container
*
* @since 3.3
*
* @return void
*/
public function register() {
$options = $this->getContainer()->get( 'options' );
$this->getContainer()->add( 'preload_mobile_detect', WP_Rocket_Mobile_Detect::class );
$this->getContainer()->add( 'wp_direct_filesystem', WP_Filesystem_Direct::class )
->addArgument( [] );
$wp_file_system = $this->getContainer()->get( 'wp_direct_filesystem' );
$this->getContainer()->add( 'preload_caches_table', CacheTable::class );
$this->getContainer()->add( 'preload_caches_query', CacheQuery::class )
->addArgument( new Logger() );
$this->getContainer()->get( 'preload_caches_table' );
$cache_query = $this->getContainer()->get( 'preload_caches_query' );
$this->getContainer()->add( 'preload_queue', Queue::class );
$queue = $this->getContainer()->get( 'preload_queue' );
$this->getContainer()->add( 'preload_url_controller', PreloadUrl::class )
->addArgument( $options )
->addArgument( $queue )
->addArgument( $cache_query )
->addArgument( $wp_file_system );
$preload_url_controller = $this->getContainer()->get( 'preload_url_controller' );
$this->getContainer()->add( 'homepage_crawler', CrawlHomepage::class );
$crawl_homepage = $this->getContainer()->get( 'homepage_crawler' );
$this->getContainer()->add( 'sitemap_parser', SitemapParser::class );
$sitemap_parser = $this->getContainer()->get( 'sitemap_parser' );
$this->getContainer()->add( 'fetch_sitemap_controller', FetchSitemap::class )
->addArgument( $sitemap_parser )
->addArgument( $queue )
->addArgument( $cache_query );
$fetch_sitemap_controller = $this->getContainer()->get( 'fetch_sitemap_controller' );
$this->getContainer()->add( 'load_initial_sitemap_controller', LoadInitialSitemap::class )
->addArgument( $queue )
->addArgument( $cache_query )
->addArgument( $crawl_homepage );
$this->getContainer()->add( 'preload_activation', Activation::class )
->addArgument( $preload_url_controller )
->addArgument( $queue )
->addArgument( $cache_query )
->addArgument( $options );
$this->getContainer()->add( 'preload_settings', Settings::class )
->addArgument( $options )
->addArgument( $preload_url_controller );
$preload_settings = $this->getContainer()->get( 'preload_settings' );
$this->getContainer()->add( 'check_finished_controller', CheckFinished::class )
->addArgument( $preload_settings )
->addArgument( $cache_query )
->addArgument( $queue );
$check_finished_controller = $this->getContainer()->get( 'check_finished_controller' );
$this->getContainer()->share( 'preload_front_subscriber', FrontEndSubscriber::class )
->addArgument( $fetch_sitemap_controller )
->addArgument( $preload_url_controller )
->addArgument( $check_finished_controller )
->addArgument( $this->getContainer()->get( 'load_initial_sitemap_controller' ) )
->addTag( 'common_subscriber' );
$this->getContainer()->add( 'preload_clean_controller', ClearCache::class )
->addArgument( $cache_query );
$clean_controller = $this->getContainer()->get( 'preload_clean_controller' );
$this->getContainer()->share( 'preload_subscriber', Subscriber::class )
->addArgument( $options )
->addArgument( $this->getContainer()->get( 'load_initial_sitemap_controller' ) )
->addArgument( $cache_query )
->addArgument( $this->getContainer()->get( 'preload_activation' ) )
->addArgument( $this->getContainer()->get( 'preload_mobile_detect' ) )
->addArgument( $clean_controller )
->addArgument( $queue )
->addTag( 'common_subscriber' );
$this->getContainer()->share( 'preload_cron_subscriber', CronSubscriber::class )
->addArgument( $preload_settings )
->addArgument( $cache_query )
->addArgument( $preload_url_controller )
->addTag( 'common_subscriber' );
$this->getContainer()->share( 'fonts_preload_subscriber', Fonts::class )
->addArgument( $options )
->addArgument( $this->getContainer()->get( 'cdn' ) )
->addTag( 'common_subscriber' );
$this->getContainer()->add( 'preload_admin_subscriber', AdminSubscriber::class )
->addArgument( $options )
->addArgument( $preload_settings )
->addTag( 'common_subscriber' );
}
}
@@ -0,0 +1,547 @@
<?php
declare(strict_types=1);
namespace WP_Rocket\Engine\Preload;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Preload\Activation\Activation;
use WP_Rocket\Engine\Preload\Controller\CheckExcludedTrait;
use WP_Rocket\Engine\Preload\Controller\ClearCache;
use WP_Rocket\Engine\Preload\Controller\LoadInitialSitemap;
use WP_Rocket\Engine\Preload\Controller\Queue;
use WP_Rocket\Engine\Preload\Database\Queries\Cache;
use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Rocket_Mobile_Detect;
class Subscriber implements Subscriber_Interface {
use CheckExcludedTrait;
/**
* Options instance.
*
* @var Options_Data
*/
protected $options;
/**
* Controller to load initial tasks.
*
* @var LoadInitialSitemap
*/
protected $controller;
/**
* Clear cache controller.
*
* @var ClearCache
*/
protected $clear_cache;
/**
* Cache query instance.
*
* @var Cache
*/
private $query;
/**
* Activation manager.
*
* @var Activation
*/
protected $activation;
/**
* Mobile detector instance.
*
* @var WP_Rocket_Mobile_Detect
*/
protected $mobile_detect;
/**
* Preload queue.
*
* @var Queue
*/
protected $queue;
/**
* Creates an instance of the class.
*
* @param Options_Data $options Options instance.
* @param LoadInitialSitemap $controller controller creating the initial task.
* @param Cache $query Cache query instance.
* @param Activation $activation Activation manager.
* @param WP_Rocket_Mobile_Detect $mobile_detect Mobile detector instance.
* @param ClearCache $clear_cache Clear cache controller.
* @param Queue $queue Preload queue.
*/
public function __construct( Options_Data $options, LoadInitialSitemap $controller, $query, Activation $activation, WP_Rocket_Mobile_Detect $mobile_detect, ClearCache $clear_cache, Queue $queue ) {
$this->options = $options;
$this->controller = $controller;
$this->query = $query;
$this->activation = $activation;
$this->mobile_detect = $mobile_detect;
$this->clear_cache = $clear_cache;
$this->queue = $queue;
}
/**
* Return an array of events that this subscriber listens to.
*
* @return array
*/
public static function get_subscribed_events() {
return [
'update_option_' . WP_ROCKET_SLUG => [
[ 'maybe_load_initial_sitemap', 10, 2 ],
[ 'maybe_cancel_preload', 10, 2 ],
],
'rocket_after_process_buffer' => 'update_cache_row',
'rocket_deactivation' => 'on_deactivation',
'rocket_reset_preload' => 'on_permalink_changed',
'permalink_structure_changed' => 'on_permalink_changed',
'rocket_domain_changed' => 'on_permalink_changed',
'wp_rocket_upgrade' => [ 'on_update', 16, 2 ],
'rocket_rucss_complete_job_status' => 'clean_url',
'rocket_rucss_after_clearing_usedcss' => [ 'clean_url', 20 ],
'rocket_after_automatic_cache_purge' => 'preload_after_automatic_cache_purge',
'after_rocket_clean_post' => [ 'clean_partial_cache', 10, 3 ],
'after_rocket_clean_term' => [ 'clean_partial_cache', 10, 3 ],
'after_rocket_clean_file' => 'clean_url',
'set_404' => 'delete_url_on_not_found',
'rocket_after_clean_terms' => 'clean_urls',
'after_rocket_clean_domain' => 'clean_full_cache',
'delete_post' => 'delete_post_preload_cache',
'pre_delete_term' => 'delete_term_preload_cache',
'rocket_preload_format_url' => 'format_preload_url',
'rocket_preload_lock_url' => 'lock_url',
'rocket_preload_unlock_url' => 'unlock_url',
'rocket_preload_unlock_all_urls' => 'unlock_all_urls',
'rocket_preload_exclude_urls' => [
[ 'add_preload_excluded_uri' ],
[ 'add_cache_reject_uri_to_excluded' ],
[ 'exclude_private_post_uri', 10, 2 ],
],
'rocket_rucss_after_clearing_failed_url' => [ 'clean_urls', 20 ],
'transition_post_status' => [ 'remove_private_post', 10, 3 ],
];
}
/**
* Load first tasks from preload when preload option is enabled.
*
* @param array $old_value old configuration values.
* @param array $value new configuration values.
* @return void
*/
public function maybe_load_initial_sitemap( $old_value, $value ) {
if ( ! isset( $value['manual_preload'], $old_value['manual_preload'] ) ) {
return;
}
if ( $value['manual_preload'] === $old_value['manual_preload'] ) {
return;
}
if ( ! $value['manual_preload'] ) {
return;
}
rocket_renew_box( 'preload_notice' );
$this->controller->load_initial_sitemap();
}
/**
* Cancel preload when configuration from sitemap changed.
*
* @param array $old_value old configuration values.
* @param array $value new configuration values.
* @return void
*/
public function maybe_cancel_preload( $old_value, $value ) {
if ( ! isset( $value['manual_preload'], $old_value['manual_preload'] ) ) {
return;
}
if ( $value['manual_preload'] === $old_value['manual_preload'] ) {
return;
}
if ( $value['manual_preload'] ) {
return;
}
$this->controller->cancel_preload();
}
/**
* Create or update the cache row after processing the buffer
*
* @return void
*/
public function update_cache_row() {
global $wp;
if ( is_user_logged_in() ) {
return;
}
$url = home_url( add_query_arg( [], $wp->request ) );
$detected = $this->mobile_detect->isMobile() && ! $this->mobile_detect->isTablet() ? 'mobile' : 'desktop';
/**
* Fires when the preload from an URL is completed.
*
* @param string $url URL preladed.
* @param string $device Device from the cache.
*/
do_action( 'rocket_preload_completed', $url, $detected );
if ( ! empty( (array) $_GET ) || ( $this->query->is_pending( $url ) && $this->options->get( 'do_caching_mobile_files', false ) ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
return;
}
if ( $this->is_excluded_by_filter( $url ) ) {
$this->query->delete_by_url( $url );
return;
}
$this->query->create_or_update(
[
'url' => $url,
'status' => 'completed',
'last_accessed' => true,
]
);
}
/**
* Delete url from the Preload when a 404 is risen.
*
* @return void
*/
public function delete_url_on_not_found() {
global $wp;
$url = home_url( $wp->request );
$this->query->delete_by_url( $url );
}
/**
* Preload on permalink changed.
*
* @return void
*/
public function on_permalink_changed() {
$this->query->remove_all();
$this->queue->cancel_pending_jobs();
$this->queue->add_job_preload_job_load_initial_sitemap_async();
}
/**
* Disable cron and jobs on update.
*
* @param string $new_version new version from the plugin.
* @param string $old_version old version from the plugin.
* @return void
*/
public function on_update( $new_version, $old_version ) {
$this->activation->clean_on_update( $new_version, $old_version );
if ( ! $this->options->get( 'manual_preload', false ) ) {
return;
}
$this->activation->refresh_on_update( $new_version, $old_version );
}
/**
* Clear preload on deactivation.
*
* @return void
*/
public function on_deactivation() {
$this->activation->deactivation();
}
/**
* Clean the url.
*
* @param string $url url.
* @return void
*/
public function clean_url( string $url ) {
$this->clear_cache->partial_clean( [ $url ] );
}
/**
* Preload after clearing full cache.
*
* @return void
*/
public function clean_full_cache() {
set_transient( 'wpr_preload_running', true );
$this->queue->add_job_preload_job_check_finished_async();
$this->clear_cache->full_clean();
}
/**
* Preload after clearing some cache.
*
* @param stdClass $object object modified.
* @param array $urls urls cleaned.
* @param string $lang lang from the website.
* @return void
*/
public function clean_partial_cache( $object, array $urls, $lang ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.objectFound
// Add Homepage URL to $purge_urls for preload.
$urls[] = get_rocket_i18n_home_url( $lang );
$urls = array_filter( $urls );
$this->clear_cache->partial_clean( $urls );
}
/**
* Clean the list of urls.
*
* @param array $urls urls.
* @return void
*/
public function clean_urls( array $urls ) {
$this->clear_cache->partial_clean( $urls );
}
/**
* Delete URL from a post from the preload.
*
* @param int $post_id ID from the post.
* @return void
*/
public function delete_post_preload_cache( $post_id ) {
if ( ! $this->options->get( 'manual_preload', 0 ) ) {
return;
}
$url = get_permalink( $post_id );
if ( empty( $url ) ) {
return;
}
$this->clear_cache->delete_url( $url );
}
/**
* Delete URL from a term from the preload.
*
* @param int $term_id ID from the term.
* @return void
*/
public function delete_term_preload_cache( $term_id ) {
if ( ! $this->options->get( 'manual_preload', 0 ) ) {
return;
}
$url = get_term_link( (int) $term_id );
if ( empty( $url ) ) {
return;
}
$this->clear_cache->delete_url( $url );
}
/**
* Pushes URLs to preload to the queue after cache directories are purged.
*
* @since 3.4
*
* @param array $deleted {
* An array of arrays, described like: {.
* @type string $home_url The home URL.
* @type string $home_path Path to home.
* @type bool $logged_in True if the home path corresponds to a logged in user’s folder.
* @type array $files A list of paths of files that have been deleted.
* }
* }
*/
public function preload_after_automatic_cache_purge( $deleted ) {
if ( ! $deleted || ! $this->options->get( 'manual_preload' ) ) {
return;
}
foreach ( $deleted as $data ) {
if ( $data['logged_in'] ) {
// Logged in user: no need to preload those since we would need the corresponding cookies.
continue;
}
foreach ( $data['files'] as $file_path ) {
if ( strpos( $file_path, '#' ) ) {
// URL with query string.
$file_path = preg_replace( '/#/', '?', $file_path, 1 );
} else {
$file_path = untrailingslashit( $file_path );
$data['home_path'] = untrailingslashit( $data['home_path'] );
$data['home_url'] = untrailingslashit( $data['home_url'] );
if ( '/' === substr( get_option( 'permalink_structure' ), -1 ) ) {
$file_path .= '/';
$data['home_path'] .= '/';
$data['home_url'] .= '/';
}
}
$this->clear_cache->partial_clean( [ str_replace( $data['home_path'], $data['home_url'], $file_path ) ] );
}
}
}
/**
* Remove index from url.
*
* @param string $url url to reformat.
*
* @return string
*/
public function format_preload_url( string $url ) {
return preg_replace( '/(index(-https)?\.html$)|(index(-https)?\.html_gzip$)/', '', $url );
}
/**
* Lock a URL.
*
* @param string $url URL to lock.
*
* @return void
*/
public function lock_url( string $url ) {
$this->query->lock( $url );
}
/**
* Unlock all URL.
*
* @return void
*/
public function unlock_all_urls() {
$this->query->unlock_all();
}
/**
* Unlock a URL.
*
* @param string $url URL to unlock.
*
* @return void
*/
public function unlock_url( string $url ) {
$this->query->unlock( $url );
}
/**
* Add the excluded uri from the preload to the filter.
*
* @param array $regexes regexes containing excluded uris.
* @return array|false
*/
public function add_preload_excluded_uri( $regexes ): array {
$preload_excluded_uri = (array) $this->options->get( 'preload_excluded_uri', [] );
if ( empty( $preload_excluded_uri ) ) {
return $regexes;
}
return array_merge( $regexes, $preload_excluded_uri );
}
/**
* Remove private post from cache.
*
* @param string $new_status New post status.
* @param string $old_status Old post status.
* @param WP_Post $post Wp post object.
* @return void
*/
public function remove_private_post( string $new_status, string $old_status, $post ) {
if ( $new_status === $old_status ) {
return;
}
if ( 'private' !== $new_status ) {
return;
}
$this->delete_post_preload_cache( $post->ID );
}
/**
* Get all private urls for public post types.
*
* @return array
*/
private function get_all_private_urls() {
static $private_urls;
if ( rocket_get_constant( 'WP_ROCKET_IS_TESTING', false ) ) {
$private_urls = null;
}
if ( isset( $private_urls ) ) {
return $private_urls;
}
$private_urls = [];
$public_post_types = get_post_types( [ 'public' => true ] );
unset( $public_post_types['attachment'] );
$arg = [
'post_type' => $public_post_types,
'post_status' => 'private',
'posts_per_page' => -1,
];
$query = new \WP_Query( $arg );
if ( ! $query->have_posts() ) {
return [];
}
foreach ( $query->posts as $post ) {
// Temporarily cast publish status to get pretty url.
$post->post_status = 'publish';
$private_post_url = get_permalink( $post );
$private_urls[ md5( $private_post_url ) ] = $private_post_url;
}
return $private_urls;
}
/**
* Exclude private urls.
*
* @param array $regexes regexes containing excluded uris.
* @param string $url Current url to test against.
* @return array
*/
public function exclude_private_post_uri( $regexes, $url ): array {
if ( ! is_array( $regexes ) ) {
$regexes = (array) $regexes;
}
$private_urls = $this->get_all_private_urls();
if ( empty( $private_urls ) ) {
return $regexes;
}
if ( ! isset( $private_urls[ md5( $url ) ] ) ) {
return $regexes;
}
$regexes[] = $url;
return $regexes;
}
}