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