SimpleCalendarAssets

Front end scripts and styles.

Defined (1)

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

/includes/assets.php  
  1. class Assets { 
  2.  
  3. /** 
  4. * Load minified assets. 
  5. * @access private 
  6. * @var string 
  7. */ 
  8. private $min = '.min'; 
  9.  
  10. /** 
  11. * Scripts. 
  12. * @access private 
  13. * @var array 
  14. */ 
  15. private $scripts = array(); 
  16.  
  17. /** 
  18. * Styles. 
  19. * @access private 
  20. * @var array 
  21. */ 
  22. private $styles = array(); 
  23.  
  24. /** 
  25. * Disable styles. 
  26. * @access public 
  27. * @var bool 
  28. */ 
  29. public $disable_styles = false; 
  30.  
  31. /** 
  32. * Hook in tabs. 
  33. * @since 3.0.0 
  34. */ 
  35. public function __construct() { 
  36.  
  37. $this->min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG == true ) ? '' : '.min'; 
  38.  
  39. $settings = get_option( 'simple-calendar_settings_advanced' ); 
  40.  
  41. if ( isset( $settings['assets']['disable_css'] ) ) { 
  42. $this->disable_styles = 'yes' == $settings['assets']['disable_css'] ? true : false; 
  43.  
  44. add_action( 'init', array( $this, 'register' ), 20 ); 
  45. add_action( 'init', array( $this, 'enqueue' ), 40 ); 
  46.  
  47. /** 
  48. * Register scripts and styles. 
  49. * @since 3.0.0 
  50. */ 
  51. public function register() { 
  52. do_action( 'simcal_register_assets', $this->min ); 
  53.  
  54. /** 
  55. * Enqueue scripts and styles. 
  56. * @since 3.0.0 
  57. */ 
  58. public function enqueue() { 
  59.  
  60. add_action( 'wp_enqueue_scripts', array( $this, 'load' ), 10 ); 
  61.  
  62. do_action( 'simcal_enqueue_assets', $this->min ); 
  63.  
  64.  
  65. $min = $this->min; 
  66. // Improves compatibility with themes and plugins using Isotope and Masonry. 
  67. add_action( 'wp_enqueue_scripts',  
  68. function () use ( $min ) { 
  69. if ( wp_script_is( 'simcal-qtip', 'enqueued' ) ) { 
  70. wp_enqueue_script( 
  71. 'simplecalendar-imagesloaded',  
  72. SIMPLE_CALENDAR_ASSETS . 'js/vendor/imagesloaded.pkgd' . $min . '.js',  
  73. array( 'simcal-qtip' ),  
  74. SIMPLE_CALENDAR_VERSION,  
  75. true 
  76. ); 
  77. }, 1000 ); 
  78.  
  79. /** 
  80. * Load scripts and styles. 
  81. * @since 3.0.0 
  82. */ 
  83. public function load() { 
  84.  
  85. $types = simcal_get_calendar_types(); 
  86.  
  87. foreach ( $types as $calendar => $views ) { 
  88. foreach( $views as $key => $view ) { 
  89.  
  90. $view = simcal_get_calendar_view( 0, $calendar . '-' . $view ); 
  91.  
  92. $scripts[] = $view->scripts( $this->min ); 
  93. $styles[] = $view->styles( $this->min ); 
  94.  
  95. $this->get_widgets_assets(); 
  96. $this->scripts = apply_filters( 'simcal_front_end_scripts', $scripts, $this->min ); 
  97. // First check if there is a multi-dimensional array of scripts 
  98. if ( isset( $this->scripts[0] ) ) { 
  99. foreach ( $this->scripts as $script ) { 
  100. $this->load_scripts ( $script ); 
  101. } else { 
  102. $this->load_scripts( $this->scripts ); 
  103. $this->styles = apply_filters( 'simcal_front_end_styles', $styles, $this->min ); 
  104. // First check if there is a multi-dimensional array of styles 
  105. if ( isset( $this->styles[0] ) ) { 
  106. foreach( $this->styles as $style ) { 
  107. $this->load_styles( $style ); 
  108. } else { 
  109. $this->load_styles( $this->styles ); 
  110.  
  111. /** 
  112. * Get widgets assets. 
  113. * @since 3.0.0 
  114. */ 
  115. public function get_widgets_assets() { 
  116.  
  117. $widgets = get_option( 'widget_gce_widget' ); 
  118.  
  119. if ( ! empty( $widgets ) && is_array( $widgets ) ) { 
  120.  
  121. foreach ( $widgets as $settings ) { 
  122.  
  123. if ( ! empty( $settings ) && is_array( $settings ) ) { 
  124.  
  125. if ( isset( $settings['calendar_id'] ) ) { 
  126.  
  127. $view = simcal_get_calendar_view( absint( $settings['calendar_id'] ) ); 
  128.  
  129. if ( $view instanceof Calendar_View ) { 
  130. add_filter( 'simcal_front_end_scripts', function ( $scripts, $min ) use ( $view ) { 
  131. return array_merge( $scripts, $view->scripts( $min ) ); 
  132. }, 100, 2 ); 
  133. add_filter( 'simcal_front_end_styles', function ( $styles, $min ) use ( $view ) { 
  134. return array_merge( $styles, $view->styles( $min ) ); 
  135. }, 100, 2 ); 
  136.  
  137.  
  138.  
  139.  
  140. /** 
  141. * Scripts. 
  142. * @since 3.0.0 
  143. * @param array $scripts 
  144. */ 
  145. public function load_scripts( $scripts ) { 
  146.  
  147. // Only load if not disabled in the settings 
  148. if ( ! empty( $scripts ) && is_array( $scripts ) ) { 
  149.  
  150. foreach ( $scripts as $script => $v ) { 
  151.  
  152. /** Plugin compatibility fixes */ 
  153.  
  154. // Dequeue moment.js if detected from WP Simple Pay Pro. 
  155. if ( ( wp_script_is( 'stripe-checkout-pro-moment', 'enqueued' ) ) && $script == 'simcal-fullcal-moment' ) { 
  156. continue; 
  157.  
  158. if ( ! empty( $v['src'] ) ) { 
  159.  
  160. // Enqueued individually so we can dequeue if already enqueued by another plugin. 
  161. // TODO Rework dependencies part (or remove completely). 
  162.  
  163. $src = esc_url( $v['src'] ); 
  164. $in_footer = isset( $v['in_footer'] ) ? $v['in_footer'] : false; 
  165.  
  166. wp_enqueue_script( $script, $src, array(), SIMPLE_CALENDAR_VERSION, $in_footer ); 
  167.  
  168. if ( ! empty( $v['localize'] ) && is_array( $v['localize'] ) ) { 
  169. foreach ( $v['localize'] as $object => $l10n ) { 
  170. wp_localize_script( $script, $object, $l10n ); 
  171.  
  172. } elseif ( is_string( $v ) && ! empty( $v ) ) { 
  173.  
  174. wp_enqueue_script( $v ); 
  175.  
  176.  
  177. /** 
  178. * Styles. 
  179. * @since 3.0.0 
  180. * @param array $styles 
  181. */ 
  182. public function load_styles( $styles ) { 
  183.  
  184. // Only load if not disabled in the settings 
  185. if ( ! empty( $styles ) && is_array( $styles ) && false === $this->disable_styles ) { 
  186.  
  187. foreach ( $styles as $style => $v ) { 
  188.  
  189. if ( ! empty( $v['src'] ) ) { 
  190.  
  191. // Enqueued individually so we can dequeue if already enqueued by another plugin. 
  192. // TODO Rework dependencies part (or remove completely). 
  193.  
  194. $src = esc_url( $v['src'] ); 
  195. $media = isset( $v['media'] ) ? $v['media'] : 'all'; 
  196.  
  197. wp_enqueue_style( $style, $src, array(), SIMPLE_CALENDAR_VERSION, $media ); 
  198.  
  199. } elseif ( is_string( $v ) && ! empty( $v ) ) { 
  200.  
  201. wp_enqueue_style( $v ); 
  202.  
  203.