enqueues.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. /**
  3. * Enqueue styles and scripts used by the theme
  4. *
  5. * @package vamtam/consulting
  6. */
  7. /**
  8. * class VamtamEnqueues
  9. */
  10. class VamtamEnqueues {
  11. private static $use_min;
  12. private static $widget_styles = array(
  13. 'WP_Nav_Menu_Widget' => 'nav-menu',
  14. 'WP_Widget_Tag_Cloud' => 'tagcloud',
  15. 'WP_Widget_RSS' => 'rss',
  16. 'WP_Widget_Search' => 'search',
  17. 'WC_Widget_Product_Search' => 'search',
  18. 'WP_Widget_Calendar' => 'calendar',
  19. );
  20. /**
  21. * Hook the relevant actions
  22. */
  23. public static function actions() {
  24. self::$use_min = ! ( WP_DEBUG || ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) || ( defined( 'VAMTAM_SCRIPT_DEBUG' ) && VAMTAM_SCRIPT_DEBUG ) );
  25. add_action( 'wp_enqueue_scripts', array( __CLASS__, 'scripts' ) );
  26. add_action( 'wp_enqueue_scripts', array( __CLASS__, 'styles' ), 999 );
  27. add_action( 'wp', array( __CLASS__, 'preload_styles' ) );
  28. if ( ! is_admin() ) {
  29. add_action( 'the_widget', array( __CLASS__, 'widget_styles' ) );
  30. add_action( 'dynamic_sidebar', array( __CLASS__, 'widget_styles_dynamic_sidebar' ) );
  31. }
  32. add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_scripts' ) );
  33. add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_styles' ), 999 );
  34. add_action( 'customize_controls_enqueue_scripts', array( __CLASS__, 'customize_controls_enqueue_scripts' ) );
  35. add_action( 'customize_preview_init', array( __CLASS__, 'customize_preview_init' ) );
  36. }
  37. private static function is_our_admin_page() {
  38. if ( ! is_admin() ) return false;
  39. $screen = get_current_screen();
  40. return
  41. in_array( $screen->base, array( 'post', 'widgets', 'themes', 'upload' ) ) ||
  42. strpos( $screen->base, 'vamtam_' ) !== false ||
  43. strpos( $screen->base, 'toplevel_page_vamtam' ) === 0 ||
  44. strpos( $screen->base, 'toplevel_page_vamtam' ) === 0 ||
  45. $screen->base === 'media_page_vamtam_icons';
  46. }
  47. private static function inject_dependency( $handle, $dep ) {
  48. global $wp_scripts;
  49. $script = $wp_scripts->query( $handle, 'registered' );
  50. if ( ! $script )
  51. return false;
  52. if ( ! in_array( $dep, $script->deps ) ) {
  53. $script->deps[] = $dep;
  54. }
  55. return true;
  56. }
  57. /**
  58. * Front-end scripts
  59. */
  60. public static function scripts() {
  61. global $content_width;
  62. if ( is_admin() || VamtamTemplates::is_login() ) return;
  63. $cache_timestamp = get_option( 'vamtam-css-cache-timestamp' );
  64. if ( is_singular() && comments_open() ) {
  65. wp_enqueue_script( 'comment-reply', false, false, false, true );
  66. }
  67. wp_register_script( 'vamtam-ls-height-fix', VAMTAM_JS . 'layerslider-height.js', array( 'jquery-core' ), $cache_timestamp, true );
  68. wp_register_script( 'vamtam-splash-screen', VAMTAM_JS . 'splash-screen.js', array( 'jquery-core', 'imagesloaded' ), $cache_timestamp, true );
  69. $cube_path = VAMTAM_ASSETS_URI . 'cubeportfolio/js/jquery.cubeportfolio' . ( self::$use_min ? '.min' : '' ) . '.js';
  70. wp_register_script( 'cubeportfolio', $cube_path, array( 'jquery-core' ), '4.1.1', true );
  71. wp_register_script( 'vamtam-hide-widgets', VAMTAM_JS . 'hide-widgets.js', array(), $cache_timestamp, true );
  72. $all_js_path = self::$use_min ? 'all.min.js' : 'all.js';
  73. $all_js_deps = array(
  74. 'jquery-core',
  75. );
  76. if ( class_exists( 'FLBuilderModel' ) && FLBuilderModel::is_builder_enabled() ) {
  77. $all_js_deps[] = 'fl-builder-layout-' . get_the_ID();
  78. }
  79. wp_enqueue_script( 'vamtam-all', VAMTAM_JS . $all_js_path, $all_js_deps, $cache_timestamp, true );
  80. self::inject_dependency( 'wc-cart-fragments', 'vamtam-all' );
  81. $script_vars = array(
  82. 'ajaxurl' => admin_url( 'admin-ajax.php' ),
  83. 'jspath' => VAMTAM_JS,
  84. 'mobile_header_breakpoint' => vamtam_get_mobile_header_breakpoint(),
  85. 'cube_path' => $cube_path,
  86. 'beaver_responsive' => class_exists( 'FLBuilderModel' ) ? (int) FLBuilderModel::get_global_settings()->medium_breakpoint : 768,
  87. 'beaver_small' => class_exists( 'FLBuilderModel' ) ? (int) FLBuilderModel::get_global_settings()->responsive_breakpoint : 768,
  88. 'content_width' => (int) $content_width,
  89. );
  90. wp_localize_script( 'vamtam-all', 'VAMTAM_FRONT', $script_vars );
  91. $sticky_header_js_path = self::$use_min ? 'sticky-header.min.js' : 'sticky-header.js';
  92. wp_register_script( 'vamtam-sticky-header', VAMTAM_JS . 'build/' . $sticky_header_js_path, 'vamtam-all', $cache_timestamp, true );
  93. }
  94. /**
  95. * Admin scripts
  96. */
  97. public static function admin_scripts() {
  98. if ( ! self::is_our_admin_page() ) return;
  99. $cache_timestamp = VamtamFramework::get_version();
  100. wp_enqueue_script( 'jquery-magnific-popup', VAMTAM_JS . 'plugins/thirdparty/jquery.magnific.js', array( 'jquery-core' ), $cache_timestamp, true );
  101. wp_enqueue_script( 'common' );
  102. wp_enqueue_script( 'editor' );
  103. wp_enqueue_script( 'jquery-ui-tabs' );
  104. wp_enqueue_script( 'farbtastic' );
  105. wp_enqueue_media();
  106. wp_enqueue_script( 'vamtam_admin', VAMTAM_ADMIN_ASSETS_URI . 'js/admin-all.js', array( 'jquery-core', 'underscore', 'backbone' ), $cache_timestamp, true );
  107. wp_localize_script(
  108. 'vamtam_admin', 'VAMTAM_ADMIN', array(
  109. 'addNewIcon' => esc_html__( 'Add New Icon', 'vamtam-consulting' ),
  110. 'iconName' => esc_html__( 'Icon', 'vamtam-consulting' ),
  111. 'iconText' => esc_html__( 'Text', 'vamtam-consulting' ),
  112. 'iconLink' => esc_html__( 'Link', 'vamtam-consulting' ),
  113. 'iconChange' => esc_html__( 'Change', 'vamtam-consulting' ),
  114. 'fonts' => $GLOBALS['vamtam_fonts'],
  115. )
  116. );
  117. }
  118. /**
  119. * Front-end styles
  120. */
  121. public static function styles() {
  122. global $content_width;
  123. if ( is_admin() || VamtamTemplates::is_login() ) return;
  124. $cache_timestamp = get_option( 'vamtam-css-cache-timestamp' );
  125. $preview = is_customize_preview() || ( isset( $_POST['wp_customize'] ) && $_POST['wp_customize'] == 'on' && isset( $_POST['customized'] ) && ! empty( $_POST['customized'] ) && ! isset( $_POST['action'] ) ? '-preview' : '' );
  126. $fonts_url = empty( $preview ) ? rd_vamtam_get_option( 'google_fonts' ) : vamtam_customizer_preview_fonts_url();
  127. wp_enqueue_style( 'vamtam-gfonts', $fonts_url, array(), $cache_timestamp );
  128. wp_register_style( 'cubeportfolio', VAMTAM_ASSETS_URI . 'cubeportfolio/css/cubeportfolio' . ( self::$use_min ? '.min' : '' ) . '.css', array( 'front-all' ), '4.1.1' );
  129. $generated_deps = array();
  130. if ( vamtam_has_woocommerce() ) {
  131. $generated_deps[] = 'woocommerce-layout';
  132. $generated_deps[] = 'woocommerce-smallscreen';
  133. $generated_deps[] = 'woocommerce-general';
  134. }
  135. wp_enqueue_style( 'front-all', VAMTAM_ASSETS_URI . 'css/dist/all.css', $generated_deps, $cache_timestamp );
  136. $theme_url = VAMTAM_THEME_URI;
  137. $theme_icons_css = "
  138. @font-face {
  139. font-family: 'icomoon';
  140. src: url({$theme_url}vamtam/assets/fonts/icons/icomoon.woff2) format('woff2'),
  141. url( {$theme_url}vamtam/assets/fonts/icons/icomoon.woff) format('woff'),
  142. url({$theme_url}vamtam/assets/fonts/icons/icomoon.ttf) format('ttf');
  143. font-weight: normal;
  144. font-style: normal;
  145. }
  146. @font-face {
  147. font-family: 'theme';
  148. src: url({$theme_url}vamtam/assets/fonts/theme-icons/theme-icons.woff2) format('woff2'),
  149. url({$theme_url}vamtam/assets/fonts/theme-icons/theme-icons.woff) format('woff'),
  150. url({$theme_url}vamtam/assets/fonts/theme-icons/theme-icons.ttf) format('truetype');
  151. font-weight: normal;
  152. font-style: normal;
  153. }
  154. ";
  155. wp_add_inline_style( 'front-all', $theme_icons_css );
  156. // content paddings
  157. ob_start();
  158. $small_breakpoint = class_exists( 'FLBuilderModel' ) ? (int) FLBuilderModel::get_global_settings()->responsive_breakpoint : 768;
  159. $medium_breakpoint = class_exists( 'FLBuilderModel' ) ? (int) FLBuilderModel::get_global_settings()->medium_breakpoint : 992;
  160. include VAMTAM_CSS_DIR . 'beaver.php';
  161. include VAMTAM_CSS_DIR . 'header-slider.php';
  162. wp_add_inline_style( 'front-all', ob_get_clean() );
  163. wp_enqueue_style( 'vamtam-theme-print', VAMTAM_ASSETS_URI . 'css/print.css', array( 'front-all' ), $cache_timestamp, 'print' );
  164. $responsive_stylesheets = array(
  165. 'mobile-header' => '(max-width: ' . vamtam_get_mobile_header_breakpoint() . ')',
  166. 'layout-max-low' => '(min-width: ' . ( $medium_breakpoint + 1 ) . "px) and (max-width: {$content_width}px)",
  167. 'layout-max' => '(min-width: ' . ( $medium_breakpoint + 1 ) . 'px)',
  168. 'layout-below-max' => "(max-width: {$medium_breakpoint}px)",
  169. 'layout-small' => '(max-width: {$small_breakpoint}px)',
  170. 'wc-small-screen' => 'only screen and (max-width: ' . apply_filters( 'woocommerce_style_smallscreen_breakpoint', $breakpoint = '768px' ) . ')',
  171. );
  172. $url_prefix = VAMTAM_ASSETS_URI . 'css/dist/responsive/';
  173. foreach ( $responsive_stylesheets as $file => $media ) {
  174. wp_enqueue_style( 'vamtam-theme-'. $file, $url_prefix . $file . '.css', array( 'front-all' ), $cache_timestamp, $media );
  175. }
  176. wp_register_style( 'vamtam-widgets-general', VAMTAM_ASSETS_URI . 'css/dist/widgets/general.css' , array( 'front-all' ), $cache_timestamp );
  177. foreach ( array_unique( self::$widget_styles ) as $class => $file ) {
  178. wp_register_style( 'vamtam-widget-' . $file, VAMTAM_ASSETS_URI . 'css/dist/widgets/' . $file . '.css' , array( 'front-all', 'vamtam-widgets-general' ), $cache_timestamp );
  179. }
  180. self::print_theme_options();
  181. }
  182. /**
  183. * Output Link rel=preload headers for critical styles
  184. */
  185. public static function preload_styles() {
  186. $cache_timestamp = get_option( 'vamtam-css-cache-timestamp' );
  187. $url_prefix = VAMTAM_ASSETS_URI . 'css/dist/responsive/';
  188. if ( wp_is_mobile() ) {
  189. header( "Link: <{$url_prefix}layout-below-max.css?ver={$cache_timestamp}>; rel=preload; as=style", false );
  190. header( "Link: <{$url_prefix}mobile-header.css?ver={$cache_timestamp}>; rel=preload; as=style", false );
  191. } else {
  192. header( "Link: <{$url_prefix}layout-max.css?ver={$cache_timestamp}>; rel=preload; as=style", false );
  193. }
  194. }
  195. /**
  196. * Enqueue widget styles, hooked to the_widget
  197. */
  198. public static function widget_styles( $widget ) {
  199. // this one is for all widgets, anywhere
  200. wp_enqueue_style( 'vamtam-widgets-general' );
  201. // some widgets have their own style sheets
  202. if ( isset( self::$widget_styles[ $widget ] ) ) {
  203. wp_enqueue_style( 'vamtam-widget-' . self::$widget_styles[ $widget ] );
  204. }
  205. }
  206. /**
  207. * Enqueue widget styles, hooked to dynamic_sidebar
  208. */
  209. public static function widget_styles_dynamic_sidebar( $widget ) {
  210. self::widget_styles( get_class( $widget['callback'][0] ) );
  211. }
  212. /**
  213. * Admin styles
  214. */
  215. public static function admin_styles() {
  216. if ( ! self::is_our_admin_page() ) return;
  217. wp_enqueue_style( 'magnific', VAMTAM_ADMIN_ASSETS_URI . 'css/magnific.css' );
  218. wp_enqueue_style( 'vamtam_admin', VAMTAM_ADMIN_ASSETS_URI . 'css/vamtam_admin.css' );
  219. wp_enqueue_style( 'farbtastic' );
  220. }
  221. /**
  222. * Customizer styles
  223. */
  224. public static function customize_controls_enqueue_scripts() {
  225. $cache_timestamp = VamtamFramework::get_version();
  226. wp_enqueue_style( 'vamtam-customizer', VAMTAM_ADMIN_ASSETS_URI . 'css/customizer.css', array(), $cache_timestamp );
  227. wp_enqueue_script( 'vamtam-customize-controls-conditionals', VAMTAM_ADMIN_ASSETS_URI . 'js/customize-controls-conditionals.js', array( 'jquery-core', 'customize-controls' ), $cache_timestamp, true );
  228. }
  229. public static function customize_preview_init() {
  230. $cache_timestamp = VamtamFramework::get_version();
  231. wp_enqueue_script( 'vamtam-customizer-preview', VAMTAM_ADMIN_ASSETS_URI . 'js/customizer-preview.js', array( 'jquery-core', 'customize-preview' ), $cache_timestamp, true );
  232. wp_localize_script(
  233. 'vamtam-customizer-preview', 'VAMTAM_CUSTOMIZE_PREVIEW', array(
  234. 'compiler_options' => vamtam_custom_css_options(),
  235. 'ajaxurl' => admin_url( 'admin-ajax.php' ),
  236. 'percentages' => VamtamLessBridge::$percentages,
  237. 'numbers' => VamtamLessBridge::$numbers,
  238. )
  239. );
  240. }
  241. public static function print_theme_options() {
  242. $vars_raw = $GLOBALS['vamtam_theme_customizer']->get_options();
  243. $option_defs = $GLOBALS['vamtam_theme_customizer']->get_fields_by_id();
  244. $options_to_export = array();
  245. foreach ( $option_defs as $option ) {
  246. if ( isset( $option['compiler'] ) && $option['compiler'] ) {
  247. $options_to_export[ $option['id'] ] = apply_filters( 'vamtam_get_option', $vars_raw[ $option['id'] ], $option['id'] );
  248. }
  249. }
  250. $options = VamtamLessBridge::prepare_vars_for_export( $options_to_export );
  251. echo '<style id="vamtam-theme-options">';
  252. echo ':root {';
  253. foreach ( $options as $name => $value ) {
  254. echo '--vamtam-' . esc_html( $name ) . ':' . wp_kses_data( $value ) . ";\n";
  255. }
  256. echo "--vamtam-loading-animation:url('" . esc_attr( VAMTAM_IMAGES . 'loader-ring.gif') . "');\n";
  257. for ( $i = 1; $i <= 8; $i++ ) {
  258. $name = "accent-color-$i";
  259. $color = new VamtamColor( $options[ $name ] );
  260. $readable = '';
  261. $hc = '';
  262. if ( $color->L > .8 ) {
  263. $readable = '#' . $color->darken( 50 );
  264. $hc = '#000000';
  265. } else {
  266. $readable = '#' . $color->lighten( 50 );
  267. $hc = '#ffffff';
  268. }
  269. echo '--vamtam-' . esc_html( $name ) . '-readable:' . wp_kses_data( $readable ) . ";\n";
  270. echo '--vamtam-' . esc_html( $name ) . '-hc:' . wp_kses_data( $hc ) . ";\n";
  271. echo '--vamtam-' . esc_html( $name ) . '-transparent:rgba(' . wp_kses_data( implode( ',', $color->getRgb() ) ) . ",0);\n";
  272. }
  273. echo '}';
  274. echo '</style>';
  275. }
  276. }