select.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Field: select
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! class_exists( 'CSF_Field_select' ) ) {
  11. class CSF_Field_select extends CSF_Fields {
  12. public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) {
  13. parent::__construct( $field, $value, $unique, $where, $parent );
  14. }
  15. public function render() {
  16. $args = wp_parse_args( $this->field, array(
  17. 'placeholder' => '',
  18. 'chosen' => false,
  19. 'multiple' => false,
  20. 'sortable' => false,
  21. 'ajax' => false,
  22. 'settings' => array(),
  23. 'query_args' => array(),
  24. ) );
  25. $this->value = ( is_array( $this->value ) ) ? $this->value : array_filter( (array) $this->value );
  26. echo $this->field_before();
  27. if ( isset( $this->field['options'] ) ) {
  28. if ( ! empty( $args['ajax'] ) ) {
  29. $args['settings']['data']['type'] = $args['options'];
  30. $args['settings']['data']['nonce'] = wp_create_nonce( 'csf_chosen_ajax_nonce' );
  31. if ( ! empty( $args['query_args'] ) ) {
  32. $args['settings']['data']['query_args'] = $args['query_args'];
  33. }
  34. }
  35. $chosen_rtl = ( is_rtl() ) ? ' chosen-rtl' : '';
  36. $multiple_name = ( $args['multiple'] ) ? '[]' : '';
  37. $multiple_attr = ( $args['multiple'] ) ? ' multiple="multiple"' : '';
  38. $chosen_sortable = ( $args['chosen'] && $args['sortable'] ) ? ' csf-chosen-sortable' : '';
  39. $chosen_ajax = ( $args['chosen'] && $args['ajax'] ) ? ' csf-chosen-ajax' : '';
  40. $placeholder_attr = ( $args['chosen'] && $args['placeholder'] ) ? ' data-placeholder="'. esc_attr( $args['placeholder'] ) .'"' : '';
  41. $field_class = ( $args['chosen'] ) ? ' class="csf-chosen'. esc_attr( $chosen_rtl . $chosen_sortable . $chosen_ajax ) .'"' : '';
  42. $field_name = $this->field_name( $multiple_name );
  43. $field_attr = $this->field_attributes();
  44. $maybe_options = $this->field['options'];
  45. $chosen_data_attr = ( $args['chosen'] && ! empty( $args['settings'] ) ) ? ' data-chosen-settings="'. esc_attr( json_encode( $args['settings'] ) ) .'"' : '';
  46. if ( is_string( $maybe_options ) && ! empty( $args['chosen'] ) && ! empty( $args['ajax'] ) ) {
  47. $options = $this->field_wp_query_data_title( $maybe_options, $this->value );
  48. } else if ( is_string( $maybe_options ) ) {
  49. $options = $this->field_data( $maybe_options, false, $args['query_args'] );
  50. } else {
  51. $options = $maybe_options;
  52. }
  53. if ( ( is_array( $options ) && ! empty( $options ) ) || ( ! empty( $args['chosen'] ) && ! empty( $args['ajax'] ) ) ) {
  54. if ( ! empty( $args['chosen'] ) && ! empty( $args['multiple'] ) ) {
  55. echo '<select name="'. $field_name .'" class="csf-hide-select hidden"'. $multiple_attr . $field_attr .'>';
  56. foreach ( $this->value as $option_key ) {
  57. echo '<option value="'. esc_attr( $option_key ) .'" selected>'. esc_attr( $option_key ) .'</option>';
  58. }
  59. echo '</select>';
  60. $field_name = '_pseudo';
  61. $field_attr = '';
  62. }
  63. // These attributes has been serialized above.
  64. echo '<select name="'. esc_attr( $field_name ) .'"'. $field_class . $multiple_attr . $placeholder_attr . $field_attr . $chosen_data_attr .'>';
  65. if ( $args['placeholder'] && empty( $args['multiple'] ) ) {
  66. if ( ! empty( $args['chosen'] ) ) {
  67. echo '<option value=""></option>';
  68. } else {
  69. echo '<option value="">'. esc_attr( $args['placeholder'] ) .'</option>';
  70. }
  71. }
  72. foreach ( $options as $option_key => $option ) {
  73. if ( is_array( $option ) && ! empty( $option ) ) {
  74. echo '<optgroup label="'. esc_attr( $option_key ) .'">';
  75. foreach ( $option as $sub_key => $sub_value ) {
  76. $selected = ( in_array( $sub_key, $this->value ) ) ? ' selected' : '';
  77. echo '<option value="'. esc_attr( $sub_key ) .'" '. esc_attr( $selected ) .'>'. esc_attr( $sub_value ) .'</option>';
  78. }
  79. echo '</optgroup>';
  80. } else {
  81. $selected = ( in_array( $option_key, $this->value ) ) ? ' selected' : '';
  82. echo '<option value="'. esc_attr( $option_key ) .'" '. esc_attr( $selected ) .'>'. esc_attr( $option ) .'</option>';
  83. }
  84. }
  85. echo '</select>';
  86. } else {
  87. echo ( ! empty( $this->field['empty_message'] ) ) ? esc_attr( $this->field['empty_message'] ) : esc_html__( 'No data available.', 'sakurairo_csf' );
  88. }
  89. }
  90. echo $this->field_after();
  91. }
  92. public function enqueue() {
  93. if ( ! wp_script_is( 'jquery-ui-sortable' ) ) {
  94. wp_enqueue_script( 'jquery-ui-sortable' );
  95. }
  96. }
  97. }
  98. }