CMB2_JS

Handles the dependencies and enqueueing of the CMB2 JS scripts.

Defined (1)

The class is defined in the following location(s).

/includes/CMB2_JS.php  
  1. class CMB2_JS { 
  2.  
  3. /** 
  4. * The CMB2 JS handle 
  5. * @var string 
  6. * @since 2.0.7 
  7. */ 
  8. protected static $handle = 'cmb2-scripts'; 
  9.  
  10. /** 
  11. * The CMB2 JS variable name 
  12. * @var string 
  13. * @since 2.0.7 
  14. */ 
  15. protected static $js_variable = 'cmb2_l10'; 
  16.  
  17. /** 
  18. * Array of CMB2 JS dependencies 
  19. * @var array 
  20. * @since 2.0.7 
  21. */ 
  22. protected static $dependencies = array( 'jquery' => 'jquery' ); 
  23.  
  24. /** 
  25. * Add a dependency to the array of CMB2 JS dependencies 
  26. * @since 2.0.7 
  27. * @param array|string $dependencies Array (or string) of dependencies to add 
  28. */ 
  29. public static function add_dependencies( $dependencies ) { 
  30. foreach ( (array) $dependencies as $dependency ) { 
  31. self::$dependencies[ $dependency ] = $dependency; 
  32.  
  33. /** 
  34. * Enqueue the CMB2 JS 
  35. * @since 2.0.7 
  36. */ 
  37. public static function enqueue() { 
  38. // Filter required script dependencies 
  39. $dependencies = apply_filters( 'cmb2_script_dependencies', self::$dependencies ); 
  40.  
  41. // Only use minified files if SCRIPT_DEBUG is off 
  42. $debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; 
  43.  
  44. $min = $debug ? '' : '.min'; 
  45.  
  46. // if colorpicker 
  47. if ( ! is_admin() && isset( $dependencies['wp-color-picker'] ) ) { 
  48. self::colorpicker_frontend(); 
  49.  
  50. // if file/file_list 
  51. if ( isset( $dependencies['media-editor'] ) ) { 
  52. wp_enqueue_media(); 
  53.  
  54. // if timepicker 
  55. if ( isset( $dependencies['jquery-ui-datetimepicker'] ) ) { 
  56. wp_register_script( 'jquery-ui-datetimepicker', CMB2_Utils::url( 'js/jquery-ui-timepicker-addon.min.js' ), array( 'jquery-ui-slider' ), CMB2_VERSION ); 
  57.  
  58. // if cmb2-wysiwyg 
  59. $enqueue_wysiwyg = isset( $dependencies['cmb2-wysiwyg'] ) && $debug; 
  60. unset( $dependencies['cmb2-wysiwyg'] ); 
  61.  
  62. // Enqueue cmb JS 
  63. wp_enqueue_script( self::$handle, CMB2_Utils::url( "js/cmb2{$min}.js" ), $dependencies, CMB2_VERSION, true ); 
  64.  
  65. // if SCRIPT_DEBUG, we need to enqueue separately. 
  66. if ( $enqueue_wysiwyg ) { 
  67. wp_enqueue_script( 'cmb2-wysiwyg', CMB2_Utils::url( 'js/cmb2-wysiwyg.js' ), array( 'jquery', 'wp-util' ), CMB2_VERSION ); 
  68.  
  69. self::localize( $debug ); 
  70.  
  71. /** 
  72. * We need to register colorpicker on the front-end 
  73. * @since 2.0.7 
  74. */ 
  75. protected static function colorpicker_frontend() { 
  76. wp_register_script( 'iris', admin_url( 'js/iris.min.js' ), array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), CMB2_VERSION ); 
  77. wp_register_script( 'wp-color-picker', admin_url( 'js/color-picker.min.js' ), array( 'iris' ), CMB2_VERSION ); 
  78. wp_localize_script( 'wp-color-picker', 'wpColorPickerL10n', array( 
  79. 'clear' => esc_html__( 'Clear', 'cmb2' ),  
  80. 'defaultString' => esc_html__( 'Default', 'cmb2' ),  
  81. 'pick' => esc_html__( 'Select Color', 'cmb2' ),  
  82. 'current' => esc_html__( 'Current Color', 'cmb2' ),  
  83. ) ); 
  84.  
  85. /** 
  86. * Localize the php variables for CMB2 JS 
  87. * @since 2.0.7 
  88. */ 
  89. protected static function localize( $debug ) { 
  90. static $localized = false; 
  91. if ( $localized ) { 
  92. return; 
  93.  
  94. $localized = true; 
  95. $l10n = array( 
  96. 'ajax_nonce' => wp_create_nonce( 'ajax_nonce' ),  
  97. 'ajaxurl' => admin_url( '/admin-ajax.php' ),  
  98. 'script_debug' => $debug,  
  99. 'up_arrow_class' => 'dashicons dashicons-arrow-up-alt2',  
  100. 'down_arrow_class' => 'dashicons dashicons-arrow-down-alt2',  
  101. 'defaults' => array( 
  102. 'color_picker' => false,  
  103. 'date_picker' => array( 
  104. 'changeMonth' => true,  
  105. 'changeYear' => true,  
  106. 'dateFormat' => _x( 'mm/dd/yy', 'Valid formatDate string for jquery-ui datepicker', 'cmb2' ),  
  107. 'dayNames' => explode( ', ', esc_html__( 'Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday', 'cmb2' ) ),  
  108. 'dayNamesMin' => explode( ', ', esc_html__( 'Su, Mo, Tu, We, Th, Fr, Sa', 'cmb2' ) ),  
  109. 'dayNamesShort' => explode( ', ', esc_html__( 'Sun, Mon, Tue, Wed, Thu, Fri, Sat', 'cmb2' ) ),  
  110. 'monthNames' => explode( ', ', esc_html__( 'January, February, March, April, May, June, July, August, September, October, November, December', 'cmb2' ) ),  
  111. 'monthNamesShort' => explode( ', ', esc_html__( 'Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec', 'cmb2' ) ),  
  112. 'nextText' => esc_html__( 'Next', 'cmb2' ),  
  113. 'prevText' => esc_html__( 'Prev', 'cmb2' ),  
  114. 'currentText' => esc_html__( 'Today', 'cmb2' ),  
  115. 'closeText' => esc_html__( 'Done', 'cmb2' ),  
  116. 'clearText' => esc_html__( 'Clear', 'cmb2' ),  
  117. ),  
  118. 'time_picker' => array( 
  119. 'timeOnlyTitle' => esc_html__( 'Choose Time', 'cmb2' ),  
  120. 'timeText' => esc_html__( 'Time', 'cmb2' ),  
  121. 'hourText' => esc_html__( 'Hour', 'cmb2' ),  
  122. 'minuteText' => esc_html__( 'Minute', 'cmb2' ),  
  123. 'secondText' => esc_html__( 'Second', 'cmb2' ),  
  124. 'currentText' => esc_html__( 'Now', 'cmb2' ),  
  125. 'closeText' => esc_html__( 'Done', 'cmb2' ),  
  126. 'timeFormat' => _x( 'hh:mm TT', 'Valid formatting string, as per http://trentrichardson.com/examples/timepicker/', 'cmb2' ),  
  127. 'controlType' => 'select',  
  128. 'stepMinute' => 5,  
  129. ),  
  130. ),  
  131. 'strings' => array( 
  132. 'upload_file' => esc_html__( 'Use this file', 'cmb2' ),  
  133. 'upload_files' => esc_html__( 'Use these files', 'cmb2' ),  
  134. 'remove_image' => esc_html__( 'Remove Image', 'cmb2' ),  
  135. 'remove_file' => esc_html__( 'Remove', 'cmb2' ),  
  136. 'file' => esc_html__( 'File:', 'cmb2' ),  
  137. 'download' => esc_html__( 'Download', 'cmb2' ),  
  138. 'check_toggle' => esc_html__( 'Select / Deselect All', 'cmb2' ),  
  139. ),  
  140. ); 
  141.  
  142. wp_localize_script( self::$handle, self::$js_variable, apply_filters( 'cmb2_localized_data', $l10n ) ); 
  143.