CMB2_JS::add_dependencies()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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
	 *
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
	 * Add a dependency to the array of CMB2 JS dependencies
41
	 *
42
	 * @since 2.0.7
43 13
	 * @param array|string $dependencies Array (or string) of dependencies to add
44 13
	 */
45 13
	public static function add_dependencies( $dependencies ) {
46 13
		foreach ( (array) $dependencies as $dependency ) {
47 13
			self::$dependencies[ $dependency ] = $dependency;
48
		}
49
	}
50
51
	/**
52
	 * Enqueue the CMB2 JS
53
	 *
54
	 * @since  2.0.7
55
	 */
56
	public static function enqueue() {
57
		// Filter required script dependencies
58
		$dependencies = apply_filters( 'cmb2_script_dependencies', self::$dependencies );
59
60
		// Only use minified files if SCRIPT_DEBUG is off
61
		$debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
62
63
		$min = $debug ? '' : '.min';
64
65
		// if colorpicker
66
		if ( ! is_admin() && isset( $dependencies['wp-color-picker'] ) ) {
67
			self::colorpicker_frontend();
68
		}
69
70
		// if file/file_list
71
		if ( isset( $dependencies['media-editor'] ) ) {
72
			wp_enqueue_media();
73
			CMB2_Type_File_Base::output_js_underscore_templates();
0 ignored issues
show
Unused Code introduced by
The call to the method CMB2_Type_File_Base::out..._underscore_templates() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
74
		}
75
76
		// if timepicker
77
		if ( isset( $dependencies['jquery-ui-datetimepicker'] ) ) {
78
			wp_register_script( 'jquery-ui-datetimepicker', CMB2_Utils::url( 'js/jquery-ui-timepicker-addon.min.js' ), array( 'jquery-ui-slider' ), CMB2_VERSION );
79
		}
80
81
		// if cmb2-wysiwyg
82
		$enqueue_wysiwyg = isset( $dependencies['cmb2-wysiwyg'] ) && $debug;
83
		unset( $dependencies['cmb2-wysiwyg'] );
84
85
		// Enqueue cmb JS
86
		wp_enqueue_script( self::$handle, CMB2_Utils::url( "js/cmb2{$min}.js" ), $dependencies, CMB2_VERSION, true );
87
88
		// if SCRIPT_DEBUG, we need to enqueue separately.
89
		if ( $enqueue_wysiwyg ) {
90
			wp_enqueue_script( 'cmb2-wysiwyg', CMB2_Utils::url( 'js/cmb2-wysiwyg.js' ), array( 'jquery', 'wp-util' ), CMB2_VERSION );
91
		}
92
93
		self::localize( $debug );
94
95
		do_action( 'cmb2_footer_enqueue' );
96
	}
97
98
	/**
99
	 * We need to register colorpicker on the front-end
100
	 *
101
	 * @since  2.0.7
102
	 */
103
	protected static function colorpicker_frontend() {
104
		wp_register_script( 'iris', admin_url( 'js/iris.min.js' ), array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), CMB2_VERSION );
105
		wp_register_script( 'wp-color-picker', admin_url( 'js/color-picker.min.js' ), array( 'iris' ), CMB2_VERSION );
106
		wp_localize_script( 'wp-color-picker', 'wpColorPickerL10n', array(
107
			'clear'         => esc_html__( 'Clear', 'cmb2' ),
108
			'defaultString' => esc_html__( 'Default', 'cmb2' ),
109
			'pick'          => esc_html__( 'Select Color', 'cmb2' ),
110
			'current'       => esc_html__( 'Current Color', 'cmb2' ),
111
		) );
112
	}
113
114
	/**
115
	 * Localize the php variables for CMB2 JS
116
	 *
117
	 * @since  2.0.7
118
	 */
119
	protected static function localize( $debug ) {
120
		static $localized = false;
121
		if ( $localized ) {
122
			return;
123
		}
124
125
		$localized = true;
126
		$l10n = array(
127
			'ajax_nonce'       => wp_create_nonce( 'ajax_nonce' ),
128
			'ajaxurl'          => admin_url( '/admin-ajax.php' ),
129
			'script_debug'     => $debug,
130
			'up_arrow_class'   => 'dashicons dashicons-arrow-up-alt2',
131
			'down_arrow_class' => 'dashicons dashicons-arrow-down-alt2',
132
			'defaults'         => array(
133
				'color_picker' => false,
134
				'date_picker'  => array(
135
					'changeMonth'     => true,
136
					'changeYear'      => true,
137
					'dateFormat'      => _x( 'mm/dd/yy', 'Valid formatDate string for jquery-ui datepicker', 'cmb2' ),
138
					'dayNames'        => explode( ',', esc_html__( 'Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday', 'cmb2' ) ),
139
					'dayNamesMin'     => explode( ',', esc_html__( 'Su, Mo, Tu, We, Th, Fr, Sa', 'cmb2' ) ),
140
					'dayNamesShort'   => explode( ',', esc_html__( 'Sun, Mon, Tue, Wed, Thu, Fri, Sat', 'cmb2' ) ),
141
					'monthNames'      => explode( ',', esc_html__( 'January, February, March, April, May, June, July, August, September, October, November, December', 'cmb2' ) ),
142
					'monthNamesShort' => explode( ',', esc_html__( 'Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec', 'cmb2' ) ),
143
					'nextText'        => esc_html__( 'Next', 'cmb2' ),
144
					'prevText'        => esc_html__( 'Prev', 'cmb2' ),
145
					'currentText'     => esc_html__( 'Today', 'cmb2' ),
146
					'closeText'       => esc_html__( 'Done', 'cmb2' ),
147
					'clearText'       => esc_html__( 'Clear', 'cmb2' ),
148
				),
149
				'time_picker'  => array(
150
					'timeOnlyTitle' => esc_html__( 'Choose Time', 'cmb2' ),
151
					'timeText'      => esc_html__( 'Time', 'cmb2' ),
152
					'hourText'      => esc_html__( 'Hour', 'cmb2' ),
153
					'minuteText'    => esc_html__( 'Minute', 'cmb2' ),
154
					'secondText'    => esc_html__( 'Second', 'cmb2' ),
155
					'currentText'   => esc_html__( 'Now', 'cmb2' ),
156
					'closeText'     => esc_html__( 'Done', 'cmb2' ),
157
					'timeFormat'    => _x( 'hh:mm TT', 'Valid formatting string, as per http://trentrichardson.com/examples/timepicker/', 'cmb2' ),
158
					'controlType'   => 'select',
159
					'stepMinute'    => 5,
160
				),
161
			),
162
			'strings' => array(
163
				'upload_file'  => esc_html__( 'Use this file', 'cmb2' ),
164
				'upload_files' => esc_html__( 'Use these files', 'cmb2' ),
165
				'remove_image' => esc_html__( 'Remove Image', 'cmb2' ),
166
				'remove_file'  => esc_html__( 'Remove', 'cmb2' ),
167
				'file'         => esc_html__( 'File:', 'cmb2' ),
168
				'download'     => esc_html__( 'Download', 'cmb2' ),
169
				'check_toggle' => esc_html__( 'Select / Deselect All', 'cmb2' ),
170
			),
171
		);
172
173
		wp_localize_script( self::$handle, self::$js_variable, apply_filters( 'cmb2_localized_data', $l10n ) );
174
	}
175
176
}
177