• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / dd6e866a9e6d253114633104d9e3858d807178ba

19 Jun 2024 10:03AM UTC coverage: 48.628% (-4.3%) from 52.936%
dd6e866a9e6d253114633104d9e3858d807178ba

push

github

web-flow
Merge pull request #21431 from Yoast/21429-update-copy-in-the-introduction-and-consent-modals

Updates the copy for the introduction and consent modals

7441 of 13454 branches covered (55.31%)

Branch coverage included in aggregate %.

0 of 3 new or added lines in 2 files covered. (0.0%)

3718 existing lines in 107 files now uncovered.

25100 of 53464 relevant lines covered (46.95%)

62392.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/admin/class-yoast-form.php
1
<?php
2
/**
3
 * WPSEO plugin file.
4
 *
5
 * @package WPSEO\Admin
6
 */
7

8
use Yoast\WP\SEO\Presenters\Admin\Light_Switch_Presenter;
9
use Yoast\WP\SEO\Presenters\Admin\Sidebar_Presenter;
10

11
/**
12
 * Admin form class.
13
 *
14
 * @since 2.0
15
 */
16
class Yoast_Form {
17

18
        /**
19
         * Instance of this class
20
         *
21
         * @var Yoast_Form
22
         * @since 2.0
23
         */
24
        public static $instance;
25

26
        /**
27
         * The short name of the option to use for the current page.
28
         *
29
         * @var string
30
         * @since 2.0
31
         */
32
        public $option_name;
33

34
        /**
35
         * Option instance.
36
         *
37
         * @since 8.4
38
         * @var WPSEO_Option|null
39
         */
40
        protected $option_instance = null;
41

42
        /**
43
         * Get the singleton instance of this class.
44
         *
45
         * @since 2.0
46
         *
47
         * @return Yoast_Form
48
         */
49
        public static function get_instance() {
×
50
                if ( ! ( self::$instance instanceof self ) ) {
×
51
                        self::$instance = new self();
×
52
                }
53

54
                return self::$instance;
×
55
        }
56

57
        /**
58
         * Generates the header for admin pages.
59
         *
60
         * @since 2.0
61
         *
62
         * @param bool   $form             Whether or not the form start tag should be included.
63
         * @param string $option           The short name of the option to use for the current page.
64
         * @param bool   $contains_files   Whether the form should allow for file uploads.
65
         * @param bool   $option_long_name Group name of the option.
66
         *
67
         * @return void
68
         */
69
        public function admin_header( $form = true, $option = 'wpseo', $contains_files = false, $option_long_name = false ) {
×
70
                if ( ! $option_long_name ) {
×
71
                        $option_long_name = WPSEO_Options::get_group_name( $option );
×
72
                }
73
                ?>
74
                <div class="wrap yoast wpseo-admin-page <?php echo esc_attr( 'page-' . $option ); ?>">
75
                <?php
76
                /**
77
                 * Display the updated/error messages.
78
                 * Only needed as our settings page is not under options, otherwise it will automatically be included.
79
                 *
80
                 * @see settings_errors()
81
                 */
82
                require_once ABSPATH . 'wp-admin/options-head.php';
×
83
                ?>
84
                <h1 id="wpseo-title"><?php echo esc_html( get_admin_page_title() ); ?></h1>
85
                <div id="yst-settings-header-root"></div>
86
                <div class="wpseo_content_wrapper">
87
                <div class="wpseo_content_cell" id="wpseo_content_top">
88
                <?php
89
                if ( $form === true ) {
×
90
                        $enctype = ( $contains_files ) ? ' enctype="multipart/form-data"' : '';
×
91

92
                        $network_admin = new Yoast_Network_Admin();
×
93
                        if ( $network_admin->meets_requirements() ) {
×
94
                                $action_url       = network_admin_url( 'settings.php' );
×
95
                                $hidden_fields_cb = [ $network_admin, 'settings_fields' ];
×
96
                        }
97
                        else {
98
                                $action_url       = admin_url( 'options.php' );
×
99
                                $hidden_fields_cb = 'settings_fields';
×
100
                        }
101

102
                        echo '<form action="'
103
                                . esc_url( $action_url )
×
104
                                . '" method="post" id="wpseo-conf"'
×
105
                                . $enctype . ' accept-charset="' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- nothing to escape.
×
106
                                . esc_attr( get_bloginfo( 'charset' ) )
×
107
                                . '" novalidate="novalidate">';
×
108
                        call_user_func( $hidden_fields_cb, $option_long_name );
×
109
                }
110
                $this->set_option( $option );
×
111
        }
112

113
        /**
114
         * Set the option used in output for form elements.
115
         *
116
         * @since 2.0
117
         *
118
         * @param string $option_name Option key.
119
         *
120
         * @return void
121
         */
UNCOV
122
        public function set_option( $option_name ) {
×
UNCOV
123
                $this->option_name = $option_name;
×
124

UNCOV
125
                $this->option_instance = WPSEO_Options::get_option_instance( $option_name );
×
UNCOV
126
                if ( ! $this->option_instance ) {
×
UNCOV
127
                        $this->option_instance = null;
×
128
                }
129
        }
130

131
        /**
132
         * Generates the footer for admin pages.
133
         *
134
         * @since 2.0
135
         *
136
         * @param bool $submit       Whether or not a submit button and form end tag should be shown.
137
         * @param bool $show_sidebar Whether or not to show the banner sidebar - used by premium plugins to disable it.
138
         *
139
         * @return void
140
         */
141
        public function admin_footer( $submit = true, $show_sidebar = true ) {
×
142
                if ( $submit ) {
×
143
                        $settings_changed_listener = new WPSEO_Admin_Settings_Changed_Listener();
×
144
                        echo '<div id="wpseo-submit-container">';
×
145

146
                        echo '<div id="wpseo-submit-container-float" class="wpseo-admin-submit">';
×
147
                        submit_button( __( 'Save changes', 'wordpress-seo' ) );
×
148
                        $settings_changed_listener->show_success_message();
×
149
                        echo '</div>';
×
150

151
                        echo '<div id="wpseo-submit-container-fixed" class="wpseo-admin-submit wpseo-admin-submit-fixed" style="display: none;">';
×
152
                        submit_button( __( 'Save changes', 'wordpress-seo' ) );
×
153
                        $settings_changed_listener->show_success_message();
×
154
                        echo '</div>';
×
155

156
                        echo '</div>';
×
157

158
                        echo '
×
159
                        </form>';
160
                }
161

162
                /**
163
                 * Apply general admin_footer hooks.
164
                 */
165
                do_action( 'wpseo_admin_footer', $this );
×
166

167
                /**
168
                 * Run possibly set actions to add for example an i18n box.
169
                 */
170
                do_action( 'wpseo_admin_promo_footer' );
×
171

172
                echo '
×
173
                        </div><!-- end of div wpseo_content_top -->';
174

175
                if ( $show_sidebar ) {
×
176
                        $this->admin_sidebar();
×
177
                }
178

179
                echo '</div><!-- end of div wpseo_content_wrapper -->';
×
180

181
                do_action( 'wpseo_admin_below_content', $this );
×
182

183
                echo '
×
184
                        </div><!-- end of wrap -->';
185
        }
186

187
        /**
188
         * Generates the sidebar for admin pages.
189
         *
190
         * @since 2.0
191
         *
192
         * @return void
193
         */
194
        public function admin_sidebar() {
×
195
                // No banners in Premium.
196
                $addon_manager = new WPSEO_Addon_Manager();
×
197
                if ( YoastSEO()->helpers->product->is_premium() && $addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG ) ) {
×
198
                        return;
×
199
                }
200

201
                $sidebar_presenter = new Sidebar_Presenter();
×
202
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in presenter.
203
                echo $sidebar_presenter->present();
×
204
        }
205

206
        /**
207
         * Output a label element.
208
         *
209
         * @since 2.0
210
         *
211
         * @param string $text Label text string, which can contain escaped html.
212
         * @param array  $attr HTML attributes set.
213
         *
214
         * @return void
215
         */
216
        public function label( $text, $attr ) {
×
217
                $defaults = [
218
                        'class'      => 'checkbox',
×
219
                        'close'      => true,
220
                        'for'        => '',
221
                        'aria_label' => '',
222
                ];
223

224
                $attr       = wp_parse_args( $attr, $defaults );
×
225
                $aria_label = '';
×
226
                if ( $attr['aria_label'] !== '' ) {
×
227
                        $aria_label = ' aria-label="' . esc_attr( $attr['aria_label'] ) . '"';
×
228
                }
229

230
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before. Specifically, the $text variable can contain escaped html.
231
                echo "<label class='" . esc_attr( $attr['class'] ) . "' for='" . esc_attr( $attr['for'] ) . "'$aria_label>$text";
×
232
                if ( $attr['close'] ) {
×
233
                        echo '</label>';
×
234
                }
235
        }
236

237
        /**
238
         * Output a legend element.
239
         *
240
         * @since 3.4
241
         *
242
         * @param string $text Legend text string.
243
         * @param array  $attr HTML attributes set.
244
         *
245
         * @return void
246
         */
247
        public function legend( $text, $attr ) {
×
248
                $defaults = [
249
                        'id'    => '',
×
250
                        'class' => '',
251
                ];
252
                $attr     = wp_parse_args( $attr, $defaults );
×
253

254
                $id = ( $attr['id'] === '' ) ? '' : ' id="' . esc_attr( $attr['id'] ) . '"';
×
255
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
256
                echo '<legend class="' . esc_attr( 'yoast-form-legend ' . $attr['class'] ) . '"' . $id . '>' . $text . '</legend>';
×
257
        }
258

259
        /**
260
         * Create a Checkbox input field.
261
         *
262
         * @since 2.0
263
         *
264
         * @param string $variable   The variable within the option to create the checkbox for.
265
         * @param string $label      The label to show for the variable.
266
         * @param bool   $label_left Whether the label should be left (true) or right (false).
267
         * @param array  $attr       Extra attributes to add to the checkbox.
268
         *
269
         * @return void
270
         */
271
        public function checkbox( $variable, $label, $label_left = false, $attr = [] ) {
×
272
                $val = $this->get_field_value( $variable, false );
×
273

274
                $defaults = [
275
                        'disabled' => false,
×
276
                ];
277
                $attr     = wp_parse_args( $attr, $defaults );
×
278

279
                if ( $val === true ) {
×
280
                        $val = 'on';
×
281
                }
282

283
                $class = '';
×
284
                if ( $label_left !== false ) {
×
285
                        $this->label( $label_left, [ 'for' => $variable ] );
×
286
                }
287
                else {
288
                        $class = 'double';
×
289
                }
290

291
                $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
292

293
                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded and all other output is properly escaped.
294
                echo '<input class="', esc_attr( 'checkbox ' . $class ), '" type="checkbox" id="', esc_attr( $variable ), '" name="', esc_attr( $this->option_name . '[' . $variable . ']' ), '" value="on"', checked( $val, 'on', false ), $disabled_attribute, '/>';
×
295

296
                if ( ! empty( $label ) ) {
×
297
                        $this->label( $label, [ 'for' => $variable ] );
×
298
                }
299

300
                echo '<br class="clear" />';
×
301
        }
302

303
        /**
304
         * Creates a Checkbox input field list.
305
         *
306
         * @since 12.8
307
         *
308
         * @param string $variable The variables within the option to create the checkbox list for.
309
         * @param string $labels   The labels to show for the variable.
310
         * @param array  $attr     Extra attributes to add to the checkbox list.
311
         *
312
         * @return void
313
         */
314
        public function checkbox_list( $variable, $labels, $attr = [] ) {
×
315
                $defaults = [
316
                        'disabled' => false,
×
317
                ];
318
                $attr     = wp_parse_args( $attr, $defaults );
×
319

320
                $values = $this->get_field_value( $variable, [] );
×
321

322
                foreach ( $labels as $name => $label ) {
×
323
                        printf(
×
324
                                '<input class="checkbox double" id="%1$s" type="checkbox" name="%2$s" %3$s %5$s value="%4$s"/>',
×
325
                                esc_attr( $variable . '-' . $name ),
×
326
                                esc_attr( $this->option_name . '[' . $variable . '][' . $name . ']' ),
×
327
                                checked( ! empty( $values[ $name ] ), true, false ),
×
328
                                esc_attr( $name ),
×
329
                                disabled( ( isset( $attr['disabled'] ) && $attr['disabled'] ), true, false )
×
330
                        );
331

332
                        printf(
×
333
                                '<label class="checkbox" for="%1$s">%2$s</label>',
×
334
                                esc_attr( $variable . '-' . $name ), // #1
×
335
                                esc_html( $label )
×
336
                        );
337
                        echo '<br class="clear">';
×
338
                }
339
        }
340

341
        /**
342
         * Create a light switch input field using a single checkbox.
343
         *
344
         * @since 3.1
345
         *
346
         * @param string $variable The variable within the option to create the checkbox for.
347
         * @param string $label    The visual label text for the toggle.
348
         * @param array  $buttons  Array of two visual labels for the buttons (defaults Disabled/Enabled).
349
         * @param bool   $reverse  Reverse order of buttons (default true).
350
         * @param string $help     Inline Help that will be printed out before the toggle.
351
         * @param bool   $strong   Whether the visual label is displayed in strong text. Default is false.
352
         *                         Starting from Yoast SEO 16.5, the visual label is forced to bold via CSS.
353
         * @param array  $attr     Extra attributes to add to the light switch.
354
         *
355
         * @return void
356
         */
357
        public function light_switch( $variable, $label, $buttons = [], $reverse = true, $help = '', $strong = false, $attr = [] ) {
×
358
                $val = $this->get_field_value( $variable, false );
×
359

360
                $defaults = [
361
                        'disabled' => false,
×
362
                ];
363
                $attr     = wp_parse_args( $attr, $defaults );
×
364

365
                if ( $val === true ) {
×
366
                        $val = 'on';
×
367
                }
368

369
                $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
370

371
                $output = new Light_Switch_Presenter(
×
372
                        $variable,
×
373
                        $label,
×
374
                        $buttons,
×
375
                        $this->option_name . '[' . $variable . ']',
×
376
                        $val,
×
377
                        $reverse,
×
378
                        $help,
×
379
                        $strong,
×
380
                        $disabled_attribute
×
381
                );
382

383
                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: All output is properly escaped or hardcoded in the presenter.
384
                echo $output;
×
385
        }
386

387
        /**
388
         * Create a Text input field.
389
         *
390
         * @since 2.0
391
         * @since 2.1 Introduced the `$attr` parameter.
392
         *
393
         * @param string       $variable The variable within the option to create the text input field for.
394
         * @param string       $label    The label to show for the variable.
395
         * @param array|string $attr     Extra attributes to add to the input field. Can be class, disabled, autocomplete.
396
         *
397
         * @return void
398
         */
399
        public function textinput( $variable, $label, $attr = [] ) {
×
400
                $type = 'text';
×
401
                if ( ! is_array( $attr ) ) {
×
402
                        $attr = [
403
                                'class'    => $attr,
×
404
                                'disabled' => false,
405
                        ];
406
                }
407

408
                $defaults = [
409
                        'placeholder' => '',
×
410
                        'class'       => '',
411
                ];
412
                $attr     = wp_parse_args( $attr, $defaults );
×
413
                $val      = $this->get_field_value( $variable, '' );
×
414
                if ( isset( $attr['type'] ) && $attr['type'] === 'url' ) {
×
415
                        $val  = urldecode( $val );
×
416
                        $type = 'url';
×
417
                }
418
                $attributes = isset( $attr['autocomplete'] ) ? ' autocomplete="' . esc_attr( $attr['autocomplete'] ) . '"' : '';
×
419

420
                $this->label(
×
421
                        $label,
×
422
                        [
423
                                'for'   => $variable,
×
424
                                'class' => 'textinput',
×
425
                        ]
426
                );
427

428
                $aria_attributes = Yoast_Input_Validation::get_the_aria_invalid_attribute( $variable );
×
429

430
                Yoast_Input_Validation::set_error_descriptions();
×
431
                $aria_attributes .= Yoast_Input_Validation::get_the_aria_describedby_attribute( $variable );
×
432

433
                $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
434

435
                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded and all other output is properly escaped.
436
                echo '<input', $attributes, $aria_attributes, ' class="', esc_attr( 'textinput ' . $attr['class'] ), '" placeholder="', esc_attr( $attr['placeholder'] ), '" type="', $type, '" id="', esc_attr( $variable ), '" name="', esc_attr( $this->option_name . '[' . $variable . ']' ), '" value="', esc_attr( $val ), '"', $disabled_attribute, '/>', '<br class="clear" />';
×
437
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in getter.
438
                echo Yoast_Input_Validation::get_the_error_description( $variable );
×
439
        }
440

441
        /**
442
         * Create a Number input field.
443
         *
444
         * @param string       $variable The variable within the option to create the text input field for.
445
         * @param string       $label    The label to show for the variable.
446
         * @param array|string $attr     Extra attributes to add to the input field. Can be class, disabled, autocomplete.
447
         *
448
         * @return void
449
         */
450
        public function number( $variable, $label, $attr = [] ) {
×
451
                $type     = 'number';
×
452
                $defaults = [
453
                        'placeholder' => '',
×
454
                        'class'       => 'number',
455
                        'disabled'    => false,
456
                        'min'         => 0,
457
                        'max'         => 100,
458
                ];
459
                $attr     = wp_parse_args( $attr, $defaults );
×
460
                $val      = $this->get_field_value( $variable, 0 );
×
461

462
                $this->label(
×
463
                        $label,
×
464
                        [
465
                                'for'   => $variable,
×
466
                                'class' => 'textinput ' . $attr['class'],
×
467
                        ]
468
                );
469

470
                $aria_attributes = Yoast_Input_Validation::get_the_aria_invalid_attribute( $variable );
×
471

472
                Yoast_Input_Validation::set_error_descriptions();
×
473
                $aria_attributes .= Yoast_Input_Validation::get_the_aria_describedby_attribute( $variable );
×
474

475
                $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
476

477
                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded and all other output is properly escaped.
478
                echo '<input' . $aria_attributes . ' class="' . esc_attr( $attr['class'] ) . '" type="' . $type . '" id="', esc_attr( $variable ), '" min="', esc_attr( $attr['min'] ), '" max="', esc_attr( $attr['max'] ), '" name="', esc_attr( $this->option_name . '[' . $variable . ']' ), '" value="', esc_attr( $val ), '"', $disabled_attribute, '/>', '<br class="clear" />';
×
479
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in getter.
480
                echo Yoast_Input_Validation::get_the_error_description( $variable );
×
481
        }
482

483
        /**
484
         * Creates a text input field with with the ability to add content after the label.
485
         *
486
         * @param string $variable The variable within the option to create the text input field for.
487
         * @param string $label    The label to show for the variable.
488
         * @param array  $attr     Extra attributes to add to the input field.
489
         *
490
         * @return void
491
         */
492
        public function textinput_extra_content( $variable, $label, $attr = [] ) {
×
493
                $type = 'text';
×
494

495
                $defaults = [
496
                        'class'       => 'yoast-field-group__inputfield',
×
497
                        'disabled'    => false,
498
                ];
499

500
                $attr = wp_parse_args( $attr, $defaults );
×
501
                $val  = $this->get_field_value( $variable, '' );
×
502

503
                if ( isset( $attr['type'] ) && $attr['type'] === 'url' ) {
×
504
                        $val  = urldecode( $val );
×
505
                        $type = 'url';
×
506
                }
507

508
                echo '<div class="yoast-field-group__title">';
×
509
                $this->label(
×
510
                        $label,
×
511
                        [
512
                                'for'   => $variable,
×
513
                                'class' => $attr['class'] . '--label',
×
514
                        ]
515
                );
516

517
                if ( isset( $attr['extra_content'] ) ) {
×
518
                        // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: may contain HTML that should not be escaped.
519
                        echo $attr['extra_content'];
×
520
                }
521
                echo '</div>';
×
522

523
                $has_input_error = Yoast_Input_Validation::yoast_form_control_has_error( $variable );
×
524
                $aria_attributes = Yoast_Input_Validation::get_the_aria_invalid_attribute( $variable );
×
525

526
                Yoast_Input_Validation::set_error_descriptions();
×
527
                $aria_attributes .= Yoast_Input_Validation::get_the_aria_describedby_attribute( $variable );
×
528

529
                // phpcs:disable WordPress.Security.EscapeOutput -- Reason: output is properly escaped or hardcoded.
530
                printf(
×
531
                        '<input type="%1$s" name="%2$s" id="%3$s" class="%4$s"%5$s%6$s%7$s value="%8$s"%9$s>',
×
532
                        $type,
×
533
                        esc_attr( $this->option_name . '[' . $variable . ']' ),
×
534
                        esc_attr( $variable ),
×
535
                        esc_attr( $attr['class'] ),
×
536
                        isset( $attr['placeholder'] ) ? ' placeholder="' . esc_attr( $attr['placeholder'] ) . '"' : '',
×
537
                        isset( $attr['autocomplete'] ) ? ' autocomplete="' . esc_attr( $attr['autocomplete'] ) . '"' : '',
×
538
                        $aria_attributes,
×
539
                        esc_attr( $val ),
×
540
                        $this->get_disabled_attribute( $variable, $attr )
×
541
                );
542
                // phpcs:enable
543
                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: output is properly escaped.
544
                echo Yoast_Input_Validation::get_the_error_description( $variable );
×
545
        }
546

547
        /**
548
         * Create a textarea.
549
         *
550
         * @since 2.0
551
         *
552
         * @param string       $variable The variable within the option to create the textarea for.
553
         * @param string       $label    The label to show for the variable.
554
         * @param string|array $attr     The CSS class or an array of attributes to assign to the textarea.
555
         *
556
         * @return void
557
         */
558
        public function textarea( $variable, $label, $attr = [] ) {
×
559
                if ( ! is_array( $attr ) ) {
×
560
                        $attr = [
561
                                'class' => $attr,
×
562
                        ];
563
                }
564

565
                $defaults = [
566
                        'cols'     => '',
×
567
                        'rows'     => '',
568
                        'class'    => '',
569
                        'disabled' => false,
570
                ];
571
                $attr     = wp_parse_args( $attr, $defaults );
×
572
                $val      = $this->get_field_value( $variable, '' );
×
573

574
                $this->label(
×
575
                        $label,
×
576
                        [
577
                                'for'   => $variable,
×
578
                                'class' => 'textinput',
×
579
                        ]
580
                );
581

582
                $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
583

584
                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded and all other output is properly escaped.
585
                echo '<textarea cols="' . esc_attr( $attr['cols'] ) . '" rows="' . esc_attr( $attr['rows'] ) . '" class="' . esc_attr( 'textinput ' . $attr['class'] ) . '" id="' . esc_attr( $variable ) . '" name="' . esc_attr( $this->option_name . '[' . $variable . ']' ), '"', $disabled_attribute, '>' . esc_textarea( $val ) . '</textarea><br class="clear" />';
×
586
        }
587

588
        /**
589
         * Create a hidden input field.
590
         *
591
         * @since 2.0
592
         *
593
         * @param string $variable The variable within the option to create the hidden input for.
594
         * @param string $id       The ID of the element.
595
         * @param mixed  $val      Optional. The value to set in the input field. Otherwise the value from the options will be used.
596
         *
597
         * @return void
598
         */
599
        public function hidden( $variable, $id = '', $val = null ) {
×
600
                if ( is_null( $val ) ) {
×
601
                        $val = $this->get_field_value( $variable, '' );
×
602
                }
603

604
                if ( is_bool( $val ) ) {
×
605
                        $val = ( $val === true ) ? 'true' : 'false';
×
606
                }
607

608
                if ( $id === '' ) {
×
609
                        $id = 'hidden_' . $variable;
×
610
                }
611

612
                echo '<input type="hidden" id="' . esc_attr( $id ) . '" name="' . esc_attr( $this->option_name . '[' . $variable . ']' ), '" value="' . esc_attr( $val ) . '"/>';
×
613
        }
614

615
        /**
616
         * Create a Select Box.
617
         *
618
         * @since 2.0
619
         *
620
         * @param string $variable       The variable within the option to create the select for.
621
         * @param string $label          The label to show for the variable.
622
         * @param array  $select_options The select options to choose from.
623
         * @param string $styled         The select style. Use 'styled' to get a styled select. Default 'unstyled'.
624
         * @param bool   $show_label     Whether or not to show the label, if not, it will be applied as an aria-label.
625
         * @param array  $attr           Extra attributes to add to the select.
626
         * @param string $help           Optional. Inline Help HTML that will be printed after the label. Default is empty.
627
         *
628
         * @return void
629
         */
630
        public function select( $variable, $label, array $select_options, $styled = 'unstyled', $show_label = true, $attr = [], $help = '' ) {
×
631
                if ( empty( $select_options ) ) {
×
632
                        return;
×
633
                }
634

635
                $defaults = [
636
                        'disabled' => false,
×
637
                ];
638
                $attr     = wp_parse_args( $attr, $defaults );
×
639

640
                if ( $show_label ) {
×
641
                        $this->label(
×
642
                                $label,
×
643
                                [
644
                                        'for'   => $variable,
×
645
                                        'class' => 'select',
×
646
                                ]
647
                        );
648
                        echo $help; // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: The help contains HTML.
×
649
                }
650

651
                $select_name       = esc_attr( $this->option_name ) . '[' . esc_attr( $variable ) . ']';
×
652
                $active_option     = $this->get_field_value( $variable, '' );
×
653
                $wrapper_start_tag = '';
×
654
                $wrapper_end_tag   = '';
×
655

656
                $select = new Yoast_Input_Select( $variable, $select_name, $select_options, $active_option );
×
657
                $select->add_attribute( 'class', 'select' );
×
658

659
                if ( $this->is_control_disabled( $variable )
×
660
                        || ( isset( $attr['disabled'] ) && $attr['disabled'] ) ) {
×
661
                        $select->add_attribute( 'disabled', 'disabled' );
×
662
                }
663

664
                if ( ! $show_label ) {
×
665
                        $select->add_attribute( 'aria-label', $label );
×
666
                }
667

668
                if ( $styled === 'styled' ) {
×
669
                        $wrapper_start_tag = '<span class="yoast-styled-select">';
×
670
                        $wrapper_end_tag   = '</span>';
×
671
                }
672

673
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
674
                echo $wrapper_start_tag;
×
675
                $select->output_html();
×
676
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
677
                echo $wrapper_end_tag;
×
678
                echo '<br class="clear"/>';
×
679
        }
680

681
        /**
682
         * Create a File upload field.
683
         *
684
         * @since 2.0
685
         *
686
         * @param string $variable The variable within the option to create the file upload field for.
687
         * @param string $label    The label to show for the variable.
688
         * @param array  $attr     Extra attributes to add to the file upload input.
689
         *
690
         * @return void
691
         */
692
        public function file_upload( $variable, $label, $attr = [] ) {
×
693
                $val = $this->get_field_value( $variable, '' );
×
694
                if ( is_array( $val ) ) {
×
695
                        $val = $val['url'];
×
696
                }
697

698
                $defaults = [
699
                        'disabled' => false,
×
700
                ];
701
                $attr     = wp_parse_args( $attr, $defaults );
×
702

703
                $var_esc = esc_attr( $variable );
×
704
                $this->label(
×
705
                        $label,
×
706
                        [
707
                                'for'   => $variable,
×
708
                                'class' => 'select',
×
709
                        ]
710
                );
711

712
                $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
713

714
                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded and all other output is properly escaped.
715
                echo '<input type="file" value="' . esc_attr( $val ) . '" class="textinput" name="' . esc_attr( $this->option_name ) . '[' . $var_esc . ']" id="' . $var_esc . '"', $disabled_attribute, '/>';
×
716

717
                // Need to save separate array items in hidden inputs, because empty file inputs type will be deleted by settings API.
718
                if ( ! empty( $val ) ) {
×
719
                        $this->hidden( 'file', $this->option_name . '_file' );
×
720
                        $this->hidden( 'url', $this->option_name . '_url' );
×
721
                        $this->hidden( 'type', $this->option_name . '_type' );
×
722
                }
723
                echo '<br class="clear"/>';
×
724
        }
725

726
        /**
727
         * Media input.
728
         *
729
         * @since 2.0
730
         *
731
         * @param string $variable Option name.
732
         * @param string $label    Label message.
733
         * @param array  $attr     Extra attributes to add to the media input and buttons.
734
         *
735
         * @return void
736
         */
737
        public function media_input( $variable, $label, $attr = [] ) {
×
738
                $val      = $this->get_field_value( $variable, '' );
×
739
                $id_value = $this->get_field_value( $variable . '_id', '' );
×
740

741
                $var_esc = esc_attr( $variable );
×
742

743
                $defaults = [
744
                        'disabled' => false,
×
745
                ];
746
                $attr     = wp_parse_args( $attr, $defaults );
×
747

748
                $this->label(
×
749
                        $label,
×
750
                        [
751
                                'for'   => 'wpseo_' . $variable,
×
752
                                'class' => 'select',
×
753
                        ]
754
                );
755

756
                $id_field_id = 'wpseo_' . $var_esc . '_id';
×
757

758
                $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
759

760
                echo '<span>';
×
761
                        echo '<input',
×
762
                                ' class="textinput"',
×
763
                                ' id="wpseo_', $var_esc, '"', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
×
764
                                ' type="text" size="36"',
×
765
                                ' name="', esc_attr( $this->option_name ), '[', $var_esc, ']"', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
×
766
                                ' value="', esc_attr( $val ), '"',
×
767
                                ' readonly="readonly"',
×
768
                                ' /> ';
×
769
                        echo '<input',
×
770
                                ' id="wpseo_', $var_esc, '_button"', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
×
771
                                ' class="wpseo_image_upload_button button"',
×
772
                                ' type="button"',
×
773
                                ' value="', esc_attr__( 'Upload Image', 'wordpress-seo' ), '"',
×
774
                                ' data-target-id="', esc_attr( $id_field_id ), '"',
×
775
                                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded.
776
                                $disabled_attribute,
×
777
                                ' /> ';
×
778
                        echo '<input',
×
779
                                ' class="wpseo_image_remove_button button"',
×
780
                                ' type="button"',
×
781
                                ' value="', esc_attr__( 'Clear Image', 'wordpress-seo' ), '"',
×
782
                                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded.
783
                                $disabled_attribute,
×
784
                                ' />';
×
785
                        echo '<input',
×
786
                                ' type="hidden"',
×
787
                                ' id="', esc_attr( $id_field_id ), '"',
×
788
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
789
                                ' name="', esc_attr( $this->option_name ), '[', $var_esc, '_id]"',
×
790
                                ' value="', esc_attr( $id_value ), '"',
×
791
                                ' />';
×
792
                echo '</span>';
×
793
                echo '<br class="clear"/>';
×
794
        }
795

796
        /**
797
         * Create a Radio input field.
798
         *
799
         * @since 2.0
800
         *
801
         * @param string $variable    The variable within the option to create the radio button for.
802
         * @param array  $values      The radio options to choose from.
803
         * @param string $legend      Optional. The legend to show for the field set, if any.
804
         * @param array  $legend_attr Optional. The attributes for the legend, if any.
805
         * @param array  $attr        Extra attributes to add to the radio button.
806
         *
807
         * @return void
808
         */
809
        public function radio( $variable, $values, $legend = '', $legend_attr = [], $attr = [] ) {
×
810
                if ( ! is_array( $values ) || $values === [] ) {
×
811
                        return;
×
812
                }
813
                $val = $this->get_field_value( $variable, false );
×
814

815
                $var_esc = esc_attr( $variable );
×
816

817
                $defaults = [
818
                        'disabled' => false,
×
819
                ];
820
                $attr     = wp_parse_args( $attr, $defaults );
×
821

822
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
823
                echo '<fieldset class="yoast-form-fieldset wpseo_radio_block" id="' . $var_esc . '">';
×
824

825
                if ( is_string( $legend ) && $legend !== '' ) {
×
826

827
                        $legend_defaults = [
828
                                'id'    => '',
×
829
                                'class' => 'radiogroup',
830
                        ];
831

832
                        $legend_attr = wp_parse_args( $legend_attr, $legend_defaults );
×
833

834
                        $this->legend( $legend, $legend_attr );
×
835
                }
836

837
                foreach ( $values as $key => $value ) {
×
838
                        $label      = $value;
×
839
                        $aria_label = '';
×
840

841
                        if ( is_array( $value ) ) {
×
842
                                $label      = ( $value['label'] ?? '' );
×
843
                                $aria_label = ( $value['aria_label'] ?? '' );
×
844
                        }
845

846
                        $key_esc = esc_attr( $key );
×
847

848
                        $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
849

850
                        // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded and all other output is properly escaped.
851
                        echo '<input type="radio" class="radio" id="' . $var_esc . '-' . $key_esc . '" name="' . esc_attr( $this->option_name ) . '[' . $var_esc . ']" value="' . $key_esc . '" ' . checked( $val, $key_esc, false ) . $disabled_attribute . ' />';
×
852
                        $this->label(
×
853
                                $label,
×
854
                                [
855
                                        'for'        => $var_esc . '-' . $key_esc,
×
856
                                        'class'      => 'radio',
×
857
                                        'aria_label' => $aria_label,
×
858
                                ]
859
                        );
860
                }
861
                echo '</fieldset>';
×
862
        }
863

864
        /**
865
         * Create a toggle switch input field using two radio buttons.
866
         *
867
         * @since 3.1
868
         *
869
         * @param string $variable The variable within the option to create the radio buttons for.
870
         * @param array  $values   Associative array of on/off keys and their values to be used as
871
         *                         the label elements text for the radio buttons. Optionally, each
872
         *                         value can be an array of visible label text and screen reader text.
873
         * @param string $label    The visual label for the radio buttons group, used as the fieldset legend.
874
         * @param string $help     Inline Help that will be printed out before the visible toggles text.
875
         * @param array  $attr     Extra attributes to add to the toggle switch.
876
         *
877
         * @return void
878
         */
879
        public function toggle_switch( $variable, $values, $label, $help = '', $attr = [] ) {
×
880
                if ( ! is_array( $values ) || $values === [] ) {
×
881
                        return;
×
882
                }
883

884
                $defaults = [
885
                        'disabled' => false,
×
886
                ];
887
                $attr     = wp_parse_args( $attr, $defaults );
×
888

889
                if ( isset( $attr['preserve_disabled_value'] ) && $attr['preserve_disabled_value'] ) {
×
890
                        $this->hidden( $variable );
×
891
                        $variable .= '_disabled';
×
892
                }
893

894
                $val = $this->get_field_value( $variable, false );
×
895
                if ( $val === true ) {
×
896
                        $val = 'on';
×
897
                }
898
                if ( $val === false ) {
×
899
                        $val = 'off';
×
900
                }
901

902
                $help_class = ! empty( $help ) ? ' switch-container__has-help' : '';
×
903

904
                $has_premium_upsell = ( isset( $attr['show_premium_upsell'] ) && $attr['show_premium_upsell'] && isset( $attr['premium_upsell_url'] ) && ! empty( $attr['premium_upsell_url'] ) );
×
905
                $upsell_class       = ( $has_premium_upsell ) ? ' premium-upsell' : '';
×
906

907
                $var_esc = esc_attr( $variable );
×
908

909
                printf( '<div class="%s">', esc_attr( 'switch-container' . $help_class . $upsell_class ) );
×
910
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
911
                echo '<fieldset id="', $var_esc, '" class="fieldset-switch-toggle"><legend>', $label, '</legend>', $help;
×
912

913
                // Show disabled note if attribute does not exists or does exist and is set to true.
914
                if ( ! isset( $attr['show_disabled_note'] ) || ( $attr['show_disabled_note'] === true ) ) {
×
915
                        if ( isset( $attr['note_when_disabled'] ) ) {
×
916
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
917
                                echo $this->get_disabled_note( $variable, $attr['note_when_disabled'] );
×
918
                        }
919
                        else {
920
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
921
                                echo $this->get_disabled_note( $variable );
×
922
                        }
923
                }
924

925
                echo '<div class="switch-toggle switch-candy switch-yoast-seo">';
×
926

927
                foreach ( $values as $key => $value ) {
×
928
                        $screen_reader_text_html = '';
×
929

930
                        if ( is_array( $value ) ) {
×
931
                                $screen_reader_text      = $value['screen_reader_text'];
×
932
                                $screen_reader_text_html = '<span class="screen-reader-text"> ' . esc_html( $screen_reader_text ) . '</span>';
×
933
                                $value                   = $value['text'];
×
934
                        }
935

936
                        $key_esc            = esc_attr( $key );
×
937
                        $for                = $var_esc . '-' . $key_esc;
×
938
                        $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
939

940
                        // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded and all other output is properly escaped.
941
                        echo '<input type="radio" id="' . $for . '" name="' . esc_attr( $this->option_name ) . '[' . $var_esc . ']" value="' . $key_esc . '" ' . checked( $val, $key_esc, false ) . $disabled_attribute . ' />',
×
942
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
943
                        '<label for="', $for, '">', esc_html( $value ), $screen_reader_text_html, '</label>';
×
944
                }
945

946
                $upsell_button = '';
×
947
                if ( $has_premium_upsell ) {
×
948
                        $upsell_button = '<a class="yoast-button yoast-button--buy yoast-button--small" data-action="load-nfd-ctb" data-ctb-id="f6a84663-465f-4cb5-8ba5-f7a6d72224b2" href='
949
                                                        . esc_url( $attr['premium_upsell_url'] ) . ' target="_blank">'
×
950
                                                        . esc_html__( 'Unlock with Premium!', 'wordpress-seo' )
×
951
                                                        /* translators: Hidden accessibility text. */
952
                                                        . '<span class="screen-reader-text">' . esc_html__( '(Opens in a new browser tab)', 'wordpress-seo' ) . '</span>'
×
953
                                                        . '<span aria-hidden="true" class="yoast-button--buy__caret"></span></a>';
×
954
                }
955

956
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- All variable output is escaped above.
957
                echo '<a></a></div></fieldset><div class="clear"></div>' . $upsell_button . '</div>' . PHP_EOL . PHP_EOL;
×
958
        }
959

960
        /**
961
         * Creates a toggle switch to define whether an indexable should be indexed or not.
962
         *
963
         * @param string $variable The variable within the option to create the radio buttons for.
964
         * @param string $label    The visual label for the radio buttons group, used as the fieldset legend.
965
         * @param string $help     Inline Help that will be printed out before the visible toggles text.
966
         * @param array  $attr     Extra attributes to add to the index switch.
967
         *
968
         * @return void
969
         */
970
        public function index_switch( $variable, $label, $help = '', $attr = [] ) {
×
971
                $defaults = [
972
                        'disabled' => false,
×
973
                ];
974
                $attr     = wp_parse_args( $attr, $defaults );
×
975

976
                $index_switch_values = [
977
                        'off' => __( 'On', 'wordpress-seo' ),
×
978
                        'on'  => __( 'Off', 'wordpress-seo' ),
×
979
                ];
980

981
                $is_disabled = ( isset( $attr['disabled'] ) && $attr['disabled'] );
×
982

983
                $this->toggle_switch(
×
984
                        $variable,
×
985
                        $index_switch_values,
×
986
                        sprintf(
×
987
                                /* translators: %s expands to an indexable object's name, like a post type or taxonomy */
988
                                esc_html__( 'Show %s in search results?', 'wordpress-seo' ),
×
989
                                $label
×
990
                        ),
991
                        $help,
×
992
                        [ 'disabled' => $is_disabled ]
×
993
                );
994
        }
995

996
        /**
997
         * Creates a toggle switch to show hide certain options.
998
         *
999
         * @param string $variable     The variable within the option to create the radio buttons for.
1000
         * @param string $label        The visual label for the radio buttons group, used as the fieldset legend.
1001
         * @param bool   $inverse_keys Whether or not the option keys need to be inverted to support older functions.
1002
         * @param string $help         Inline Help that will be printed out before the visible toggles text.
1003
         * @param array  $attr         Extra attributes to add to the show-hide switch.
1004
         *
1005
         * @return void
1006
         */
1007
        public function show_hide_switch( $variable, $label, $inverse_keys = false, $help = '', $attr = [] ) {
×
1008
                $defaults = [
1009
                        'disabled' => false,
×
1010
                ];
1011
                $attr     = wp_parse_args( $attr, $defaults );
×
1012

1013
                $on_key  = ( $inverse_keys ) ? 'off' : 'on';
×
1014
                $off_key = ( $inverse_keys ) ? 'on' : 'off';
×
1015

1016
                $show_hide_switch = [
1017
                        $on_key  => __( 'On', 'wordpress-seo' ),
×
1018
                        $off_key => __( 'Off', 'wordpress-seo' ),
×
1019
                ];
1020

1021
                $is_disabled = ( isset( $attr['disabled'] ) && $attr['disabled'] );
×
1022

1023
                $this->toggle_switch(
×
1024
                        $variable,
×
1025
                        $show_hide_switch,
×
1026
                        $label,
×
1027
                        $help,
×
1028
                        [ 'disabled' => $is_disabled ]
×
1029
                );
1030
        }
1031

1032
        /**
1033
         * Retrieves the value for the form field.
1034
         *
1035
         * @param string      $field_name    The field name to retrieve the value for.
1036
         * @param string|null $default_value The default value, when field has no value.
1037
         *
1038
         * @return mixed|null The retrieved value.
1039
         */
1040
        protected function get_field_value( $field_name, $default_value = null ) {
×
1041
                // On multisite subsites, the Usage tracking feature should always be set to Off.
1042
                if ( $this->is_tracking_on_subsite( $field_name ) ) {
×
1043
                        return false;
×
1044
                }
1045

1046
                return WPSEO_Options::get( $field_name, $default_value );
×
1047
        }
1048

1049
        /**
1050
         * Checks whether a given control should be disabled.
1051
         *
1052
         * @param string $variable The variable within the option to check whether its control should be disabled.
1053
         *
1054
         * @return bool True if control should be disabled, false otherwise.
1055
         */
UNCOV
1056
        protected function is_control_disabled( $variable ) {
×
UNCOV
1057
                if ( $this->option_instance === null ) {
×
UNCOV
1058
                        return false;
×
1059
                }
1060

1061
                // Disable the Usage tracking feature for multisite subsites.
UNCOV
1062
                if ( $this->is_tracking_on_subsite( $variable ) ) {
×
1063
                        return true;
×
1064
                }
1065

UNCOV
1066
                return $this->option_instance->is_disabled( $variable );
×
1067
        }
1068

1069
        /**
1070
         * Gets the explanation note to print if a given control is disabled.
1071
         *
1072
         * @param string $variable    The variable within the option to print a disabled note for.
1073
         * @param string $custom_note An optional custom note to print instead.
1074
         *
1075
         * @return string Explanation note HTML string, or empty string if no note necessary.
1076
         */
1077
        protected function get_disabled_note( $variable, $custom_note = '' ) {
×
1078
                if ( $custom_note === '' && ! $this->is_control_disabled( $variable ) ) {
×
1079
                        return '';
×
1080
                }
1081
                $disabled_message = esc_html__( 'This feature has been disabled by the network admin.', 'wordpress-seo' );
×
1082

1083
                // The explanation to show when disabling the Usage tracking feature for multisite subsites.
1084
                if ( $this->is_tracking_on_subsite( $variable ) ) {
×
1085
                        $disabled_message = esc_html__( 'This feature has been disabled since subsites never send tracking data.', 'wordpress-seo' );
×
1086
                }
1087

1088
                if ( $custom_note ) {
×
1089
                        $disabled_message = esc_html( $custom_note );
×
1090
                }
1091

1092
                return '<p class="disabled-note">' . $disabled_message . '</p>';
×
1093
        }
1094

1095
        /**
1096
         * Determines whether we are dealing with the Usage tracking feature on a multisite subsite.
1097
         * This feature requires specific behavior for the toggle switch.
1098
         *
1099
         * @param string $feature_setting The feature setting.
1100
         *
1101
         * @return bool True if we are dealing with the Usage tracking feature on a multisite subsite.
1102
         */
1103
        protected function is_tracking_on_subsite( $feature_setting ) {
×
1104
                return ( $feature_setting === 'tracking' && ! is_network_admin() && ! is_main_site() );
×
1105
        }
1106

1107
        /**
1108
         * Returns the disabled attribute HTML.
1109
         *
1110
         * @param string $variable The variable within the option of the related form element.
1111
         * @param array  $attr     Extra attributes added to the form element.
1112
         *
1113
         * @return string The disabled attribute HTML.
1114
         */
1115
        protected function get_disabled_attribute( $variable, $attr ) {
×
1116
                if ( $this->is_control_disabled( $variable ) || ( isset( $attr['disabled'] ) && $attr['disabled'] ) ) {
×
1117
                        return ' disabled';
×
1118
                }
1119

1120
                return '';
×
1121
        }
1122
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc