Passed
Push — trunk ( 467725...420cca )
by Justin
02:31
created

CMB2_JS::add_field_data()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 2
eloc 3
nc 2
nop 1
crap 6
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 14
	protected static $fields = array();
46 14
47 14
	/**
48 14
	 * Add a dependency to the array of CMB2 JS dependencies
49 14
	 *
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 = 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;
0 ignored issues
show
Bug introduced by
The constant SCRIPT_DEBUG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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();
0 ignored issues
show
Bug introduced by
The function wp_enqueue_media was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
			/** @scrutinizer ignore-call */ 
101
   wp_enqueue_media();
Loading history...
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
		// Enqueue cmb JS
114
		wp_enqueue_script( self::$handle, CMB2_Utils::url( "js/cmb2{$min}.js" ), $dependencies, CMB2_VERSION, true );
0 ignored issues
show
Bug introduced by
The function wp_enqueue_script was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
		/** @scrutinizer ignore-call */ 
115
  wp_enqueue_script( self::$handle, CMB2_Utils::url( "js/cmb2{$min}.js" ), $dependencies, CMB2_VERSION, true );
Loading history...
115
116
		// if SCRIPT_DEBUG, we need to enqueue separately.
117
		if ( $enqueue_wysiwyg ) {
118
			wp_enqueue_script( 'cmb2-wysiwyg', CMB2_Utils::url( 'js/cmb2-wysiwyg.js' ), array( 'jquery', 'wp-util' ), CMB2_VERSION );
119
		}
120
121
		self::localize( $debug );
122
123
		do_action( 'cmb2_footer_enqueue' );
124
	}
125
126
	/**
127
	 * Register or enqueue the wp-color-picker-alpha script.
128
	 *
129
	 * @since  2.2.7
130
	 *
131
	 * @param  boolean $enqueue
132
	 *
133
	 * @return void
134
	 */
135
	public static function register_colorpicker_alpha( $enqueue = false ) {
136
		// Only use minified files if SCRIPT_DEBUG is off
137
		$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
0 ignored issues
show
Bug introduced by
The constant SCRIPT_DEBUG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
138
		$func = $enqueue ? 'wp_enqueue_script' : 'wp_register_script';
139
		$func( 'wp-color-picker-alpha', CMB2_Utils::url( "js/wp-color-picker-alpha{$min}.js" ), array( 'wp-color-picker' ), '2.1.3' );
140
	}
141
142
	/**
143
	 * Register or enqueue the jquery-ui-datetimepicker script.
144
	 *
145
	 * @since  2.2.7
146
	 *
147
	 * @param  boolean $enqueue
148
	 *
149
	 * @return void
150
	 */
151
	public static function register_datetimepicker( $enqueue = false ) {
152
		$func = $enqueue ? 'wp_enqueue_script' : 'wp_register_script';
153
		$func( 'jquery-ui-datetimepicker', CMB2_Utils::url( 'js/jquery-ui-timepicker-addon.min.js' ), array( 'jquery-ui-slider' ), '1.5.0' );
154
	}
155
156
	/**
157
	 * We need to register colorpicker on the front-end
158
	 *
159
	 * @since  2.0.7
160
	 */
161
	protected static function colorpicker_frontend() {
162
		wp_register_script( 'iris', admin_url( 'js/iris.min.js' ), array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), CMB2_VERSION );
0 ignored issues
show
Bug introduced by
The function wp_register_script was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

162
		/** @scrutinizer ignore-call */ 
163
  wp_register_script( 'iris', admin_url( 'js/iris.min.js' ), array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), CMB2_VERSION );
Loading history...
Bug introduced by
The function admin_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

162
		wp_register_script( 'iris', /** @scrutinizer ignore-call */ admin_url( 'js/iris.min.js' ), array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), CMB2_VERSION );
Loading history...
163
		wp_register_script( 'wp-color-picker', admin_url( 'js/color-picker.min.js' ), array( 'iris' ), CMB2_VERSION );
164
		wp_localize_script( 'wp-color-picker', 'wpColorPickerL10n', array(
0 ignored issues
show
Bug introduced by
The function wp_localize_script was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

164
		/** @scrutinizer ignore-call */ 
165
  wp_localize_script( 'wp-color-picker', 'wpColorPickerL10n', array(
Loading history...
165
			'clear'         => esc_html__( 'Clear', 'cmb2' ),
166
			'defaultString' => esc_html__( 'Default', 'cmb2' ),
167
			'pick'          => esc_html__( 'Select Color', 'cmb2' ),
168
			'current'       => esc_html__( 'Current Color', 'cmb2' ),
169
		) );
170
	}
171
172
	/**
173
	 * Localize the php variables for CMB2 JS
174
	 *
175
	 * @since  2.0.7
176
	 */
177
	protected static function localize( $debug ) {
178
		static $localized = false;
179
		if ( $localized ) {
180
			return;
181
		}
182
183
		$localized = true;
184
		$l10n = array(
185
			'fields'            => self::$fields,
186
			'ajax_nonce'        => wp_create_nonce( 'ajax_nonce' ),
0 ignored issues
show
Bug introduced by
The function wp_create_nonce was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

186
			'ajax_nonce'        => /** @scrutinizer ignore-call */ wp_create_nonce( 'ajax_nonce' ),
Loading history...
187
			'ajaxurl'           => admin_url( '/admin-ajax.php' ),
0 ignored issues
show
Bug introduced by
The function admin_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

187
			'ajaxurl'           => /** @scrutinizer ignore-call */ admin_url( '/admin-ajax.php' ),
Loading history...
188
			'script_debug'      => $debug,
189
			'up_arrow_class'    => 'dashicons dashicons-arrow-up-alt2',
190
			'down_arrow_class'  => 'dashicons dashicons-arrow-down-alt2',
191
			'user_can_richedit' => user_can_richedit(),
0 ignored issues
show
Bug introduced by
The function user_can_richedit was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

191
			'user_can_richedit' => /** @scrutinizer ignore-call */ user_can_richedit(),
Loading history...
192
			'defaults'          => array(
193
				'code_editor'  => false,
194
				'color_picker' => false,
195
				'date_picker'  => array(
196
					'changeMonth'     => true,
197
					'changeYear'      => true,
198
					'dateFormat'      => _x( 'mm/dd/yy', 'Valid formatDate string for jquery-ui datepicker', 'cmb2' ),
0 ignored issues
show
Bug introduced by
The function _x was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

198
					'dateFormat'      => /** @scrutinizer ignore-call */ _x( 'mm/dd/yy', 'Valid formatDate string for jquery-ui datepicker', 'cmb2' ),
Loading history...
199
					'dayNames'        => explode( ',', esc_html__( 'Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday', 'cmb2' ) ),
200
					'dayNamesMin'     => explode( ',', esc_html__( 'Su, Mo, Tu, We, Th, Fr, Sa', 'cmb2' ) ),
201
					'dayNamesShort'   => explode( ',', esc_html__( 'Sun, Mon, Tue, Wed, Thu, Fri, Sat', 'cmb2' ) ),
202
					'monthNames'      => explode( ',', esc_html__( 'January, February, March, April, May, June, July, August, September, October, November, December', 'cmb2' ) ),
203
					'monthNamesShort' => explode( ',', esc_html__( 'Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec', 'cmb2' ) ),
204
					'nextText'        => esc_html__( 'Next', 'cmb2' ),
205
					'prevText'        => esc_html__( 'Prev', 'cmb2' ),
206
					'currentText'     => esc_html__( 'Today', 'cmb2' ),
207
					'closeText'       => esc_html__( 'Done', 'cmb2' ),
208
					'clearText'       => esc_html__( 'Clear', 'cmb2' ),
209
				),
210
				'time_picker'  => array(
211
					'timeOnlyTitle' => esc_html__( 'Choose Time', 'cmb2' ),
212
					'timeText'      => esc_html__( 'Time', 'cmb2' ),
213
					'hourText'      => esc_html__( 'Hour', 'cmb2' ),
214
					'minuteText'    => esc_html__( 'Minute', 'cmb2' ),
215
					'secondText'    => esc_html__( 'Second', 'cmb2' ),
216
					'currentText'   => esc_html__( 'Now', 'cmb2' ),
217
					'closeText'     => esc_html__( 'Done', 'cmb2' ),
218
					'timeFormat'    => _x( 'hh:mm TT', 'Valid formatting string, as per http://trentrichardson.com/examples/timepicker/', 'cmb2' ),
219
					'controlType'   => 'select',
220
					'stepMinute'    => 5,
221
				),
222
			),
223
			'strings' => array(
224
				'upload_file'  => esc_html__( 'Use this file', 'cmb2' ),
225
				'upload_files' => esc_html__( 'Use these files', 'cmb2' ),
226
				'remove_image' => esc_html__( 'Remove Image', 'cmb2' ),
227
				'remove_file'  => esc_html__( 'Remove', 'cmb2' ),
228
				'file'         => esc_html__( 'File:', 'cmb2' ),
229
				'download'     => esc_html__( 'Download', 'cmb2' ),
230
				'check_toggle' => esc_html__( 'Select / Deselect All', 'cmb2' ),
231
			),
232
		);
233
234
		if ( function_exists( 'wp_enqueue_code_editor' ) ) {
235
			$l10n['defaults']['code_editor'] = wp_enqueue_code_editor( array(
236
				'type' => 'text/html',
237
			) );
238
		}
239
240
		wp_localize_script( self::$handle, self::$js_variable, apply_filters( 'cmb2_localized_data', $l10n ) );
0 ignored issues
show
Bug introduced by
The function wp_localize_script was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

240
		/** @scrutinizer ignore-call */ 
241
  wp_localize_script( self::$handle, self::$js_variable, apply_filters( 'cmb2_localized_data', $l10n ) );
Loading history...
241
	}
242
243
}
244