Yoast_Input_Select

Class for generating a html select.

Defined (1)

The class is defined in the following location(s).

/admin/views/class-yoast-input-select.php  
  1. class Yoast_Input_Select { 
  2.  
  3. /** 
  4. * @var string 
  5. */ 
  6. private $select_id; 
  7.  
  8. /** 
  9. * @var string 
  10. */ 
  11. private $select_name; 
  12.  
  13. /** 
  14. * @var array 
  15. */ 
  16. private $select_attributes = array(); 
  17.  
  18. /** 
  19. * @var array Array with the options to parse. 
  20. */ 
  21. private $select_options; 
  22.  
  23. /** 
  24. * @var string The current selected option. 
  25. */ 
  26. private $selected_option; 
  27.  
  28. /** 
  29. * Constructor. 
  30. * @param string $select_id ID for the select. 
  31. * @param string $select_name Name for the select. 
  32. * @param array $select_options Array with the options to parse. 
  33. * @param string $selected_option The current selected option. 
  34. */ 
  35. public function __construct( $select_id, $select_name, array $select_options, $selected_option ) { 
  36. $this->select_id = $select_id; 
  37. $this->select_name = $select_name; 
  38. $this->select_options = $select_options; 
  39. $this->selected_option = $selected_option; 
  40.  
  41. /** 
  42. * Print the rendered view. 
  43. */ 
  44. public function output_html() { 
  45. // Extract it, because we want each value accessible via a variable instead of accessing it as an array. 
  46. extract( $this->get_select_values() ); 
  47.  
  48. require( dirname( WPSEO_FILE ) . '/admin/views/form/select.php' ); 
  49.  
  50. /** 
  51. * Return the rendered view 
  52. * @return string 
  53. */ 
  54. public function get_html() { 
  55. ob_start(); 
  56.  
  57. $this->output_html(); 
  58.  
  59. $rendered_output = ob_get_contents(); 
  60. ob_end_clean(); 
  61.  
  62. return $rendered_output; 
  63.  
  64. /** 
  65. * Add an attribute to the attributes property 
  66. * @param string $attribute The name of the attribute to add. 
  67. * @param string $value The value of the attribute. 
  68. */ 
  69. public function add_attribute( $attribute, $value ) { 
  70. $this->select_attributes[ $attribute ] = $value; 
  71.  
  72. /** 
  73. * Return the set fields for the select 
  74. * @return array 
  75. */ 
  76. private function get_select_values() { 
  77. return array( 
  78. 'id' => $this->select_id,  
  79. 'name' => $this->select_name,  
  80. 'attributes' => $this->get_attributes(),  
  81. 'options' => $this->select_options,  
  82. 'selected' => $this->selected_option,  
  83. ); 
  84.  
  85. /** 
  86. * Return the attribute string, when there are attributes set. 
  87. * @return string 
  88. */ 
  89. private function get_attributes() { 
  90. $attributes = $this->select_attributes; 
  91.  
  92. if ( ! empty( $attributes ) ) { 
  93. array_walk( $attributes, array( $this, 'parse_attribute' ) ); 
  94.  
  95. return implode( ' ', $attributes ) . ' '; 
  96.  
  97. return ''; 
  98.  
  99. /** 
  100. * Get an attribute from the attributes. 
  101. * @param string $value The value of the attribute. 
  102. * @param string $attribute The attribute to look for. 
  103. */ 
  104. private function parse_attribute( & $value, $attribute ) { 
  105. $value = sprintf( '%s="%s"', esc_html( $attribute ), esc_attr( $value ) );