class-yoast-input-select.php 2.7 KB

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