PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.1
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / admin / views / class-yoast-input-select.php

class-yoast-input-select.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.1, at admin/views/class-yoast-input-select.php

141 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\Admin
6 */
7
8 /**
9 * Class for generating a html select.
10 */
11 class Yoast_Input_Select {
12
13 /**
14 * The id attribute value.
15 *
16 * @var string
17 */
18 private $select_id;
19
20 /**
21 * The name attribute value.
22 *
23 * @var string
24 */
25 private $select_name;
26
27 /**
28 * Additional select attributes.
29 *
30 * @var array
31 */
32 private $select_attributes = [];
33
34 /**
35 * Array with the options to parse.
36 *
37 * @var array
38 */
39 private $select_options;
40
41 /**
42 * The current selected option.
43 *
44 * @var string
45 */
46 private $selected_option;
47
48 /**
49 * Constructor.
50 *
51 * @param string $select_id ID for the select.
52 * @param string $select_name Name for the select.
53 * @param array $select_options Array with the options to parse.
54 * @param string $selected_option The current selected option.
55 */
56 public function __construct( $select_id, $select_name, array $select_options, $selected_option ) {
57 $this->select_id = $select_id;
58 $this->select_name = $select_name;
59 $this->select_options = $select_options;
60 $this->selected_option = $selected_option;
61 }
62
63 /**
64 * Print the rendered view.
65 */
66 public function output_html() {
67 // Extract it, because we want each value accessible via a variable instead of accessing it as an array.
68 extract( $this->get_select_values() );
69
70 require WPSEO_PATH . 'admin/views/form/select.php';
71 }
72
73 /**
74 * Return the rendered view.
75 *
76 * @return string
77 */
78 public function get_html() {
79 ob_start();
80
81 $this->output_html();
82
83 $rendered_output = ob_get_contents();
84 ob_end_clean();
85
86 return $rendered_output;
87 }
88
89 /**
90 * Add an attribute to the attributes property.
91 *
92 * @param string $attribute The name of the attribute to add.
93 * @param string $value The value of the attribute.
94 */
95 public function add_attribute( $attribute, $value ) {
96 $this->select_attributes[ $attribute ] = $value;
97 }
98
99 /**
100 * Return the set fields for the select.
101 *
102 * @return array
103 */
104 private function get_select_values() {
105 return [
106 'id' => $this->select_id,
107 'name' => $this->select_name,
108 'attributes' => $this->get_attributes(),
109 'options' => $this->select_options,
110 'selected' => $this->selected_option,
111 ];
112 }
113
114 /**
115 * Return the attribute string, when there are attributes set.
116 *
117 * @return string
118 */
119 private function get_attributes() {
120 $attributes = $this->select_attributes;
121
122 if ( ! empty( $attributes ) ) {
123 array_walk( $attributes, [ $this, 'parse_attribute' ] );
124
125 return implode( ' ', $attributes ) . ' ';
126 }
127
128 return '';
129 }
130
131 /**
132 * Get an attribute from the attributes.
133 *
134 * @param string $value The value of the attribute.
135 * @param string $attribute The attribute to look for.
136 */
137 private function parse_attribute( &$value, $attribute ) {
138 $value = sprintf( '%s="%s"', sanitize_key( $attribute ), esc_attr( $value ) );
139 }
140 }
141