Completed
Push — trunk ( f02feb...6ee722 )
by Justin
07:11
created

CMB2_JS::enqueue()   C

Complexity

Conditions 7
Paths 32

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 32
nop 0
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
ccs 0
cts 16
cp 0
crap 56
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 {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
		// if colorpicker
54
		if ( ! is_admin() && isset( $dependencies['wp-color-picker'] ) ) {
55
			self::colorpicker_frontend();
56
		}
57
58
		// if file/file_list
59
		if ( isset( $dependencies['media-editor'] ) ) {
60
			wp_enqueue_media();
61
		}
62
63
		// if timepicker
64
		if ( isset( $dependencies['jquery-ui-datetimepicker'] ) ) {
65
			wp_register_script( 'jquery-ui-datetimepicker', cmb2_utils()->url( 'js/jquery-ui-timepicker-addon.min.js' ), array( 'jquery-ui-slider' ), CMB2_VERSION );
66
		}
67
68
		// Only use minified files if SCRIPT_DEBUG is off
69
		$debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
70
		$min   = $debug ? '' : '.min';
71
72
		// Register cmb JS
73
		wp_enqueue_script( self::$handle, cmb2_utils()->url( "js/cmb2{$min}.js" ), $dependencies, CMB2_VERSION, true );
74
75
		self::localize( $debug );
76
	}
77
78
	/**
79
	 * We need to register colorpicker on the front-end
80
	 * @since  2.0.7
81
	 */
82
	protected static function colorpicker_frontend() {
83
		wp_register_script( 'iris', admin_url( 'js/iris.min.js' ), array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), CMB2_VERSION );
84
		wp_register_script( 'wp-color-picker', admin_url( 'js/color-picker.min.js' ), array( 'iris' ), CMB2_VERSION );
85
		wp_localize_script( 'wp-color-picker', 'wpColorPickerL10n', array(
86
			'clear'         => __( 'Clear', 'cmb2' ),
87
			'defaultString' => __( 'Default', 'cmb2' ),
88
			'pick'          => __( 'Select Color', 'cmb2' ),
89
			'current'       => __( 'Current Color', 'cmb2' ),
90
		) );
91
	}
92
93
	/**
94
	 * Localize the php variables for CMB2 JS
95
	 * @since  2.0.7
96
	 */
97
	protected static function localize( $debug ) {
98
		static $localized = false;
99
		if ( $localized ) {
100
			return;
101
		}
102
103
		$localized = true;
104
		$l10n = array(
105
			'ajax_nonce'       => wp_create_nonce( 'ajax_nonce' ),
106
			'ajaxurl'          => admin_url( '/admin-ajax.php' ),
107
			'script_debug'     => $debug,
108
			'up_arrow_class'   => 'dashicons dashicons-arrow-up-alt2',
109
			'down_arrow_class' => 'dashicons dashicons-arrow-down-alt2',
110
			'defaults'         => array(
111
				'color_picker' => false,
112
				'date_picker'  => array(
113
					'changeMonth'     => true,
114
					'changeYear'      => true,
115
					'dateFormat'      => _x( 'mm/dd/yy', 'Valid formatDate string for jquery-ui datepicker', 'cmb2' ),
116
					'dayNames'        => explode( ',', __( 'Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday', 'cmb2' ) ),
117
					'dayNamesMin'     => explode( ',', __( 'Su, Mo, Tu, We, Th, Fr, Sa', 'cmb2' ) ),
118
					'dayNamesShort'   => explode( ',', __( 'Sun, Mon, Tue, Wed, Thu, Fri, Sat', 'cmb2' ) ),
119
					'monthNames'      => explode( ',', __( 'January, February, March, April, May, June, July, August, September, October, November, December', 'cmb2' ) ),
120
					'monthNamesShort' => explode( ',', __( 'Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec', 'cmb2' ) ),
121
					'nextText'        => __( 'Next', 'cmb2' ),
122
					'prevText'        => __( 'Prev', 'cmb2' ),
123
					'currentText'     => __( 'Today', 'cmb2' ),
124
					'closeText'       => __( 'Done', 'cmb2' ),
125
					'clearText'       => __( 'Clear', 'cmb2' ),
126
				),
127
				'time_picker'  => array(
128
					'timeOnlyTitle' => __( 'Choose Time', 'cmb2' ),
129
					'timeText'      => __( 'Time', 'cmb2' ),
130
					'hourText'      => __( 'Hour', 'cmb2' ),
131
					'minuteText'    => __( 'Minute', 'cmb2' ),
132
					'secondText'    => __( 'Second', 'cmb2' ),
133
					'currentText'   => __( 'Now', 'cmb2' ),
134
					'closeText'     => __( 'Done', 'cmb2' ),
135
					'timeFormat'    => _x( 'hh:mm TT', 'Valid formatting string, as per http://trentrichardson.com/examples/timepicker/', 'cmb2' ),
136
					'controlType'   => 'select',
137
					'stepMinute'    => 5,
138
				),
139
			),
140
			'strings' => array(
141
				'upload_file'  => __( 'Use this file', 'cmb2' ),
142
				'upload_files' => __( 'Use these files', 'cmb2' ),
143
				'remove_image' => __( 'Remove Image', 'cmb2' ),
144
				'remove_file'  => __( 'Remove', 'cmb2' ),
145
				'file'         => __( 'File:', 'cmb2' ),
146
				'download'     => __( 'Download', 'cmb2' ),
147
				'check_toggle' => __( 'Select / Deselect All', 'cmb2' ),
148
			),
149
		);
150
151
		wp_localize_script( self::$handle, self::$js_variable, apply_filters( 'cmb2_localized_data', $l10n ) );
152
	}
153
154
}
155