Completed
Push — trunk ( 3edf8a...58eb32 )
by Justin
10s
created

CMB2_JS   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 5.81%

Importance

Changes 0
Metric Value
dl 0
loc 157
ccs 5
cts 86
cp 0.0581
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 2

4 Methods

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