PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.7
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.7
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / helpers / indexable-helper.php

indexable-helper.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.7, at src/helpers/indexable-helper.php

262 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Helpers;
4
5 use Yoast\WP\SEO\Actions\Indexing\Indexable_Post_Indexation_Action;
6 use Yoast\WP\SEO\Actions\Indexing\Indexable_Post_Type_Archive_Indexation_Action;
7 use Yoast\WP\SEO\Actions\Indexing\Indexable_Term_Indexation_Action;
8 use Yoast\WP\SEO\Config\Indexing_Reasons;
9 use Yoast\WP\SEO\Models\Indexable;
10 use Yoast\WP\SEO\Repositories\Indexable_Repository;
11
12 /**
13 * A helper object for indexables.
14 */
15 class Indexable_Helper {
16
17 /**
18 * Represents the indexable repository.
19 *
20 * @var Indexable_Repository
21 */
22 protected $repository;
23
24 /**
25 * Represents the options helper.
26 *
27 * @var Options_Helper
28 */
29 protected $options_helper;
30
31 /**
32 * Represents the environment helper.
33 *
34 * @var Environment_Helper
35 */
36 protected $environment_helper;
37
38 /**
39 * Represents the indexing helper.
40 *
41 * @var Indexing_Helper
42 */
43 protected $indexing_helper;
44
45 /**
46 * Default values of certain columns.
47 *
48 * @var array
49 */
50 protected $default_values = [
51 'title' => [
52 'default_value' => null,
53 ],
54 'description' => [
55 'default_value' => null,
56 ],
57 'open_graph_title' => [
58 'default_value' => null,
59 ],
60 'open_graph_description' => [
61 'default_value' => null,
62 ],
63 'twitter_title' => [
64 'default_value' => null,
65 ],
66 'twitter_description' => [
67 'default_value' => null,
68 ],
69 'canonical' => [
70 'default_value' => null,
71 ],
72 'primary_focus_keyword' => [
73 'default_value' => null,
74 ],
75 'is_robots_noindex' => [
76 'default_value' => null,
77 ],
78 'is_robots_nofollow' => [
79 'default_value' => false,
80 ],
81 'is_robots_noarchive' => [
82 'default_value' => null,
83 ],
84 'is_robots_noimageindex' => [
85 'default_value' => null,
86 ],
87 'is_robots_nosnippet' => [
88 'default_value' => null,
89 ],
90 ];
91
92 /**
93 * Indexable_Helper constructor.
94 *
95 * @param Options_Helper $options_helper The options helper.
96 * @param Environment_Helper $environment_helper The environment helper.
97 * @param Indexing_Helper $indexing_helper The indexing helper.
98 */
99 public function __construct( Options_Helper $options_helper, Environment_Helper $environment_helper, Indexing_Helper $indexing_helper ) {
100 $this->options_helper = $options_helper;
101 $this->environment_helper = $environment_helper;
102 $this->indexing_helper = $indexing_helper;
103 }
104
105 /**
106 * Sets the indexable repository. Done to avoid circular dependencies.
107 *
108 * @required
109 *
110 * @param Indexable_Repository $repository The indexable repository.
111 */
112 public function set_indexable_repository( Indexable_Repository $repository ) {
113 $this->repository = $repository;
114 }
115
116 /**
117 * Returns the page type of an indexable.
118 *
119 * @param Indexable $indexable The indexable.
120 *
121 * @return string|false The page type. False if it could not be determined.
122 */
123 public function get_page_type_for_indexable( $indexable ) {
124 switch ( $indexable->object_type ) {
125 case 'post':
126 $front_page_id = (int) \get_option( 'page_on_front' );
127 if ( $indexable->object_id === $front_page_id ) {
128 return 'Static_Home_Page';
129 }
130 $posts_page_id = (int) \get_option( 'page_for_posts' );
131 if ( $indexable->object_id === $posts_page_id ) {
132 return 'Static_Posts_Page';
133 }
134
135 return 'Post_Type';
136 case 'term':
137 return 'Term_Archive';
138 case 'user':
139 return 'Author_Archive';
140 case 'home-page':
141 return 'Home_Page';
142 case 'post-type-archive':
143 return 'Post_Type_Archive';
144 case 'date-archive':
145 return 'Date_Archive';
146 case 'system-page':
147 if ( $indexable->object_sub_type === 'search-result' ) {
148 return 'Search_Result_Page';
149 }
150 if ( $indexable->object_sub_type === '404' ) {
151 return 'Error_Page';
152 }
153 }
154
155 return false;
156 }
157
158 /**
159 * Resets the permalinks of the indexables.
160 *
161 * @param string|null $type The type of the indexable.
162 * @param string|null $subtype The subtype. Can be null.
163 * @param string $reason The reason that the permalink has been changed.
164 */
165 public function reset_permalink_indexables( $type = null, $subtype = null, $reason = Indexing_Reasons::REASON_PERMALINK_SETTINGS ) {
166 $result = $this->repository->reset_permalink( $type, $subtype );
167
168 $this->indexing_helper->set_reason( $reason );
169
170 if ( $result !== false && $result > 0 ) {
171 \delete_transient( Indexable_Post_Indexation_Action::UNINDEXED_COUNT_TRANSIENT );
172 \delete_transient( Indexable_Post_Type_Archive_Indexation_Action::UNINDEXED_COUNT_TRANSIENT );
173 \delete_transient( Indexable_Term_Indexation_Action::UNINDEXED_COUNT_TRANSIENT );
174 }
175 }
176
177 /**
178 * Determines whether indexing indexables is appropriate at this time.
179 *
180 * @return bool Whether the indexables should be indexed.
181 */
182 public function should_index_indexables() {
183 // Currently, the only reason to index is when we're on a production website.
184 $should_index = $this->environment_helper->is_production_mode();
185
186 /**
187 * Filter: 'Yoast\WP\SEO\should_index_indexables' - Allow developers to enable / disable
188 * creating indexables. Warning: overriding
189 * the intended action may cause problems when moving from a staging to a
190 * production environment because indexable permalinks may get set incorrectly.
191 *
192 * @since 18.2
193 *
194 * @param bool $should_index Whether the site's indexables should be created.
195 */
196 return (bool) \apply_filters( 'Yoast\WP\SEO\should_index_indexables', $should_index );
197 }
198
199 /**
200 * Returns whether or not dynamic permalinks should be used.
201 *
202 * @return bool Whether or not the dynamic permalinks should be used.
203 */
204 public function dynamic_permalinks_enabled() {
205 /**
206 * Filters the value of the `dynamic_permalinks` option.
207 *
208 * @param bool $value The value of the `dynamic_permalinks` option.
209 */
210 return (bool) \apply_filters( 'wpseo_dynamic_permalinks_enabled', $this->options_helper->get( 'dynamic_permalinks', false ) );
211 }
212
213 /**
214 * Sets a boolean to indicate that the indexing of the indexables has completed.
215 *
216 * @return void
217 */
218 public function finish_indexing() {
219 $this->options_helper->set( 'indexables_indexing_completed', true );
220 }
221
222 /**
223 * Checks whether the indexable has default values in given fields.
224 *
225 * @param Indexable $indexable The Yoast indexable that we're checking.
226 * @param array $fields The Yoast indexable fields that we're checking against.
227 *
228 * @return bool Whether the indexable has default values.
229 */
230 public function check_if_default_indexable( $indexable, $fields ) {
231 foreach ( $fields as $field ) {
232 $is_default = $this->check_if_default_field( $indexable, $field );
233 if ( ! $is_default ) {
234 break;
235 }
236 }
237
238 return $is_default;
239 }
240
241 /**
242 * Checks if an indexable field contains the default value.
243 *
244 * @param Indexable $indexable The Yoast indexable that we're checking.
245 * @param string $field The field that we're checking.
246 *
247 * @return bool True if default value.
248 */
249 public function check_if_default_field( $indexable, $field ) {
250 $defaults = $this->default_values;
251 if ( ! isset( $defaults[ $field ] ) ) {
252 return false;
253 }
254
255 if ( $indexable->$field === $defaults[ $field ]['default_value'] ) {
256 return true;
257 }
258
259 return false;
260 }
261 }
262