|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Handles the dependencies and enqueueing of the CMB2 JS scripts |
|
4
|
|
|
* |
|
5
|
|
|
* @category WordPress_Plugin |
|
6
|
|
|
* @package CMB2 |
|
7
|
|
|
* @author CMB2 team |
|
8
|
|
|
* @license GPL-2.0+ |
|
9
|
|
|
* @link https://cmb2.io |
|
10
|
|
|
*/ |
|
11
|
|
|
class CMB2_JS { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* The CMB2 JS handle |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
* @since 2.0.7 |
|
18
|
|
|
*/ |
|
19
|
|
|
protected static $handle = 'cmb2-scripts'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The CMB2 JS variable name |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
* @since 2.0.7 |
|
26
|
|
|
*/ |
|
27
|
|
|
protected static $js_variable = 'cmb2_l10'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Array of CMB2 JS dependencies |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
* @since 2.0.7 |
|
34
|
|
|
*/ |
|
35
|
|
|
protected static $dependencies = array( |
|
36
|
|
|
'jquery' => 'jquery', |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Array of CMB2 fields model data for JS. |
|
41
|
|
|
* |
|
42
|
|
|
* @var array |
|
43
|
|
|
* @since 2.4.0 |
|
44
|
|
|
*/ |
|
45
|
|
|
protected static $fields = array(); |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Add a dependency to the array of CMB2 JS dependencies |
|
49
|
|
|
* |
|
50
|
|
|
* @since 2.0.7 |
|
51
|
|
|
* @param array|string $dependencies Array (or string) of dependencies to add. |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function add_dependencies( $dependencies ) { |
|
54
|
|
|
foreach ( (array) $dependencies as $dependency ) { |
|
55
|
|
|
self::$dependencies[ $dependency ] = $dependency; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Add field model data to the array for JS. |
|
61
|
|
|
* |
|
62
|
|
|
* @since 2.4.0 |
|
63
|
|
|
* |
|
64
|
|
|
* @param CMB2_Field $field Field object. |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function add_field_data( CMB2_Field $field ) { |
|
67
|
|
|
$hash = $field->hash_id(); |
|
68
|
|
|
if ( ! isset( self::$fields[ $hash ] ) ) { |
|
69
|
|
|
self::$fields[ $hash ] = $field->js_data(); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Enqueue the CMB2 JS |
|
75
|
|
|
* |
|
76
|
|
|
* @since 2.0.7 |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function enqueue() { |
|
79
|
|
|
// Filter required script dependencies. |
|
80
|
|
|
$dependencies = self::$dependencies = apply_filters( 'cmb2_script_dependencies', self::$dependencies ); |
|
81
|
|
|
|
|
82
|
|
|
// Only use minified files if SCRIPT_DEBUG is off. |
|
83
|
|
|
$debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; |
|
84
|
|
|
|
|
85
|
|
|
$min = $debug ? '' : '.min'; |
|
86
|
|
|
|
|
87
|
|
|
// if colorpicker. |
|
88
|
|
|
if ( isset( $dependencies['wp-color-picker'] ) ) { |
|
89
|
|
|
if ( ! is_admin() ) { |
|
90
|
|
|
self::colorpicker_frontend(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if ( isset( $dependencies['wp-color-picker-alpha'] ) ) { |
|
94
|
|
|
self::register_colorpicker_alpha(); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// if file/file_list. |
|
99
|
|
|
if ( isset( $dependencies['media-editor'] ) ) { |
|
100
|
|
|
wp_enqueue_media(); |
|
101
|
|
|
CMB2_Type_File_Base::output_js_underscore_templates(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// if timepicker. |
|
105
|
|
|
if ( isset( $dependencies['jquery-ui-datetimepicker'] ) ) { |
|
106
|
|
|
self::register_datetimepicker(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// if cmb2-wysiwyg. |
|
110
|
|
|
$enqueue_wysiwyg = isset( $dependencies['cmb2-wysiwyg'] ) && $debug; |
|
111
|
|
|
unset( $dependencies['cmb2-wysiwyg'] ); |
|
112
|
|
|
|
|
113
|
|
|
// if cmb2-char-counter. |
|
114
|
|
|
$enqueue_char_counter = isset( $dependencies['cmb2-char-counter'] ) && $debug; |
|
115
|
|
|
unset( $dependencies['cmb2-char-counter'] ); |
|
116
|
|
|
|
|
117
|
|
|
// Enqueue cmb JS. |
|
118
|
|
|
wp_enqueue_script( self::$handle, CMB2_Utils::url( "js/cmb2{$min}.js" ), array_values( $dependencies ), CMB2_VERSION, true ); |
|
119
|
|
|
|
|
120
|
|
|
// if SCRIPT_DEBUG, we need to enqueue separately. |
|
121
|
|
|
if ( $enqueue_wysiwyg ) { |
|
122
|
|
|
wp_enqueue_script( 'cmb2-wysiwyg', CMB2_Utils::url( 'js/cmb2-wysiwyg.js' ), array( 'jquery', 'wp-util' ), CMB2_VERSION ); |
|
123
|
|
|
} |
|
124
|
|
|
if ( $enqueue_char_counter ) { |
|
125
|
|
|
wp_enqueue_script( 'cmb2-char-counter', CMB2_Utils::url( 'js/cmb2-char-counter.js' ), array( 'jquery', 'wp-util' ), CMB2_VERSION ); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
self::localize( $debug ); |
|
129
|
|
|
|
|
130
|
|
|
do_action( 'cmb2_footer_enqueue' ); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Register or enqueue the wp-color-picker-alpha script. |
|
135
|
|
|
* |
|
136
|
|
|
* @since 2.2.7 |
|
137
|
|
|
* |
|
138
|
|
|
* @param boolean $enqueue Whether or not to enqueue. |
|
139
|
|
|
* |
|
140
|
|
|
* @return void |
|
141
|
|
|
*/ |
|
142
|
|
|
public static function register_colorpicker_alpha( $enqueue = false ) { |
|
143
|
|
|
// Only use minified files if SCRIPT_DEBUG is off. |
|
144
|
|
|
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
145
|
|
|
$func = $enqueue ? 'wp_enqueue_script' : 'wp_register_script'; |
|
146
|
|
|
$func( 'wp-color-picker-alpha', CMB2_Utils::url( "js/wp-color-picker-alpha{$min}.js" ), array( 'wp-color-picker' ), '2.1.3' ); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Register or enqueue the jquery-ui-datetimepicker script. |
|
151
|
|
|
* |
|
152
|
|
|
* @since 2.2.7 |
|
153
|
|
|
* |
|
154
|
|
|
* @param boolean $enqueue Whether or not to enqueue. |
|
155
|
|
|
* |
|
156
|
|
|
* @return void |
|
157
|
|
|
*/ |
|
158
|
|
|
public static function register_datetimepicker( $enqueue = false ) { |
|
159
|
|
|
$func = $enqueue ? 'wp_enqueue_script' : 'wp_register_script'; |
|
160
|
|
|
$func( 'jquery-ui-datetimepicker', CMB2_Utils::url( 'js/jquery-ui-timepicker-addon.min.js' ), array( 'jquery-ui-slider' ), '1.5.0' ); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* We need to register colorpicker on the front-end |
|
165
|
|
|
* |
|
166
|
|
|
* @since 2.0.7 |
|
167
|
|
|
*/ |
|
168
|
|
|
protected static function colorpicker_frontend() { |
|
169
|
|
|
wp_register_script( 'iris', admin_url( 'js/iris.min.js' ), array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), CMB2_VERSION ); |
|
170
|
|
|
wp_register_script( 'wp-color-picker', admin_url( 'js/color-picker.min.js' ), array( 'iris' ), CMB2_VERSION ); |
|
171
|
|
|
wp_localize_script( 'wp-color-picker', 'wpColorPickerL10n', array( |
|
172
|
|
|
'clear' => esc_html__( 'Clear', 'cmb2' ), |
|
173
|
|
|
'defaultString' => esc_html__( 'Default', 'cmb2' ), |
|
174
|
|
|
'pick' => esc_html__( 'Select Color', 'cmb2' ), |
|
175
|
|
|
'current' => esc_html__( 'Current Color', 'cmb2' ), |
|
176
|
|
|
) ); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Localize the php variables for CMB2 JS |
|
181
|
|
|
* |
|
182
|
|
|
* @since 2.0.7 |
|
183
|
|
|
* |
|
184
|
|
|
* @param mixed $debug Whether or not we are debugging. |
|
185
|
|
|
*/ |
|
186
|
|
|
protected static function localize( $debug ) { |
|
187
|
|
|
static $localized = false; |
|
188
|
|
|
if ( $localized ) { |
|
189
|
|
|
return; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$localized = true; |
|
193
|
|
|
$l10n = array( |
|
194
|
|
|
'fields' => self::$fields, |
|
195
|
|
|
'ajax_nonce' => wp_create_nonce( 'ajax_nonce' ), |
|
196
|
|
|
'ajaxurl' => admin_url( '/admin-ajax.php' ), |
|
197
|
|
|
'script_debug' => $debug, |
|
198
|
|
|
'up_arrow_class' => 'dashicons dashicons-arrow-up-alt2', |
|
199
|
|
|
'down_arrow_class' => 'dashicons dashicons-arrow-down-alt2', |
|
200
|
|
|
'user_can_richedit' => user_can_richedit(), |
|
201
|
|
|
'defaults' => array( |
|
202
|
|
|
'code_editor' => false, |
|
203
|
|
|
'color_picker' => false, |
|
204
|
|
|
'date_picker' => array( |
|
205
|
|
|
'changeMonth' => true, |
|
206
|
|
|
'changeYear' => true, |
|
207
|
|
|
'dateFormat' => _x( 'mm/dd/yy', 'Valid formatDate string for jquery-ui datepicker', 'cmb2' ), |
|
208
|
|
|
'dayNames' => explode( ',', esc_html__( 'Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday', 'cmb2' ) ), |
|
209
|
|
|
'dayNamesMin' => explode( ',', esc_html__( 'Su, Mo, Tu, We, Th, Fr, Sa', 'cmb2' ) ), |
|
210
|
|
|
'dayNamesShort' => explode( ',', esc_html__( 'Sun, Mon, Tue, Wed, Thu, Fri, Sat', 'cmb2' ) ), |
|
211
|
|
|
'monthNames' => explode( ',', esc_html__( 'January, February, March, April, May, June, July, August, September, October, November, December', 'cmb2' ) ), |
|
212
|
|
|
'monthNamesShort' => explode( ',', esc_html__( 'Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec', 'cmb2' ) ), |
|
213
|
|
|
'nextText' => esc_html__( 'Next', 'cmb2' ), |
|
214
|
|
|
'prevText' => esc_html__( 'Prev', 'cmb2' ), |
|
215
|
|
|
'currentText' => esc_html__( 'Today', 'cmb2' ), |
|
216
|
|
|
'closeText' => esc_html__( 'Done', 'cmb2' ), |
|
217
|
|
|
'clearText' => esc_html__( 'Clear', 'cmb2' ), |
|
218
|
|
|
), |
|
219
|
|
|
'time_picker' => array( |
|
220
|
|
|
'timeOnlyTitle' => esc_html__( 'Choose Time', 'cmb2' ), |
|
221
|
|
|
'timeText' => esc_html__( 'Time', 'cmb2' ), |
|
222
|
|
|
'hourText' => esc_html__( 'Hour', 'cmb2' ), |
|
223
|
|
|
'minuteText' => esc_html__( 'Minute', 'cmb2' ), |
|
224
|
|
|
'secondText' => esc_html__( 'Second', 'cmb2' ), |
|
225
|
|
|
'currentText' => esc_html__( 'Now', 'cmb2' ), |
|
226
|
|
|
'closeText' => esc_html__( 'Done', 'cmb2' ), |
|
227
|
|
|
'timeFormat' => _x( 'hh:mm TT', 'Valid formatting string, as per http://trentrichardson.com/examples/timepicker/', 'cmb2' ), |
|
228
|
|
|
'controlType' => 'select', |
|
229
|
|
|
'stepMinute' => 5, |
|
230
|
|
|
), |
|
231
|
|
|
), |
|
232
|
|
|
'strings' => array( |
|
233
|
|
|
'upload_file' => esc_html__( 'Use this file', 'cmb2' ), |
|
234
|
|
|
'upload_files' => esc_html__( 'Use these files', 'cmb2' ), |
|
235
|
|
|
'remove_image' => esc_html__( 'Remove Image', 'cmb2' ), |
|
236
|
|
|
'remove_file' => esc_html__( 'Remove', 'cmb2' ), |
|
237
|
|
|
'file' => esc_html__( 'File:', 'cmb2' ), |
|
238
|
|
|
'download' => esc_html__( 'Download', 'cmb2' ), |
|
239
|
|
|
'check_toggle' => esc_html__( 'Select / Deselect All', 'cmb2' ), |
|
240
|
|
|
), |
|
241
|
|
|
); |
|
242
|
|
|
|
|
243
|
|
|
if ( isset( self::$dependencies['code-editor'] ) && function_exists( 'wp_enqueue_code_editor' ) ) { |
|
244
|
|
|
$l10n['defaults']['code_editor'] = wp_enqueue_code_editor( array( |
|
245
|
|
|
'type' => 'text/html', |
|
246
|
|
|
) ); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
wp_localize_script( self::$handle, self::$js_variable, apply_filters( 'cmb2_localized_data', $l10n ) ); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
} |
|
253
|
|
|
|