class-frontend.php
47.6 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
<?php
/**
* @package Frontend
*
* Main frontend code.
*/
if ( ! defined( 'WPSEO_VERSION' ) ) {
header( 'HTTP/1.0 403 Forbidden' );
die;
}
/**
* 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();
/**
* Class constructor
*
* Adds and removes a lot of filters.
*/
function __construct() {
$this->options = get_wpseo_options();
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, 'author' ), 22 );
add_action( 'wpseo_head', array( $this, 'publisher' ), 23 );
add_action( 'wpseo_head', array( $this, 'webmaster_tools_authentication' ), 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 );
if ( isset( $_GET['replytocom'] ) ) {
remove_action( 'wp_head', 'wp_no_robots' );
}
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, 1 );
add_action( 'template_redirect', array( $this, 'noindex_feed' ) );
add_filter( 'loginout', array( $this, 'nofollow_link' ) );
add_filter( 'register', array( $this, 'nofollow_link' ) );
if ( isset( $this->options['hide-rsdlink'] ) && $this->options['hide-rsdlink'] )
remove_action( 'wp_head', 'rsd_link' );
if ( isset( $this->options['hide-wlwmanifest'] ) && $this->options['hide-wlwmanifest'] )
remove_action( 'wp_head', 'wlwmanifest_link' );
if ( isset( $this->options['hide-shortlink'] ) && $this->options['hide-shortlink'] ) {
remove_action( 'wp_head', 'wp_shortlink_wp_head' );
remove_action( 'template_redirect', 'wp_shortlink_header' );
}
if ( isset( $this->options['hide-feedlinks'] ) && $this->options['hide-feedlinks'] ) {
// @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 ( ( isset( $this->options['disable-date'] ) && $this->options['disable-date'] ) ||
( isset( $this->options['disable-author'] ) && $this->options['disable-author'] ) ||
( isset( $this->options['disable-post_formats'] ) && $this->options['disable-post_formats'] )
)
add_action( 'wp', array( $this, 'archive_redirect' ) );
if ( isset( $this->options['redirectattachment'] ) && $this->options['redirectattachment'] )
add_action( 'template_redirect', array( $this, 'attachment_redirect' ), 1 );
if ( isset( $this->options['trailingslash'] ) && $this->options['trailingslash'] )
add_filter( 'user_trailingslashit', array( $this, 'add_trailingslash' ), 10, 2 );
if ( isset( $this->options['cleanpermalinks'] ) && $this->options['cleanpermalinks'] )
add_action( 'template_redirect', array( $this, 'clean_permalink' ), 1 );
if ( isset( $this->options['cleanreplytocom'] ) && $this->options['cleanreplytocom'] )
add_filter( 'comment_reply_link', array( $this, 'remove_reply_to_com' ) );
add_filter( 'the_content_feed', array( $this, 'embed_rssfooter' ) );
add_filter( 'the_excerpt_rss', array( $this, 'embed_rssfooter_excerpt' ) );
if ( isset( $this->options['forcerewritetitle'] ) && $this->options['forcerewritetitle'] ) {
add_action( 'get_header', array( $this, 'force_rewrite_output_buffer' ) );
add_action( 'wp_footer', array( $this, 'flush_cache' ) );
}
if ( isset( $this->options['title_test'] ) && $this->options['title_test'] )
add_filter( 'wpseo_title', array( $this, 'title_test_helper' ) );
if ( isset( $_GET['replytocom'] ) ) {
remove_action( 'wp_head', 'wp_no_robots' );
add_action( 'template_redirect', array( $this, 'replytocom_redirect' ), 1 );
}
}
/**
* Determine whether the current page is the homepage and shows posts.
*
* @return bool
*/
function is_home_posts_page() {
return ( is_home() && 'page' != 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_get_value( 'title', $object->ID );
if ( ! empty( $title ) )
return wpseo_replace_vars( $title, (array) $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 = trim( wpseo_get_term_meta( $object, $object->taxonomy, 'title' ) );
if ( ! empty( $title ) )
return wpseo_replace_vars( $title, (array) $object );
return $this->get_title_from_options( 'title-' . $object->taxonomy, $object );
}
/**
* Used for author titles.
*
* @return string
*/
function get_author_title() {
$author_id = get_query_var( 'author' );
$title = get_the_author_meta( 'wpseo_title', $author_id );
if ( ! empty( $title ) )
return wpseo_replace_vars( $title, array() );
return $this->get_title_from_options( 'title-author' );
}
/**
* 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 pul variables from.
*
* @return string
*/
function get_title_from_options( $index, $var_source = array() ) {
if ( ! isset( $this->options[$index] ) || empty( $this->options[$index] ) ) {
if ( is_singular() )
return wpseo_replace_vars( '%%title%% %%sep%% %%sitename%%', (array) $var_source );
else
return '';
}
return wpseo_replace_vars( $this->options[$index], (array) $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 ( empty( $title ) ) {
$title = get_bloginfo( 'name' );
$title = $this->add_paging_to_title( $sep, $seplocation, $title );
$title = $this->add_to_title( $sep, $seplocation, $title, get_bloginfo( 'description' ) );
return $title;
}
$title = $this->add_paging_to_title( $sep, $seplocation, $title );
$title = $this->add_to_title( $sep, $seplocation, $title, 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 $sepinput Separator determined in theme.
* @param string $seplocation Whether the separator should be left or right.
*
* @return string
*/
function title( $title, $sepinput = '-', $seplocation = '' ) {
global $sep;
$sep = $sepinput;
if ( is_feed() )
return $title;
// This needs to be kept track of in order to generate
// default titles for singular pages.
$original_title = $title;
// This conditional ensures that sites that use of wp_title(''); as the plugin
// used to suggest will still work properly with these changes.
if ( '' == trim( $sep ) && '' == $seplocation ) {
$sep = '-';
$seplocation = 'right';
} // In the event that $seplocation is left empty, the direction will be
// determined by whether the site is in rtl mode or not. This is based
// upon my findings that rtl sites tend to reverse the flow of the site titles.
else if ( '' == $seplocation )
$seplocation = ( is_rtl() ) ? 'left' : 'right';
$sep = ' ' . trim( $sep ) . ' ';
// 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();
}
else if ( $this->is_home_posts_page() ) {
$title = $this->get_title_from_options( 'title-home' );
}
else if ( $this->is_posts_page() ) {
$title = $this->get_content_title( get_post( get_option( 'page_for_posts' ) ) );
}
else if ( is_singular() ) {
$title = $this->get_content_title();
if ( empty( $title ) )
$title_part = $original_title;
}
else if ( is_search() ) {
$title = $this->get_title_from_options( 'title-search' );
if ( empty( $title ) )
$title_part = sprintf( __( 'Search for "%s"', 'wordpress-seo' ), esc_html( get_search_query() ) );
}
else if ( is_category() || is_tag() || is_tax() ) {
$title = $this->get_taxonomy_title();
if ( empty( $title ) ) {
if ( is_category() )
$title_part = single_cat_title( '', false );
else if ( is_tag() )
$title_part = single_tag_title( '', false );
else if ( function_exists( 'single_term_title' ) ) {
$title_part = single_term_title( '', false );
}
else {
global $wp_query;
$term = $wp_query->get_queried_object();
$title_part = $term->name;
}
}
}
else if ( is_author() ) {
$title = $this->get_author_title();
if ( empty( $title ) )
$title_part = get_the_author_meta( 'display_name', get_query_var( 'author' ) );
}
else if ( function_exists( 'is_post_type_archive' ) && is_post_type_archive() ) {
$post_type = get_query_var( 'post_type' );
$title = $this->get_title_from_options( 'title-ptarchive-' . $post_type );
if ( empty( $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;
else if ( isset( $post_type_obj->name ) )
$title_part = $post_type_obj->name;
else
$title_part = ''; //To be determined what this should be
}
}
else if ( is_archive() ) {
$title = $this->get_title_from_options( 'title-archive' );
if ( empty( $title ) ) {
if ( is_month() )
$title_part = sprintf( __( '%s Archives', 'wordpress-seo' ), single_month_title( ' ', false ) );
else if ( is_year() )
$title_part = sprintf( __( '%s Archives', 'wordpress-seo' ), get_query_var( 'year' ) );
else if ( is_day() )
$title_part = sprintf( __( '%s Archives', 'wordpress-seo' ), get_the_date() );
else
$title_part = __( 'Archives', 'wordpress-seo' );
}
}
else if ( is_404() ) {
if ( 0 !== get_query_var( 'year' ) || ( 0 !== get_query_var( 'monthnum' ) || 0 !== get_query_var( 'day' ) ) ) {
if ( 0 !== get_query_var( 'day' ) ) {
global $post;
$original_p = $post;
$post->post_date = sprintf( "%04d-%02d-%02d 00:00:00", get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) );
$title_part = sprintf( __( '%s Archives', 'wordpress-seo' ), get_the_date() );
$post = $original_p;
}
else if ( 0 !== get_query_var( 'monthnum' ) ) {
$title_part = sprintf( __( '%s Archives', 'wordpress-seo' ), single_month_title( ' ', false ) );
}
else if ( 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' );
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( $sep, $seplocation, $title_part );
if ( defined( 'ICL_LANGUAGE_CODE' ) && false !== strpos( $title, ICL_LANGUAGE_CODE ) )
$title = str_replace( ' @' . ICL_LANGUAGE_CODE, '', $title );
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 . " - http://yoast.com/wordpress/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() ) {
if ( ! empty( $this->options['googleverify'] ) ) {
$google_meta = $this->options['googleverify'];
if ( strpos( $google_meta, 'content' ) ) {
preg_match( '`content="([^"]+)"`', $google_meta, $match );
$google_meta = $match[1];
}
echo "<meta name=\"google-site-verification\" content=\"$google_meta\" />\n";
}
if ( ! empty( $this->options['msverify'] ) ) {
$bing_meta = $this->options['msverify'];
if ( strpos( $bing_meta, 'content' ) ) {
preg_match( '`content="([^"]+)"`', $bing_meta, $match );
$bing_meta = $match[1];
}
echo "<meta name=\"msvalidate.01\" content=\"$bing_meta\" />\n";
}
if ( ! empty( $this->options['alexaverify'] ) ) {
echo "<meta name=\"alexaVerifyID\" content=\"" . esc_attr( $this->options['alexaverify'] ) . "\" />\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();
}
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.
*/
public function robots() {
global $wp_query;
$robots = array();
$robots['index'] = 'index';
$robots['follow'] = 'follow';
$robots['other'] = array();
if ( is_singular() ) {
global $post;
if ( isset( $this->options['noindex-' . $post->post_type] ) && $this->options['noindex-' . $post->post_type] ) {
$robots['index'] = 'noindex';
}
if ( (int) wpseo_get_value( 'meta-robots-noindex' ) === 1 ) {
$robots['index'] = 'noindex';
} else if ( (int) wpseo_get_value( 'meta-robots-noindex' ) === 2 ) {
$robots['index'] = 'index';
}
if ( (int) wpseo_get_value( 'meta-robots-nofollow' ) === 0 ) {
$robots['follow'] = 'follow';
}else if ( (int) wpseo_get_value( 'meta-robots-nofollow' ) === 1 ) {
$robots['follow'] = 'nofollow';
}
if ( wpseo_get_value( 'meta-robots-adv' ) && wpseo_get_value( 'meta-robots-adv' ) != 'none' ) {
foreach ( explode( ',', wpseo_get_value( 'meta-robots-adv' ) ) as $r ) {
$robots['other'][] = $r;
}
}
}
else {
if ( is_search() ) {
$robots['index'] = 'noindex';
}
else if ( is_tax() || is_tag() || is_category() ) {
$term = $wp_query->get_queried_object();
if ( isset( $this->options['noindex-' . $term->taxonomy] ) && $this->options['noindex-' . $term->taxonomy] )
$robots['index'] = 'noindex';
// Three possible values, index, noindex and default, do nothing for default
$term_meta = wpseo_get_term_meta( $term, $term->taxonomy, 'noindex' );
if ( 'noindex' == $term_meta || 'on' == $term_meta ) // on is for backwards compatibility
$robots['index'] = 'noindex';
if ( 'index' == $term_meta )
$robots['index'] = 'index';
}
else if (
( is_author() && isset( $this->options['noindex-author'] ) && $this->options['noindex-author'] ) ||
( is_date() && isset( $this->options['noindex-archive'] ) && $this->options['noindex-archive'] ) ||
( is_home() && get_query_var( 'paged' ) > 1 )
) {
$robots['index'] = 'noindex';
}
else if ( function_exists( 'is_post_type_archive' ) && is_post_type_archive() ) {
$post_type = get_query_var( 'post_type' );
if ( isset( $this->options['noindex-ptarchive-' . $post_type] ) && $this->options['noindex-ptarchive-' . $post_type] )
$robots['index'] = 'noindex';
}
if ( isset( $wp_query->query_vars['paged'] ) && ( $wp_query->query_vars['paged'] && $wp_query->query_vars['paged'] > 1 ) && ( isset( $this->options['noindex-subpages'] ) && $this->options['noindex-subpages'] ) ) {
$robots['index'] = 'noindex';
$robots['follow'] = 'follow';
}
}
// Force override to respect the WP settings
if ( '0' == get_option('blog_public') || isset( $_GET['replytocom'] ) ) {
$robots['index'] = 'noindex';
}
foreach ( array( 'noodp', 'noydir' ) as $robot ) {
if ( isset( $this->options[$robot] ) && $this->options[$robot] ) {
$robots['other'][] = $robot;
}
}
$robotsstr = $robots['index'] . ',' . $robots['follow'];
$robots['other'] = array_unique( $robots['other'] );
foreach ( $robots['other'] as $robot ) {
$robotsstr .= ',' . $robot;
}
$robotsstr = preg_replace( '`^index,follow,?`', '', $robotsstr );
$robotsstr = apply_filters( 'wpseo_robots', $robotsstr );
if ( $robotsstr != '' ) {
echo '<meta name="robots" content="' . esc_attr( $robotsstr ) . '"/>' . "\n";
}
}
/**
* 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() ) {
if ( ! $no_override && wpseo_get_value( 'canonical' ) && wpseo_get_value( 'canonical' ) != '' ) {
$canonical = wpseo_get_value( 'canonical' );
$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' ) );
}
}
}
}
}
else {
if ( is_search() ) {
$canonical = get_search_link();
}
else if ( is_front_page() ) {
$canonical = home_url( '/' );
}
else if ( $this->is_posts_page() ) {
$canonical = get_permalink( get_option( 'page_for_posts' ) );
}
else if ( is_tax() || is_tag() || is_category() ) {
$term = get_queried_object();
if ( ! $no_override ) {
$canonical = wpseo_get_term_meta( $term, $term->taxonomy, 'canonical' );
if ( $canonical )
$skip_pagination = true;
}
if ( ! $canonical )
$canonical = get_term_link( $term, $term->taxonomy );
}
else if ( function_exists( 'get_post_type_archive_link' ) && is_post_type_archive() ) {
$canonical = get_post_type_archive_link( get_query_var( 'post_type' ) );
}
else if ( is_author() ) {
$canonical = get_author_posts_url( get_query_var( 'author' ), get_query_var( 'author_name' ) );
}
else if ( is_archive() ) {
if ( is_date() ) {
if ( is_day() ) {
$canonical = get_day_link( get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) );
}
else if ( is_month() ) {
$canonical = get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) );
}
else if ( 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() ) {
$base = $GLOBALS['wp_rewrite']->using_index_permalinks() ? 'index.php/' : '/';
$canonical = home_url( $base );
}
$canonical = user_trailingslashit( trailingslashit( $canonical ) . trailingslashit( $wp_rewrite->pagination_base ) . get_query_var( 'paged' ) );
}
}
}
if ( $canonical && isset( $this->options['force_transport'] ) && 'default' != $this->options['force_transport'] ) {
$canonical = preg_replace( '`^http[s]?`', $this->options['force_transport'], $canonical );
}
$canonical = apply_filters( 'wpseo_canonical', $canonical );
if ( $canonical && ! is_wp_error( $canonical ) ) {
if ( $echo !== false )
echo '<link rel="canonical" href="' . esc_url( $canonical, null, 'other' ) . '" />' . "\n";
else
return $canonical;
}
else {
return false;
}
}
/**
* 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.
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() ) {
$base = $GLOBALS['wp_rewrite']->using_index_permalinks() ? 'index.php/' : '/';
$url = home_url( $base );
}
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 ) {
global $wp_rewrite;
if ( ! $wp_rewrite->using_permalinks() ) {
if ( $page > 1 )
$url = add_query_arg( 'paged', $page, $url );
}
else {
if ( $page > 1 ) {
$base = '';
if ( $incl_pagination_base )
$base = trailingslashit( $wp_rewrite->pagination_base );
$url = user_trailingslashit( trailingslashit( $url ) . $base . $page );
}
}
$link = apply_filters( "wpseo_" . $rel . "_rel_link", "<link rel=\"$rel\" href=\"$url\" />\n" );
if ( $link )
echo $link;
}
/**
* Output the rel=publisher code on every page of the site.
*/
public function publisher() {
if ( isset( $this->options['plus-publisher'] ) && ! empty( $this->options['plus-publisher'] ) )
echo '<link rel="publisher" href="' . esc_attr( $this->options['plus-publisher'] ) . '"/>' . "\n";
}
/**
* Outputs the rel=author
*/
public function author() {
$gplus = false;
if ( is_home() || is_front_page() ) {
if ( isset( $this->options['plus-author'] ) && $this->options['plus-author'] != - 1 )
$gplus = get_the_author_meta( 'googleplus', $this->options['plus-author'] );
}
else if ( is_singular() ) {
global $post;
$gplus = get_the_author_meta( 'googleplus', $post->post_author );
// unset gplus when authorship is disabled for this post type
if ( isset( $this->options['noauthorship-' . $post->post_type] ) && $this->options['noauthorship-' . $post->post_type] ) {
$gplus = false;
}
}
$gplus = apply_filters( 'wpseo_author_link', $gplus );
if ( $gplus )
echo '<link rel="author" href="' . $gplus . '"/>' . "\n";
}
/**
* Outputs the meta keywords element.
*
* @return string
*/
public function metakeywords() {
global $wp_query;
if ( ! isset( $this->options['usemetakeywords'] ) || ! $this->options['usemetakeywords'] )
return;
$metakey = '';
if ( is_singular() ) {
global $post;
$metakey = wpseo_get_value( 'metakeywords' );
if ( isset( $this->options['metakey-' . $post->post_type] ) && ( ! $metakey || empty( $metakey ) ) ) {
$metakey = wpseo_replace_vars( $this->options['metakey-' . $post->post_type], (array) $post );
}
}
else {
if ( $this->is_home_posts_page() && isset( $this->options['metakey-home'] ) ) {
$metakey = wpseo_replace_vars( $this->options['metakey-home'], array() );
}
else if ( $this->is_home_static_page() ) {
global $post;
$metakey = wpseo_get_value( 'metakey' );
if ( ( $metakey == '' || ! $metakey ) && isset( $this->options['metakey-' . $post->post_type] ) )
$metakey = wpseo_replace_vars( $this->options['metakey-' . $post->post_type], (array) $post );
}
else if ( is_category() || is_tag() || is_tax() ) {
$term = $wp_query->get_queried_object();
$metakey = wpseo_get_term_meta( $term, $term->taxonomy, 'metakey' );
if ( ! $metakey && isset( $this->options['metakey-' . $term->taxonomy] ) )
$metakey = wpseo_replace_vars( $this->options['metakey-' . $term->taxonomy], (array) $term );
}
else if ( is_author() ) {
$author_id = get_query_var( 'author' );
$metakey = get_the_author_meta( 'metakey', $author_id );
if ( ! $metakey && isset( $this->options['metakey-author'] ) )
$metakey = wpseo_replace_vars( $this->options['metakey-author'], (array) $wp_query->get_queried_object() );
}
}
$metakey = apply_filters( 'wpseo_metakey', trim( $metakey ) );
if ( ! empty( $metakey ) )
echo '<meta name="keywords" content="' . esc_attr( strip_tags( stripslashes( $metakey ) ) ) . '"/>' . "\n";
}
/**
* Outputs the meta description element or returns the description text.
*
* @param bool $echo Whether or not to echo the description.
*
* @return string
*/
public function metadesc( $echo = true ) {
if ( get_query_var( 'paged' ) && get_query_var( 'paged' ) > 1 )
return;
global $post, $wp_query;
$metadesc = '';
if ( is_singular() ) {
$metadesc = wpseo_get_value( 'metadesc' );
if ( $metadesc == '' || ! $metadesc ) {
if ( ( isset( $post ) && isset( $this->options['metadesc-' . $post->post_type] ) ) && $this->options['metadesc-' . $post->post_type] != '' )
$metadesc = wpseo_replace_vars( $this->options['metadesc-' . $post->post_type], (array) $post );
}
}
else {
if ( is_search() ) {
$metadesc = '';
}
else if ( $this->is_home_posts_page() && isset( $this->options['metadesc-home'] ) ) {
$metadesc = wpseo_replace_vars( $this->options['metadesc-home'], array() );
}
else if ( $this->is_posts_page() ) {
$metadesc = wpseo_get_value( 'metadesc', get_option( 'page_for_posts' ) );
if ( ( $metadesc == '' || ! $metadesc ) && ( isset( $post ) && isset( $this->options['metadesc-' . $post->post_type] ) ) ) {
$page = get_post( get_option( 'page_for_posts' ) );
$metadesc = wpseo_replace_vars( $this->options['metadesc-' . $post->post_type], (array) $page );
}
}
else if ( $this->is_home_static_page() ) {
global $post;
$metadesc = wpseo_get_value( 'metadesc' );
if ( ( $metadesc == '' || ! $metadesc ) && ( isset( $post ) && isset( $this->options['metadesc-' . $post->post_type] ) ) )
$metadesc = wpseo_replace_vars( $this->options['metadesc-' . $post->post_type], (array) $post );
}
else if ( is_category() || is_tag() || is_tax() ) {
$term = $wp_query->get_queried_object();
$metadesc = wpseo_get_term_meta( $term, $term->taxonomy, 'desc' );
if ( ! $metadesc && isset( $this->options['metadesc-' . $term->taxonomy] ) )
$metadesc = wpseo_replace_vars( $this->options['metadesc-' . $term->taxonomy], (array) $term );
}
else if ( is_author() ) {
$author_id = get_query_var( 'author' );
$metadesc = get_the_author_meta( 'wpseo_metadesc', $author_id );
if ( ! $metadesc && isset( $this->options['metadesc-author'] ) )
$metadesc = wpseo_replace_vars( $this->options['metadesc-author'], (array) $wp_query->get_queried_object() );
}
else if ( function_exists( 'is_post_type_archive' ) && is_post_type_archive() ) {
$post_type = get_query_var( 'post_type' );
if ( isset( $this->options['metadesc-ptarchive-' . $post_type] ) && '' != $this->options['metadesc-ptarchive-' . $post_type] ) {
$metadesc = wpseo_replace_vars( $this->options['metadesc-ptarchive-' . $post_type], (array) $wp_query->get_queried_object() );
}
}
else if ( is_archive() ) {
if ( isset( $this->options['metadesc-archive'] ) && '' != $this->options['metadesc-archive'] ) {
$metadesc = wpseo_replace_vars( $this->options['metadesc-archive'], (array) $wp_query->get_queried_object() );
}
}
}
$metadesc = apply_filters( 'wpseo_metadesc', trim( $metadesc ) );
if ( $echo !== false ) {
if ( ! empty( $metadesc ) )
echo '<meta name="description" content="' . esc_attr( strip_tags( stripslashes( $metadesc ) ) ) . '"/>' . "\n";
else if ( current_user_can( 'manage_options' ) && is_singular() )
echo '<!-- ' . __( 'Admin only notice: this page doesn\'t show a meta description because it doesn\'t have one, either write it for this page specifically or go into the SEO -> Titles menu and set up a template.', 'wordpress-seo' ) . ' -->' . "\n";
}
else {
return $metadesc;
}
}
/**
* Based on the redirect meta value, this function determines whether it should redirect the current post / page.
*
* @return mixed
*/
function page_redirect() {
if ( is_singular() ) {
global $post;
if ( ! isset( $post ) )
return;
$redir = wpseo_get_value( 'redirect', $post->ID );
if ( ! empty( $redir ) ) {
wp_redirect( $redir, 301 );
exit;
}
}
}
/**
* Outputs noindex values for the current page.
*/
public function noindex_page() {
echo '<meta name="robots" content="noindex" />' . "\n";
}
/**
* Send a Robots HTTP header preventing feeds from being indexed in the search results while allowing search engines to follow the links in the feed.
*
* @since 1.1.7
*/
public function noindex_feed() {
if ( is_feed() && headers_sent() === false )
header( "X-Robots-Tag: noindex,follow", true );
}
/**
* Adds rel="nofollow" to a link, only used for login / registration links.
*
* @param string $input The link element as a string.
*
* @return string
*/
public function nofollow_link( $input ) {
return str_replace( '<a ', '<a rel="nofollow" ', $input );
}
/**
* When certain archives are disabled, this redirects those to the homepage.
*/
function archive_redirect() {
global $wp_query;
if (
( isset( $this->options['disable-date'] ) && $this->options['disable-date'] && $wp_query->is_date ) ||
( isset( $this->options['disable-author'] ) && $this->options['disable-author'] && $wp_query->is_author ) ||
( isset( $this->options['disable-post_formats'] ) && $this->options['disable-post_formats'] && $wp_query->is_tax( 'post_format' ) )
) {
wp_safe_redirect( get_bloginfo( 'url' ), 301 );
exit;
}
}
/**
* If the option to redirect attachments to their parent is checked, this performs the redirect.
*
* An extra check is done for when the attachment has no parent.
*/
function attachment_redirect() {
global $post;
if ( is_attachment() && isset( $post->post_parent ) && is_numeric( $post->post_parent ) && $post->post_parent != 0 ) {
wp_safe_redirect( get_permalink( $post->post_parent ), 301 );
exit;
}
}
/**
* Trailing slashes for everything except is_single().
*
* Thanks to Mark Jaquith for this code.
*
* @param string $url
* @param string $type
*
* @return string
*/
function add_trailingslash( $url, $type ) {
if ( 'single' === $type || 'single_paged' === $type ) {
return $url;
}
else {
return trailingslashit( $url );
}
}
/**
* Removes the ?replytocom variable from the link, replacing it with a #comment-<number> anchor.
*
* @todo Should this function also allow for relative urls ?
*
* @param string $link The comment link as a string.
*
* @return string
*/
public function remove_reply_to_com( $link ) {
return preg_replace( '`href=(["\'])(?:.*(?:\?|&|&)replytocom=(\d+)#respond)`', 'href=$1#comment-$2', $link );
}
/**
* Redirect out the ?replytocom variables when cleanreplytocom is enabled
*
* @since 1.4.13
*/
function replytocom_redirect() {
if ( ! isset( $this->options['cleanreplytocom'] ) || ! $this->options['cleanreplytocom'] )
return;
if ( isset( $_GET['replytocom'] ) && is_singular() ) {
global $post;
$url = get_permalink( $post->ID );
$hash = $_GET['replytocom'];
$query_string = remove_query_arg( 'replytocom', $_SERVER['QUERY_STRING'] );
if ( ! empty( $query_string ) )
$url .= '?' . $query_string;
$url .= '#comment-' . $hash;
wp_safe_redirect( $url, 301 );
exit;
}
}
/**
* Removes unneeded query variables from the URL.
*/
public function clean_permalink() {
if ( is_robots() || get_query_var( 'sitemap' ) )
return;
global $wp_query;
// Recreate current URL
$cururl = 'http';
if ( isset( $_SERVER["HTTPS"] ) && $_SERVER["HTTPS"] == "on" ) {
$cururl .= "s";
}
$cururl .= "://";
if ( $_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443")
$cururl .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
else
$cururl .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
$properurl = '';
if ( is_singular() ) {
global $post;
if ( empty( $post ) )
$post = $wp_query->get_queried_object();
$properurl = get_permalink( $post->ID );
$page = get_query_var( 'page' );
if ( $page && $page != 1 ) {
$post = get_post( $post->ID );
$page_count = substr_count( $post->post_content, '<!--nextpage-->' );
if ( $page > ( $page_count + 1 ) )
$properurl = user_trailingslashit( trailingslashit( $properurl ) . ( $page_count + 1 ) );
else
$properurl = user_trailingslashit( trailingslashit( $properurl ) . $page );
}
// Fix reply to comment links, whoever decided this should be a GET variable?
$result = preg_match( '`(\?replytocom=[^&]+)`', $_SERVER["REQUEST_URI"], $matches );
if ( $result )
$properurl .= str_replace( '?replytocom=', '#comment-', $matches[0] );
// Prevent cleaning out posts & page previews for people capable of viewing them
if ( isset( $_GET['preview'] ) && isset( $_GET['preview_nonce'] ) && current_user_can( 'edit_post' ) )
$properurl = '';
}
else if ( is_front_page() ) {
if ( $this->is_home_posts_page() ) {
$properurl = get_bloginfo( 'url' ) . '/';
}
elseif ( $this->is_home_static_page() ) {
global $post;
$properurl = get_permalink( $post->ID );
}
}
else if ( is_category() || is_tag() || is_tax() ) {
$term = $wp_query->get_queried_object();
if ( is_feed() )
$properurl = get_term_feed_link( $term->term_id, $term->taxonomy );
else
$properurl = get_term_link( $term, $term->taxonomy );
}
else if ( is_search() ) {
$s = preg_replace( '`(%20|\+)`', ' ', get_search_query() );
$properurl = get_bloginfo( 'url' ) . '/?s=' . rawurlencode( $s );
}
else if ( is_404() ) {
if ( function_exists( 'is_multisite' ) && is_multisite() && ! is_subdomain_install() && is_main_site() ) {
if ( $cururl == get_bloginfo( 'url' ) . '/blog/' || $cururl == get_bloginfo( 'url' ) . '/blog' ) {
if ( $this->is_home_static_page() )
$properurl = get_permalink( get_option( 'page_for_posts' ) );
else
$properurl = get_bloginfo( 'url' ) . '/';
}
}
}
if ( ! empty( $properurl ) && $wp_query->query_vars['paged'] != 0 && $wp_query->post_count != 0 ) {
if ( is_search() ) {
$properurl = get_bloginfo( 'url' ) . '/page/' . $wp_query->query_vars['paged'] . '/?s=' . rawurlencode( get_search_query() );
}
else {
$properurl = user_trailingslashit( trailingslashit( $properurl ) . 'page/' . $wp_query->query_vars['paged'] );
}
}
// Prevent cleaning out the WP Subscription managers interface for everyone
foreach ( array( 'wp-subscription-manager' ) as $get ) {
if ( isset( $_GET[$get] ) ) {
$properurl = '';
}
}
// Allow plugins to register their own variables not to clean
$whitelisted_extravars = apply_filters( 'wpseo_whitelist_permalink_vars', array() );
if ( isset( $this->options['cleanpermalink-googlesitesearch'] ) && $this->options['cleanpermalink-googlesitesearch'] ) {
// Prevent cleaning out Google Site searches
$whitelisted_extravars = array_merge( $whitelisted_extravars, array( 'q', 'cx', 'debug', 'cof', 'ie', 'sa' ) );
}
if ( isset( $this->options['cleanpermalink-googlecampaign'] ) && $this->options['cleanpermalink-googlecampaign'] ) {
// Prevent cleaning out Google Analytics campaign variables
$whitelisted_extravars = array_merge( $whitelisted_extravars, array( 'utm_campaign', 'utm_medium', 'utm_source', 'utm_content', 'utm_term' ) );
}
if ( isset( $this->options['cleanpermalink-extravars'] ) && strlen( $this->options['cleanpermalink-extravars'] ) > 0 ) {
$whitelisted_extravars = array_merge( $whitelisted_extravars, explode( ',', $this->options['cleanpermalink-extravars'] ) );
}
foreach ( $whitelisted_extravars as $get ) {
if ( isset( $_GET[trim( $get )] ) ) {
$properurl = '';
}
}
if ( ! empty( $properurl ) && $cururl != $properurl ) {
wp_safe_redirect( $properurl, 301 );
exit;
}
}
/**
* Replaces the possible RSS variables with their actual values.
*
* @param string $content The RSS content that should have the variables replaced.
*
* @return string
*/
function rss_replace_vars( $content ) {
global $post;
/**
* @param bool $unsigned Whether or not to follow the links in RSS feed, defaults to true.
*
* @since 1.4.20
*/
$no_follow = apply_filters( 'nofollow_rss_links', true );
$no_follow_attr = '';
if ( $no_follow )
$no_follow_attr = 'rel="nofollow" ';
$author_link = '<a rel="' . ( ( $no_follow ) ? 'nofollow ' : '' ) . 'author" href="' . get_author_posts_url( $post->post_author ) . '">' . get_the_author() . '</a>';
$post_link = '<a ' . $no_follow_attr . 'href="' . get_permalink() . '">' . get_the_title() . "</a>";
$blog_link = '<a ' . $no_follow_attr . 'href="' . get_bloginfo( 'url' ) . '">' . get_bloginfo( 'name' ) . '</a>';
$blog_desc_link = '<a ' . $no_follow_attr . 'href="' . get_bloginfo( 'url' ) . '">' . get_bloginfo( 'name' ) . ' - ' . get_bloginfo( 'description' ) . '</a>';
$content = stripslashes( trim( $content ) );
$content = str_replace( "%%AUTHORLINK%%", $author_link, $content );
$content = str_replace( "%%POSTLINK%%", $post_link, $content );
$content = str_replace( "%%BLOGLINK%%", $blog_link, $content );
$content = str_replace( "%%BLOGDESCLINK%%", $blog_desc_link, $content );
return $content;
}
/**
* Adds the RSS footer (or header) to the full RSS feed item.
*
* @param string $content Feed item content.
*
* @return string
*/
function embed_rssfooter( $content ) {
if ( is_feed() ) {
$content = $this->embed_rss( $content, 'full' );
}
return $content;
}
/**
* Adds the RSS footer (or header) to the excerpt RSS feed item.
*
* @param string $content Feed item excerpt.
*
* @return string
*/
function embed_rssfooter_excerpt( $content ) {
if ( is_feed() ) {
$content = $this->embed_rss( $content, 'excerpt' );
}
return $content;
}
/**
* Adds the RSS footer and/or header to an RSS feed item.
*
* @since 1.4.14
*
* @param string $content Feed item content.
* @param string $context Feed item context, either 'excerpt' or 'full'.
*
* @return string
*/
function embed_rss( $content, $context = 'full' ) {
if ( is_feed() ) {
$before = '';
$after = '';
if ( isset( $this->options['rssbefore'] ) && trim( $this->options['rssbefore'] ) !== '' ) {
$before = $this->rss_replace_vars( $this->options['rssbefore'] );
$before = ( $before !== '' ) ? '<p>' . $before . '</p>' : '';
}
if ( isset( $this->options['rssafter'] ) && trim( $this->options['rssafter'] ) !== '' ) {
$after = $this->rss_replace_vars( $this->options['rssafter'] );
$after = ( $after !== '' ) ? '<p>' . $after . '</p>' : '';
}
if ( $before !== '' || $after !== '' ) {
if ( ( isset( $context ) && $context === 'excerpt' ) && trim( $content ) !== '' ) {
$content = '<p>' . $content . '</p>';
}
$content = $before . $content . $after;
}
}
return $content;
}
/**
* Used in the force rewrite functionality this retrieves the output, replaces the title with the proper SEO
* title and then flushes the output.
*/
function flush_cache() {
global $wp_query, $wpseo_ob, $sep;
if ( ! $wpseo_ob )
return;
$content = ob_get_contents();
$old_wp_query = $wp_query;
wp_reset_query();
$title = $this->title( '', $sep );
// Find all titles, strip them out and add the new one in within the debug marker, so it's easily identified whether a site uses force rewrite.
if ( preg_match_all( '`<title>(.*)?<\/title>`i', $content, $matches ) ) {
$count = count( $matches[0] );
if ( $count > 0 ) {
$i = 0;
while ( $count > $i ) {
$content = str_replace( $matches[0][$i], '', $content );
$i ++;
}
}
}
$content = str_replace( $this->debug_marker( false ), $this->debug_marker( false ) . "\n" . '<title>' . $title . '</title>', $content );
ob_end_clean();
$GLOBALS['wp_query'] = $old_wp_query;
echo $content;
}
/**
* Starts the output buffer so it can later be fixed by flush_cache()
*/
function force_rewrite_output_buffer() {
global $wpseo_ob;
$wpseo_ob = true;
ob_start();
}
/**
* Function used in testing whether the title should be force rewritten or not.
*
* @param string $title
*
* @return string
*/
function title_test_helper( $title ) {
$this->options['title_test']++;
update_option( 'wpseo_titles', $this->options );
// Prevent this setting from being on forever when something breaks, as it breaks caching.
if ( $this->options['title_test'] > 10 ) {
unset( $this->options['title_test'] );
update_option( 'wpseo_titles', $this->options );
return $title;
}
if ( ! defined( 'DONOTCACHEPAGE' ) )
define( 'DONOTCACHEPAGE', true );
if ( ! defined( 'DONOTCACHCEOBJECT' ) )
define( 'DONOTCACHCEOBJECT', true );
if ( ! defined( 'DONOTMINIFY' ) )
define( 'DONOTMINIFY', true );
global $wp_version;
if ( $_SERVER['HTTP_USER_AGENT'] == "WordPress/${wp_version}; " . get_bloginfo( 'url' ) . " - Yoast" )
return 'This is a Yoast Test Title';
return $title;
}
}
function initialize_wpseo_front() {
global $wpseo_front;
$wpseo_front = new WPSEO_Frontend;
}
add_action( 'init', 'initialize_wpseo_front' );