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

Yoast / wordpress-seo / b78c91bd80d89b034df841d284a428c954224385

03 Sep 2024 07:50AM UTC coverage: 54.503% (+0.4%) from 54.072%
b78c91bd80d89b034df841d284a428c954224385

push

github

YoastBot
Bump version to 23.4 on free

7504 of 13559 branches covered (55.34%)

Branch coverage included in aggregate %.

29831 of 54942 relevant lines covered (54.3%)

41571.6 hits per line

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

2.64
/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
        }
4✔
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
         *
725
         * @param string $variable Option name.
726
         * @param string $label    Label message.
727
         * @param array  $attr     Extra attributes to add to the media input and buttons.
728
         *
729
         * @return void
730
         */
731
        public function media_input( $variable, $label, $attr = [] ) {
×
732
                $val      = $this->get_field_value( $variable, '' );
×
733
                $id_value = $this->get_field_value( $variable . '_id', '' );
×
734

735
                $var_esc = esc_attr( $variable );
×
736

737
                $defaults = [
738
                        'disabled' => false,
×
739
                ];
740
                $attr     = wp_parse_args( $attr, $defaults );
×
741

742
                $this->label(
×
743
                        $label,
×
744
                        [
745
                                'for'   => 'wpseo_' . $variable,
×
746
                                'class' => 'select',
×
747
                        ]
748
                );
749

750
                $id_field_id = 'wpseo_' . $var_esc . '_id';
×
751

752
                $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
753

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

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

809
                $var_esc = esc_attr( $variable );
×
810

811
                $defaults = [
812
                        'disabled' => false,
×
813
                ];
814
                $attr     = wp_parse_args( $attr, $defaults );
×
815

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

819
                if ( is_string( $legend ) && $legend !== '' ) {
×
820

821
                        $legend_defaults = [
822
                                'id'    => '',
×
823
                                'class' => 'radiogroup',
824
                        ];
825

826
                        $legend_attr = wp_parse_args( $legend_attr, $legend_defaults );
×
827

828
                        $this->legend( $legend, $legend_attr );
×
829
                }
830

831
                foreach ( $values as $key => $value ) {
×
832
                        $label      = $value;
×
833
                        $aria_label = '';
×
834

835
                        if ( is_array( $value ) ) {
×
836
                                $label      = ( $value['label'] ?? '' );
×
837
                                $aria_label = ( $value['aria_label'] ?? '' );
×
838
                        }
839

840
                        $key_esc = esc_attr( $key );
×
841

842
                        $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
843

844
                        // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded and all other output is properly escaped.
845
                        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 . ' />';
×
846
                        $this->label(
×
847
                                $label,
×
848
                                [
849
                                        'for'        => $var_esc . '-' . $key_esc,
×
850
                                        'class'      => 'radio',
×
851
                                        'aria_label' => $aria_label,
×
852
                                ]
853
                        );
854
                }
855
                echo '</fieldset>';
×
856
        }
857

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

878
                $defaults = [
879
                        'disabled' => false,
×
880
                ];
881
                $attr     = wp_parse_args( $attr, $defaults );
×
882

883
                if ( isset( $attr['preserve_disabled_value'] ) && $attr['preserve_disabled_value'] ) {
×
884
                        $this->hidden( $variable );
×
885
                        $variable .= '_disabled';
×
886
                }
887

888
                $val = $this->get_field_value( $variable, false );
×
889
                if ( $val === true ) {
×
890
                        $val = 'on';
×
891
                }
892
                if ( $val === false ) {
×
893
                        $val = 'off';
×
894
                }
895

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

898
                $has_premium_upsell = ( isset( $attr['show_premium_upsell'] ) && $attr['show_premium_upsell'] && isset( $attr['premium_upsell_url'] ) && ! empty( $attr['premium_upsell_url'] ) );
×
899
                $upsell_class       = ( $has_premium_upsell ) ? ' premium-upsell' : '';
×
900

901
                $var_esc = esc_attr( $variable );
×
902

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

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

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

921
                foreach ( $values as $key => $value ) {
×
922
                        $screen_reader_text_html = '';
×
923

924
                        if ( is_array( $value ) ) {
×
925
                                $screen_reader_text      = $value['screen_reader_text'];
×
926
                                $screen_reader_text_html = '<span class="screen-reader-text"> ' . esc_html( $screen_reader_text ) . '</span>';
×
927
                                $value                   = $value['text'];
×
928
                        }
929

930
                        $key_esc            = esc_attr( $key );
×
931
                        $for                = $var_esc . '-' . $key_esc;
×
932
                        $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
933

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

940
                $upsell_button = '';
×
941
                if ( $has_premium_upsell ) {
×
942
                        $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='
943
                                                        . esc_url( $attr['premium_upsell_url'] ) . ' target="_blank">'
×
944
                                                        . esc_html__( 'Unlock with Premium!', 'wordpress-seo' )
×
945
                                                        /* translators: Hidden accessibility text. */
946
                                                        . '<span class="screen-reader-text">' . esc_html__( '(Opens in a new browser tab)', 'wordpress-seo' ) . '</span>'
×
947
                                                        . '<span aria-hidden="true" class="yoast-button--buy__caret"></span></a>';
×
948
                }
949

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

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

970
                $index_switch_values = [
971
                        'off' => __( 'On', 'wordpress-seo' ),
×
972
                        'on'  => __( 'Off', 'wordpress-seo' ),
×
973
                ];
974

975
                $is_disabled = ( isset( $attr['disabled'] ) && $attr['disabled'] );
×
976

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

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

1007
                $on_key  = ( $inverse_keys ) ? 'off' : 'on';
×
1008
                $off_key = ( $inverse_keys ) ? 'on' : 'off';
×
1009

1010
                $show_hide_switch = [
1011
                        $on_key  => __( 'On', 'wordpress-seo' ),
×
1012
                        $off_key => __( 'Off', 'wordpress-seo' ),
×
1013
                ];
1014

1015
                $is_disabled = ( isset( $attr['disabled'] ) && $attr['disabled'] );
×
1016

1017
                $this->toggle_switch(
×
1018
                        $variable,
×
1019
                        $show_hide_switch,
×
1020
                        $label,
×
1021
                        $help,
×
1022
                        [ 'disabled' => $is_disabled ]
×
1023
                );
1024
        }
1025

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

1040
                return WPSEO_Options::get( $field_name, $default_value );
×
1041
        }
1042

1043
        /**
1044
         * Checks whether a given control should be disabled.
1045
         *
1046
         * @param string $variable The variable within the option to check whether its control should be disabled.
1047
         *
1048
         * @return bool True if control should be disabled, false otherwise.
1049
         */
1050
        protected function is_control_disabled( $variable ) {
8✔
1051
                if ( $this->option_instance === null ) {
8✔
1052
                        return false;
4✔
1053
                }
1054

1055
                // Disable the Usage tracking feature for multisite subsites.
1056
                if ( $this->is_tracking_on_subsite( $variable ) ) {
4✔
1057
                        return true;
×
1058
                }
1059

1060
                return $this->option_instance->is_disabled( $variable );
4✔
1061
        }
1062

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

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

1082
                if ( $custom_note ) {
×
1083
                        $disabled_message = esc_html( $custom_note );
×
1084
                }
1085

1086
                return '<p class="disabled-note">' . $disabled_message . '</p>';
×
1087
        }
1088

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

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

1114
                return '';
×
1115
        }
1116
}
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