| 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 |
* @return void |
| 67 |
*/ |
| 68 |
public function output_html() { |
| 69 |
// Extract it, because we want each value accessible via a variable instead of accessing it as an array. |
| 70 |
extract( $this->get_select_values() ); |
| 71 |
|
| 72 |
require WPSEO_PATH . 'admin/views/form/select.php'; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Return the rendered view. |
| 77 |
* |
| 78 |
* @return string |
| 79 |
*/ |
| 80 |
public function get_html() { |
| 81 |
ob_start(); |
| 82 |
|
| 83 |
$this->output_html(); |
| 84 |
|
| 85 |
$rendered_output = ob_get_contents(); |
| 86 |
ob_end_clean(); |
| 87 |
|
| 88 |
return $rendered_output; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Add an attribute to the attributes property. |
| 93 |
* |
| 94 |
* @param string $attribute The name of the attribute to add. |
| 95 |
* @param string $value The value of the attribute. |
| 96 |
* |
| 97 |
* @return void |
| 98 |
*/ |
| 99 |
public function add_attribute( $attribute, $value ) { |
| 100 |
$this->select_attributes[ $attribute ] = $value; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Return the set fields for the select. |
| 105 |
* |
| 106 |
* @return array |
| 107 |
*/ |
| 108 |
private function get_select_values() { |
| 109 |
return [ |
| 110 |
'id' => $this->select_id, |
| 111 |
'name' => $this->select_name, |
| 112 |
'attributes' => $this->get_attributes(), |
| 113 |
'options' => $this->select_options, |
| 114 |
'selected' => $this->selected_option, |
| 115 |
]; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Return the attribute string, when there are attributes set. |
| 120 |
* |
| 121 |
* @return string |
| 122 |
*/ |
| 123 |
private function get_attributes() { |
| 124 |
$attributes = $this->select_attributes; |
| 125 |
|
| 126 |
if ( ! empty( $attributes ) ) { |
| 127 |
array_walk( $attributes, [ $this, 'parse_attribute' ] ); |
| 128 |
|
| 129 |
return implode( ' ', $attributes ) . ' '; |
| 130 |
} |
| 131 |
|
| 132 |
return ''; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get an attribute from the attributes. |
| 137 |
* |
| 138 |
* @param string $value The value of the attribute. |
| 139 |
* @param string $attribute The attribute to look for. |
| 140 |
* |
| 141 |
* @return void |
| 142 |
*/ |
| 143 |
private function parse_attribute( &$value, $attribute ) { |
| 144 |
$value = sprintf( '%s="%s"', sanitize_key( $attribute ), esc_attr( $value ) ); |
| 145 |
} |
| 146 |
} |
| 147 |
|