ReactSelect.php
150 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Customizer Control: kirki-select. |
| 4 | * |
| 5 | * @package kirki-framework/control-select |
| 6 | * @copyright Copyright (c) 2023, Themeum |
| 7 | * @license https://opensource.org/licenses/MIT |
| 8 | * @since 1.0 |
| 9 | */ |
| 10 | |
| 11 | namespace Kirki\Control; |
| 12 | |
| 13 | use Kirki\Control\Base; |
| 14 | use Kirki\URL; |
| 15 | |
| 16 | /** |
| 17 | * Select control. |
| 18 | * |
| 19 | * @since 1.0 |
| 20 | */ |
| 21 | class ReactSelect extends Base { |
| 22 | |
| 23 | /** |
| 24 | * The control type. |
| 25 | * |
| 26 | * @access public |
| 27 | * @since 1.0 |
| 28 | * @var string |
| 29 | */ |
| 30 | public $type = 'kirki-react-select'; |
| 31 | |
| 32 | /** |
| 33 | * Placeholder text. |
| 34 | * |
| 35 | * @access public |
| 36 | * @since 1.0 |
| 37 | * @var string|false |
| 38 | */ |
| 39 | public $placeholder = false; |
| 40 | |
| 41 | /** |
| 42 | * Whether the select should be clearable or not. |
| 43 | * |
| 44 | * @since 1.0 |
| 45 | * @var bool |
| 46 | */ |
| 47 | public $clearable = false; |
| 48 | |
| 49 | /** |
| 50 | * Whether this is a multi-select or not. |
| 51 | * |
| 52 | * *Backwards compatibility note: |
| 53 | * |
| 54 | * Previously (when Kirki used Select2), $multiple is used to: |
| 55 | * - Determine whether the select is multiple or not. |
| 56 | * - Determine the maximum number of selection. |
| 57 | * |
| 58 | * Start from Kirki 4 (when Kirki uses react-select), |
| 59 | * $multiple is used to determine whether the select is multiple or not. |
| 60 | * The maximum selection number is now set in $max_selection. |
| 61 | * |
| 62 | * @access public |
| 63 | * @since 1.0 |
| 64 | * @var bool |
| 65 | */ |
| 66 | public $multiple = false; |
| 67 | |
| 68 | /** |
| 69 | * The maximum selection length for multiple selection. |
| 70 | * |
| 71 | * @access public |
| 72 | * @since 1.1 |
| 73 | * @var bool |
| 74 | */ |
| 75 | public $max_selection_number = 999; |
| 76 | |
| 77 | /** |
| 78 | * The version. Used in scripts & styles for cache-busting. |
| 79 | * |
| 80 | * @static |
| 81 | * @access public |
| 82 | * @since 1.0 |
| 83 | * @var string |
| 84 | */ |
| 85 | public static $control_ver = '1.1.5'; |
| 86 | |
| 87 | /** |
| 88 | * Enqueue control related scripts/styles. |
| 89 | * |
| 90 | * @access public |
| 91 | * @since 1.0 |
| 92 | * @return void |
| 93 | */ |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * Get the URL for the control folder. |
| 98 | * |
| 99 | * This is a static method because there are more controls in the Kirki framework |
| 100 | * that use colorpickers, and they all need to enqueue the same assets. |
| 101 | * |
| 102 | * @static |
| 103 | * @access public |
| 104 | * @since 1.0.6 |
| 105 | * @return string |
| 106 | */ |
| 107 | public static function get_control_path_url() { |
| 108 | |
| 109 | return URL::get_from_path( dirname( __DIR__ ) ); |
| 110 | |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Refresh the parameters passed to the JavaScript via JSON. |
| 115 | * |
| 116 | * @see WP_Customize_Control::to_json() |
| 117 | * |
| 118 | * @access public |
| 119 | * @since 1.0 |
| 120 | * @return void |
| 121 | */ |
| 122 | public function to_json() { |
| 123 | |
| 124 | parent::to_json(); |
| 125 | |
| 126 | if ( isset( $this->json['label'] ) ) { |
| 127 | $this->json['label'] = html_entity_decode( $this->json['label'] ); |
| 128 | } |
| 129 | |
| 130 | if ( isset( $this->json['description'] ) ) { |
| 131 | $this->json['description'] = html_entity_decode( $this->json['description'] ); |
| 132 | } |
| 133 | |
| 134 | // @link https://react-select.com/props |
| 135 | $this->json['isClearable'] = $this->clearable; |
| 136 | $this->json['isMulti'] = $this->multiple; |
| 137 | $this->json['placeholder'] = ( $this->placeholder ) ? $this->placeholder : esc_html__( 'Select...', 'kirki' ); |
| 138 | |
| 139 | // Will be a custom implementation, couldn't find an official prop to set this in react-select. |
| 140 | $this->json['maxSelectionNumber'] = $this->max_selection_number; |
| 141 | |
| 142 | $this->json['messages'] = [ |
| 143 | // translators: %s is the limit of selection number. |
| 144 | 'maxLimitReached' => sprintf( esc_html__( 'You can only select %s items', 'kirki' ), $this->max_selection_number ), |
| 145 | ]; |
| 146 | |
| 147 | } |
| 148 | |
| 149 | } |
| 150 |