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

Yoast / wordpress-seo / 6638054992

25 Oct 2023 08:55AM UTC coverage: 49.106%. First build
6638054992

Pull #20653

github

vraja-pro
Merge remote-tracking branch 'origin/feature/upgrade-react-and-tests' into feature/upgrade-react-and-tests
Pull Request #20653: Feature/upgrade react and tests

64 of 157 new or added lines in 34 files covered. (40.76%)

13135 of 26748 relevant lines covered (49.11%)

3.96 hits per line

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

2.28
/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
        public function admin_header( $form = true, $option = 'wpseo', $contains_files = false, $option_long_name = false ) {
68
                if ( ! $option_long_name ) {
×
69
                        $option_long_name = WPSEO_Options::get_group_name( $option );
×
70
                }
71
                ?>
72
                <div class="wrap yoast wpseo-admin-page <?php echo esc_attr( 'page-' . $option ); ?>">
73
                <?php
74
                /**
75
                 * Display the updated/error messages.
76
                 * Only needed as our settings page is not under options, otherwise it will automatically be included.
77
                 *
78
                 * @see settings_errors()
79
                 */
80
                require_once ABSPATH . 'wp-admin/options-head.php';
×
81
                ?>
82
                <h1 id="wpseo-title"><?php echo esc_html( get_admin_page_title() ); ?></h1>
83
                <div id="yst-settings-header-root"></div>
84
                <div class="wpseo_content_wrapper">
85
                <div class="wpseo_content_cell" id="wpseo_content_top">
86
                <?php
87
                if ( $form === true ) {
×
88
                        $enctype = ( $contains_files ) ? ' enctype="multipart/form-data"' : '';
×
89

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

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

111
        /**
112
         * Set the option used in output for form elements.
113
         *
114
         * @since 2.0
115
         *
116
         * @param string $option_name Option key.
117
         */
118
        public function set_option( $option_name ) {
119
                $this->option_name = $option_name;
8✔
120

121
                $this->option_instance = WPSEO_Options::get_option_instance( $option_name );
8✔
122
                if ( ! $this->option_instance ) {
8✔
123
                        $this->option_instance = null;
4✔
124
                }
125
        }
4✔
126

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

140
                        echo '<div id="wpseo-submit-container-float" class="wpseo-admin-submit">';
×
141
                        submit_button( __( 'Save changes', 'wordpress-seo' ) );
×
142
                        $settings_changed_listener->show_success_message();
×
143
                        echo '</div>';
×
144

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

150
                        echo '</div>';
×
151

152
                        echo '
×
153
                        </form>';
154
                }
155

156
                /**
157
                 * Apply general admin_footer hooks.
158
                 */
159
                do_action( 'wpseo_admin_footer', $this );
×
160

161
                /**
162
                 * Run possibly set actions to add for example an i18n box.
163
                 */
164
                do_action( 'wpseo_admin_promo_footer' );
×
165

166
                echo '
×
167
                        </div><!-- end of div wpseo_content_top -->';
168

169
                if ( $show_sidebar ) {
×
170
                        $this->admin_sidebar();
×
171
                }
172

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

175
                do_action( 'wpseo_admin_below_content', $this );
×
176

177
                echo '
×
178
                        </div><!-- end of wrap -->';
179
        }
180

181
        /**
182
         * Generates the sidebar for admin pages.
183
         *
184
         * @since 2.0
185
         */
186
        public function admin_sidebar() {
187
                // No banners in Premium.
188
                $addon_manager = new WPSEO_Addon_Manager();
×
189
                if ( YoastSEO()->helpers->product->is_premium() && $addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG ) ) {
×
190
                        return;
×
191
                }
192

193
                $sidebar_presenter = new Sidebar_Presenter();
×
194
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in presenter.
195
                echo $sidebar_presenter->present();
×
196
        }
197

198
        /**
199
         * Output a label element.
200
         *
201
         * @since 2.0
202
         *
203
         * @param string $text Label text string, which can contain escaped html.
204
         * @param array  $attr HTML attributes set.
205
         */
206
        public function label( $text, $attr ) {
207
                $defaults = [
208
                        'class'      => 'checkbox',
×
209
                        'close'      => true,
210
                        'for'        => '',
211
                        'aria_label' => '',
212
                ];
213

214
                $attr       = wp_parse_args( $attr, $defaults );
×
215
                $aria_label = '';
×
216
                if ( $attr['aria_label'] !== '' ) {
×
217
                        $aria_label = ' aria-label="' . esc_attr( $attr['aria_label'] ) . '"';
×
218
                }
219

220
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before. Specifically, the $text variable can contain escaped html.
221
                echo "<label class='" . esc_attr( $attr['class'] ) . "' for='" . esc_attr( $attr['for'] ) . "'$aria_label>$text";
×
222
                if ( $attr['close'] ) {
×
223
                        echo '</label>';
×
224
                }
225
        }
226

227
        /**
228
         * Output a legend element.
229
         *
230
         * @since 3.4
231
         *
232
         * @param string $text Legend text string.
233
         * @param array  $attr HTML attributes set.
234
         */
235
        public function legend( $text, $attr ) {
236
                $defaults = [
237
                        'id'    => '',
×
238
                        'class' => '',
239
                ];
240
                $attr     = wp_parse_args( $attr, $defaults );
×
241

242
                $id = ( $attr['id'] === '' ) ? '' : ' id="' . esc_attr( $attr['id'] ) . '"';
×
243
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
244
                echo '<legend class="' . esc_attr( 'yoast-form-legend ' . $attr['class'] ) . '"' . $id . '>' . $text . '</legend>';
×
245
        }
246

247
        /**
248
         * Create a Checkbox input field.
249
         *
250
         * @since 2.0
251
         *
252
         * @param string $variable   The variable within the option to create the checkbox for.
253
         * @param string $label      The label to show for the variable.
254
         * @param bool   $label_left Whether the label should be left (true) or right (false).
255
         * @param array  $attr       Extra attributes to add to the checkbox.
256
         */
257
        public function checkbox( $variable, $label, $label_left = false, $attr = [] ) {
258
                $val = $this->get_field_value( $variable, false );
×
259

260
                $defaults = [
261
                        'disabled' => false,
×
262
                ];
263
                $attr     = wp_parse_args( $attr, $defaults );
×
264

265
                if ( $val === true ) {
×
266
                        $val = 'on';
×
267
                }
268

269
                $class = '';
×
270
                if ( $label_left !== false ) {
×
271
                        $this->label( $label_left, [ 'for' => $variable ] );
×
272
                }
273
                else {
274
                        $class = 'double';
×
275
                }
276

277
                $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
278

279
                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded and all other output is properly escaped.
280
                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, '/>';
×
281

282
                if ( ! empty( $label ) ) {
×
283
                        $this->label( $label, [ 'for' => $variable ] );
×
284
                }
285

286
                echo '<br class="clear" />';
×
287
        }
288

289
        /**
290
         * Creates a Checkbox input field list.
291
         *
292
         * @since 12.8
293
         *
294
         * @param string $variable The variables within the option to create the checkbox list for.
295
         * @param string $labels   The labels to show for the variable.
296
         * @param array  $attr     Extra attributes to add to the checkbox list.
297
         */
298
        public function checkbox_list( $variable, $labels, $attr = [] ) {
299
                $defaults = [
300
                        'disabled' => false,
×
301
                ];
302
                $attr     = wp_parse_args( $attr, $defaults );
×
303

304
                $values = $this->get_field_value( $variable, [] );
×
305

306
                foreach ( $labels as $name => $label ) {
×
307
                        printf(
×
308
                                '<input class="checkbox double" id="%1$s" type="checkbox" name="%2$s" %3$s %5$s value="%4$s"/>',
×
309
                                esc_attr( $variable . '-' . $name ),
×
310
                                esc_attr( $this->option_name . '[' . $variable . '][' . $name . ']' ),
×
311
                                checked( ! empty( $values[ $name ] ), true, false ),
×
312
                                esc_attr( $name ),
×
313
                                disabled( ( isset( $attr['disabled'] ) && $attr['disabled'] ), true, false )
×
314
                        );
315

316
                        printf(
×
317
                                '<label class="checkbox" for="%1$s">%2$s</label>',
×
318
                                esc_attr( $variable . '-' . $name ), // #1
×
319
                                esc_html( $label )
×
320
                        );
321
                        echo '<br class="clear">';
×
322
                }
323
        }
324

325
        /**
326
         * Create a light switch input field using a single checkbox.
327
         *
328
         * @since 3.1
329
         *
330
         * @param string $variable The variable within the option to create the checkbox for.
331
         * @param string $label    The visual label text for the toggle.
332
         * @param array  $buttons  Array of two visual labels for the buttons (defaults Disabled/Enabled).
333
         * @param bool   $reverse  Reverse order of buttons (default true).
334
         * @param string $help     Inline Help that will be printed out before the toggle.
335
         * @param bool   $strong   Whether the visual label is displayed in strong text. Default is false.
336
         *                         Starting from Yoast SEO 16.5, the visual label is forced to bold via CSS.
337
         * @param array  $attr     Extra attributes to add to the light switch.
338
         */
339
        public function light_switch( $variable, $label, $buttons = [], $reverse = true, $help = '', $strong = false, $attr = [] ) {
340
                $val = $this->get_field_value( $variable, false );
×
341

342
                $defaults = [
343
                        'disabled' => false,
×
344
                ];
345
                $attr     = wp_parse_args( $attr, $defaults );
×
346

347
                if ( $val === true ) {
×
348
                        $val = 'on';
×
349
                }
350

351
                $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
352

353
                $output = new Light_Switch_Presenter(
×
354
                        $variable,
×
355
                        $label,
×
356
                        $buttons,
×
357
                        $this->option_name . '[' . $variable . ']',
×
358
                        $val,
×
359
                        $reverse,
×
360
                        $help,
×
361
                        $strong,
×
362
                        $disabled_attribute
×
363
                );
364

365
                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: All output is properly escaped or hardcoded in the presenter.
366
                echo $output;
×
367
        }
368

369
        /**
370
         * Create a Text input field.
371
         *
372
         * @since 2.0
373
         * @since 2.1 Introduced the `$attr` parameter.
374
         *
375
         * @param string       $variable The variable within the option to create the text input field for.
376
         * @param string       $label    The label to show for the variable.
377
         * @param array|string $attr     Extra attributes to add to the input field. Can be class, disabled, autocomplete.
378
         */
379
        public function textinput( $variable, $label, $attr = [] ) {
380
                $type = 'text';
×
381
                if ( ! is_array( $attr ) ) {
×
382
                        $attr = [
383
                                'class'    => $attr,
×
384
                                'disabled' => false,
385
                        ];
386
                }
387

388
                $defaults = [
389
                        'placeholder' => '',
×
390
                        'class'       => '',
391
                ];
392
                $attr     = wp_parse_args( $attr, $defaults );
×
393
                $val      = $this->get_field_value( $variable, '' );
×
394
                if ( isset( $attr['type'] ) && $attr['type'] === 'url' ) {
×
395
                        $val  = urldecode( $val );
×
396
                        $type = 'url';
×
397
                }
398
                $attributes = isset( $attr['autocomplete'] ) ? ' autocomplete="' . esc_attr( $attr['autocomplete'] ) . '"' : '';
×
399

400
                $this->label(
×
401
                        $label,
×
402
                        [
403
                                'for'   => $variable,
×
404
                                'class' => 'textinput',
×
405
                        ]
406
                );
407

408
                $aria_attributes = Yoast_Input_Validation::get_the_aria_invalid_attribute( $variable );
×
409

410
                Yoast_Input_Validation::set_error_descriptions();
×
411
                $aria_attributes .= Yoast_Input_Validation::get_the_aria_describedby_attribute( $variable );
×
412

413
                $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
414

415
                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded and all other output is properly escaped.
416
                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" />';
×
417
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in getter.
418
                echo Yoast_Input_Validation::get_the_error_description( $variable );
×
419
        }
420

421
        /**
422
         * Create a Number input field.
423
         *
424
         * @param string       $variable The variable within the option to create the text input field for.
425
         * @param string       $label    The label to show for the variable.
426
         * @param array|string $attr     Extra attributes to add to the input field. Can be class, disabled, autocomplete.
427
         */
428
        public function number( $variable, $label, $attr = [] ) {
429
                $type     = 'number';
×
430
                $defaults = [
431
                        'placeholder' => '',
×
432
                        'class'       => 'number',
433
                        'disabled'    => false,
434
                        'min'         => 0,
435
                        'max'         => 100,
436
                ];
437
                $attr     = wp_parse_args( $attr, $defaults );
×
438
                $val      = $this->get_field_value( $variable, 0 );
×
439

440
                $this->label(
×
441
                        $label,
×
442
                        [
443
                                'for'   => $variable,
×
444
                                'class' => 'textinput ' . $attr['class'],
×
445
                        ]
446
                );
447

448
                $aria_attributes = Yoast_Input_Validation::get_the_aria_invalid_attribute( $variable );
×
449

450
                Yoast_Input_Validation::set_error_descriptions();
×
451
                $aria_attributes .= Yoast_Input_Validation::get_the_aria_describedby_attribute( $variable );
×
452

453
                $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
454

455
                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded and all other output is properly escaped.
456
                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" />';
×
457
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in getter.
458
                echo Yoast_Input_Validation::get_the_error_description( $variable );
×
459
        }
460

461
        /**
462
         * Creates a text input field with with the ability to add content after the label.
463
         *
464
         * @param string $variable The variable within the option to create the text input field for.
465
         * @param string $label    The label to show for the variable.
466
         * @param array  $attr     Extra attributes to add to the input field.
467
         *
468
         * @return void
469
         */
470
        public function textinput_extra_content( $variable, $label, $attr = [] ) {
471
                $type = 'text';
×
472

473
                $defaults = [
474
                        'class'       => 'yoast-field-group__inputfield',
×
475
                        'disabled'    => false,
476
                ];
477

478
                $attr = \wp_parse_args( $attr, $defaults );
×
479
                $val  = $this->get_field_value( $variable, '' );
×
480

481
                if ( isset( $attr['type'] ) && $attr['type'] === 'url' ) {
×
482
                        $val  = urldecode( $val );
×
483
                        $type = 'url';
×
484
                }
485

486
                echo '<div class="yoast-field-group__title">';
×
487
                $this->label(
×
488
                        $label,
×
489
                        [
490
                                'for'   => $variable,
×
491
                                'class' => $attr['class'] . '--label',
×
492
                        ]
493
                );
494

495
                if ( isset( $attr['extra_content'] ) ) {
×
496
                        // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: may contain HTML that should not be escaped.
497
                        echo $attr['extra_content'];
×
498
                }
499
                echo '</div>';
×
500

501
                $has_input_error = Yoast_Input_Validation::yoast_form_control_has_error( $variable );
×
502
                $aria_attributes = Yoast_Input_Validation::get_the_aria_invalid_attribute( $variable );
×
503

504
                Yoast_Input_Validation::set_error_descriptions();
×
505
                $aria_attributes .= Yoast_Input_Validation::get_the_aria_describedby_attribute( $variable );
×
506

507
                // phpcs:disable WordPress.Security.EscapeOutput -- Reason: output is properly escaped or hardcoded.
508
                printf(
×
509
                        '<input type="%1$s" name="%2$s" id="%3$s" class="%4$s"%5$s%6$s%7$s value="%8$s"%9$s>',
×
510
                        $type,
×
511
                        \esc_attr( $this->option_name . '[' . $variable . ']' ),
×
512
                        \esc_attr( $variable ),
×
513
                        \esc_attr( $attr['class'] ),
×
514
                        isset( $attr['placeholder'] ) ? ' placeholder="' . \esc_attr( $attr['placeholder'] ) . '"' : '',
×
515
                        isset( $attr['autocomplete'] ) ? ' autocomplete="' . \esc_attr( $attr['autocomplete'] ) . '"' : '',
×
516
                        $aria_attributes,
×
517
                        \esc_attr( $val ),
×
518
                        $this->get_disabled_attribute( $variable, $attr )
×
519
                );
520
                // phpcs:enable
521
                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: output is properly escaped.
522
                echo Yoast_Input_Validation::get_the_error_description( $variable );
×
523
        }
524

525
        /**
526
         * Create a textarea.
527
         *
528
         * @since 2.0
529
         *
530
         * @param string       $variable The variable within the option to create the textarea for.
531
         * @param string       $label    The label to show for the variable.
532
         * @param string|array $attr     The CSS class or an array of attributes to assign to the textarea.
533
         */
534
        public function textarea( $variable, $label, $attr = [] ) {
535
                if ( ! is_array( $attr ) ) {
×
536
                        $attr = [
537
                                'class' => $attr,
×
538
                        ];
539
                }
540

541
                $defaults = [
542
                        'cols'     => '',
×
543
                        'rows'     => '',
544
                        'class'    => '',
545
                        'disabled' => false,
546
                ];
547
                $attr     = wp_parse_args( $attr, $defaults );
×
548
                $val      = $this->get_field_value( $variable, '' );
×
549

550
                $this->label(
×
551
                        $label,
×
552
                        [
553
                                'for'   => $variable,
×
554
                                'class' => 'textinput',
×
555
                        ]
556
                );
557

558
                $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
559

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

564
        /**
565
         * Create a hidden input field.
566
         *
567
         * @since 2.0
568
         *
569
         * @param string $variable The variable within the option to create the hidden input for.
570
         * @param string $id       The ID of the element.
571
         * @param mixed  $val      Optional. The value to set in the input field. Otherwise the value from the options will be used.
572
         */
573
        public function hidden( $variable, $id = '', $val = null ) {
574
                if ( is_null( $val ) ) {
×
575
                        $val = $this->get_field_value( $variable, '' );
×
576
                }
577

578
                if ( is_bool( $val ) ) {
×
579
                        $val = ( $val === true ) ? 'true' : 'false';
×
580
                }
581

582
                if ( $id === '' ) {
×
583
                        $id = 'hidden_' . $variable;
×
584
                }
585

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

589
        /**
590
         * Create a Select Box.
591
         *
592
         * @since 2.0
593
         *
594
         * @param string $variable       The variable within the option to create the select for.
595
         * @param string $label          The label to show for the variable.
596
         * @param array  $select_options The select options to choose from.
597
         * @param string $styled         The select style. Use 'styled' to get a styled select. Default 'unstyled'.
598
         * @param bool   $show_label     Whether or not to show the label, if not, it will be applied as an aria-label.
599
         * @param array  $attr           Extra attributes to add to the select.
600
         * @param string $help           Optional. Inline Help HTML that will be printed after the label. Default is empty.
601
         */
602
        public function select( $variable, $label, array $select_options, $styled = 'unstyled', $show_label = true, $attr = [], $help = '' ) {
603
                if ( empty( $select_options ) ) {
×
604
                        return;
×
605
                }
606

607
                $defaults = [
608
                        'disabled' => false,
×
609
                ];
610
                $attr     = wp_parse_args( $attr, $defaults );
×
611

612
                if ( $show_label ) {
×
613
                        $this->label(
×
614
                                $label,
×
615
                                [
616
                                        'for'   => $variable,
×
617
                                        'class' => 'select',
×
618
                                ]
619
                        );
620
                        echo $help; // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: The help contains HTML.
×
621
                }
622

623
                $select_name       = esc_attr( $this->option_name ) . '[' . esc_attr( $variable ) . ']';
×
624
                $active_option     = $this->get_field_value( $variable, '' );
×
625
                $wrapper_start_tag = '';
×
626
                $wrapper_end_tag   = '';
×
627

628
                $select = new Yoast_Input_Select( $variable, $select_name, $select_options, $active_option );
×
629
                $select->add_attribute( 'class', 'select' );
×
630

631
                if ( $this->is_control_disabled( $variable )
×
632
                        || ( isset( $attr['disabled'] ) && $attr['disabled'] ) ) {
×
633
                        $select->add_attribute( 'disabled', 'disabled' );
×
634
                }
635

636
                if ( ! $show_label ) {
×
637
                        $select->add_attribute( 'aria-label', $label );
×
638
                }
639

640
                if ( $styled === 'styled' ) {
×
641
                        $wrapper_start_tag = '<span class="yoast-styled-select">';
×
642
                        $wrapper_end_tag   = '</span>';
×
643
                }
644

645
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
646
                echo $wrapper_start_tag;
×
647
                $select->output_html();
×
648
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
649
                echo $wrapper_end_tag;
×
650
                echo '<br class="clear"/>';
×
651
        }
652

653
        /**
654
         * Create a File upload field.
655
         *
656
         * @since 2.0
657
         *
658
         * @param string $variable The variable within the option to create the file upload field for.
659
         * @param string $label    The label to show for the variable.
660
         * @param array  $attr     Extra attributes to add to the file upload input.
661
         */
662
        public function file_upload( $variable, $label, $attr = [] ) {
663
                $val = $this->get_field_value( $variable, '' );
×
664
                if ( is_array( $val ) ) {
×
665
                        $val = $val['url'];
×
666
                }
667

668
                $defaults = [
669
                        'disabled' => false,
×
670
                ];
671
                $attr     = wp_parse_args( $attr, $defaults );
×
672

673
                $var_esc = esc_attr( $variable );
×
674
                $this->label(
×
675
                        $label,
×
676
                        [
677
                                'for'   => $variable,
×
678
                                'class' => 'select',
×
679
                        ]
680
                );
681

682
                $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
683

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

687
                // Need to save separate array items in hidden inputs, because empty file inputs type will be deleted by settings API.
688
                if ( ! empty( $val ) ) {
×
689
                        $this->hidden( 'file', $this->option_name . '_file' );
×
690
                        $this->hidden( 'url', $this->option_name . '_url' );
×
691
                        $this->hidden( 'type', $this->option_name . '_type' );
×
692
                }
693
                echo '<br class="clear"/>';
×
694
        }
695

696
        /**
697
         * Media input.
698
         *
699
         * @since 2.0
700
         *
701
         * @param string $variable Option name.
702
         * @param string $label    Label message.
703
         * @param array  $attr     Extra attributes to add to the media input and buttons.
704
         */
705
        public function media_input( $variable, $label, $attr = [] ) {
706
                $val      = $this->get_field_value( $variable, '' );
×
707
                $id_value = $this->get_field_value( $variable . '_id', '' );
×
708

709
                $var_esc = esc_attr( $variable );
×
710

711
                $defaults = [
712
                        'disabled' => false,
×
713
                ];
714
                $attr     = wp_parse_args( $attr, $defaults );
×
715

716
                $this->label(
×
717
                        $label,
×
718
                        [
719
                                'for'   => 'wpseo_' . $variable,
×
720
                                'class' => 'select',
×
721
                        ]
722
                );
723

724
                $id_field_id = 'wpseo_' . $var_esc . '_id';
×
725

726
                $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
727

728
                echo '<span>';
×
729
                        echo '<input',
×
730
                                ' class="textinput"',
×
731
                                ' id="wpseo_', $var_esc, '"', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
×
732
                                ' type="text" size="36"',
×
733
                                ' name="', esc_attr( $this->option_name ), '[', $var_esc, ']"', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
×
734
                                ' value="', esc_attr( $val ), '"',
×
735
                                ' readonly="readonly"',
×
736
                                ' /> ';
×
737
                        echo '<input',
×
738
                                ' id="wpseo_', $var_esc, '_button"', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
×
739
                                ' class="wpseo_image_upload_button button"',
×
740
                                ' type="button"',
×
741
                                ' value="', esc_attr__( 'Upload Image', 'wordpress-seo' ), '"',
×
742
                                ' data-target-id="', esc_attr( $id_field_id ), '"',
×
743
                                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded.
744
                                $disabled_attribute,
×
745
                                ' /> ';
×
746
                        echo '<input',
×
747
                                ' class="wpseo_image_remove_button button"',
×
748
                                ' type="button"',
×
749
                                ' value="', esc_attr__( 'Clear Image', 'wordpress-seo' ), '"',
×
750
                                // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded.
751
                                $disabled_attribute,
×
752
                                ' />';
×
753
                        echo '<input',
×
754
                                ' type="hidden"',
×
755
                                ' id="', esc_attr( $id_field_id ), '"',
×
756
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
757
                                ' name="', esc_attr( $this->option_name ), '[', $var_esc, '_id]"',
×
758
                                ' value="', esc_attr( $id_value ), '"',
×
759
                                ' />';
×
760
                echo '</span>';
×
761
                echo '<br class="clear"/>';
×
762
        }
763

764
        /**
765
         * Create a Radio input field.
766
         *
767
         * @since 2.0
768
         *
769
         * @param string $variable    The variable within the option to create the radio button for.
770
         * @param array  $values      The radio options to choose from.
771
         * @param string $legend      Optional. The legend to show for the field set, if any.
772
         * @param array  $legend_attr Optional. The attributes for the legend, if any.
773
         * @param array  $attr        Extra attributes to add to the radio button.
774
         */
775
        public function radio( $variable, $values, $legend = '', $legend_attr = [], $attr = [] ) {
776
                if ( ! is_array( $values ) || $values === [] ) {
×
777
                        return;
×
778
                }
779
                $val = $this->get_field_value( $variable, false );
×
780

781
                $var_esc = esc_attr( $variable );
×
782

783
                $defaults = [
784
                        'disabled' => false,
×
785
                ];
786
                $attr     = wp_parse_args( $attr, $defaults );
×
787

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

791
                if ( is_string( $legend ) && $legend !== '' ) {
×
792

793
                        $legend_defaults = [
794
                                'id'    => '',
×
795
                                'class' => 'radiogroup',
796
                        ];
797

798
                        $legend_attr = wp_parse_args( $legend_attr, $legend_defaults );
×
799

800
                        $this->legend( $legend, $legend_attr );
×
801
                }
802

803
                foreach ( $values as $key => $value ) {
×
804
                        $label      = $value;
×
805
                        $aria_label = '';
×
806

807
                        if ( is_array( $value ) ) {
×
808
                                $label      = isset( $value['label'] ) ? $value['label'] : '';
×
809
                                $aria_label = isset( $value['aria_label'] ) ? $value['aria_label'] : '';
×
810
                        }
811

812
                        $key_esc = esc_attr( $key );
×
813

814
                        $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
815

816
                        // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded and all other output is properly escaped.
817
                        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 . ' />';
×
818
                        $this->label(
×
819
                                $label,
×
820
                                [
821
                                        'for'        => $var_esc . '-' . $key_esc,
×
822
                                        'class'      => 'radio',
×
823
                                        'aria_label' => $aria_label,
×
824
                                ]
825
                        );
826
                }
827
                echo '</fieldset>';
×
828
        }
829

830
        /**
831
         * Create a toggle switch input field using two radio buttons.
832
         *
833
         * @since 3.1
834
         *
835
         * @param string $variable The variable within the option to create the radio buttons for.
836
         * @param array  $values   Associative array of on/off keys and their values to be used as
837
         *                         the label elements text for the radio buttons. Optionally, each
838
         *                         value can be an array of visible label text and screen reader text.
839
         * @param string $label    The visual label for the radio buttons group, used as the fieldset legend.
840
         * @param string $help     Inline Help that will be printed out before the visible toggles text.
841
         * @param array  $attr     Extra attributes to add to the toggle switch.
842
         */
843
        public function toggle_switch( $variable, $values, $label, $help = '', $attr = [] ) {
844
                if ( ! is_array( $values ) || $values === [] ) {
×
845
                        return;
×
846
                }
847

848
                $defaults = [
849
                        'disabled' => false,
×
850
                ];
851
                $attr     = wp_parse_args( $attr, $defaults );
×
852

853
                if ( isset( $attr['preserve_disabled_value'] ) && $attr['preserve_disabled_value'] ) {
×
854
                        $this->hidden( $variable );
×
855
                        $variable .= '_disabled';
×
856
                }
857

858
                $val = $this->get_field_value( $variable, false );
×
859
                if ( $val === true ) {
×
860
                        $val = 'on';
×
861
                }
862
                if ( $val === false ) {
×
863
                        $val = 'off';
×
864
                }
865

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

868
                $has_premium_upsell = ( isset( $attr['show_premium_upsell'] ) && $attr['show_premium_upsell'] && isset( $attr['premium_upsell_url'] ) && ! empty( $attr['premium_upsell_url'] ) );
×
869
                $upsell_class       = ( $has_premium_upsell ) ? ' premium-upsell' : '';
×
870

871
                $var_esc = esc_attr( $variable );
×
872

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

877
                // Show disabled note if attribute does not exists or does exist and is set to true.
878
                if ( ! isset( $attr['show_disabled_note'] ) || ( $attr['show_disabled_note'] === true ) ) {
×
879
                        if ( isset( $attr['note_when_disabled'] ) ) {
×
880
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
881
                                echo $this->get_disabled_note( $variable, $attr['note_when_disabled'] );
×
882
                        }
883
                        else {
884
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
885
                                echo $this->get_disabled_note( $variable );
×
886
                        }
887
                }
888

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

891
                foreach ( $values as $key => $value ) {
×
892
                        $screen_reader_text_html = '';
×
893

894
                        if ( is_array( $value ) ) {
×
895
                                $screen_reader_text      = $value['screen_reader_text'];
×
896
                                $screen_reader_text_html = '<span class="screen-reader-text"> ' . esc_html( $screen_reader_text ) . '</span>';
×
897
                                $value                   = $value['text'];
×
898
                        }
899

900
                        $key_esc            = esc_attr( $key );
×
901
                        $for                = $var_esc . '-' . $key_esc;
×
902
                        $disabled_attribute = $this->get_disabled_attribute( $variable, $attr );
×
903

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

910
                $upsell_button = '';
×
911
                if ( $has_premium_upsell ) {
×
912
                        $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=' .
NEW
913
                                                        esc_url( $attr['premium_upsell_url'] ) . ' target="_blank">' .
×
NEW
914
                                                        esc_html__( 'Unlock with Premium!', 'wordpress-seo' ) .
×
915
                                                        /* translators: Hidden accessibility text. */
NEW
916
                                                        '<span class="screen-reader-text">' . esc_html__( '(Opens in a new browser tab)', 'wordpress-seo' ) . '</span>' .
×
NEW
917
                                                        '<span aria-hidden="true" class="yoast-button--buy__caret"></span></a>';
×
918
                }
919

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

924
        /**
925
         * Creates a toggle switch to define whether an indexable should be indexed or not.
926
         *
927
         * @param string $variable The variable within the option to create the radio buttons for.
928
         * @param string $label    The visual label for the radio buttons group, used as the fieldset legend.
929
         * @param string $help     Inline Help that will be printed out before the visible toggles text.
930
         * @param array  $attr     Extra attributes to add to the index switch.
931
         *
932
         * @return void
933
         */
934
        public function index_switch( $variable, $label, $help = '', $attr = [] ) {
935
                $defaults = [
936
                        'disabled' => false,
×
937
                ];
938
                $attr     = wp_parse_args( $attr, $defaults );
×
939

940
                $index_switch_values = [
941
                        'off' => __( 'On', 'wordpress-seo' ),
×
942
                        'on'  => __( 'Off', 'wordpress-seo' ),
×
943
                ];
944

945
                $is_disabled = ( isset( $attr['disabled'] ) && $attr['disabled'] );
×
946

947
                $this->toggle_switch(
×
948
                        $variable,
×
949
                        $index_switch_values,
×
950
                        sprintf(
×
951
                                /* translators: %s expands to an indexable object's name, like a post type or taxonomy */
952
                                esc_html__( 'Show %s in search results?', 'wordpress-seo' ),
×
953
                                $label
×
954
                        ),
955
                        $help,
×
956
                        [ 'disabled' => $is_disabled ]
×
957
                );
958
        }
959

960
        /**
961
         * Creates a toggle switch to show hide certain options.
962
         *
963
         * @param string $variable     The variable within the option to create the radio buttons for.
964
         * @param string $label        The visual label for the radio buttons group, used as the fieldset legend.
965
         * @param bool   $inverse_keys Whether or not the option keys need to be inverted to support older functions.
966
         * @param string $help         Inline Help that will be printed out before the visible toggles text.
967
         * @param array  $attr         Extra attributes to add to the show-hide switch.
968
         *
969
         * @return void
970
         */
971
        public function show_hide_switch( $variable, $label, $inverse_keys = false, $help = '', $attr = [] ) {
972
                $defaults = [
973
                        'disabled' => false,
×
974
                ];
975
                $attr     = wp_parse_args( $attr, $defaults );
×
976

977
                $on_key  = ( $inverse_keys ) ? 'off' : 'on';
×
978
                $off_key = ( $inverse_keys ) ? 'on' : 'off';
×
979

980
                $show_hide_switch = [
981
                        $on_key  => __( 'On', 'wordpress-seo' ),
×
982
                        $off_key => __( 'Off', 'wordpress-seo' ),
×
983
                ];
984

985
                $is_disabled = ( isset( $attr['disabled'] ) && $attr['disabled'] );
×
986

987
                $this->toggle_switch(
×
988
                        $variable,
×
989
                        $show_hide_switch,
×
990
                        $label,
×
991
                        $help,
×
992
                        [ 'disabled' => $is_disabled ]
×
993
                );
994
        }
995

996
        /**
997
         * Retrieves the value for the form field.
998
         *
999
         * @param string      $field_name    The field name to retrieve the value for.
1000
         * @param string|null $default_value The default value, when field has no value.
1001
         *
1002
         * @return mixed|null The retrieved value.
1003
         */
1004
        protected function get_field_value( $field_name, $default_value = null ) {
1005
                // On multisite subsites, the Usage tracking feature should always be set to Off.
1006
                if ( $this->is_tracking_on_subsite( $field_name ) ) {
×
1007
                        return false;
×
1008
                }
1009

1010
                return WPSEO_Options::get( $field_name, $default_value );
×
1011
        }
1012

1013
        /**
1014
         * Checks whether a given control should be disabled.
1015
         *
1016
         * @param string $variable The variable within the option to check whether its control should be disabled.
1017
         *
1018
         * @return bool True if control should be disabled, false otherwise.
1019
         */
1020
        protected function is_control_disabled( $variable ) {
1021
                if ( $this->option_instance === null ) {
8✔
1022
                        return false;
4✔
1023
                }
1024

1025
                // Disable the Usage tracking feature for multisite subsites.
1026
                if ( $this->is_tracking_on_subsite( $variable ) ) {
4✔
1027
                        return true;
×
1028
                }
1029

1030
                return $this->option_instance->is_disabled( $variable );
4✔
1031
        }
1032

1033
        /**
1034
         * Gets the explanation note to print if a given control is disabled.
1035
         *
1036
         * @param string $variable    The variable within the option to print a disabled note for.
1037
         * @param string $custom_note An optional custom note to print instead.
1038
         *
1039
         * @return string Explanation note HTML string, or empty string if no note necessary.
1040
         */
1041
        protected function get_disabled_note( $variable, $custom_note = '' ) {
1042
                if ( $custom_note === '' && ! $this->is_control_disabled( $variable ) ) {
×
1043
                        return '';
×
1044
                }
1045
                $disabled_message = esc_html__( 'This feature has been disabled by the network admin.', 'wordpress-seo' );
×
1046

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

1052
                if ( $custom_note ) {
×
1053
                        $disabled_message = esc_html( $custom_note );
×
1054
                }
1055

1056
                return '<p class="disabled-note">' . $disabled_message . '</p>';
×
1057
        }
1058

1059
        /**
1060
         * Determines whether we are dealing with the Usage tracking feature on a multisite subsite.
1061
         * This feature requires specific behavior for the toggle switch.
1062
         *
1063
         * @param string $feature_setting The feature setting.
1064
         *
1065
         * @return bool True if we are dealing with the Usage tracking feature on a multisite subsite.
1066
         */
1067
        protected function is_tracking_on_subsite( $feature_setting ) {
1068
                return ( $feature_setting === 'tracking' && ! is_network_admin() && ! is_main_site() );
×
1069
        }
1070

1071
        /**
1072
         * Returns the disabled attribute HTML.
1073
         *
1074
         * @param string $variable The variable within the option of the related form element.
1075
         * @param array  $attr     Extra attributes added to the form element.
1076
         *
1077
         * @return string The disabled attribute HTML.
1078
         */
1079
        protected function get_disabled_attribute( $variable, $attr ) {
1080
                if ( $this->is_control_disabled( $variable ) || ( isset( $attr['disabled'] ) && $attr['disabled'] ) ) {
×
1081
                        return ' disabled';
×
1082
                }
1083

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

© 2025 Coveralls, Inc