1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Handles the dependencies and enqueueing of the CMB2 JS scripts |
4
|
|
|
* |
5
|
|
|
* @category WordPress_Plugin |
6
|
|
|
* @package CMB2 |
7
|
|
|
* @author WebDevStudios |
8
|
|
|
* @license GPL-2.0+ |
9
|
|
|
* @link http://webdevstudios.com |
10
|
|
|
*/ |
11
|
|
|
class CMB2_JS { |
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The CMB2 JS handle |
15
|
|
|
* @var string |
16
|
|
|
* @since 2.0.7 |
17
|
|
|
*/ |
18
|
|
|
protected static $handle = 'cmb2-scripts'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The CMB2 JS variable name |
22
|
|
|
* @var string |
23
|
|
|
* @since 2.0.7 |
24
|
|
|
*/ |
25
|
|
|
protected static $js_variable = 'cmb2_l10'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Array of CMB2 JS dependencies |
29
|
|
|
* @var array |
30
|
|
|
* @since 2.0.7 |
31
|
|
|
*/ |
32
|
|
|
protected static $dependencies = array( 'jquery' => 'jquery' ); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Add a dependency to the array of CMB2 JS dependencies |
36
|
|
|
* @since 2.0.7 |
37
|
|
|
* @param array|string $dependencies Array (or string) of dependencies to add |
38
|
|
|
*/ |
39
|
12 |
|
public static function add_dependencies( $dependencies ) { |
40
|
12 |
|
foreach ( (array) $dependencies as $dependency ) { |
41
|
12 |
|
self::$dependencies[ $dependency ] = $dependency; |
42
|
12 |
|
} |
43
|
12 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Enqueue the CMB2 JS |
47
|
|
|
* @since 2.0.7 |
48
|
|
|
*/ |
49
|
|
|
public static function enqueue() { |
50
|
|
|
// Filter required script dependencies |
51
|
|
|
$dependencies = apply_filters( 'cmb2_script_dependencies', self::$dependencies ); |
52
|
|
|
|
53
|
|
|
// Only use minified files if SCRIPT_DEBUG is off |
54
|
|
|
$debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; |
55
|
|
|
|
56
|
|
|
$min = $debug ? '' : '.min'; |
57
|
|
|
|
58
|
|
|
// if colorpicker |
59
|
|
|
if ( ! is_admin() && isset( $dependencies['wp-color-picker'] ) ) { |
60
|
|
|
self::colorpicker_frontend(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// if file/file_list |
64
|
|
|
if ( isset( $dependencies['media-editor'] ) ) { |
65
|
|
|
wp_enqueue_media(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// if timepicker |
69
|
|
|
if ( isset( $dependencies['jquery-ui-datetimepicker'] ) ) { |
70
|
|
|
wp_register_script( 'jquery-ui-datetimepicker', cmb2_utils()->url( 'js/jquery-ui-timepicker-addon.min.js' ), array( 'jquery-ui-slider' ), CMB2_VERSION ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// if cmb2-wysiwyg |
74
|
|
|
$enqueue_wysiwyg = isset( $dependencies['cmb2-wysiwyg'] ) && $debug; |
75
|
|
|
unset( $dependencies['cmb2-wysiwyg'] ); |
76
|
|
|
|
77
|
|
|
// Enqueue cmb JS |
78
|
|
|
wp_enqueue_script( self::$handle, cmb2_utils()->url( "js/cmb2{$min}.js" ), $dependencies, CMB2_VERSION, true ); |
79
|
|
|
|
80
|
|
|
// if SCRIPT_DEBUG, we need to enqueue separately. |
81
|
|
|
if ( $enqueue_wysiwyg ) { |
82
|
|
|
wp_enqueue_script( 'cmb2-wysiwyg', cmb2_utils()->url( 'js/cmb2-wysiwyg.js' ), array( 'jquery', 'wp-util' ), CMB2_VERSION ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
self::localize( $debug ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* We need to register colorpicker on the front-end |
90
|
|
|
* @since 2.0.7 |
91
|
|
|
*/ |
92
|
|
|
protected static function colorpicker_frontend() { |
93
|
|
|
wp_register_script( 'iris', admin_url( 'js/iris.min.js' ), array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), CMB2_VERSION ); |
94
|
|
|
wp_register_script( 'wp-color-picker', admin_url( 'js/color-picker.min.js' ), array( 'iris' ), CMB2_VERSION ); |
95
|
|
|
wp_localize_script( 'wp-color-picker', 'wpColorPickerL10n', array( |
96
|
|
|
'clear' => __( 'Clear', 'cmb2' ), |
97
|
|
|
'defaultString' => __( 'Default', 'cmb2' ), |
98
|
|
|
'pick' => __( 'Select Color', 'cmb2' ), |
99
|
|
|
'current' => __( 'Current Color', 'cmb2' ), |
100
|
|
|
) ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Localize the php variables for CMB2 JS |
105
|
|
|
* @since 2.0.7 |
106
|
|
|
*/ |
107
|
|
|
protected static function localize( $debug ) { |
108
|
|
|
static $localized = false; |
109
|
|
|
if ( $localized ) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$localized = true; |
114
|
|
|
$l10n = array( |
115
|
|
|
'ajax_nonce' => wp_create_nonce( 'ajax_nonce' ), |
116
|
|
|
'ajaxurl' => admin_url( '/admin-ajax.php' ), |
117
|
|
|
'script_debug' => $debug, |
118
|
|
|
'up_arrow_class' => 'dashicons dashicons-arrow-up-alt2', |
119
|
|
|
'down_arrow_class' => 'dashicons dashicons-arrow-down-alt2', |
120
|
|
|
'defaults' => array( |
121
|
|
|
'color_picker' => false, |
122
|
|
|
'date_picker' => array( |
123
|
|
|
'changeMonth' => true, |
124
|
|
|
'changeYear' => true, |
125
|
|
|
'dateFormat' => _x( 'mm/dd/yy', 'Valid formatDate string for jquery-ui datepicker', 'cmb2' ), |
126
|
|
|
'dayNames' => explode( ',', __( 'Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday', 'cmb2' ) ), |
127
|
|
|
'dayNamesMin' => explode( ',', __( 'Su, Mo, Tu, We, Th, Fr, Sa', 'cmb2' ) ), |
128
|
|
|
'dayNamesShort' => explode( ',', __( 'Sun, Mon, Tue, Wed, Thu, Fri, Sat', 'cmb2' ) ), |
129
|
|
|
'monthNames' => explode( ',', __( 'January, February, March, April, May, June, July, August, September, October, November, December', 'cmb2' ) ), |
130
|
|
|
'monthNamesShort' => explode( ',', __( 'Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec', 'cmb2' ) ), |
131
|
|
|
'nextText' => __( 'Next', 'cmb2' ), |
132
|
|
|
'prevText' => __( 'Prev', 'cmb2' ), |
133
|
|
|
'currentText' => __( 'Today', 'cmb2' ), |
134
|
|
|
'closeText' => __( 'Done', 'cmb2' ), |
135
|
|
|
'clearText' => __( 'Clear', 'cmb2' ), |
136
|
|
|
), |
137
|
|
|
'time_picker' => array( |
138
|
|
|
'timeOnlyTitle' => __( 'Choose Time', 'cmb2' ), |
139
|
|
|
'timeText' => __( 'Time', 'cmb2' ), |
140
|
|
|
'hourText' => __( 'Hour', 'cmb2' ), |
141
|
|
|
'minuteText' => __( 'Minute', 'cmb2' ), |
142
|
|
|
'secondText' => __( 'Second', 'cmb2' ), |
143
|
|
|
'currentText' => __( 'Now', 'cmb2' ), |
144
|
|
|
'closeText' => __( 'Done', 'cmb2' ), |
145
|
|
|
'timeFormat' => _x( 'hh:mm TT', 'Valid formatting string, as per http://trentrichardson.com/examples/timepicker/', 'cmb2' ), |
146
|
|
|
'controlType' => 'select', |
147
|
|
|
'stepMinute' => 5, |
148
|
|
|
), |
149
|
|
|
), |
150
|
|
|
'strings' => array( |
151
|
|
|
'upload_file' => __( 'Use this file', 'cmb2' ), |
152
|
|
|
'upload_files' => __( 'Use these files', 'cmb2' ), |
153
|
|
|
'remove_image' => __( 'Remove Image', 'cmb2' ), |
154
|
|
|
'remove_file' => __( 'Remove', 'cmb2' ), |
155
|
|
|
'file' => __( 'File:', 'cmb2' ), |
156
|
|
|
'download' => __( 'Download', 'cmb2' ), |
157
|
|
|
'check_toggle' => __( 'Select / Deselect All', 'cmb2' ), |
158
|
|
|
), |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
wp_localize_script( self::$handle, self::$js_variable, apply_filters( 'cmb2_localized_data', $l10n ) ); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.