-
Notifications
You must be signed in to change notification settings - Fork 882
/
class-metabox.php
1218 lines (1028 loc) · 41.6 KB
/
class-metabox.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
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin
*/
use Yoast\WP\SEO\Conditionals\Third_Party\Jetpack_Boost_Active_Conditional;
use Yoast\WP\SEO\Conditionals\Third_Party\Jetpack_Boost_Not_Premium_Conditional;
use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional;
use Yoast\WP\SEO\Editors\Application\Site\Website_Information_Repository;
use Yoast\WP\SEO\Presenters\Admin\Alert_Presenter;
use Yoast\WP\SEO\Presenters\Admin\Meta_Fields_Presenter;
/**
* This class generates the metabox on the edit post / page as well as contains all page analysis functionality.
*/
class WPSEO_Metabox extends WPSEO_Meta {
/**
* Whether the social tab is enabled.
*
* @var bool
*/
private $social_is_enabled;
/**
* Helper to determine whether the SEO analysis is enabled.
*
* @var WPSEO_Metabox_Analysis_SEO
*/
protected $seo_analysis;
/**
* Helper to determine whether the readability analysis is enabled.
*
* @var WPSEO_Metabox_Analysis_Readability
*/
protected $readability_analysis;
/**
* Helper to determine whether the inclusive language analysis is enabled.
*
* @var WPSEO_Metabox_Analysis_Inclusive_Language
*/
protected $inclusive_language_analysis;
/**
* The metabox editor object.
*
* @var WPSEO_Metabox_Editor
*/
protected $editor;
/**
* The Metabox post.
*
* @var WP_Post
*/
protected $post = null;
/**
* Whether the advanced metadata is enabled.
*
* @var bool
*/
protected $is_advanced_metadata_enabled;
/**
* Class constructor.
*/
public function __construct() {
if ( $this->is_internet_explorer() ) {
add_action( 'add_meta_boxes', [ $this, 'internet_explorer_metabox' ] );
return;
}
add_action( 'add_meta_boxes', [ $this, 'add_meta_box' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue' ] );
add_action( 'wp_insert_post', [ $this, 'save_postdata' ] );
add_action( 'edit_attachment', [ $this, 'save_postdata' ] );
add_action( 'add_attachment', [ $this, 'save_postdata' ] );
add_action( 'admin_init', [ $this, 'translate_meta_boxes' ] );
$this->editor = new WPSEO_Metabox_Editor();
$this->editor->register_hooks();
$this->social_is_enabled = WPSEO_Options::get( 'opengraph', false ) || WPSEO_Options::get( 'twitter', false );
$this->is_advanced_metadata_enabled = WPSEO_Capability_Utils::current_user_can( 'wpseo_edit_advanced_metadata' ) || WPSEO_Options::get( 'disableadvanced_meta' ) === false;
$this->seo_analysis = new WPSEO_Metabox_Analysis_SEO();
$this->readability_analysis = new WPSEO_Metabox_Analysis_Readability();
$this->inclusive_language_analysis = new WPSEO_Metabox_Analysis_Inclusive_Language();
}
/**
* Checks whether the request comes from an IE 11 browser.
*
* @return bool Whether the request comes from an IE 11 browser.
*/
public static function is_internet_explorer() {
if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
return false;
}
$user_agent = sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) );
if ( stripos( $user_agent, 'Trident/7.0' ) === false ) {
return false;
}
return true;
}
/**
* Adds an alternative metabox for internet explorer users.
*
* @return void
*/
public function internet_explorer_metabox() {
$post_types = WPSEO_Post_Type::get_accessible_post_types();
$post_types = array_filter( $post_types, [ $this, 'display_metabox' ] );
if ( ! is_array( $post_types ) || $post_types === [] ) {
return;
}
$product_title = $this->get_product_title();
foreach ( $post_types as $post_type ) {
add_filter( "postbox_classes_{$post_type}_wpseo_meta", [ $this, 'wpseo_metabox_class' ] );
add_meta_box(
'wpseo_meta',
$product_title,
[ $this, 'render_internet_explorer_notice' ],
$post_type,
'normal',
apply_filters( 'wpseo_metabox_prio', 'high' ),
[ '__block_editor_compatible_meta_box' => true ]
);
}
}
/**
* Renders the content for the internet explorer metabox.
*
* @return void
*/
public function render_internet_explorer_notice() {
$content = sprintf(
/* translators: 1: Link start tag to the Firefox website, 2: Link start tag to the Chrome website, 3: Link start tag to the Edge website, 4: Link closing tag. */
esc_html__( 'The browser you are currently using is unfortunately rather dated. Since we strive to give you the best experience possible, we no longer support this browser. Instead, please use %1$sFirefox%4$s, %2$sChrome%4$s or %3$sMicrosoft Edge%4$s.', 'wordpress-seo' ),
'<a href="https://www.mozilla.org/firefox/new/">',
'<a href="https://www.google.com/chrome/">',
'<a href="https://www.microsoft.com/windows/microsoft-edge">',
'</a>'
);
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped above.
echo new Alert_Presenter( $content );
}
/**
* Translates text strings for use in the meta box.
*
* IMPORTANT: if you want to add a new string (option) somewhere, make sure you add that array key to
* the main meta box definition array in the class WPSEO_Meta() as well!!!!
*
* @return void
*/
public static function translate_meta_boxes() {
WPSEO_Meta::$meta_fields['general']['title']['title'] = __( 'SEO title', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['general']['metadesc']['title'] = __( 'Meta description', 'wordpress-seo' );
/* translators: %s expands to the post type name. */
WPSEO_Meta::$meta_fields['advanced']['meta-robots-noindex']['title'] = __( 'Allow search engines to show this %s in search results?', 'wordpress-seo' );
if ( (string) get_option( 'blog_public' ) === '0' ) {
WPSEO_Meta::$meta_fields['advanced']['meta-robots-noindex']['description'] = '<span class="error-message">' . __( 'Warning: even though you can set the meta robots setting here, the entire site is set to noindex in the sitewide privacy settings, so these settings won\'t have an effect.', 'wordpress-seo' ) . '</span>';
}
/* translators: %1$s expands to Yes or No, %2$s expands to the post type name.*/
WPSEO_Meta::$meta_fields['advanced']['meta-robots-noindex']['options']['0'] = __( 'Default for %2$s, currently: %1$s', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['meta-robots-noindex']['options']['2'] = __( 'Yes', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['meta-robots-noindex']['options']['1'] = __( 'No', 'wordpress-seo' );
/* translators: %1$s expands to the post type name.*/
WPSEO_Meta::$meta_fields['advanced']['meta-robots-']['title'] = __( 'Should search engines follow links on this %1$s?', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['meta-robots-']['options']['0'] = __( 'Yes', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['meta-robots-']['options']['1'] = __( 'No', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['meta-robots-adv']['title'] = __( 'Meta robots advanced', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['meta-robots-adv']['description'] = __( 'If you want to apply advanced <code>meta</code> robots settings for this page, please define them in the following field.', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['meta-robots-adv']['options']['noimageindex'] = __( 'No Image Index', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['meta-robots-adv']['options']['noarchive'] = __( 'No Archive', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['meta-robots-adv']['options']['nosnippet'] = __( 'No Snippet', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['bctitle']['title'] = __( 'Breadcrumbs Title', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['bctitle']['description'] = __( 'Title to use for this page in breadcrumb paths', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['canonical']['title'] = __( 'Canonical URL', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['canonical']['description'] = sprintf(
/* translators: 1: link open tag; 2: link close tag. */
__( 'The canonical URL that this page should point to. Leave empty to default to permalink. %1$sCross domain canonical%2$s supported too.', 'wordpress-seo' ),
'<a href="https://googlewebmastercentral.blogspot.com/2009/12/handling-legitimate-cross-domain.html" target="_blank" rel="noopener">',
WPSEO_Admin_Utils::get_new_tab_message() . '</a>'
);
/* translators: %s expands to the post type name. */
WPSEO_Meta::$meta_fields['advanced']['wordproof_timestamp']['title'] = __( 'Timestamp this %s', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['wordproof_timestamp']['description'] = __( 'Use WordProof to timestamp this page to comply with legal regulations and join the fight for a more transparant and accountable internet.', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['wordproof_timestamp']['options']['0'] = __( 'Off', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['wordproof_timestamp']['options']['1'] = __( 'On', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['wordproof_timestamp']['type'] = 'hidden';
WPSEO_Meta::$meta_fields['advanced']['redirect']['title'] = __( '301 Redirect', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['redirect']['description'] = __( 'The URL that this page should redirect to.', 'wordpress-seo' );
do_action( 'wpseo_tab_translate' );
}
/**
* Determines whether the metabox should be shown for the passed identifier.
*
* By default the check is done for post types, but can also be used for taxonomies.
*
* @param string|null $identifier The identifier to check.
* @param string $type The type of object to check. Defaults to post_type.
*
* @return bool Whether or not the metabox should be displayed.
*/
public function display_metabox( $identifier = null, $type = 'post_type' ) {
return WPSEO_Utils::is_metabox_active( $identifier, $type );
}
/**
* Adds the Yoast SEO meta box to the edit boxes in the edit post, page,
* attachment, and custom post types pages.
*
* @return void
*/
public function add_meta_box() {
$post_types = WPSEO_Post_Type::get_accessible_post_types();
$post_types = array_filter( $post_types, [ $this, 'display_metabox' ] );
if ( ! is_array( $post_types ) || $post_types === [] ) {
return;
}
$product_title = $this->get_product_title();
foreach ( $post_types as $post_type ) {
add_filter( "postbox_classes_{$post_type}_wpseo_meta", [ $this, 'wpseo_metabox_class' ] );
add_meta_box(
'wpseo_meta',
$product_title,
[ $this, 'meta_box' ],
$post_type,
'normal',
apply_filters( 'wpseo_metabox_prio', 'high' ),
[ '__block_editor_compatible_meta_box' => true ]
);
}
}
/**
* Adds CSS classes to the meta box.
*
* @param string[] $classes An array of postbox CSS classes.
*
* @return string[] List of classes that will be applied to the editbox container.
*/
public function wpseo_metabox_class( $classes ) {
$classes[] = 'yoast wpseo-metabox';
return $classes;
}
/**
* Passes variables to js for use with the post-scraper.
*
* @return array<string,string|array<string|int|bool>|bool|int>
*/
public function get_metabox_script_data() {
$permalink = $this->get_permalink();
$post_formatter = new WPSEO_Metabox_Formatter(
new WPSEO_Post_Metabox_Formatter( $this->get_metabox_post(), [], $permalink )
);
$values = $post_formatter->get_values();
/** This filter is documented in admin/filters/class-cornerstone-filter.php. */
$post_types = apply_filters( 'wpseo_cornerstone_post_types', WPSEO_Post_Type::get_accessible_post_types() );
if ( $values['cornerstoneActive'] && ! in_array( $this->get_metabox_post()->post_type, $post_types, true ) ) {
$values['cornerstoneActive'] = false;
}
if ( $values['semrushIntegrationActive'] && $this->post->post_type === 'attachment' ) {
$values['semrushIntegrationActive'] = 0;
}
if ( $values['wincherIntegrationActive'] && $this->post->post_type === 'attachment' ) {
$values['wincherIntegrationActive'] = 0;
}
return $values;
}
/**
* Determines whether or not the current post type has registered taxonomies.
*
* @return bool Whether the current post type has taxonomies.
*/
private function current_post_type_has_taxonomies() {
$post_taxonomies = get_object_taxonomies( get_post_type() );
return ! empty( $post_taxonomies );
}
/**
* Determines the scope based on the post type.
* This can be used by the replacevar plugin to determine if a replacement needs to be executed.
*
* @return string String describing the current scope.
*/
private function determine_scope() {
if ( $this->get_metabox_post()->post_type === 'page' ) {
return 'page';
}
return 'post';
}
/**
* Outputs the meta box.
*
* @return void
*/
public function meta_box() {
$this->render_hidden_fields();
$this->render_tabs();
}
/**
* Renders the metabox hidden fields.
*
* @return void
*/
protected function render_hidden_fields() {
wp_nonce_field( 'yoast_free_metabox', 'yoast_free_metabox_nonce' );
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in class.
echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'general' );
if ( $this->is_advanced_metadata_enabled ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in class.
echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'advanced' );
}
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in class.
echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'schema', $this->get_metabox_post()->post_type );
if ( $this->social_is_enabled ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in class.
echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'social' );
}
/**
* Filter: 'wpseo_content_meta_section_content' - Allow filtering the metabox content before outputting.
*
* @param string $post_content The metabox content string.
*/
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output should be escaped in the filter.
echo apply_filters( 'wpseo_content_meta_section_content', '' );
}
/**
* Renders the metabox tabs.
*
* @return void
*/
protected function render_tabs() {
echo '<div class="wpseo-metabox-content">';
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: $this->get_product_title() returns a hard-coded string.
printf( '<div class="wpseo-metabox-menu"><ul role="tablist" class="yoast-aria-tabs" aria-label="%s">', $this->get_product_title() );
$tabs = $this->get_tabs();
foreach ( $tabs as $tab ) {
if ( $tab->name === 'premium' ) {
continue;
}
$tab->display_link();
}
echo '</ul></div>';
foreach ( $tabs as $tab ) {
$tab->display_content();
}
echo '</div>';
}
/**
* Returns the relevant metabox tabs for the current view.
*
* @return WPSEO_Metabox_Section[]
*/
private function get_tabs() {
$tabs = [];
$label = __( 'SEO', 'wordpress-seo' );
if ( $this->seo_analysis->is_enabled() ) {
$label = '<span class="wpseo-score-icon-container" id="wpseo-seo-score-icon"></span>' . $label;
}
$tabs[] = new WPSEO_Metabox_Section_React( 'content', $label );
if ( $this->readability_analysis->is_enabled() ) {
$tabs[] = new WPSEO_Metabox_Section_Readability();
}
if ( $this->inclusive_language_analysis->is_enabled() ) {
$tabs[] = new WPSEO_Metabox_Section_Inclusive_Language();
}
if ( $this->is_advanced_metadata_enabled ) {
$tabs[] = new WPSEO_Metabox_Section_React(
'schema',
'<span class="wpseo-schema-icon"></span>' . __( 'Schema', 'wordpress-seo' ),
''
);
}
if ( $this->social_is_enabled ) {
$tabs[] = new WPSEO_Metabox_Section_React(
'social',
'<span class="dashicons dashicons-share"></span>' . __( 'Social', 'wordpress-seo' ),
'',
[
'html_after' => '<div id="wpseo-section-social"></div>',
]
);
}
$tabs = array_merge( $tabs, $this->get_additional_tabs() );
return $tabs;
}
/**
* Returns the metabox tabs that have been added by other plugins.
*
* @return WPSEO_Metabox_Section_Additional[]
*/
protected function get_additional_tabs() {
$tabs = [];
/**
* Private filter: 'yoast_free_additional_metabox_sections'.
*
* Meant for internal use only. Allows adding additional tabs to the Yoast SEO metabox.
*
* @since 11.9
*
* @param array[] $tabs {
* An array of arrays with tab specifications.
*
* @type array $tab {
* A tab specification.
*
* @type string $name The name of the tab. Used in the HTML IDs, href and aria properties.
* @type string $link_content The content of the tab link.
* @type string $content The content of the tab.
* @type array $options {
* Optional. Extra options.
*
* @type string $link_class Optional. The class for the tab link.
* @type string $link_aria_label Optional. The aria label of the tab link.
* }
* }
* }
*/
$requested_tabs = apply_filters( 'yoast_free_additional_metabox_sections', [] );
foreach ( $requested_tabs as $tab ) {
if ( is_array( $tab ) && array_key_exists( 'name', $tab ) && array_key_exists( 'link_content', $tab ) && array_key_exists( 'content', $tab ) ) {
$options = array_key_exists( 'options', $tab ) ? $tab['options'] : [];
$tabs[] = new WPSEO_Metabox_Section_Additional(
$tab['name'],
$tab['link_content'],
$tab['content'],
$options
);
}
}
return $tabs;
}
/**
* Adds a line in the meta box.
*
* @todo [JRF] Check if $class is added appropriately everywhere.
*
* @param string[] $meta_field_def Contains the vars based on which output is generated.
* @param string $key Internal key (without prefix).
*
* @return string
*/
public function do_meta_box( $meta_field_def, $key = '' ) {
$content = '';
$esc_form_key = esc_attr( WPSEO_Meta::$form_prefix . $key );
$meta_value = WPSEO_Meta::get_value( $key, $this->get_metabox_post()->ID );
$class = '';
if ( isset( $meta_field_def['class'] ) && $meta_field_def['class'] !== '' ) {
$class = ' ' . $meta_field_def['class'];
}
$placeholder = '';
if ( isset( $meta_field_def['placeholder'] ) && $meta_field_def['placeholder'] !== '' ) {
$placeholder = $meta_field_def['placeholder'];
}
$aria_describedby = '';
$description = '';
if ( isset( $meta_field_def['description'] ) ) {
$aria_describedby = ' aria-describedby="' . $esc_form_key . '-desc"';
$description = '<p id="' . $esc_form_key . '-desc" class="yoast-metabox__description">' . $meta_field_def['description'] . '</p>';
}
// Add a hide_on_pages option that returns nothing when the field is rendered on a page.
if ( isset( $meta_field_def['hide_on_pages'] ) && $meta_field_def['hide_on_pages'] && get_post_type() === 'page' ) {
return '';
}
switch ( $meta_field_def['type'] ) {
case 'text':
$ac = '';
if ( isset( $meta_field_def['autocomplete'] ) && $meta_field_def['autocomplete'] === false ) {
$ac = 'autocomplete="off" ';
}
if ( $placeholder !== '' ) {
$placeholder = ' placeholder="' . esc_attr( $placeholder ) . '"';
}
$content .= '<input type="text"' . $placeholder . ' id="' . $esc_form_key . '" ' . $ac . 'name="' . $esc_form_key . '" value="' . esc_attr( $meta_value ) . '" class="large-text' . $class . '"' . $aria_describedby . '/>';
break;
case 'url':
if ( $placeholder !== '' ) {
$placeholder = ' placeholder="' . esc_attr( $placeholder ) . '"';
}
$content .= '<input type="url"' . $placeholder . ' id="' . $esc_form_key . '" name="' . $esc_form_key . '" value="' . esc_attr( urldecode( $meta_value ) ) . '" class="large-text' . $class . '"' . $aria_describedby . '/>';
break;
case 'textarea':
$rows = 3;
if ( isset( $meta_field_def['rows'] ) && $meta_field_def['rows'] > 0 ) {
$rows = $meta_field_def['rows'];
}
$content .= '<textarea class="large-text' . $class . '" rows="' . esc_attr( $rows ) . '" id="' . $esc_form_key . '" name="' . $esc_form_key . '"' . $aria_describedby . '>' . esc_textarea( $meta_value ) . '</textarea>';
break;
case 'hidden':
$default = '';
if ( isset( $meta_field_def['default'] ) ) {
$default = sprintf( ' data-default="%s"', esc_attr( $meta_field_def['default'] ) );
}
$content .= '<input type="hidden" id="' . $esc_form_key . '" name="' . $esc_form_key . '" value="' . esc_attr( $meta_value ) . '"' . $default . '/>' . "\n";
break;
case 'select':
if ( isset( $meta_field_def['options'] ) && is_array( $meta_field_def['options'] ) && $meta_field_def['options'] !== [] ) {
$content .= '<select name="' . $esc_form_key . '" id="' . $esc_form_key . '" class="yoast' . $class . '">';
foreach ( $meta_field_def['options'] as $val => $option ) {
$selected = selected( $meta_value, $val, false );
$content .= '<option ' . $selected . ' value="' . esc_attr( $val ) . '">' . esc_html( $option ) . '</option>';
}
unset( $val, $option, $selected );
$content .= '</select>';
}
break;
case 'multiselect':
if ( isset( $meta_field_def['options'] ) && is_array( $meta_field_def['options'] ) && $meta_field_def['options'] !== [] ) {
// Set $meta_value as $selected_arr.
$selected_arr = $meta_value;
// If the multiselect field is 'meta-robots-adv' we should explode on ,.
if ( $key === 'meta-robots-adv' ) {
$selected_arr = explode( ',', $meta_value );
}
if ( ! is_array( $selected_arr ) ) {
$selected_arr = (array) $selected_arr;
}
$options_count = count( $meta_field_def['options'] );
$content .= '<select multiple="multiple" size="' . esc_attr( $options_count ) . '" name="' . $esc_form_key . '[]" id="' . $esc_form_key . '" class="yoast' . $class . '"' . $aria_describedby . '>';
foreach ( $meta_field_def['options'] as $val => $option ) {
$selected = '';
if ( in_array( $val, $selected_arr, true ) ) {
$selected = ' selected="selected"';
}
$content .= '<option ' . $selected . ' value="' . esc_attr( $val ) . '">' . esc_html( $option ) . '</option>';
}
$content .= '</select>';
unset( $val, $option, $selected, $selected_arr, $options_count );
}
break;
case 'checkbox':
$checked = checked( $meta_value, 'on', false );
$expl = ( isset( $meta_field_def['expl'] ) ) ? esc_html( $meta_field_def['expl'] ) : '';
$content .= '<input type="checkbox" id="' . $esc_form_key . '" name="' . $esc_form_key . '" ' . $checked . ' value="on" class="yoast' . $class . '"' . $aria_describedby . '/> <label for="' . $esc_form_key . '">' . $expl . '</label>';
unset( $checked, $expl );
break;
case 'radio':
if ( isset( $meta_field_def['options'] ) && is_array( $meta_field_def['options'] ) && $meta_field_def['options'] !== [] ) {
foreach ( $meta_field_def['options'] as $val => $option ) {
$checked = checked( $meta_value, $val, false );
$content .= '<input type="radio" ' . $checked . ' id="' . $esc_form_key . '_' . esc_attr( $val ) . '" name="' . $esc_form_key . '" value="' . esc_attr( $val ) . '"/> <label for="' . $esc_form_key . '_' . esc_attr( $val ) . '">' . esc_html( $option ) . '</label> ';
}
unset( $val, $option, $checked );
}
break;
case 'upload':
$content .= '<input'
. ' id="' . $esc_form_key . '"'
. ' type="text"'
. ' size="36"'
. ' class="' . $class . '"'
. ' name="' . $esc_form_key . '"'
. ' value="' . esc_attr( $meta_value ) . '"' . $aria_describedby
. ' readonly="readonly"'
. ' /> ';
$content .= '<input'
. ' id="' . esc_attr( $esc_form_key ) . '_button"'
. ' class="wpseo_image_upload_button button"'
. ' data-target="' . esc_attr( $esc_form_key ) . '"'
. ' data-target-id="' . esc_attr( $esc_form_key ) . '-id"'
. ' type="button"'
. ' value="' . esc_attr__( 'Upload Image', 'wordpress-seo' ) . '"'
. ' /> ';
$content .= '<input'
. ' class="wpseo_image_remove_button button"'
. ' type="button"'
. ' value="' . esc_attr__( 'Clear Image', 'wordpress-seo' ) . '"'
. ' />';
break;
}
$html = '';
if ( $content === '' ) {
$content = apply_filters( 'wpseo_do_meta_box_field_' . $key, $content, $meta_value, $esc_form_key, $meta_field_def, $key );
}
if ( $content !== '' ) {
$title = esc_html( $meta_field_def['title'] );
// By default, use the field title as a label element.
$label = '<label for="' . $esc_form_key . '">' . $title . '</label>';
// Set the inline help and help panel, if any.
$help_button = '';
$help_panel = '';
if ( isset( $meta_field_def['help'] ) && $meta_field_def['help'] !== '' ) {
$help = new WPSEO_Admin_Help_Panel( $key, $meta_field_def['help-button'], $meta_field_def['help'] );
$help_button = $help->get_button_html();
$help_panel = $help->get_panel_html();
}
// If it's a set of radio buttons, output proper fieldset and legend.
if ( $meta_field_def['type'] === 'radio' ) {
return '<fieldset><legend>' . $title . '</legend>' . $help_button . $help_panel . $content . $description . '</fieldset>';
}
// If it's a single checkbox, ignore the title.
if ( $meta_field_def['type'] === 'checkbox' ) {
$label = '';
}
// Other meta box content or form fields.
if ( $meta_field_def['type'] === 'hidden' ) {
$html = $content;
}
else {
$html = $label . $description . $help_button . $help_panel . $content;
}
}
return $html;
}
/**
* Saves the WP SEO metadata for posts.
*
* {@internal $_POST parameters are validated via sanitize_post_meta().}}
*
* @param int $post_id Post ID.
*
* @return bool|void Boolean false if invalid save post request.
*/
public function save_postdata( $post_id ) {
// Bail if this is a multisite installation and the site has been switched.
if ( is_multisite() && ms_is_switched() ) {
return false;
}
if ( $post_id === null ) {
return false;
}
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized in wp_verify_none.
if ( ! isset( $_POST['yoast_free_metabox_nonce'] ) || ! wp_verify_nonce( wp_unslash( $_POST['yoast_free_metabox_nonce'] ), 'yoast_free_metabox' ) ) {
return false;
}
if ( wp_is_post_revision( $post_id ) ) {
$post_id = wp_is_post_revision( $post_id );
}
/**
* Determine we're not accidentally updating a different post.
* We can't use filter_input here as the ID isn't available at this point, other than in the $_POST data.
*/
if ( ! isset( $_POST['ID'] ) || $post_id !== (int) $_POST['ID'] ) {
return false;
}
clean_post_cache( $post_id );
$post = get_post( $post_id );
if ( ! is_object( $post ) ) {
// Non-existent post.
return false;
}
do_action( 'wpseo_save_compare_data', $post );
$social_fields = [];
if ( $this->social_is_enabled ) {
$social_fields = WPSEO_Meta::get_meta_field_defs( 'social' );
}
$meta_boxes = apply_filters( 'wpseo_save_metaboxes', [] );
$meta_boxes = array_merge(
$meta_boxes,
WPSEO_Meta::get_meta_field_defs( 'general', $post->post_type ),
WPSEO_Meta::get_meta_field_defs( 'advanced' ),
$social_fields,
WPSEO_Meta::get_meta_field_defs( 'schema', $post->post_type )
);
foreach ( $meta_boxes as $key => $meta_box ) {
// If analysis is disabled remove that analysis score value from the DB.
if ( $this->is_meta_value_disabled( $key ) ) {
WPSEO_Meta::delete( $key, $post_id );
continue;
}
$data = null;
$field_name = WPSEO_Meta::$form_prefix . $key;
if ( $meta_box['type'] === 'checkbox' ) {
$data = isset( $_POST[ $field_name ] ) ? 'on' : 'off';
}
else {
if ( isset( $_POST[ $field_name ] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- We're preparing to do just that.
$data = wp_unslash( $_POST[ $field_name ] );
// For multi-select.
if ( is_array( $data ) ) {
$data = array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], $data );
}
if ( is_string( $data ) ) {
$data = ( $key !== 'canonical' ) ? WPSEO_Utils::sanitize_text_field( $data ) : WPSEO_Utils::sanitize_url( $data );
}
}
// Reset options when no entry is present with multiselect - only applies to `meta-robots-adv` currently.
if ( ! isset( $_POST[ $field_name ] ) && ( $meta_box['type'] === 'multiselect' ) ) {
$data = [];
}
}
if ( $data !== null ) {
WPSEO_Meta::set_value( $key, $data, $post_id );
}
}
do_action( 'wpseo_saved_postdata' );
}
/**
* Determines if the given meta value key is disabled.
*
* @param string $key The key of the meta value.
*
* @return bool Whether the given meta value key is disabled.
*/
public function is_meta_value_disabled( $key ) {
if ( $key === 'linkdex' && ! $this->seo_analysis->is_enabled() ) {
return true;
}
if ( $key === 'content_score' && ! $this->readability_analysis->is_enabled() ) {
return true;
}
if ( $key === 'inclusive_language_score' && ! $this->inclusive_language_analysis->is_enabled() ) {
return true;
}
return false;
}
/**
* Enqueues all the needed JS and CSS.
*
* @todo [JRF => whomever] Create css/metabox-mp6.css file and add it to the below allowed colors array when done.
*
* @return void
*/
public function enqueue() {
global $pagenow;
$asset_manager = new WPSEO_Admin_Asset_Manager();
$is_editor = self::is_post_overview( $pagenow ) || self::is_post_edit( $pagenow );
if ( self::is_post_overview( $pagenow ) ) {
$asset_manager->enqueue_style( 'edit-page' );
$asset_manager->enqueue_script( 'edit-page' );
return;
}
/* Filter 'wpseo_always_register_metaboxes_on_admin' documented in wpseo-main.php */
if ( ( $is_editor === false && apply_filters( 'wpseo_always_register_metaboxes_on_admin', false ) === false ) || $this->display_metabox() === false ) {
return;
}
$post_id = get_queried_object_id();
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
if ( empty( $post_id ) && isset( $_GET['post'] ) && is_string( $_GET['post'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
$post_id = sanitize_text_field( wp_unslash( $_GET['post'] ) );
}
if ( $post_id !== 0 ) {
// Enqueue files needed for upload functionality.
wp_enqueue_media( [ 'post' => $post_id ] );
}
$asset_manager->enqueue_style( 'metabox-css' );
$asset_manager->enqueue_style( 'scoring' );
$asset_manager->enqueue_style( 'monorepo' );
$asset_manager->enqueue_style( 'ai-generator' );
$is_block_editor = WP_Screen::get()->is_block_editor();
$post_edit_handle = 'post-edit';
if ( ! $is_block_editor ) {
$post_edit_handle = 'post-edit-classic';
}
$asset_manager->enqueue_script( $post_edit_handle );
$asset_manager->enqueue_style( 'admin-css' );
/**
* Removes the emoji script as it is incompatible with both React and any
* contenteditable fields.
*/
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
$asset_manager->localize_script( $post_edit_handle, 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
$plugins_script_data = [
'replaceVars' => [
'no_parent_text' => __( '(no parent)', 'wordpress-seo' ),
'replace_vars' => $this->get_replace_vars(),
'hidden_replace_vars' => $this->get_hidden_replace_vars(),
'recommended_replace_vars' => $this->get_recommended_replace_vars(),
'scope' => $this->determine_scope(),
'has_taxonomies' => $this->current_post_type_has_taxonomies(),
],
'shortcodes' => [
'wpseo_shortcode_tags' => $this->get_valid_shortcode_tags(),
'wpseo_filter_shortcodes_nonce' => wp_create_nonce( 'wpseo-filter-shortcodes' ),
],
];
$worker_script_data = [
'url' => YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-analysis-worker' ),
'dependencies' => YoastSEO()->helpers->asset->get_dependency_urls_by_handle( 'yoast-seo-analysis-worker' ),
'keywords_assessment_url' => YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-used-keywords-assessment' ),
'log_level' => WPSEO_Utils::get_analysis_worker_log_level(),
];
$woocommerce_conditional = new WooCommerce_Conditional();
$woocommerce_active = $woocommerce_conditional->is_met();
$wpseo_plugin_availability_checker = new WPSEO_Plugin_Availability();
$woocommerce_seo_file = 'wpseo-woocommerce/wpseo-woocommerce.php';
$woocommerce_seo_active = $wpseo_plugin_availability_checker->is_active( $woocommerce_seo_file );
$script_data = [
// @todo replace this translation with JavaScript translations.
'media' => [ 'choose_image' => __( 'Use Image', 'wordpress-seo' ) ],
'metabox' => $this->get_metabox_script_data(),
'userLanguageCode' => WPSEO_Language_Utils::get_language( get_user_locale() ),
'isPost' => true,
'isBlockEditor' => $is_block_editor,
'postId' => $post_id,
'postStatus' => get_post_status( $post_id ),
'postType' => get_post_type( $post_id ),
'usedKeywordsNonce' => wp_create_nonce( 'wpseo-keyword-usage-and-post-types' ),
'analysis' => [
'plugins' => $plugins_script_data,
'worker' => $worker_script_data,
],
'isJetpackBoostActive' => ( $is_block_editor ) ? YoastSEO()->classes->get( Jetpack_Boost_Active_Conditional::class )->is_met() : false,
'isJetpackBoostNotPremium' => ( $is_block_editor ) ? YoastSEO()->classes->get( Jetpack_Boost_Not_Premium_Conditional::class )->is_met() : false,
'isWooCommerceSeoActive' => $woocommerce_seo_active,
'isWooCommerceActive' => $woocommerce_active,
'woocommerceUpsell' => get_post_type( $post_id ) === 'product' && ! $woocommerce_seo_active && $woocommerce_active,
];
/**
* The website information repository.
*
* @var $repo Website_Information_Repository
*/
$repo = YoastSEO()->classes->get( Website_Information_Repository::class );
$site_information = $repo->get_post_site_information();
$site_information->set_permalink( $this->get_permalink() );
$script_data = array_merge_recursive( $site_information->get_legacy_site_information(), $script_data );
if ( post_type_supports( get_post_type(), 'thumbnail' ) ) {
$asset_manager->enqueue_style( 'featured-image' );
// @todo replace this translation with JavaScript translations.
$script_data['featuredImage'] = [
'featured_image_notice' => __( 'SEO issue: The featured image should be at least 200 by 200 pixels to be picked up by Facebook and other social media sites.', 'wordpress-seo' ),
];
}
$asset_manager->localize_script( $post_edit_handle, 'wpseoScriptData', $script_data );
$asset_manager->enqueue_user_language_script();
}
/**
* Returns post in metabox context.
*
* @return WP_Post|array<string|int|bool>
*/
protected function get_metabox_post() {
if ( $this->post !== null ) {
return $this->post;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
if ( isset( $_GET['post'] ) && is_string( $_GET['post'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are not processing form information, Sanitization happens in the validate_int function.
$post_id = (int) WPSEO_Utils::validate_int( wp_unslash( $_GET['post'] ) );
$this->post = get_post( $post_id );
return $this->post;
}
if ( isset( $GLOBALS['post'] ) ) {
$this->post = $GLOBALS['post'];
return $this->post;
}
return [];
}
/**
* Returns an array with shortcode tags for all registered shortcodes.
*
* @return string[]
*/
private function get_valid_shortcode_tags() {
$shortcode_tags = [];
foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
$shortcode_tags[] = $tag;
}
return $shortcode_tags;
}