FetchSitemap.php
1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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 );
}
}
}