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

Yoast / wordpress-seo / fe4d74ae81b231d2986738b4e4648c24370c79ea

26 Feb 2025 02:54PM UTC coverage: 45.177% (-5.5%) from 50.712%
fe4d74ae81b231d2986738b4e4648c24370c79ea

push

github

enricobattocchi
Drop compatibility with PHP 7.2 and 7.3

15990 of 35394 relevant lines covered (45.18%)

4.08 hits per line

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

2.16
/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
         */
122
        public function set_option( $option_name ) {
8✔
123
                $this->option_name = $option_name;
8✔
124

125
                $this->option_instance = WPSEO_Options::get_option_instance( $option_name );
8✔
126
                if ( ! $this->option_instance ) {
8✔
127
                        $this->option_instance = null;
4✔
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
                $aria_attributes .= Yoast_Input_Validation::get_the_aria_describedby_attribute( $variable );
×
431

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

434
                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded and all other output is properly escaped.
435
                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" />';
×
436
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in getter.
437
                echo Yoast_Input_Validation::get_the_error_description( $variable );
×
438
        }
439

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

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

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

472
                $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
473

474
                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded and all other output is properly escaped.
475
                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" />';
×
476
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in getter.
477
                echo Yoast_Input_Validation::get_the_error_description( $variable );
×
478
        }
479

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

492
                $defaults = [
×
493
                        'class'       => 'yoast-field-group__inputfield',
×
494
                        'disabled'    => false,
×
495
                ];
×
496

497
                $attr = wp_parse_args( $attr, $defaults );
×
498
                $val  = $this->get_field_value( $variable, '' );
×
499

500
                if ( isset( $attr['type'] ) && $attr['type'] === 'url' ) {
×
501
                        $val  = urldecode( $val );
×
502
                        $type = 'url';
×
503
                }
504

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

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

520
                $aria_attributes  = Yoast_Input_Validation::get_the_aria_invalid_attribute( $variable );
×
521
                $aria_attributes .= Yoast_Input_Validation::get_the_aria_describedby_attribute( $variable );
×
522

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

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

559
                $defaults = [
×
560
                        'cols'     => '',
×
561
                        'rows'     => '',
×
562
                        'class'    => '',
×
563
                        'disabled' => false,
×
564
                ];
×
565
                $attr     = wp_parse_args( $attr, $defaults );
×
566
                $val      = $this->get_field_value( $variable, '' );
×
567

568
                $this->label(
×
569
                        $label,
×
570
                        [
×
571
                                'for'   => $variable,
×
572
                                'class' => 'textinput',
×
573
                        ]
×
574
                );
×
575

576
                $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
577

578
                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded and all other output is properly escaped.
579
                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" />';
×
580
        }
581

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

598
                if ( is_bool( $val ) ) {
×
599
                        $val = ( $val === true ) ? 'true' : 'false';
×
600
                }
601

602
                if ( $id === '' ) {
×
603
                        $id = 'hidden_' . $variable;
×
604
                }
605

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

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

629
                $defaults = [
×
630
                        'disabled' => false,
×
631
                ];
×
632
                $attr     = wp_parse_args( $attr, $defaults );
×
633

634
                if ( $show_label ) {
×
635
                        $this->label(
×
636
                                $label,
×
637
                                [
×
638
                                        'for'   => $variable,
×
639
                                        'class' => 'select',
×
640
                                ]
×
641
                        );
×
642
                        echo $help; // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: The help contains HTML.
×
643
                }
644

645
                $select_name       = esc_attr( $this->option_name ) . '[' . esc_attr( $variable ) . ']';
×
646
                $active_option     = $this->get_field_value( $variable, '' );
×
647
                $wrapper_start_tag = '';
×
648
                $wrapper_end_tag   = '';
×
649

650
                $select = new Yoast_Input_Select( $variable, $select_name, $select_options, $active_option );
×
651
                $select->add_attribute( 'class', 'select' );
×
652

653
                if ( $this->is_control_disabled( $variable )
×
654
                        || ( isset( $attr['disabled'] ) && $attr['disabled'] ) ) {
×
655
                        $select->add_attribute( 'disabled', 'disabled' );
×
656
                }
657

658
                if ( ! $show_label ) {
×
659
                        $select->add_attribute( 'aria-label', $label );
×
660
                }
661

662
                if ( $styled === 'styled' ) {
×
663
                        $wrapper_start_tag = '<span class="yoast-styled-select">';
×
664
                        $wrapper_end_tag   = '</span>';
×
665
                }
666

667
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
668
                echo $wrapper_start_tag;
×
669
                $select->output_html();
×
670
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
671
                echo $wrapper_end_tag;
×
672
                echo '<br class="clear"/>';
×
673
        }
674

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

692
                $defaults = [
×
693
                        'disabled' => false,
×
694
                ];
×
695
                $attr     = wp_parse_args( $attr, $defaults );
×
696

697
                $var_esc = esc_attr( $variable );
×
698
                $this->label(
×
699
                        $label,
×
700
                        [
×
701
                                'for'   => $variable,
×
702
                                'class' => 'select',
×
703
                        ]
×
704
                );
×
705

706
                $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
707

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

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

720
        /**
721
         * Media input.
722
         *
723
         * @since 2.0
724
         * @deprecated 23.5
725
         * @codeCoverageIgnore
726
         *
727
         * @param string $variable Option name.
728
         * @param string $label    Label message.
729
         * @param array  $attr     Extra attributes to add to the media input and buttons.
730
         *
731
         * @return void
732
         */
733
        public function media_input( $variable, $label, $attr = [] ) {
734

735
                _deprecated_function( __METHOD__, 'Yoast SEO 23.5' );
736

737
                $val      = $this->get_field_value( $variable, '' );
738
                $id_value = $this->get_field_value( $variable . '_id', '' );
739

740
                $var_esc = esc_attr( $variable );
741

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

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

755
                $id_field_id = 'wpseo_' . $var_esc . '_id';
756

757
                echo '<span>';
758
                        echo '<input',
759
                                ' class="textinput"',
760
                                ' id="wpseo_', $var_esc, '"', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
761
                                ' type="text" size="36"',
762
                                ' name="', esc_attr( $this->option_name ), '[', $var_esc, ']"', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
763
                                ' value="', esc_attr( $val ), '"',
764
                                ' readonly="readonly"',
765
                                ' /> ';
766
                        echo '<input',
767
                                ' type="hidden"',
768
                                ' id="', esc_attr( $id_field_id ), '"',
769
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
770
                                ' name="', esc_attr( $this->option_name ), '[', $var_esc, '_id]"',
771
                                ' value="', esc_attr( $id_value ), '"',
772
                                ' />';
773
                echo '</span>';
774
                echo '<br class="clear"/>';
775
        }
776

777
        /**
778
         * Create a Radio input field.
779
         *
780
         * @since 2.0
781
         *
782
         * @param string $variable    The variable within the option to create the radio button for.
783
         * @param array  $values      The radio options to choose from.
784
         * @param string $legend      Optional. The legend to show for the field set, if any.
785
         * @param array  $legend_attr Optional. The attributes for the legend, if any.
786
         * @param array  $attr        Extra attributes to add to the radio button.
787
         *
788
         * @return void
789
         */
790
        public function radio( $variable, $values, $legend = '', $legend_attr = [], $attr = [] ) {
×
791
                if ( ! is_array( $values ) || $values === [] ) {
×
792
                        return;
×
793
                }
794
                $val = $this->get_field_value( $variable, false );
×
795

796
                $var_esc = esc_attr( $variable );
×
797

798
                $defaults = [
×
799
                        'disabled' => false,
×
800
                ];
×
801
                $attr     = wp_parse_args( $attr, $defaults );
×
802

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

806
                if ( is_string( $legend ) && $legend !== '' ) {
×
807

808
                        $legend_defaults = [
×
809
                                'id'    => '',
×
810
                                'class' => 'radiogroup',
×
811
                        ];
×
812

813
                        $legend_attr = wp_parse_args( $legend_attr, $legend_defaults );
×
814

815
                        $this->legend( $legend, $legend_attr );
×
816
                }
817

818
                foreach ( $values as $key => $value ) {
×
819
                        $label      = $value;
×
820
                        $aria_label = '';
×
821

822
                        if ( is_array( $value ) ) {
×
823
                                $label      = ( $value['label'] ?? '' );
×
824
                                $aria_label = ( $value['aria_label'] ?? '' );
×
825
                        }
826

827
                        $key_esc = esc_attr( $key );
×
828

829
                        $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
830

831
                        // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded and all other output is properly escaped.
832
                        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 . ' />';
×
833
                        $this->label(
×
834
                                $label,
×
835
                                [
×
836
                                        'for'        => $var_esc . '-' . $key_esc,
×
837
                                        'class'      => 'radio',
×
838
                                        'aria_label' => $aria_label,
×
839
                                ]
×
840
                        );
×
841
                }
842
                echo '</fieldset>';
×
843
        }
844

845
        /**
846
         * Create a toggle switch input field using two radio buttons.
847
         *
848
         * @since 3.1
849
         *
850
         * @param string $variable The variable within the option to create the radio buttons for.
851
         * @param array  $values   Associative array of on/off keys and their values to be used as
852
         *                         the label elements text for the radio buttons. Optionally, each
853
         *                         value can be an array of visible label text and screen reader text.
854
         * @param string $label    The visual label for the radio buttons group, used as the fieldset legend.
855
         * @param string $help     Inline Help that will be printed out before the visible toggles text.
856
         * @param array  $attr     Extra attributes to add to the toggle switch.
857
         *
858
         * @return void
859
         */
860
        public function toggle_switch( $variable, $values, $label, $help = '', $attr = [] ) {
×
861
                if ( ! is_array( $values ) || $values === [] ) {
×
862
                        return;
×
863
                }
864

865
                $defaults = [
×
866
                        'disabled' => false,
×
867
                ];
×
868
                $attr     = wp_parse_args( $attr, $defaults );
×
869

870
                if ( isset( $attr['preserve_disabled_value'] ) && $attr['preserve_disabled_value'] ) {
×
871
                        $this->hidden( $variable );
×
872
                        $variable .= '_disabled';
×
873
                }
874

875
                $val = $this->get_field_value( $variable, false );
×
876
                if ( $val === true ) {
×
877
                        $val = 'on';
×
878
                }
879
                if ( $val === false ) {
×
880
                        $val = 'off';
×
881
                }
882

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

885
                $has_premium_upsell = ( isset( $attr['show_premium_upsell'] ) && $attr['show_premium_upsell'] && isset( $attr['premium_upsell_url'] ) && ! empty( $attr['premium_upsell_url'] ) );
×
886
                $upsell_class       = ( $has_premium_upsell ) ? ' premium-upsell' : '';
×
887

888
                $var_esc = esc_attr( $variable );
×
889

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

894
                // Show disabled note if attribute does not exists or does exist and is set to true.
895
                if ( ! isset( $attr['show_disabled_note'] ) || ( $attr['show_disabled_note'] === true ) ) {
×
896
                        if ( isset( $attr['note_when_disabled'] ) ) {
×
897
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
898
                                echo $this->get_disabled_note( $variable, $attr['note_when_disabled'] );
×
899
                        }
900
                        else {
901
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
902
                                echo $this->get_disabled_note( $variable );
×
903
                        }
904
                }
905

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

908
                foreach ( $values as $key => $value ) {
×
909
                        $screen_reader_text_html = '';
×
910

911
                        if ( is_array( $value ) ) {
×
912
                                $screen_reader_text      = $value['screen_reader_text'];
×
913
                                $screen_reader_text_html = '<span class="screen-reader-text"> ' . esc_html( $screen_reader_text ) . '</span>';
×
914
                                $value                   = $value['text'];
×
915
                        }
916

917
                        $key_esc            = esc_attr( $key );
×
918
                        $for                = $var_esc . '-' . $key_esc;
×
919
                        $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
920

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

927
                $upsell_button = '';
×
928
                if ( $has_premium_upsell ) {
×
929
                        $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='
×
930
                                                        . esc_url( $attr['premium_upsell_url'] ) . ' target="_blank">'
×
931
                                                        . esc_html__( 'Unlock with Premium!', 'wordpress-seo' )
×
932
                                                        /* translators: Hidden accessibility text. */
×
933
                                                        . '<span class="screen-reader-text">' . esc_html__( '(Opens in a new browser tab)', 'wordpress-seo' ) . '</span>'
×
934
                                                        . '<span aria-hidden="true" class="yoast-button--buy__caret"></span></a>';
×
935
                }
936

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

941
        /**
942
         * Creates a toggle switch to define whether an indexable should be indexed or not.
943
         *
944
         * @param string $variable The variable within the option to create the radio buttons for.
945
         * @param string $label    The visual label for the radio buttons group, used as the fieldset legend.
946
         * @param string $help     Inline Help that will be printed out before the visible toggles text.
947
         * @param array  $attr     Extra attributes to add to the index switch.
948
         *
949
         * @return void
950
         */
951
        public function index_switch( $variable, $label, $help = '', $attr = [] ) {
×
952
                $defaults = [
×
953
                        'disabled' => false,
×
954
                ];
×
955
                $attr     = wp_parse_args( $attr, $defaults );
×
956

957
                $index_switch_values = [
×
958
                        'off' => __( 'On', 'wordpress-seo' ),
×
959
                        'on'  => __( 'Off', 'wordpress-seo' ),
×
960
                ];
×
961

962
                $is_disabled = ( isset( $attr['disabled'] ) && $attr['disabled'] );
×
963

964
                $this->toggle_switch(
×
965
                        $variable,
×
966
                        $index_switch_values,
×
967
                        sprintf(
×
968
                                /* translators: %s expands to an indexable object's name, like a post type or taxonomy */
969
                                esc_html__( 'Show %s in search results?', 'wordpress-seo' ),
×
970
                                $label
×
971
                        ),
×
972
                        $help,
×
973
                        [ 'disabled' => $is_disabled ]
×
974
                );
×
975
        }
976

977
        /**
978
         * Creates a toggle switch to show hide certain options.
979
         *
980
         * @param string $variable     The variable within the option to create the radio buttons for.
981
         * @param string $label        The visual label for the radio buttons group, used as the fieldset legend.
982
         * @param bool   $inverse_keys Whether or not the option keys need to be inverted to support older functions.
983
         * @param string $help         Inline Help that will be printed out before the visible toggles text.
984
         * @param array  $attr         Extra attributes to add to the show-hide switch.
985
         *
986
         * @return void
987
         */
988
        public function show_hide_switch( $variable, $label, $inverse_keys = false, $help = '', $attr = [] ) {
×
989
                $defaults = [
×
990
                        'disabled' => false,
×
991
                ];
×
992
                $attr     = wp_parse_args( $attr, $defaults );
×
993

994
                $on_key  = ( $inverse_keys ) ? 'off' : 'on';
×
995
                $off_key = ( $inverse_keys ) ? 'on' : 'off';
×
996

997
                $show_hide_switch = [
×
998
                        $on_key  => __( 'On', 'wordpress-seo' ),
×
999
                        $off_key => __( 'Off', 'wordpress-seo' ),
×
1000
                ];
×
1001

1002
                $is_disabled = ( isset( $attr['disabled'] ) && $attr['disabled'] );
×
1003

1004
                $this->toggle_switch(
×
1005
                        $variable,
×
1006
                        $show_hide_switch,
×
1007
                        $label,
×
1008
                        $help,
×
1009
                        [ 'disabled' => $is_disabled ]
×
1010
                );
×
1011
        }
1012

1013
        /**
1014
         * Retrieves the value for the form field.
1015
         *
1016
         * @param string      $field_name    The field name to retrieve the value for.
1017
         * @param string|null $default_value The default value, when field has no value.
1018
         *
1019
         * @return mixed|null The retrieved value.
1020
         */
1021
        protected function get_field_value( $field_name, $default_value = null ) {
×
1022
                // On multisite subsites, the Usage tracking feature should always be set to Off.
1023
                if ( $this->is_tracking_on_subsite( $field_name ) ) {
×
1024
                        return false;
×
1025
                }
1026

1027
                return WPSEO_Options::get( $field_name, $default_value );
×
1028
        }
1029

1030
        /**
1031
         * Checks whether a given control should be disabled.
1032
         *
1033
         * @param string $variable The variable within the option to check whether its control should be disabled.
1034
         *
1035
         * @return bool True if control should be disabled, false otherwise.
1036
         */
1037
        protected function is_control_disabled( $variable ) {
8✔
1038
                if ( $this->option_instance === null ) {
8✔
1039
                        return false;
4✔
1040
                }
1041

1042
                // Disable the Usage tracking feature for multisite subsites.
1043
                if ( $this->is_tracking_on_subsite( $variable ) ) {
4✔
1044
                        return true;
×
1045
                }
1046

1047
                return $this->option_instance->is_disabled( $variable );
4✔
1048
        }
1049

1050
        /**
1051
         * Gets the explanation note to print if a given control is disabled.
1052
         *
1053
         * @param string $variable    The variable within the option to print a disabled note for.
1054
         * @param string $custom_note An optional custom note to print instead.
1055
         *
1056
         * @return string Explanation note HTML string, or empty string if no note necessary.
1057
         */
1058
        protected function get_disabled_note( $variable, $custom_note = '' ) {
×
1059
                if ( $custom_note === '' && ! $this->is_control_disabled( $variable ) ) {
×
1060
                        return '';
×
1061
                }
1062
                $disabled_message = esc_html__( 'This feature has been disabled by the network admin.', 'wordpress-seo' );
×
1063

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

1069
                if ( $custom_note ) {
×
1070
                        $disabled_message = esc_html( $custom_note );
×
1071
                }
1072

1073
                return '<p class="disabled-note">' . $disabled_message . '</p>';
×
1074
        }
1075

1076
        /**
1077
         * Determines whether we are dealing with the Usage tracking feature on a multisite subsite.
1078
         * This feature requires specific behavior for the toggle switch.
1079
         *
1080
         * @param string $feature_setting The feature setting.
1081
         *
1082
         * @return bool True if we are dealing with the Usage tracking feature on a multisite subsite.
1083
         */
1084
        protected function is_tracking_on_subsite( $feature_setting ) {
×
1085
                return ( $feature_setting === 'tracking' && ! is_network_admin() && ! is_main_site() );
×
1086
        }
1087

1088
        /**
1089
         * Returns the disabled attribute HTML.
1090
         *
1091
         * @param string $variable The variable within the option of the related form element.
1092
         * @param array  $attr     Extra attributes added to the form element.
1093
         *
1094
         * @return string The disabled attribute HTML.
1095
         */
1096
        protected function get_disabled_attribute( $variable, $attr ) {
×
1097
                if ( $this->is_control_disabled( $variable ) || ( isset( $attr['disabled'] ) && $attr['disabled'] ) ) {
×
1098
                        return ' disabled';
×
1099
                }
1100

1101
                return '';
×
1102
        }
1103
}
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

© 2026 Coveralls, Inc