Robots.php
10.5 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
namespace AIOSEO\Plugin\Common\Meta;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles the robots meta tag.
*
* @since 4.0.0
*/
class Robots {
/**
* The robots meta tag attributes.
*
* We'll already set the keys on construction so that we always output the attributes in the same order.
*
* @since 4.0.0
*
* @var array
*/
protected $attributes = [
'noindex' => '',
'nofollow' => '',
'noarchive' => '',
'nosnippet' => '',
'noimageindex' => '',
'noodp' => '',
'notranslate' => '',
'max-snippet' => '',
'max-image-preview' => '',
'max-video-preview' => ''
];
/**
* Class constructor.
*
* @since 4.0.16
*/
public function __construct() {
add_action( 'wp_loaded', [ $this, 'unregisterWooCommerceNoindex' ] );
add_action( 'template_redirect', [ $this, 'noindexFeed' ] );
add_action( 'wp_head', [ $this, 'disableWpRobotsCore' ], -1 );
}
/**
* Prevents WooCommerce from noindexing the Cart/Checkout pages.
*
* @since 4.1.3
*
* @return void
*/
public function unregisterWooCommerceNoindex() {
if ( has_action( 'wp_head', 'wc_page_noindex' ) ) {
remove_action( 'wp_head', 'wc_page_noindex' );
}
}
/**
* Prevents WP Core from outputting its own robots meta tag.
*
* @since 4.0.16
*
* @return void
*/
public function disableWpRobotsCore() {
remove_all_filters( 'wp_robots' );
}
/**
* Noindexes RSS feed pages.
*
* @since 4.0.17
*
* @return void
*/
public function noindexFeed() {
if (
! is_feed() ||
( ! aioseo()->options->searchAppearance->advanced->globalRobotsMeta->default && ! aioseo()->options->searchAppearance->advanced->globalRobotsMeta->noindexFeed )
) {
return;
}
header( 'X-Robots-Tag: noindex, follow', true );
}
/**
* Returns the robots meta tag value.
*
* @since 4.0.0
*
* @return mixed The robots meta tag value or false.
*/
public function meta() {
if ( is_category() || is_tag() || is_tax() ) {
$this->term();
return $this->metaHelper();
}
if ( is_home() && 'posts' === get_option( 'show_on_front' ) ) {
$this->globalValues();
return $this->metaHelper();
}
$post = aioseo()->helpers->getPost();
if ( $post ) {
$this->post();
return $this->metaHelper();
}
if ( is_author() ) {
$this->globalValues( [ 'archives', 'author' ] );
return $this->metaHelper();
}
if ( is_date() ) {
$this->globalValues( [ 'archives', 'date' ] );
return $this->metaHelper();
}
if ( is_search() ) {
$this->globalValues( [ 'archives', 'search' ] );
return $this->metaHelper();
}
if ( is_404() ) {
return apply_filters( 'aioseo_404_robots', 'noindex' );
}
if ( is_archive() ) {
$this->archives();
return $this->metaHelper();
}
}
/**
* Stringifies and filters the robots meta tag value.
*
* Acts as a helper for meta().
*
* @since 4.0.0
*
* @param bool $array Whether or not to return the value as an array.
* @return array|string The robots meta tag value.
*/
public function metaHelper( $array = false ) {
$pageNumber = aioseo()->helpers->getPageNumber();
if ( 1 < $pageNumber || aioseo()->helpers->getCommentPageNumber() ) {
if (
aioseo()->options->searchAppearance->advanced->globalRobotsMeta->default ||
aioseo()->options->searchAppearance->advanced->globalRobotsMeta->noindexPaginated
) {
$this->attributes['noindex'] = 'noindex';
}
if (
aioseo()->options->searchAppearance->advanced->globalRobotsMeta->default ||
aioseo()->options->searchAppearance->advanced->globalRobotsMeta->nofollowPaginated
) {
$this->attributes['nofollow'] = 'nofollow';
}
}
// Never allow users to noindex the first page of the homepage.
if ( is_front_page() && 1 === $pageNumber ) {
$this->attributes['noindex'] = '';
}
// Because we prevent WordPress Core from outputting a robots tag in disableWpRobotsCore(), we need to noindex/nofollow non-public sites ourselves.
if ( ! get_option( 'blog_public' ) ) {
$this->attributes['noindex'] = 'noindex';
$this->attributes['nofollow'] = 'nofollow';
}
$this->attributes = array_filter( apply_filters( 'aioseo_robots_meta', $this->attributes ) );
return $array ? $this->attributes : implode( ', ', $this->attributes );
}
/**
* Sets the attributes for the current post.
*
* @since 4.0.0
*
* @param \WP_Post|null $post The post object.
* @return void
*/
public function post( $post = null ) {
$dynamicOptions = aioseo()->dynamicOptions->noConflict();
$post = aioseo()->helpers->getPost( $post );
$metaData = aioseo()->meta->metaData->getMetaData( $post );
if ( ! empty( $metaData ) && ! $metaData->robots_default ) {
$this->metaValues( $metaData );
return;
}
if ( $dynamicOptions->searchAppearance->postTypes->has( $post->post_type ) ) {
$this->globalValues( [ 'postTypes', $post->post_type ], true );
}
}
/**
* Returns the robots meta tag value for the current term.
*
* @since 4.0.6
*
* @param \WP_Term|null $term The term object if any.
* @return void
*/
public function term( $term = null ) {
$dynamicOptions = aioseo()->dynamicOptions->noConflict();
$term = is_a( $term, 'WP_Term' ) ? $term : get_queried_object();
if ( $dynamicOptions->searchAppearance->taxonomies->has( $term->taxonomy ) ) {
$this->globalValues( [ 'taxonomies', $term->taxonomy ], true );
return;
}
$this->globalValues();
}
/**
* Sets the attributes for the current archive.
*
* @since 4.0.0
*
* @return void
*/
private function archives() {
$dynamicOptions = aioseo()->dynamicOptions->noConflict();
$postType = get_queried_object();
if ( ! empty( $postType->name ) && $dynamicOptions->searchAppearance->archives->has( $postType->name ) ) {
$this->globalValues( [ 'archives', $postType->name ], true );
}
}
/**
* Sets the attributes based on the global values.
*
* @since 4.0.0
*
* @param array $optionOrder The order in which the options need to be called to get the relevant robots meta settings.
* @param boolean $isDynamicOption Whether this is for a dynamic option.
* @return void
*/
protected function globalValues( $optionOrder = [], $isDynamicOption = false ) {
$robotsMeta = [];
if ( count( $optionOrder ) ) {
$options = $isDynamicOption
? aioseo()->dynamicOptions->noConflict( true )->searchAppearance
: aioseo()->options->noConflict()->searchAppearance;
foreach ( $optionOrder as $option ) {
if ( ! $options->has( $option, false ) ) {
return;
}
$options = $options->$option;
}
$clonedOptions = clone $options;
if ( ! $clonedOptions->show ) {
$this->attributes['noindex'] = 'noindex';
}
$robotsMeta = $options->advanced->robotsMeta->all();
if ( $robotsMeta['default'] ) {
$robotsMeta = aioseo()->options->searchAppearance->advanced->globalRobotsMeta->all();
}
} else {
$robotsMeta = aioseo()->options->searchAppearance->advanced->globalRobotsMeta->all();
}
$this->attributes['max-image-preview'] = 'max-image-preview:large';
if ( $robotsMeta['default'] ) {
return;
}
if ( $robotsMeta['noindex'] ) {
$this->attributes['noindex'] = 'noindex';
}
if ( $robotsMeta['nofollow'] ) {
$this->attributes['nofollow'] = 'nofollow';
}
if ( $robotsMeta['noarchive'] ) {
$this->attributes['noarchive'] = 'noarchive';
}
$noSnippet = $robotsMeta['nosnippet'];
if ( $noSnippet ) {
$this->attributes['nosnippet'] = 'nosnippet';
}
if ( $robotsMeta['noodp'] ) {
$this->attributes['noodp'] = 'noodp';
}
if ( $robotsMeta['notranslate'] ) {
$this->attributes['notranslate'] = 'notranslate';
}
$maxSnippet = $robotsMeta['maxSnippet'];
if ( ! $noSnippet && $maxSnippet && intval( $maxSnippet ) ) {
$this->attributes['max-snippet'] = "max-snippet:$maxSnippet";
}
$maxImagePreview = $robotsMeta['maxImagePreview'];
$noImageIndex = $robotsMeta['noimageindex'];
if ( ! $noImageIndex && $maxImagePreview && in_array( $maxImagePreview, [ 'none', 'standard', 'large' ], true ) ) {
$this->attributes['max-image-preview'] = "max-image-preview:$maxImagePreview";
}
$maxVideoPreview = $robotsMeta['maxVideoPreview'];
if ( $maxVideoPreview && intval( $maxVideoPreview ) ) {
$this->attributes['max-video-preview'] = "max-video-preview:$maxVideoPreview";
}
// Check this last so that we can prevent max-image-preview from being output if noimageindex is enabled.
if ( $noImageIndex ) {
$this->attributes['max-image-preview'] = '';
$this->attributes['noimageindex'] = 'noimageindex';
}
}
/**
* Sets the attributes from the meta data.
*
* @since 4.0.0
*
* @param array $metaData The post/term meta data.
* @return void
*/
protected function metaValues( $metaData ) {
if ( $metaData->robots_noindex || $this->isPasswordProtected() ) {
$this->attributes['noindex'] = 'noindex';
}
if ( $metaData->robots_nofollow ) {
$this->attributes['nofollow'] = 'nofollow';
}
if ( $metaData->robots_noarchive ) {
$this->attributes['noarchive'] = 'noarchive';
}
if ( $metaData->robots_nosnippet ) {
$this->attributes['nosnippet'] = 'nosnippet';
}
if ( $metaData->robots_noodp ) {
$this->attributes['noodp'] = 'noodp';
}
if ( $metaData->robots_notranslate ) {
$this->attributes['notranslate'] = 'notranslate';
}
if ( ! $metaData->robots_nosnippet && $metaData->robots_max_snippet && intval( $metaData->robots_max_snippet ) ) {
$this->attributes['max-snippet'] = "max-snippet:$metaData->robots_max_snippet";
}
if ( ! $metaData->robots_noimageindex && $metaData->robots_max_imagepreview && in_array( $metaData->robots_max_imagepreview, [ 'none', 'standard', 'large' ], true ) ) {
$this->attributes['max-image-preview'] = "max-image-preview:$metaData->robots_max_imagepreview";
}
if ( $metaData->robots_max_videopreview && intval( $metaData->robots_max_videopreview ) ) {
$this->attributes['max-video-preview'] = "max-video-preview:$metaData->robots_max_videopreview";
}
// Check this last so that we can prevent max-image-preview from being output if noimageindex is enabled.
if ( $metaData->robots_noimageindex ) {
$this->attributes['max-image-preview'] = '';
$this->attributes['noimageindex'] = 'noimageindex';
}
}
/**
* Checks whether the current post is password protected.
*
* @since 4.0.0
*
* @return bool Whether the post is password protected.
*/
private function isPasswordProtected() {
$post = aioseo()->helpers->getPost();
return is_object( $post ) && $post->post_password;
}
}