-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathclass-wp-seo.php
More file actions
680 lines (603 loc) · 19.8 KB
/
class-wp-seo.php
File metadata and controls
680 lines (603 loc) · 19.8 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
<?php
/**
* Class file for WP_SEO.
*
* @package WP_SEO
*/
if ( ! class_exists( 'WP_SEO' ) ) :
/**
* WP SEO core functionality.
*/
class WP_SEO {
/**
* Instance of this class.
*
* @var object
*/
private static $instance = null;
/**
* Associative array of WP_SEO_Formatting_Tag instances.
*
* @var array.
*/
public $formatting_tags = array();
/**
* The regular expression used to find formatting tags.
*
* @var string.
*/
public $formatting_tag_pattern = '';
/**
* Unused.
*
* @codeCoverageIgnore
*/
private function __construct() {
// Don't do anything, needs to be initialized via instance() method.
}
/**
* Unused.
*
* @codeCoverageIgnore
*/
public function __clone() {
wp_die( esc_html__( "Please don't __clone WP_SEO", 'wp-seo' ) );
}
/**
* Unused.
*
* @codeCoverageIgnore
*/
public function __wakeup() {
wp_die( esc_html__( "Please don't __wakeup WP_SEO", 'wp-seo' ) );
}
/**
* Get the instance of this class.
*
* @codeCoverageIgnore
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new WP_SEO;
self::$instance->setup();
}
return self::$instance;
}
/**
* Add actions and filters.
*
* @codeCoverageIgnore
*/
protected function setup() {
add_action( 'wp_loaded', array( $this, 'set_properties' ) );
if ( is_admin() ) {
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_post_fields' ) );
add_action( 'edit_attachment', array( $this, 'save_post_fields' ) );
add_action( 'add_attachment', array( $this, 'save_post_fields' ) );
add_action( 'admin_init', array( $this, 'add_term_boxes' ) );
}
add_filter( 'pre_get_document_title', array( $this, 'pre_get_document_title' ), 20 );
add_filter( 'wp_title', array( $this, 'wp_title' ), 20, 2 );
add_filter( 'wp_head', array( $this, 'wp_head' ), 5 );
add_filter( 'robots_txt', array( $this, 'robots_txt' ) );
}
/**
* Set class properties.
*
* The filter for adding custom formatting tags is applied here.
*/
public function set_properties() {
$tags = array();
/**
* Filter the available formatting tags.
*
* @see wp_seo_default_formatting_tags() for an example implementation.
*
* @param array $tags Associative array of WP_SEO_Formatting_Tag instances.
*/
foreach ( apply_filters( 'wp_seo_formatting_tags', $tags ) as $id => $tag ) {
if ( is_a( $tag, 'WP_SEO_Formatting_Tag' ) ) {
$tags[ $id ] = $tag;
}
}
$this->formatting_tags = $tags;
/**
* Filter the regular expression used to find formatting tags.
*
* You might need this if you want to add unusual custom tags.
*
* @param string WP_SEO::formatting_tag_pattern The regex.
*/
$this->formatting_tag_pattern = apply_filters( 'wp_seo_formatting_tag_pattern', '/#[a-zA-Z\_]+#/' );
}
/**
* Get the WP SEO term option value for a given term.
*
* @param int $term_id Term ID.
* @param string $taxonomy Term taxonomy.
* @return mixed The get_option() return value for the given term data.
*/
public function get_term_option( $term_id, $taxonomy ) {
$option_name = '';
$term = get_term( $term_id, $taxonomy );
if ( $term instanceof \WP_Term ) {
$option_name = $this->get_term_option_name( $term );
}
return get_option( $option_name );
}
/**
* Intersect data with the default array of WP SEO term option values.
*
* @param array $option_value Array of term SEO option values, if any.
* @return array Option values with default keys and values.
*/
public function intersect_term_option( $option_value ) {
return wp_seo_intersect_args( $option_value, array(
'title' => '',
'description' => '',
'keywords' => '',
) );
}
/**
* Add the SEO fields to post types that support them.
*
* @param string $post_type The current post type.
*/
public function add_meta_boxes( $post_type ) {
if ( WP_SEO_Settings()->has_post_fields( $post_type ) ) {
add_meta_box(
'wp_seo',
wp_seo_get_box_title(),
array( $this, 'post_meta_fields' ),
$post_type,
/**
* Filter the screen context where the fields should display.
*
* @param string @see add_meta_box().
*/
apply_filters( 'wp_seo_meta_box_context', 'normal' ),
/**
* Filter the display priority of the fields within the context.
*
* @param string @see add_meta_box().
*/
apply_filters( 'wp_seo_meta_box_priority', 'high' )
);
}
}
/**
* Display the SEO fields for a post.
*
* @param WP_Post $post The post being edited.
*/
public function post_meta_fields( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'wp-seo-nonce' );
/**
* Fires during the WP SEO post meta box display callback.
*
* @param WP_Post $post The post passed to the meta box display callback.
*/
do_action( 'wp_seo_post_meta_fields', $post );
}
/**
* Save the SEO values as post meta.
*
* @param int $post_id The post ID being edited.
*/
public function save_post_fields( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! isset( $_POST['wp-seo-nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wp-seo-nonce'] ) ), plugin_basename( __FILE__ ) ) ) {
return;
}
if ( ! isset( $_POST['post_type'] ) ) {
return;
}
$post_type = sanitize_text_field( wp_unslash( $_POST['post_type'] ) );
if ( ! WP_SEO_Settings()->has_post_fields( $post_type ) ) {
return;
}
$post_type_obj = get_post_type_object( $post_type );
if ( empty( $post_type_obj->cap->edit_post ) || ! current_user_can( $post_type_obj->cap->edit_post, $post_id ) ) {
return;
}
if ( ! isset( $_POST['post_ID'] ) ) {
return;
}
$post_id = absint( $_POST['post_ID'] );
if ( ! isset( $_POST['seo_meta'] ) ) {
$_POST['seo_meta'] = array();
}
/**
* Filter the fields that can be saved.
*
* @param array $fields Array of field names that can be saved to the post meta.
*/
$fields = (array) apply_filters( 'wp_seo_saveable_fields', array( 'title', 'description', 'keywords' ) );
foreach ( $fields as $field ) {
$data = isset( $_POST['seo_meta'][ $field ] ) ? sanitize_text_field( wp_unslash( $_POST['seo_meta'][ $field ] ) ) : '';
update_post_meta( $post_id, wp_slash( '_meta_' . $field ), wp_slash( $data ) );
}
}
/**
* Add the meta box to taxonomies with per-term fields enabled.
*/
public function add_term_boxes() {
foreach ( WP_SEO_Settings()->get_enabled_taxonomies() as $slug ) {
add_action( $slug . '_add_form_fields', array( $this, 'add_term_meta_fields' ) );
add_action( $slug . '_edit_form', array( $this, 'edit_term_meta_fields' ), 10, 2 );
}
add_action( 'created_term', array( $this, 'save_term_fields' ), 10, 3 );
add_action( 'edited_term', array( $this, 'save_term_fields' ), 10, 3 );
}
/**
* Helper to construct an option name for per-term SEO fields.
*
* @param WP_Term $term The term object.
* @return string The option name
*/
public function get_term_option_name( $term ) {
return "wp-seo-term-{$term->term_taxonomy_id}";
}
/**
* Display the SEO fields for adding a term.
*
* @param string $taxonomy The taxonomy slug.
*/
public function add_term_meta_fields( $taxonomy ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'wp-seo-nonce' );
/**
* Fires during the WP SEO add-term fields display callback.
*
* @param string $taxonomy The taxonomy slug.
*/
do_action( 'wp_seo_add_term_meta_fields', $taxonomy );
}
/**
* Display the SEO fields for editing a term.
*
* @param WP_Term $tag The term object.
* @param string $taxonomy The taxonomy slug.
*/
public function edit_term_meta_fields( $tag, $taxonomy ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'wp-seo-nonce' );
/**
* Fires during the WP SEO edit-term fields display callback.
*
* @param WP_Term $tag The term object.
* @param string $taxonomy The taxonomy slug.
*/
do_action( 'wp_seo_edit_term_meta_fields', $tag, $taxonomy );
}
/**
* Save the SEO term values as an option.
*
* @see wp_unslash(), which the Settings API and update_post_meta()
* otherwise handle.
*
* @param int $term_id Term ID.
* @param int $tt_id Term taxonomy ID.
* @param string $taxonomy Taxonomy slug.
*/
public function save_term_fields( $term_id, $tt_id, $taxonomy ) {
if ( ! isset( $_POST['taxonomy'] ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! WP_SEO_Settings()->has_term_fields( $taxonomy ) ) {
return;
}
$object = get_taxonomy( $taxonomy );
if ( empty( $object->cap->edit_terms ) || ! current_user_can( $object->cap->edit_terms ) ) {
return;
}
if ( ! isset( $_POST['wp-seo-nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wp-seo-nonce'] ) ), plugin_basename( __FILE__ ) ) ) {
return;
}
if ( ! isset( $_POST['seo_meta'] ) ) {
$_POST['seo_meta'] = array();
}
foreach ( array( 'title', 'description', 'keywords' ) as $field ) {
$data[ $field ] = isset( $_POST['seo_meta'][ $field ] ) ? sanitize_text_field( wp_unslash( $_POST['seo_meta'][ $field ] ) ) : '';
}
$name = $this->get_term_option_name( get_term( $term_id, $taxonomy ) );
if ( false === get_option( $name ) ) {
// Don't create an option unless at least one field exists.
$filtered_data = array_filter( $data );
if ( ! empty( $filtered_data ) ) {
add_option( $name, $data, null, false );
}
} else {
update_option( $name, $data );
}
}
/**
* Replace formatting tags in a string with their value for the current page.
*
* @param string $string The string with formatting tags.
* @return string|WP_Error The formatted string, or WP_Error on error.
*/
public function format( $string ) {
if ( ! is_string( $string ) ) {
return new WP_Error( 'format_error', __( "Please don't try to format() a non-string.", 'wp-seo' ) );
}
$raw_string = $string;
$matches = wp_seo_match_all_formatting_tags( $string );
if ( empty( $matches ) ) {
return $string;
}
$replacements = array();
$unique_matches = array_unique( $matches );
foreach ( $this->formatting_tags as $id => $tag ) {
if ( ! empty( $tag->tag ) && in_array( $tag->tag, $unique_matches ) ) {
/**
* Filter the value of a formatting tag for the current page.
*
* The dynamic portion of the hook name, $id, refers to the key
* used to register the tag in WP_SEO::set_properties(). For
* example, the hook for the default "#site_name#" formatting
* tag is 'wp_seo_format_site_name'.
*
* @see wp_seo_default_formatting_tags() for the defaults' keys.
*
* @param string The value returned by the formatting tag.
*/
$replacements[ $tag->tag ] = apply_filters( "wp_seo_format_{$id}", $tag->get_value() );
}
}
if ( ! empty( $replacements ) ) {
$string = str_replace( array_keys( $replacements ), array_values( $replacements ), $string );
}
/**
* Filter the formatted string.
*
* @param string $string The formatted string.
* @param string $raw_string The string as submitted.
*/
return apply_filters( 'wp_seo_after_format_string', $string, $raw_string );
}
/**
* Filter the page title with the custom title or formatted title.
*
* @param string $title The page title from wp_title().
* @param string $sep The title separator.
* @return string The custom title of the current entry, if one
* exists, or the formatted title from the settings
* for the current post type. The original title if no
* custom or formatted title exists.
*/
public function wp_title( $title, $sep ) {
if ( is_singular() ) {
if ( WP_SEO_Settings()->has_post_fields( $post_type = get_post_type() ) && $meta_title = get_post_meta( get_the_ID(), '_meta_title', true ) ) {
return $meta_title;
} else {
$key = "single_{$post_type}_title";
}
} elseif ( is_front_page() ) {
$key = 'home_title';
} elseif ( is_author() ) {
$key = 'archive_author_title';
} elseif ( is_category() || is_tag() || is_tax() ) {
if ( ( WP_SEO_Settings()->has_term_fields( $taxonomy = get_queried_object()->taxonomy ) ) && ( $option = get_option( $this->get_term_option_name( get_queried_object() ) ) ) && ( ! empty( $option['title'] ) ) ) {
return $option['title'];
} else {
$key = "archive_{$taxonomy}_title";
}
} elseif ( is_post_type_archive() ) {
$key = 'archive_' . get_queried_object()->name . '_title';
} elseif ( is_date() ) {
$key = 'archive_date_title';
} elseif ( is_search() ) {
$key = 'search_title';
} elseif ( is_404() ) {
$key = '404_title';
} else {
$key = false;
}
$title_string = null;
if ( $key ) {
$title_string = WP_SEO_Settings()->get_option( $key );
}
/**
* Filter the format string of the title tag for the current page.
*
* @param string $title_string The format string retrieved from the settings.
* @param string $key The key of the setting retrieved.
*/
$title_string = apply_filters( 'wp_seo_title_tag_format', $title_string, $key );
// Format the title string if set.
if ( ! empty( $title_string ) ) {
$title_tag = $this->format( $title_string );
if ( $title_tag && ! is_wp_error( $title_tag ) ) {
$title = $title_tag;
}
}
return $title;
}
/**
* Filter the document title before it is generated.
*
* @param string $title The document title. Default empty string.
* @return string The custom title, if any, or the received $title if none exists.
*/
public function pre_get_document_title( $title ) {
// We can lean on the logic already in WP_SEO::wp_title().
$custom = $this->wp_title( $title, '' );
if ( $custom ) {
$title = $custom;
}
return $title;
}
/**
* Render a <meta /> field.
*
* @access private.
*
* @since 0.12.0 An HTML comment was added to the output.
*
* @param string $name The value of the "name" attribute.
* @param string $content The value of the "content" attribute.
*/
private function meta_field( $name, $content ) {
/**
* Filters the "content" attribute value of the <meta /> field.
*
* @since 0.13.0
*
* @param string $content The "content" attribute value.
* @param string $name The "name" attribute value.
*/
$content = apply_filters( 'wp_seo_meta_field_content', $content, $name );
if ( ! is_string( $name ) || ! is_string( $content ) ) {
return;
}
echo "<meta name='" . esc_attr( $name ) . "' content='" . esc_attr( $content ) . "' /><!-- WP SEO -->\n";
}
/**
* Determine the <meta> values for the current page.
*
* Unlike WP_SEO::wp_title(), custom per-entry and per-term values are not
* returned immediately but rendered at the end of the method.
*
* @see WP_SEO::meta_field() for detail on how the values are rendered.
*/
public function wp_head() {
if ( is_singular() ) {
if ( WP_SEO_Settings()->has_post_fields( $post_type = get_post_type() ) ) {
$meta_description = get_post_meta( get_the_ID(), '_meta_description', true );
$meta_keywords = get_post_meta( get_the_ID(), '_meta_keywords', true );
}
$key = "single_{$post_type}";
} elseif ( is_front_page() ) {
$key = 'home';
} elseif ( is_author() ) {
$key = 'archive_author';
} elseif ( is_category() || is_tag() || is_tax() ) {
if ( WP_SEO_Settings()->has_term_fields( $taxonomy = get_queried_object()->taxonomy ) && $option = get_option( $this->get_term_option_name( get_queried_object() ) ) ) {
$meta_description = $option['description'];
$meta_keywords = $option['keywords'];
}
$key = "archive_{$taxonomy}";
} elseif ( is_post_type_archive() ) {
$key = 'archive_' . get_queried_object()->name;
} elseif ( is_date() ) {
$key = 'archive_date';
} else {
$key = false;
}
if ( empty( $meta_description ) ) {
/**
* Filter the format string of the meta description for this page.
*
* @param string The format string retrieved from the settings.
* @param string $key The key of the setting retrieved.
*/
$description_string = apply_filters(
'wp_seo_meta_description_format',
! empty( $key ) ? WP_SEO_Settings()->get_option( "{$key}_description" ) : '',
$key
);
$meta_description = $this->format( $description_string );
}
if ( ! empty( $meta_description ) && ! is_wp_error( $meta_description ) ) {
$this->meta_field( 'description', $meta_description );
}
if ( empty( $meta_keywords ) ) {
/**
* Filter the format string of the meta keywords for this page.
*
* @param string The format string retrieved from the settings.
* @param string $key The key of the setting retrieved.
*/
$keywords_string = apply_filters(
'wp_seo_meta_keywords_format',
! empty( $key ) ? WP_SEO_Settings()->get_option( "{$key}_keywords" ) : '',
$key
);
$meta_keywords = $this->format( $keywords_string );
}
if ( ! empty( $meta_keywords ) && ! is_wp_error( $meta_keywords ) ) {
$this->meta_field( 'keywords', $meta_keywords );
}
/**
* Filter the artibrary meta tags that display on this page.
*
* @param array {
* Meta tag data.
*
* @type string $name The field "name" attribute.
* @type string $content The field "content" attribute.
* }
*/
$arbitrary_tags = apply_filters( 'wp_seo_arbitrary_tags', WP_SEO_Settings()->get_option( 'arbitrary_tags' ) );
if ( is_array( $arbitrary_tags ) ) {
foreach ( $arbitrary_tags as $tag ) {
$this->meta_field( $tag['name'], $this->format( $tag['content'] ) );
}
}
}
/**
* Build the prefix and suffix for the Robots.txt file, and return it.
*
* @param string $robots The robots.txt file contents.
* @return string
*/
public function robots_txt( string $robots ): string {
/**
* Filters the network-level Robots.txt Prefix value for WP SEO.
*
* @param string $prefix The robots.txt network prefix, added in the WP SEO network settings page.
*/
$robots_network_prefix = apply_filters( 'wp_seo_robots_txt_network_prefix', WP_SEO_Settings()->get_network_option( 'robots_txt_network_prefix', '' ) );
/**
* Filters the network-level Robots.txt Suffix value for WP SEO.
*
* @param string $suffix The robots.txt network suffix, added in the WP SEO network settings page.
*/
$robots_network_suffix = apply_filters( 'wp_seo_robots_txt_network_suffix', WP_SEO_Settings()->get_network_option( 'robots_txt_network_suffix', '' ) );
/**
* Filters the Robots.txt Prefix value for WP SEO.
*
* @param string $prefix The robots.txt prefix, added in the WP SEO settings page.
*/
$robots_prefix = apply_filters( 'wp_seo_robots_txt_prefix', WP_SEO_Settings()->get_option( 'robots_txt_prefix', '' ) );
/**
* Filters the Robots.txt Suffix value for WP SEO.
*
* @param string $suffix The robots.txt suffix, added in the WP SEO settings page.
*/
$robots_suffix = apply_filters( 'wp_seo_robots_txt_suffix', WP_SEO_Settings()->get_option( 'robots_txt_suffix', '' ) );
return implode(
PHP_EOL,
array_filter(
array(
$robots_network_prefix,
$robots_prefix,
$robots,
$robots_suffix,
$robots_network_suffix
)
)
);
}
}
/**
* Helper function to use the class instance.
*
* @return WP_SEO
*/
function wp_seo() {
return WP_SEO::instance();
}
add_action( 'after_setup_theme', 'wp_seo' );
endif;