select.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor select control.
  8. *
  9. * A base control for creating select control. Displays a simple select box.
  10. * It accepts an array in which the `key` is the option value and the `value` is
  11. * the option name.
  12. *
  13. * @since 1.0.0
  14. */
  15. class Control_Select extends Base_Data_Control {
  16. /**
  17. * Get select control type.
  18. *
  19. * Retrieve the control type, in this case `select`.
  20. *
  21. * @since 1.0.0
  22. * @access public
  23. *
  24. * @return string Control type.
  25. */
  26. public function get_type() {
  27. return 'select';
  28. }
  29. /**
  30. * Get select control default settings.
  31. *
  32. * Retrieve the default settings of the select control. Used to return the
  33. * default settings while initializing the select control.
  34. *
  35. * @since 2.0.0
  36. * @access protected
  37. *
  38. * @return array Control default settings.
  39. */
  40. protected function get_default_settings() {
  41. return [
  42. 'options' => [],
  43. ];
  44. }
  45. /**
  46. * Render select control output in the editor.
  47. *
  48. * Used to generate the control HTML in the editor using Underscore JS
  49. * template. The variables for the class are available using `data` JS
  50. * object.
  51. *
  52. * @since 1.0.0
  53. * @access public
  54. */
  55. public function content_template() {
  56. $control_uid = $this->get_control_uid();
  57. ?>
  58. <div class="elementor-control-field">
  59. <label for="<?php echo $control_uid; ?>" class="elementor-control-title">{{{ data.label }}}</label>
  60. <div class="elementor-control-input-wrapper">
  61. <select id="<?php echo $control_uid; ?>" data-setting="{{ data.name }}">
  62. <#
  63. var printOptions = function( options ) {
  64. _.each( options, function( option_title, option_value ) { #>
  65. <option value="{{ option_value }}">{{{ option_title }}}</option>
  66. <# } );
  67. };
  68. if ( data.groups ) {
  69. for ( var groupIndex in data.groups ) {
  70. var groupArgs = data.groups[ groupIndex ];
  71. if ( groupArgs.options ) { #>
  72. <optgroup label="{{ groupArgs.label }}">
  73. <# printOptions( groupArgs.options ) #>
  74. </optgroup>
  75. <# } else if ( _.isString( groupArgs ) ) { #>
  76. <option value="{{ groupIndex }}">{{{ groupArgs }}}</option>
  77. <# }
  78. }
  79. } else {
  80. printOptions( data.options );
  81. }
  82. #>
  83. </select>
  84. </div>
  85. </div>
  86. <# if ( data.description ) { #>
  87. <div class="elementor-control-field-description">{{{ data.description }}}</div>
  88. <# } #>
  89. <?php
  90. }
  91. }