LoadInitialSitemap.php
3.91 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
}
}