| @@ -1,0 +1,257 @@ | ||
| 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 | + // Enqueue colorpicker | |
| 94 | + if ( ! wp_script_is( 'wp-color-picker', 'enqueued' ) ) { | |
| 95 | + wp_enqueue_script( 'wp-color-picker' ); | |
| 96 | + } | |
| 97 | + | |
| 98 | + if ( isset( $dependencies['wp-color-picker-alpha'] ) ) { | |
| 99 | + self::register_colorpicker_alpha(); | |
| 100 | + } | |
| 101 | + } | |
| 102 | + | |
| 103 | + // if file/file_list. | |
| 104 | + if ( isset( $dependencies['media-editor'] ) ) { | |
| 105 | + wp_enqueue_media(); | |
| 106 | + CMB2_Type_File_Base::output_js_underscore_templates(); | |
| 107 | + } | |
| 108 | + | |
| 109 | + // if timepicker. | |
| 110 | + if ( isset( $dependencies['jquery-ui-datetimepicker'] ) ) { | |
| 111 | + self::register_datetimepicker(); | |
| 112 | + } | |
| 113 | + | |
| 114 | + // if cmb2-wysiwyg. | |
| 115 | + $enqueue_wysiwyg = isset( $dependencies['cmb2-wysiwyg'] ) && $debug; | |
| 116 | + unset( $dependencies['cmb2-wysiwyg'] ); | |
| 117 | + | |
| 118 | + // if cmb2-char-counter. | |
| 119 | + $enqueue_char_counter = isset( $dependencies['cmb2-char-counter'] ) && $debug; | |
| 120 | + unset( $dependencies['cmb2-char-counter'] ); | |
| 121 | + | |
| 122 | + // Enqueue cmb JS. | |
| 123 | + wp_enqueue_script( self::$handle, CMB2_Utils::url( "js/cmb2{$min}.js" ), array_values( $dependencies ), CMB2_VERSION, true ); | |
| 124 | + | |
| 125 | + // if SCRIPT_DEBUG, we need to enqueue separately. | |
| 126 | + if ( $enqueue_wysiwyg ) { | |
| 127 | + wp_enqueue_script( 'cmb2-wysiwyg', CMB2_Utils::url( 'js/cmb2-wysiwyg.js' ), array( 'jquery', 'wp-util' ), CMB2_VERSION ); | |
| 128 | + } | |
| 129 | + if ( $enqueue_char_counter ) { | |
| 130 | + wp_enqueue_script( 'cmb2-char-counter', CMB2_Utils::url( 'js/cmb2-char-counter.js' ), array( 'jquery', 'wp-util' ), CMB2_VERSION ); | |
| 131 | + } | |
| 132 | + | |
| 133 | + self::localize( $debug ); | |
| 134 | + | |
| 135 | + do_action( 'cmb2_footer_enqueue' ); | |
| 136 | + } | |
| 137 | + | |
| 138 | + /** | |
| 139 | + * Register or enqueue the wp-color-picker-alpha script. | |
| 140 | + * | |
| 141 | + * @since 2.2.7 | |
| 142 | + * | |
| 143 | + * @param boolean $enqueue Whether or not to enqueue. | |
| 144 | + * | |
| 145 | + * @return void | |
| 146 | + */ | |
| 147 | + public static function register_colorpicker_alpha( $enqueue = false ) { | |
| 148 | + // Only use minified files if SCRIPT_DEBUG is off. | |
| 149 | + $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; | |
| 150 | + $func = $enqueue ? 'wp_enqueue_script' : 'wp_register_script'; | |
| 151 | + $func( 'wp-color-picker-alpha', CMB2_Utils::url( "js/wp-color-picker-alpha{$min}.js" ), array( 'wp-color-picker' ), '2.1.3' ); | |
| 152 | + } | |
| 153 | + | |
| 154 | + /** | |
| 155 | + * Register or enqueue the jquery-ui-datetimepicker script. | |
| 156 | + * | |
| 157 | + * @since 2.2.7 | |
| 158 | + * | |
| 159 | + * @param boolean $enqueue Whether or not to enqueue. | |
| 160 | + * | |
| 161 | + * @return void | |
| 162 | + */ | |
| 163 | + public static function register_datetimepicker( $enqueue = false ) { | |
| 164 | + $func = $enqueue ? 'wp_enqueue_script' : 'wp_register_script'; | |
| 165 | + $func( 'jquery-ui-datetimepicker', CMB2_Utils::url( 'js/jquery-ui-timepicker-addon.min.js' ), array( 'jquery-ui-slider' ), '1.5.0' ); | |
| 166 | + } | |
| 167 | + | |
| 168 | + /** | |
| 169 | + * We need to register colorpicker on the front-end | |
| 170 | + * | |
| 171 | + * @since 2.0.7 | |
| 172 | + */ | |
| 173 | + protected static function colorpicker_frontend() { | |
| 174 | + wp_register_script( 'iris', admin_url( 'js/iris.min.js' ), array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), CMB2_VERSION ); | |
| 175 | + wp_register_script( 'wp-color-picker', admin_url( 'js/color-picker.min.js' ), array( 'iris' ), CMB2_VERSION ); | |
| 176 | + wp_localize_script( 'wp-color-picker', 'wpColorPickerL10n', array( | |
| 177 | + 'clear' => esc_html__( 'Clear', 'cmb2' ), | |
| 178 | + 'defaultString' => esc_html__( 'Default', 'cmb2' ), | |
| 179 | + 'pick' => esc_html__( 'Select Color', 'cmb2' ), | |
| 180 | + 'current' => esc_html__( 'Current Color', 'cmb2' ), | |
| 181 | + ) ); | |
| 182 | + } | |
| 183 | + | |
| 184 | + /** | |
| 185 | + * Localize the php variables for CMB2 JS | |
| 186 | + * | |
| 187 | + * @since 2.0.7 | |
| 188 | + * | |
| 189 | + * @param mixed $debug Whether or not we are debugging. | |
| 190 | + */ | |
| 191 | + protected static function localize( $debug ) { | |
| 192 | + static $localized = false; | |
| 193 | + if ( $localized ) { | |
| 194 | + return; | |
| 195 | + } | |
| 196 | + | |
| 197 | + $localized = true; | |
| 198 | + $l10n = array( | |
| 199 | + 'fields' => self::$fields, | |
| 200 | + 'ajax_nonce' => wp_create_nonce( 'ajax_nonce' ), | |
| 201 | + 'ajaxurl' => admin_url( '/admin-ajax.php' ), | |
| 202 | + 'script_debug' => $debug, | |
| 203 | + 'up_arrow_class' => 'dashicons dashicons-arrow-up-alt2', | |
| 204 | + 'down_arrow_class' => 'dashicons dashicons-arrow-down-alt2', | |
| 205 | + 'user_can_richedit' => user_can_richedit(), | |
| 206 | + 'defaults' => array( | |
| 207 | + 'code_editor' => false, | |
| 208 | + 'color_picker' => false, | |
| 209 | + 'date_picker' => array( | |
| 210 | + 'changeMonth' => true, | |
| 211 | + 'changeYear' => true, | |
| 212 | + 'dateFormat' => _x( 'mm/dd/yy', 'Valid formatDate string for jquery-ui datepicker', 'cmb2' ), | |
| 213 | + 'dayNames' => explode( ',', esc_html__( 'Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday', 'cmb2' ) ), | |
| 214 | + 'dayNamesMin' => explode( ',', esc_html__( 'Su, Mo, Tu, We, Th, Fr, Sa', 'cmb2' ) ), | |
| 215 | + 'dayNamesShort' => explode( ',', esc_html__( 'Sun, Mon, Tue, Wed, Thu, Fri, Sat', 'cmb2' ) ), | |
| 216 | + 'monthNames' => explode( ',', esc_html__( 'January, February, March, April, May, June, July, August, September, October, November, December', 'cmb2' ) ), | |
| 217 | + 'monthNamesShort' => explode( ',', esc_html__( 'Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec', 'cmb2' ) ), | |
| 218 | + 'nextText' => esc_html__( 'Next', 'cmb2' ), | |
| 219 | + 'prevText' => esc_html__( 'Prev', 'cmb2' ), | |
| 220 | + 'currentText' => esc_html__( 'Today', 'cmb2' ), | |
| 221 | + 'closeText' => esc_html__( 'Done', 'cmb2' ), | |
| 222 | + 'clearText' => esc_html__( 'Clear', 'cmb2' ), | |
| 223 | + ), | |
| 224 | + 'time_picker' => array( | |
| 225 | + 'timeOnlyTitle' => esc_html__( 'Choose Time', 'cmb2' ), | |
| 226 | + 'timeText' => esc_html__( 'Time', 'cmb2' ), | |
| 227 | + 'hourText' => esc_html__( 'Hour', 'cmb2' ), | |
| 228 | + 'minuteText' => esc_html__( 'Minute', 'cmb2' ), | |
| 229 | + 'secondText' => esc_html__( 'Second', 'cmb2' ), | |
| 230 | + 'currentText' => esc_html__( 'Now', 'cmb2' ), | |
| 231 | + 'closeText' => esc_html__( 'Done', 'cmb2' ), | |
| 232 | + 'timeFormat' => _x( 'hh:mm TT', 'Valid formatting string, as per http://trentrichardson.com/examples/timepicker/', 'cmb2' ), | |
| 233 | + 'controlType' => 'select', | |
| 234 | + 'stepMinute' => 5, | |
| 235 | + ), | |
| 236 | + ), | |
| 237 | + 'strings' => array( | |
| 238 | + 'upload_file' => esc_html__( 'Use this file', 'cmb2' ), | |
| 239 | + 'upload_files' => esc_html__( 'Use these files', 'cmb2' ), | |
| 240 | + 'remove_image' => esc_html__( 'Remove Image', 'cmb2' ), | |
| 241 | + 'remove_file' => esc_html__( 'Remove', 'cmb2' ), | |
| 242 | + 'file' => esc_html__( 'File:', 'cmb2' ), | |
| 243 | + 'download' => esc_html__( 'Download', 'cmb2' ), | |
| 244 | + 'check_toggle' => esc_html__( 'Select / Deselect All', 'cmb2' ), | |
| 245 | + ), | |
| 246 | + ); | |
| 247 | + | |
| 248 | + if ( isset( self::$dependencies['code-editor'] ) && function_exists( 'wp_enqueue_code_editor' ) ) { | |
| 249 | + $l10n['defaults']['code_editor'] = wp_enqueue_code_editor( array( | |
| 250 | + 'type' => 'text/html', | |
| 251 | + ) ); | |
| 252 | + } | |
| 253 | + | |
| 254 | + wp_localize_script( self::$handle, self::$js_variable, apply_filters( 'cmb2_localized_data', $l10n ) ); | |
| 255 | + } | |
| 256 | + | |
| 257 | +} | |