-
Notifications
You must be signed in to change notification settings - Fork 875
/
class-frontend.php
1665 lines (1429 loc) · 54.6 KB
/
class-frontend.php
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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @package Frontend
*
* Main frontend code.
*/
if ( ! defined( 'WPSEO_VERSION' ) ) {
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit();
}
if ( ! class_exists( 'WPSEO_Frontend' ) ) {
/**
* Main frontend class for WordPress SEO, responsible for the SEO output as well as removing
* default WordPress output.
*
* @package WPSEO_Frontend
*/
class WPSEO_Frontend {
/**
* @var array Holds the plugins options.
*/
var $options = array();
/**
* @var boolean Boolean indicating wether output buffering has been started
*/
private $ob_started = false;
/**
* Class constructor
*
* Adds and removes a lot of filters.
*/
function __construct() {
$this->options = WPSEO_Options::get_all();
add_action( 'wp_head', array( $this, 'head' ), 1 );
// The head function here calls action wpseo_head, to which we hook all our functionality
add_action( 'wpseo_head', array( $this, 'debug_marker' ), 2 );
add_action( 'wpseo_head', array( $this, 'robots' ), 6 );
add_action( 'wpseo_head', array( $this, 'metadesc' ), 10 );
add_action( 'wpseo_head', array( $this, 'metakeywords' ), 11 );
add_action( 'wpseo_head', array( $this, 'canonical' ), 20 );
add_action( 'wpseo_head', array( $this, 'adjacent_rel_links' ), 21 );
add_action( 'wpseo_head', array( $this, 'publisher' ), 22 );
add_action( 'wpseo_head', array( $this, 'webmaster_tools_authentication' ), 90 );
add_action( 'wpseo_head', array( $this, 'internal_search_json_ld' ), 90 );
// Remove actions that we will handle through our wpseo_head call, and probably change the output of
remove_action( 'wp_head', 'rel_canonical' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'start_post_rel_link' );
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' );
remove_action( 'wp_head', 'noindex', 1 );
add_filter( 'wp_title', array( $this, 'title' ), 15, 3 );
add_filter( 'thematic_doctitle', array( $this, 'title' ), 15 );
add_action( 'wp', array( $this, 'page_redirect' ), 99 );
add_action( 'template_redirect', array( $this, 'noindex_feed' ) );
add_filter( 'loginout', array( $this, 'nofollow_link' ) );
add_filter( 'register', array( $this, 'nofollow_link' ) );
// Fix the WooThemes woo_title() output
add_filter( 'woo_title', array( $this, 'fix_woo_title' ), 99 );
if ( $this->options['hide-rsdlink'] === true ) {
remove_action( 'wp_head', 'rsd_link' );
}
if ( $this->options['hide-wlwmanifest'] === true ) {
remove_action( 'wp_head', 'wlwmanifest_link' );
}
if ( $this->options['hide-shortlink'] === true ) {
remove_action( 'wp_head', 'wp_shortlink_wp_head' );
remove_action( 'template_redirect', 'wp_shortlink_header', 11 );
}
if ( $this->options['hide-feedlinks'] === true ) {
// @todo: add option to display just normal feed and hide comment feed.
remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'feed_links_extra', 3 );
}
if ( ( $this->options['disable-date'] === true ||
$this->options['disable-author'] === true ) ||
( isset( $this->options['disable-post_formats'] ) && $this->options['disable-post_formats'] )
) {
add_action( 'wp', array( $this, 'archive_redirect' ) );
}
if ( $this->options['redirectattachment'] === true ) {
add_action( 'template_redirect', array( $this, 'attachment_redirect' ), 1 );
}
if ( $this->options['trailingslash'] === true ) {
add_filter( 'user_trailingslashit', array( $this, 'add_trailingslash' ), 10, 2 );
}
if ( $this->options['cleanpermalinks'] === true ) {
add_action( 'template_redirect', array( $this, 'clean_permalink' ), 1 );
}
if ( $this->options['cleanreplytocom'] === true ) {
add_filter( 'comment_reply_link', array( $this, 'remove_reply_to_com' ) );
add_action( 'template_redirect', array( $this, 'replytocom_redirect' ), 1 );
}
add_filter( 'the_content_feed', array( $this, 'embed_rssfooter' ) );
add_filter( 'the_excerpt_rss', array( $this, 'embed_rssfooter_excerpt' ) );
if ( $this->options['forcerewritetitle'] === true ) {
add_action( 'template_redirect', array( $this, 'force_rewrite_output_buffer' ), 99999 );
add_action( 'wp_footer', array( $this, 'flush_cache' ), -1 );
}
if ( $this->options['title_test'] > 0 ) {
add_filter( 'wpseo_title', array( $this, 'title_test_helper' ) );
}
}
/**
* Override Woo's title with our own.
*
* @param string $title
*
* @return string
*/
public function fix_woo_title( $title ) {
return $this->title( $title );
}
/**
* Determine whether the current page is the homepage and shows posts.
*
* @return bool
*/
function is_home_posts_page() {
return ( is_home() && 'posts' == get_option( 'show_on_front' ) );
}
/**
* Determine whether the current page is a static homepage.
*
* @return bool
*/
function is_home_static_page() {
return ( is_front_page() && 'page' == get_option( 'show_on_front' ) && is_page( get_option( 'page_on_front' ) ) );
}
/**
* Determine whether this is the posts page, regardless of whether it's the frontpage or not.
*
* @return bool
*/
function is_posts_page() {
return ( is_home() && 'page' == get_option( 'show_on_front' ) );
}
/**
* Used for static home and posts pages as well as singular titles.
*
* @param object|null $object if filled, object to get the title for
*
* @return string
*/
function get_content_title( $object = null ) {
if ( is_null( $object ) ) {
global $wp_query;
$object = $wp_query->get_queried_object();
}
$title = WPSEO_Meta::get_value( 'title', $object->ID );
if ( $title !== '' ) {
return wpseo_replace_vars( $title, $object );
}
$post_type = ( isset( $object->post_type ) ? $object->post_type : $object->query_var );
return $this->get_title_from_options( 'title-' . $post_type, $object );
}
/**
* Used for category, tag, and tax titles.
*
* @return string
*/
function get_taxonomy_title() {
global $wp_query;
$object = $wp_query->get_queried_object();
$title = WPSEO_Taxonomy_Meta::get_term_meta( $object, $object->taxonomy, 'title' );
if ( is_string( $title ) && $title !== '' ) {
return wpseo_replace_vars( $title, $object );
} else {
return $this->get_title_from_options( 'title-tax-' . $object->taxonomy, $object );
}
}
/**
* Used for author titles.
*
* @return string
*/
function get_author_title() {
$author_id = get_query_var( 'author' );
$title = trim( get_the_author_meta( 'wpseo_title', $author_id ) );
if ( $title !== '' ) {
return wpseo_replace_vars( $title, array() );
}
return $this->get_title_from_options( 'title-author-wpseo' );
}
/**
* Simple function to use to pull data from $options.
*
* All titles pulled from options will be run through the wpseo_replace_vars function.
*
* @param string $index name of the page to get the title from the settings for.
* @param object|array $var_source possible object to pull variables from.
*
* @return string
*/
function get_title_from_options( $index, $var_source = array() ) {
if ( ! isset( $this->options[ $index ] ) || $this->options[ $index ] === '' ) {
if ( is_singular() ) {
return wpseo_replace_vars( '%%title%% %%sep%% %%sitename%%', $var_source );
} else {
return '';
}
} else {
return wpseo_replace_vars( $this->options[ $index ], $var_source );
}
}
/**
* Get the default title for the current page.
*
* This is the fallback title generator used when a title hasn't been set for the specific content, taxonomy, author
* details, or in the options. It scrubs off any present prefix before or after the title (based on $seplocation) in
* order to prevent duplicate seperations from appearing in the title (this happens when a prefix is supplied to the
* wp_title call on singular pages).
*
* @param string $sep the separator used between variables
* @param string $seplocation Whether the separator should be left or right.
* @param string $title possible title that's already set
*
* @return string
*/
function get_default_title( $sep, $seplocation, $title = '' ) {
if ( 'right' == $seplocation ) {
$regex = '`\s*' . preg_quote( trim( $sep ), '`' ) . '\s*`u';
} else {
$regex = '`^\s*' . preg_quote( trim( $sep ), '`' ) . '\s*`u';
}
$title = preg_replace( $regex, '', $title );
if ( ! is_string( $title ) || ( is_string( $title ) && $title === '' ) ) {
$title = get_bloginfo( 'name' );
$title = $this->add_paging_to_title( $sep, $seplocation, $title );
$title = $this->add_to_title( $sep, $seplocation, $title, strip_tags( get_bloginfo( 'description' ) ) );
return $title;
}
$title = $this->add_paging_to_title( $sep, $seplocation, $title );
$title = $this->add_to_title( $sep, $seplocation, $title, strip_tags( get_bloginfo( 'name' ) ) );
return $title;
}
/**
* This function adds paging details to the title.
*
* @param string $sep separator used in the title
* @param string $seplocation Whether the separator should be left or right.
* @param string $title the title to append the paging info to
*
* @return string
*/
function add_paging_to_title( $sep, $seplocation, $title ) {
global $wp_query;
if ( ! empty( $wp_query->query_vars['paged'] ) && $wp_query->query_vars['paged'] > 1 ) {
return $this->add_to_title( $sep, $seplocation, $title, $wp_query->query_vars['paged'] . '/' . $wp_query->max_num_pages );
}
return $title;
}
/**
* Add part to title, while ensuring that the $seplocation variable is respected.
*
* @param string $sep separator used in the title
* @param string $seplocation Whether the separator should be left or right.
* @param string $title the title to append the title_part to
* @param string $title_part the part to append to the title
*
* @return string
*/
function add_to_title( $sep, $seplocation, $title, $title_part ) {
if ( 'right' === $seplocation ) {
return $title . $sep . $title_part;
}
return $title_part . $sep . $title;
}
/**
* Main title function.
*
* @param string $title Title that might have already been set.
* @param string $separator Separator determined in theme (unused)
* @param string $separator_location Whether the separator should be left or right.
*
* @return string
*/
function title( $title, $separator = '', $separator_location = '' ) {
if ( is_feed() ) {
return $title;
}
$separator = wpseo_replace_vars( '%%sep%%', array() );
$separator = ' ' . trim( $separator ) . ' ';
if ( '' === trim( $separator_location ) ) {
$separator_location = ( is_rtl() ) ? 'left' : 'right';
}
// This needs to be kept track of in order to generate
// default titles for singular pages.
$original_title = $title;
// This flag is used to determine if any additional
// processing should be done to the title after the
// main section of title generation completes.
$modified_title = true;
// This variable holds the page-specific title part
// that is used to generate default titles.
$title_part = '';
if ( $this->is_home_static_page() ) {
$title = $this->get_content_title();
} elseif ( $this->is_home_posts_page() ) {
$title = $this->get_title_from_options( 'title-home-wpseo' );
} elseif ( $this->is_posts_page() ) {
$title = $this->get_content_title( get_post( get_option( 'page_for_posts' ) ) );
} elseif ( is_singular() ) {
$title = $this->get_content_title();
if ( ! is_string( $title ) || '' === $title ) {
$title_part = $original_title;
}
} elseif ( is_search() ) {
$title = $this->get_title_from_options( 'title-search-wpseo' );
if ( ! is_string( $title ) || '' === $title ) {
$title_part = sprintf( __( 'Search for "%s"', 'wordpress-seo' ), esc_html( get_search_query() ) );
}
} elseif ( is_category() || is_tag() || is_tax() ) {
$title = $this->get_taxonomy_title();
if ( ! is_string( $title ) || '' === $title ) {
if ( is_category() ) {
$title_part = single_cat_title( '', false );
} elseif ( is_tag() ) {
$title_part = single_tag_title( '', false );
} else {
$title_part = single_term_title( '', false );
if ( $title_part === '' ) {
global $wp_query;
$term = $wp_query->get_queried_object();
$title_part = $term->name;
}
}
}
} elseif ( is_author() ) {
$title = $this->get_author_title();
if ( ! is_string( $title ) || '' === $title ) {
$title_part = get_the_author_meta( 'display_name', get_query_var( 'author' ) );
}
} elseif ( is_post_type_archive() ) {
$post_type = get_query_var( 'post_type' );
if ( is_array( $post_type ) ) {
$post_type = reset( $post_type );
}
$title = $this->get_title_from_options( 'title-ptarchive-' . $post_type );
if ( ! is_string( $title ) || '' === $title ) {
$post_type_obj = get_post_type_object( $post_type );
if ( isset( $post_type_obj->labels->menu_name ) ) {
$title_part = $post_type_obj->labels->menu_name;
} elseif ( isset( $post_type_obj->name ) ) {
$title_part = $post_type_obj->name;
} else {
$title_part = ''; //To be determined what this should be
}
}
} elseif ( is_archive() ) {
$title = $this->get_title_from_options( 'title-archive-wpseo' );
// @todo [JRF => Yoast] Should these not use the archive default if no title found ?
// WPSEO_Options::get_default( 'wpseo_titles', 'title-archive-wpseo' )
// Replacement would be needed!
if ( empty( $title ) ) {
if ( is_month() ) {
$title_part = sprintf( __( '%s Archives', 'wordpress-seo' ), single_month_title( ' ', false ) );
} elseif ( is_year() ) {
$title_part = sprintf( __( '%s Archives', 'wordpress-seo' ), get_query_var( 'year' ) );
} elseif ( is_day() ) {
$title_part = sprintf( __( '%s Archives', 'wordpress-seo' ), get_the_date() );
} else {
$title_part = __( 'Archives', 'wordpress-seo' );
}
}
} elseif ( is_404() ) {
if ( 0 !== get_query_var( 'year' ) || ( 0 !== get_query_var( 'monthnum' ) || 0 !== get_query_var( 'day' ) ) ) {
// @todo [JRF => Yoast] Should these not use the archive default if no title found ?
if ( 0 !== get_query_var( 'day' ) ) {
$date = sprintf( '%04d-%02d-%02d 00:00:00', get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) );
$date = mysql2date( get_option( 'date_format' ), $date, true );
$date = apply_filters( 'get_the_date', $date, '' );
$title_part = sprintf( __( '%s Archives', 'wordpress-seo' ), $date );
} elseif ( 0 !== get_query_var( 'monthnum' ) ) {
$title_part = sprintf( __( '%s Archives', 'wordpress-seo' ), single_month_title( ' ', false ) );
} elseif ( 0 !== get_query_var( 'year' ) ) {
$title_part = sprintf( __( '%s Archives', 'wordpress-seo' ), get_query_var( 'year' ) );
} else {
$title_part = __( 'Archives', 'wordpress-seo' );
}
} else {
$title = $this->get_title_from_options( 'title-404-wpseo' );
// @todo [JRF => Yoast] Should these not use the 404 default if no title found ?
// WPSEO_Options::get_default( 'wpseo_titles', 'title-404-wpseo' )
// Replacement would be needed!
if ( empty( $title ) ) {
$title_part = __( 'Page not found', 'wordpress-seo' );
}
}
} else {
// In case the page type is unknown, leave the title alone.
$modified_title = false;
// If you would like to generate a default title instead,
// the following code could be used instead of the line above:
// $title_part = $title;
}
if ( ( $modified_title && empty( $title ) ) || ! empty( $title_part ) ) {
$title = $this->get_default_title( $separator, $separator_location, $title_part );
}
if ( defined( 'ICL_LANGUAGE_CODE' ) && false !== strpos( $title, ICL_LANGUAGE_CODE ) ) {
$title = str_replace( ' @' . ICL_LANGUAGE_CODE, '', $title );
}
/**
* Filter: 'wpseo_title' - Allow changing the WP SEO <title> output
*
* @api string $title The page title being put out.
*/
return esc_html( strip_tags( stripslashes( apply_filters( 'wpseo_title', $title ) ) ) );
}
/**
* Function used when title needs to be force overridden.
*
* @return string
*/
function force_wp_title() {
global $wp_query;
$old_wp_query = null;
if ( ! $wp_query->is_main_query() ) {
$old_wp_query = $wp_query;
wp_reset_query();
}
$title = $this->title( '' );
if ( ! empty( $old_wp_query ) ) {
$GLOBALS['wp_query'] = $old_wp_query;
unset( $old_wp_query );
}
return $title;
}
/**
* Outputs or returns the debug marker, which is also used for title replacement when force rewrite is active.
*
* @param bool $echo Whether or not to echo the debug marker.
*
* @return string
*/
public function debug_marker( $echo = true ) {
$marker = '<!-- This site is optimized with the Yoast WordPress SEO plugin v' . WPSEO_VERSION . ' - https://yoast.com/wordpress/plugins/seo/ -->';
if ( $echo === false ) {
return $marker;
} else {
echo "\n${marker}\n";
}
}
/**
* Output Webmaster Tools authentication strings
*/
public function webmaster_tools_authentication() {
if ( is_front_page() ) {
// Alexa
if ( $this->options['alexaverify'] !== '' ) {
echo '<meta name="alexaVerifyID" content="' . esc_attr( $this->options['alexaverify'] ) . "\" />\n";
}
// Bing
if ( $this->options['msverify'] !== '' ) {
echo '<meta name="msvalidate.01" content="' . esc_attr( $this->options['msverify'] ) . "\" />\n";
}
// Google
if ( $this->options['googleverify'] !== '' ) {
echo '<meta name="google-site-verification" content="' . esc_attr( $this->options['googleverify'] ) . "\" />\n";
}
// Pinterest
if ( $this->options['pinterestverify'] !== '' ) {
echo '<meta name="p:domain_verify" content="' . esc_attr( $this->options['pinterestverify'] ) . "\" />\n";
}
// Yandex
if ( $this->options['yandexverify'] !== '' ) {
echo '<meta name="yandex-verification" content="' . esc_attr( $this->options['yandexverify'] ) . "\" />\n";
}
}
}
/**
* Outputs JSON+LD code to allow recognition of the internal search engine
*
* @since 1.5.7
*
* @link https://developers.google.com/webmasters/richsnippets/sitelinkssearch
*/
public function internal_search_json_ld() {
if ( ! is_front_page() ) {
return;
}
/**
* Filter: 'disable_wpseo_json_ld_search' - Allow disabling of the json+ld output
*
* @api bool $display_search Whether or not to display json+ld search on the frontend
*/
if ( apply_filters( 'disable_wpseo_json_ld_search', false ) === true ) {
return;
}
$home_url = trailingslashit( home_url() );
/**
* Filter: 'wpseo_json_ld_search_url' - Allows filtering of the search URL for WP SEO
*
* @api string $search_url The search URL for this site with a `{search_term}` variable.
*/
$search_url = apply_filters( 'wpseo_json_ld_search_url', $home_url . '?s={search_term}' );
/**
* Filter: 'wpseo_json_ld_search_output' - Allows filtering of the entire output of the function
*
* @api string $output The output of the function.
*/
echo apply_filters( 'wpseo_json_ld_search_output', '<script type="application/ld+json">{ "@context": "http://schema.org", "@type": "WebSite", "url": "' . $home_url . '", "potentialAction": { "@type": "SearchAction", "target": "' . $search_url .'", "query-input": "required name=search_term" } }</script>' . "\n" );
}
/**
* Main wrapper function attached to wp_head. This combines all the output on the frontend of the WP SEO plugin.
*/
public function head() {
global $wp_query;
$old_wp_query = null;
if ( ! $wp_query->is_main_query() ) {
$old_wp_query = $wp_query;
wp_reset_query();
}
/**
* Action: 'wpseo_head' - Allow other plugins to output inside the WP SEO section of the head section.
*/
do_action( 'wpseo_head' );
echo "<!-- / Yoast WordPress SEO plugin. -->\n\n";
if ( ! empty( $old_wp_query ) ) {
$GLOBALS['wp_query'] = $old_wp_query;
unset( $old_wp_query );
}
return;
}
/**
* Output the meta robots value.
*
* @return string
*/
public function robots() {
global $wp_query;
$robots = array();
$robots['index'] = 'index';
$robots['follow'] = 'follow';
$robots['other'] = array();
if ( is_singular() ) {
global $post;
if ( is_object( $post ) && ( isset( $this->options[ 'noindex-' . $post->post_type ] ) && $this->options[ 'noindex-' . $post->post_type ] === true ) ) {
$robots['index'] = 'noindex';
}
if ( 'private' == $post->post_status ) {
$robots['index'] = 'noindex';
}
$robots = $this->robots_for_single_post( $robots );
} else {
if ( is_search() ) {
$robots['index'] = 'noindex';
} elseif ( is_tax() || is_tag() || is_category() ) {
$term = $wp_query->get_queried_object();
if ( is_object( $term ) && ( isset( $this->options[ 'noindex-tax-' . $term->taxonomy ] ) && $this->options[ 'noindex-tax-' . $term->taxonomy ] === true ) ) {
$robots['index'] = 'noindex';
}
// Three possible values, index, noindex and default, do nothing for default
$term_meta = WPSEO_Taxonomy_Meta::get_term_meta( $term, $term->taxonomy, 'noindex' );
if ( is_string( $term_meta ) && 'default' !== $term_meta ) {
$robots['index'] = $term_meta;
}
} elseif (
( is_author() && $this->options['noindex-author-wpseo'] === true ) ||
( is_date() && $this->options['noindex-archive-wpseo'] === true )
) {
$robots['index'] = 'noindex';
} elseif ( is_home() ) {
if ( get_query_var( 'paged' ) > 1 && $this->options['noindex-subpages-wpseo'] === true ) {
$robots['index'] = 'noindex';
}
$page_for_posts = get_option( 'page_for_posts' );
if ( $page_for_posts ) {
$robots = $this->robots_for_single_post( $robots, $page_for_posts );
}
unset( $page_for_posts );
} elseif ( is_post_type_archive() ) {
$post_type = get_query_var( 'post_type' );
if ( is_array( $post_type ) ) {
$post_type = reset( $post_type );
}
if ( isset( $this->options[ 'noindex-ptarchive-' . $post_type ] ) && $this->options[ 'noindex-ptarchive-' . $post_type ] === true ) {
$robots['index'] = 'noindex';
}
}
if ( isset( $wp_query->query_vars['paged'] ) && ( $wp_query->query_vars['paged'] && $wp_query->query_vars['paged'] > 1 ) && ( $this->options['noindex-subpages-wpseo'] === true ) ) {
$robots['index'] = 'noindex';
$robots['follow'] = 'follow';
}
foreach ( array( 'noodp', 'noydir' ) as $robot ) {
if ( $this->options[ $robot ] === true ) {
$robots['other'][] = $robot;
}
}
unset( $robot );
}
// Force override to respect the WP settings
if ( '0' == get_option( 'blog_public' ) || isset( $_GET['replytocom'] ) ) {
$robots['index'] = 'noindex';
}
$robotsstr = $robots['index'] . ',' . $robots['follow'];
if ( $robots['other'] !== array() ) {
$robots['other'] = array_unique( $robots['other'] ); // most likely no longer needed, needs testing
$robotsstr .= ',' . implode( ',', $robots['other'] );
}
$robotsstr = preg_replace( '`^index,follow,?`', '', $robotsstr );
/**
* Filter: 'wpseo_robots' - Allows filtering of the meta robots output of WP SEO
*
* @api string $robotsstr The meta robots directives to be echoed.
*/
$robotsstr = apply_filters( 'wpseo_robots', $robotsstr );
if ( is_string( $robotsstr ) && $robotsstr !== '' ) {
echo '<meta name="robots" content="' . esc_attr( $robotsstr ) . '"/>' . "\n";
}
return $robotsstr;
}
/**
* Determine $robots values for a single post
*
* @param array $robots
* @param int|string $postid The postid for which to determine the $robots values, defaults to
* the current post
*
* @return array
*/
function robots_for_single_post( $robots, $postid = 0 ) {
if ( WPSEO_Meta::get_value( 'meta-robots-noindex', $postid ) === '1' ) {
$robots['index'] = 'noindex';
} elseif ( WPSEO_Meta::get_value( 'meta-robots-noindex', $postid ) === '2' ) {
$robots['index'] = 'index';
}
if ( WPSEO_Meta::get_value( 'meta-robots-nofollow', $postid ) === '1' ) {
$robots['follow'] = 'nofollow';
}
$meta_robots_adv = WPSEO_Meta::get_value( 'meta-robots-adv', $postid );
if ( $meta_robots_adv !== '' && ( $meta_robots_adv !== '-' && $meta_robots_adv !== 'none' ) ) {
$meta_robots_adv = explode( ',', $meta_robots_adv );
foreach ( $meta_robots_adv as $robot ) {
$robots['other'][] = $robot;
}
unset( $robot );
} elseif ( $meta_robots_adv === '' || $meta_robots_adv === '-' ) {
foreach ( array( 'noodp', 'noydir' ) as $robot ) {
if ( $this->options[ $robot ] === true ) {
$robots['other'][] = $robot;
}
}
unset( $robot );
}
unset( $meta_robots_adv );
return $robots;
}
/**
* This function normally outputs the canonical but is also used in other places to retrieve
* the canonical URL for the current page.
*
* @param bool $echo Whether or not to output the canonical element.
* @param bool $un_paged Whether or not to return the canonical with or without pagination added to the URL.
* @param bool $no_override Whether or not to return a manually overridden canonical
*
* @return string $canonical
*/
public function canonical( $echo = true, $un_paged = false, $no_override = false ) {
$canonical = false;
$skip_pagination = false;
// Set decent canonicals for homepage, singulars and taxonomy pages
if ( is_singular() ) {
$meta_canon = WPSEO_Meta::get_value( 'canonical' );
if ( $no_override === false && $meta_canon !== '' ) {
$canonical = $meta_canon;
$skip_pagination = true;
} else {
$obj = get_queried_object();
$canonical = get_permalink( $obj->ID );
// Fix paginated pages canonical, but only if the page is truly paginated.
if ( get_query_var( 'page' ) > 1 ) {
global $wp_rewrite;
$numpages = substr_count( $obj->post_content, '<!--nextpage-->' ) + 1;
if ( $numpages && get_query_var( 'page' ) <= $numpages ) {
if ( ! $wp_rewrite->using_permalinks() ) {
$canonical = add_query_arg( 'page', get_query_var( 'page' ), $canonical );
} else {
$canonical = user_trailingslashit( trailingslashit( $canonical ) . get_query_var( 'page' ) );
}
}
}
}
unset( $meta_canon );
} else {
if ( is_search() ) {
$canonical = get_search_link();
} elseif ( is_front_page() ) {
$canonical = home_url();
} elseif ( $this->is_posts_page() ) {
$canonical = get_permalink( get_option( 'page_for_posts' ) );
} elseif ( is_tax() || is_tag() || is_category() ) {
$term = get_queried_object();
if ( $no_override === false ) {
$canonical = WPSEO_Taxonomy_Meta::get_term_meta( $term, $term->taxonomy, 'canonical' );
if ( is_string( $canonical ) && $canonical !== '' ) {
$skip_pagination = true;
}
}
if ( ! is_string( $canonical ) || $canonical === '' ) {
$canonical = get_term_link( $term, $term->taxonomy );
}
} elseif ( is_post_type_archive() ) {
$post_type = get_query_var( 'post_type' );
if ( is_array( $post_type ) ) {
$post_type = reset( $post_type );
}
$canonical = get_post_type_archive_link( $post_type );
} elseif ( is_author() ) {
$canonical = get_author_posts_url( get_query_var( 'author' ), get_query_var( 'author_name' ) );
} elseif ( is_archive() ) {
if ( is_date() ) {
if ( is_day() ) {
$canonical = get_day_link( get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) );
} elseif ( is_month() ) {
$canonical = get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) );
} elseif ( is_year() ) {
$canonical = get_year_link( get_query_var( 'year' ) );
}
}
}
}
if ( $canonical && $un_paged ) {
return $canonical;
}
if ( $canonical && ! $skip_pagination && get_query_var( 'paged' ) > 1 ) {
global $wp_rewrite;
if ( ! $wp_rewrite->using_permalinks() ) {
$canonical = add_query_arg( 'paged', get_query_var( 'paged' ), $canonical );
} else {
if ( is_front_page() ) {
$canonical = wpseo_xml_sitemaps_base_url( '' );
}
$canonical = user_trailingslashit( trailingslashit( $canonical ) . trailingslashit( $wp_rewrite->pagination_base ) . get_query_var( 'paged' ) );
}
}
if ( $canonical && 'default' !== $this->options['force_transport'] ) {
$canonical = preg_replace( '`^http[s]?`', $this->options['force_transport'], $canonical );
}
/**
* Filter: 'wpseo_canonical' - Allow filtering of the canonical URL put out by WP SEO
*
* @api string $canonical The canonical URL
*/
$canonical = apply_filters( 'wpseo_canonical', $canonical );
if ( is_string( $canonical ) && $canonical !== '' ) {
// Force canonical links to be absolute, relative is NOT an option.
if ( wpseo_is_url_relative( $canonical ) === true ) {
$canonical = $this->base_url( $canonical );
}
if ( $echo !== false ) {
echo '<link rel="canonical" href="' . esc_url( $canonical, null, 'other' ) . '" />' . "\n";
} else {
return $canonical;
}
} else {
return false;
}
}
/**
* Parse the home URL setting to find the base URL for relative URLs.
*
* @param string $path
*
* @return string
*/
private function base_url( $path = null ) {
$url = get_option( 'home' );
$parts = parse_url( $url );
$base_url = trailingslashit( $parts['scheme'] . '://' . $parts['host'] );
if ( ! is_null( $path ) ) {
$base_url .= ltrim( $path, '/' );
}
return $base_url;
}
/**
* Adds 'prev' and 'next' links to archives.
*
* @link http://googlewebmastercentral.blogspot.com/2011/09/pagination-with-relnext-and-relprev.html
* @since 1.0.3
*/
public function adjacent_rel_links() {
// Don't do this for Genesis, as the way Genesis handles homepage functionality is different and causes issues sometimes.
/**
* Filter 'wpseo_genesis_force_adjacent_rel_home' - Allows devs to allow echoing rel="next" / rel="prev" by WP SEO on Genesis installs
*
* @api bool $unsigned Whether or not to rel=next / rel=prev
*/
if ( is_home() && function_exists( 'genesis' ) && apply_filters( 'wpseo_genesis_force_adjacent_rel_home', false ) === false ) {
return;
}
global $wp_query;
if ( ! is_singular() ) {
$url = $this->canonical( false, true, true );
if ( is_string( $url ) && $url !== '' ) {
$paged = get_query_var( 'paged' );
if ( 0 == $paged ) {
$paged = 1;
}
if ( $paged == 2 ) {
$this->adjacent_rel_link( 'prev', $url, $paged - 1, true );
}
// Make sure to use index.php when needed, done after paged == 2 check so the prev links to homepage will not have index.php erroneously.
if ( is_front_page() ) {
$url = wpseo_xml_sitemaps_base_url( '' );
}
if ( $paged > 2 ) {
$this->adjacent_rel_link( 'prev', $url, $paged - 1, true );
}
if ( $paged < $wp_query->max_num_pages ) {
$this->adjacent_rel_link( 'next', $url, $paged + 1, true );
}
}
} else {
$numpages = 0;
if ( isset( $wp_query->post->post_content ) ) {
$numpages = substr_count( $wp_query->post->post_content, '<!--nextpage-->' ) + 1;
}
if ( $numpages > 1 ) {
$page = get_query_var( 'page' );
if ( ! $page ) {
$page = 1;
}
$url = get_permalink( $wp_query->post->ID );
// If the current page is the frontpage, pagination should use /base/
if ( $this->is_home_static_page() ) {
$usebase = true;
}
else {
$usebase = false;
}
if ( $page > 1 ) {
$this->adjacent_rel_link( 'prev', $url, $page - 1, $usebase, 'single_paged' );
}
if ( $page < $numpages ) {
$this->adjacent_rel_link( 'next', $url, $page + 1, $usebase, 'single_paged' );
}
}
}
}
/**
* Get adjacent pages link for archives
*
* @param string $rel Link relationship, prev or next.
* @param string $url the unpaginated URL of the current archive.
* @param string $page the page number to add on to $url for the $link tag.
* @param boolean $incl_pagination_base whether or not to include /page/ or not.
*
* @return string $link link element
*
* @since 1.0.2
*/
private function adjacent_rel_link( $rel, $url, $page, $incl_pagination_base ) {