PluginProbe
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel / 1.8.0
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel v1.8.0
1.8.0 1.7.1 1.7.0 1.6.0 1.5.2 trunk 0.1 0.2.0 0.2.1 0.3 0.3.1 0.3.2 0.3.3 0.3.4 0.4 0.4.1 0.4.2 0.4.3 1.0 1.1 1.2 1.3.1 1.4.0 1.4.1 1.5.0 All 27 releases
flywp / includes / StaticSite / Engine / Discovery / UrlDiscovery.php

UrlDiscovery.php in FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel 1.8.0, at includes/StaticSite/Engine/Discovery/UrlDiscovery.php

155 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FlyWP\StaticSite\Engine\Discovery;
4
5 /**
6 * Collects the start URLs of a full build from all sources, and the URLs that must never be published.
7 *
8 * - Multisite is out of scope. The engine builds the site of the current home URL.
9 *
10 * @since 1.8.0
11 */
12 class UrlDiscovery {
13
14 /**
15 * Rows in one query.
16 */
17 const BATCH_SIZE = 500;
18
19 /**
20 * @var WordPressGateway
21 */
22 private $wp;
23
24 /**
25 * @var string|null
26 */
27 private $origin_url;
28
29 /**
30 * @var UrlSource[]
31 */
32 private $sources;
33
34 /**
35 * @param WordPressGateway $wp Gateway.
36 * @param string|null $origin_url Origin URL that the engine fetches. Null to use the home URL.
37 * @param UrlSource[] $sources Sources, in order.
38 */
39 public function __construct( WordPressGateway $wp, $origin_url, array $sources ) {
40 $this->wp = $wp;
41 $this->origin_url = $origin_url;
42 $this->sources = $sources;
43 }
44
45 /**
46 * @param WordPressGateway $wp Gateway.
47 * @param string|null $origin_url Origin URL.
48 *
49 * @return UrlSource[]
50 */
51 public static function default_sources( WordPressGateway $wp, $origin_url ) {
52 return [
53 new HomeSource( $wp ),
54 new PostTypeSource( $wp ),
55 new TaxonomySource( $wp ),
56 new AuthorSource( $wp ),
57 new DateArchiveSource( $wp ),
58 new FeedSource( $wp ),
59 new SitemapSource( $wp ),
60 new SiteFileSource( $wp ),
61 new FilterSource( $wp, $origin_url ),
62 ];
63 }
64
65 /**
66 * URLs on the origin, unique by URL. URLs on other hosts are removed.
67 *
68 * @return \Generator<DiscoveredUrl>
69 */
70 public function urls() {
71 $origin = new OriginUrl( $this->wp->home_url(), $this->origin_url );
72 $seen = [];
73
74 foreach ( $this->sources as $source ) {
75 foreach ( $source->urls() as $item ) {
76 $url = $origin->rebase( $item->url );
77
78 if ( null === $url ) {
79 continue;
80 }
81
82 $key = md5( $url, true );
83
84 if ( isset( $seen[ $key ] ) ) {
85 continue;
86 }
87
88 $seen[ $key ] = true;
89 $item->url = $url;
90
91 yield $item;
92 }
93 }
94 }
95
96 /**
97 * URLs that must never be published, on the origin: password-protected published posts, and posts and terms
98 * that Yoast SEO or Rank Math marks as noindex. Queries run in batches.
99 *
100 * @return \Generator<string>
101 */
102 public function excluded_urls() {
103 $origin = new OriginUrl( $this->wp->home_url(), $this->origin_url );
104
105 foreach ( $this->excluded_links() as $link ) {
106 $url = $origin->rebase( $link );
107
108 if ( null !== $url ) {
109 yield $url;
110 }
111 }
112 }
113
114 /**
115 * @return \Generator<string|null>
116 */
117 private function excluded_links() {
118 $post_types = $this->wp->public_post_types();
119 $after_id = 0;
120
121 do {
122 $rows = $this->wp->post_rows( $post_types, $after_id, self::BATCH_SIZE );
123 $full_batch = count( $rows ) === self::BATCH_SIZE;
124 $noindex = RobotsMeta::noindex_post_ids( $this->wp->seo_robots_meta( array_column( $rows, 'id' ) ) );
125
126 foreach ( $rows as $row ) {
127 $after_id = $row['id'];
128
129 if ( PostTypeSource::is_excluded( $row, $noindex ) ) {
130 yield $this->wp->permalink( $row['id'] );
131 }
132 }
133 } while ( $full_batch );
134
135 foreach ( RobotsMeta::yoast_noindex_terms( $this->wp->seo_term_meta_option() ) as $term ) {
136 yield $this->wp->term_link( $term['id'], $term['taxonomy'] );
137 }
138
139 $after_id = 0;
140
141 do {
142 $rows = $this->wp->seo_term_robots_meta( $after_id, self::BATCH_SIZE );
143 $full_batch = count( $rows ) === self::BATCH_SIZE;
144
145 foreach ( $rows as $row ) {
146 $after_id = (int) $row['term_id'];
147
148 if ( RobotsMeta::is_rank_math_noindex( $row['meta_value'] ) ) {
149 yield $this->wp->term_link( $row['term_id'], $row['taxonomy'] );
150 }
151 }
152 } while ( $full_batch );
153 }
154 }
155