class-yoast-input-select.php 2.8 KB

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