This commit is contained in:
2026-10-06 18:51:23 -06:00
parent 8424dacd03
commit e2c26b7f21
4223 changed files with 737986 additions and 53 deletions
@@ -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();
}
}