| 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 |
protected static $fields = array(); |
| 46 |
|
| 47 |
/** |
| 48 |
* Add a dependency to the array of CMB2 JS dependencies |
| 49 |
* |
| 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 = self::$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; |
| 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(); |
| 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 ); |
| 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 Whether or not to 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'; |
| 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 Whether or not to 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 ); |
| 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( |
| 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 |
* @param mixed $debug Whether or not we are debugging. |
| 178 |
*/ |
| 179 |
protected static function localize( $debug ) { |
| 180 |
static $localized = false; |
| 181 |
if ( $localized ) { |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
$localized = true; |
| 186 |
$l10n = array( |
| 187 |
'fields' => self::$fields, |
| 188 |
'ajax_nonce' => wp_create_nonce( 'ajax_nonce' ), |
| 189 |
'ajaxurl' => admin_url( '/admin-ajax.php' ), |
| 190 |
'script_debug' => $debug, |
| 191 |
'up_arrow_class' => 'dashicons dashicons-arrow-up-alt2', |
| 192 |
'down_arrow_class' => 'dashicons dashicons-arrow-down-alt2', |
| 193 |
'user_can_richedit' => user_can_richedit(), |
| 194 |
'defaults' => array( |
| 195 |
'code_editor' => false, |
| 196 |
'color_picker' => false, |
| 197 |
'date_picker' => array( |
| 198 |
'changeMonth' => true, |
| 199 |
'changeYear' => true, |
| 200 |
'dateFormat' => _x( 'mm/dd/yy', 'Valid formatDate string for jquery-ui datepicker', 'cmb2' ), |
| 201 |
'dayNames' => explode( ',', esc_html__( 'Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday', 'cmb2' ) ), |
| 202 |
'dayNamesMin' => explode( ',', esc_html__( 'Su, Mo, Tu, We, Th, Fr, Sa', 'cmb2' ) ), |
| 203 |
'dayNamesShort' => explode( ',', esc_html__( 'Sun, Mon, Tue, Wed, Thu, Fri, Sat', 'cmb2' ) ), |
| 204 |
'monthNames' => explode( ',', esc_html__( 'January, February, March, April, May, June, July, August, September, October, November, December', 'cmb2' ) ), |
| 205 |
'monthNamesShort' => explode( ',', esc_html__( 'Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec', 'cmb2' ) ), |
| 206 |
'nextText' => esc_html__( 'Next', 'cmb2' ), |
| 207 |
'prevText' => esc_html__( 'Prev', 'cmb2' ), |
| 208 |
'currentText' => esc_html__( 'Today', 'cmb2' ), |
| 209 |
'closeText' => esc_html__( 'Done', 'cmb2' ), |
| 210 |
'clearText' => esc_html__( 'Clear', 'cmb2' ), |
| 211 |
), |
| 212 |
'time_picker' => array( |
| 213 |
'timeOnlyTitle' => esc_html__( 'Choose Time', 'cmb2' ), |
| 214 |
'timeText' => esc_html__( 'Time', 'cmb2' ), |
| 215 |
'hourText' => esc_html__( 'Hour', 'cmb2' ), |
| 216 |
'minuteText' => esc_html__( 'Minute', 'cmb2' ), |
| 217 |
'secondText' => esc_html__( 'Second', 'cmb2' ), |
| 218 |
'currentText' => esc_html__( 'Now', 'cmb2' ), |
| 219 |
'closeText' => esc_html__( 'Done', 'cmb2' ), |
| 220 |
'timeFormat' => _x( 'hh:mm TT', 'Valid formatting string, as per http://trentrichardson.com/examples/timepicker/', 'cmb2' ), |
| 221 |
'controlType' => 'select', |
| 222 |
'stepMinute' => 5, |
| 223 |
), |
| 224 |
), |
| 225 |
'strings' => array( |
| 226 |
'upload_file' => esc_html__( 'Use this file', 'cmb2' ), |
| 227 |
'upload_files' => esc_html__( 'Use these files', 'cmb2' ), |
| 228 |
'remove_image' => esc_html__( 'Remove Image', 'cmb2' ), |
| 229 |
'remove_file' => esc_html__( 'Remove', 'cmb2' ), |
| 230 |
'file' => esc_html__( 'File:', 'cmb2' ), |
| 231 |
'download' => esc_html__( 'Download', 'cmb2' ), |
| 232 |
'check_toggle' => esc_html__( 'Select / Deselect All', 'cmb2' ), |
| 233 |
), |
| 234 |
); |
| 235 |
|
| 236 |
if ( isset( self::$dependencies['code-editor'] ) && function_exists( 'wp_enqueue_code_editor' ) ) { |
| 237 |
$l10n['defaults']['code_editor'] = wp_enqueue_code_editor( array( |
| 238 |
'type' => 'text/html', |
| 239 |
) ); |
| 240 |
} |
| 241 |
|
| 242 |
wp_localize_script( self::$handle, self::$js_variable, apply_filters( 'cmb2_localized_data', $l10n ) ); |
| 243 |
} |
| 244 |
|
| 245 |
} |
| 246 |
|