Sin descripción

Assets.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. class PUM_Site_Assets {
  6. /**
  7. * @var
  8. */
  9. public static $cache_url;
  10. /**
  11. * @var
  12. */
  13. public static $suffix;
  14. /**
  15. * @var
  16. */
  17. public static $js_url;
  18. /**
  19. * @var
  20. */
  21. public static $css_url;
  22. /**
  23. * @var array
  24. */
  25. public static $enqueued_scripts = array();
  26. /**
  27. * @var array
  28. */
  29. public static $enqueued_styles = array();
  30. /**
  31. * @var bool
  32. */
  33. public static $scripts_registered = false;
  34. /**
  35. * @var bool
  36. */
  37. public static $styles_registered = false;
  38. /**
  39. * @var bool Use minified libraries if SCRIPT_DEBUG is turned off.
  40. */
  41. public static $debug;
  42. /**
  43. * Initialize
  44. */
  45. public static function init() {
  46. self::$cache_url = PUM_Helpers::get_cache_dir_url();
  47. self::$debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
  48. self::$suffix = self::$debug ? '' : '.min';
  49. self::$js_url = Popup_Maker::$URL . 'assets/js/';
  50. self::$css_url = Popup_Maker::$URL . 'assets/css/';
  51. // Register assets early.
  52. add_action( 'wp_enqueue_scripts', array( __CLASS__, 'register_styles' ) );
  53. add_action( 'wp_enqueue_scripts', array( __CLASS__, 'register_scripts' ) );
  54. // Localize after popups rendered in PUM_Site_Popups
  55. add_action( 'wp_footer', array( __CLASS__, 'late_localize_scripts' ), 19 );
  56. // Checks preloaded popups in the head for which assets to enqueue.
  57. add_action( 'pum_preload_popup', array( __CLASS__, 'enqueue_popup_assets' ) );
  58. add_filter( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_page_assets' ) );
  59. add_action( 'wp_enqueue_scripts', array( __CLASS__, 'fix_broken_extension_scripts' ), 100 );
  60. // Allow forcing assets to load.
  61. add_action( 'wp_head', array( __CLASS__, 'check_force_script_loading' ) );
  62. }
  63. public static function fix_broken_extension_scripts() {
  64. if ( wp_script_is( 'pum_aweber_integration_js' ) && class_exists( 'PUM_Aweber_Integration' ) && defined( 'PUM_AWEBER_INTEGRATION_VER' ) && version_compare( PUM_AWEBER_INTEGRATION_VER, '1.1.0', '<' ) ) {
  65. wp_dequeue_script( 'pum_aweber_integration_js' );
  66. wp_dequeue_style( 'pum_aweber_integration_css' );
  67. wp_dequeue_script( 'pum_newsletter_script' );
  68. wp_dequeue_style( 'pum-newsletter-styles' );
  69. wp_enqueue_style( 'pum-newsletter-styles', PUM_AWEBER_INTEGRATION_URL . '/includes/pum-newsletters/newsletter-styles' . self::$suffix . '.css' );
  70. wp_enqueue_script( 'pum_newsletter_script', PUM_AWEBER_INTEGRATION_URL . '/includes/pum-newsletters/newsletter-scripts' . self::$suffix . '.js', array(
  71. 'jquery',
  72. 'popup-maker-site',
  73. ), false, true );
  74. }
  75. $mc_ver_test = in_array( true, array(
  76. class_exists( 'PUM_MailChimp_Integration' ) && defined( 'PUM_MAILCHIMP_INTEGRATION_VER' ) && PUM_MAILCHIMP_INTEGRATION_VER,
  77. class_exists( 'PUM_MCI' ) && version_compare( PUM_MCI::$VER, '1.3.0', '<' ),
  78. ) );
  79. if ( $mc_ver_test ) {
  80. wp_dequeue_script( 'pum_mailchimp_integration_admin_js' );
  81. wp_dequeue_style( 'pum_mailchimp_integration_admin_css' );
  82. wp_dequeue_script( 'pum-mci' );
  83. wp_dequeue_style( 'pum-mci' );
  84. wp_dequeue_script( 'pum-newsletter-site' );
  85. wp_dequeue_style( 'pum-newsletter-site' );
  86. wp_enqueue_style( 'pum-newsletter-site', PUM_NEWSLETTER_URL . 'assets/css/pum-newsletter-site' . self::$suffix . '.css', null, PUM_NEWSLETTER_VERSION );
  87. wp_enqueue_script( 'pum-newsletter-site', PUM_NEWSLETTER_URL . 'assets/js/pum-newsletter-site' . self::$suffix . '.js', array( 'jquery' ), PUM_NEWSLETTER_VERSION, true );
  88. wp_localize_script( 'pum-newsletter-site', 'pum_sub_vars', array(
  89. 'ajaxurl' => admin_url( 'admin-ajax.php' ),
  90. 'message_position' => 'top',
  91. ) );
  92. }
  93. }
  94. /**
  95. * Checks the current page content for the newsletter shortcode.
  96. */
  97. public static function enqueue_page_assets() {
  98. global $post;
  99. if ( ! empty( $post ) && has_shortcode( $post->post_content, 'pum_sub_form' ) ) {
  100. wp_enqueue_script( 'popup-maker-site' );
  101. wp_enqueue_style( 'popup-maker-site' );
  102. }
  103. }
  104. /**
  105. * @param int $popup_id
  106. */
  107. public static function enqueue_popup_assets( $popup_id = 0 ) {
  108. /**
  109. * TODO Replace this with a pum_get_popup function after new Popup model is in place.
  110. *
  111. * $popup = pum_get_popup( $popup_id );
  112. *
  113. * if ( ! pum_is_popup( $popup ) ) {
  114. * return;
  115. * }
  116. */
  117. $popup = new PUM_Popup( $popup_id );
  118. wp_enqueue_script( 'popup-maker-site' );
  119. wp_enqueue_style( 'popup-maker-site' );
  120. if ( $popup->mobile_disabled() || $popup->tablet_disabled() ) {
  121. wp_enqueue_script( 'mobile-detect' );
  122. }
  123. /**
  124. * TODO Implement this in core $popup model & advanced targeting conditions.
  125. *
  126. * if ( $popup->has_condition( array(
  127. * 'device_is_mobile',
  128. * 'device_is_phone',
  129. * 'device_is_tablet',
  130. * 'device_is_brand',
  131. * ) ) ) {
  132. * self::enqueue_script( 'mobile-detect' );
  133. * }
  134. */
  135. }
  136. /**
  137. * Register JS.
  138. */
  139. public static function register_scripts() {
  140. self::$scripts_registered = true;
  141. wp_register_script( 'mobile-detect', self::$js_url . 'vendor/mobile-detect.min.js', null, '1.3.3', true );
  142. wp_register_script( 'iframe-resizer', self::$js_url . 'vendor/iframeResizer.min.js', array( 'jquery' ) );
  143. if ( PUM_AssetCache::enabled() && false !== self::$cache_url ) {
  144. $cached = get_option( 'pum-has-cached-js' );
  145. if ( ! $cached || self::$debug ) {
  146. PUM_AssetCache::cache_js();
  147. $cached = get_option( 'pum-has-cached-js' );
  148. }
  149. wp_register_script( 'popup-maker-site', self::$cache_url . '/' . PUM_AssetCache::generate_cache_filename( 'pum-site-scripts' ) . '.js?defer&generated=' . $cached, array(
  150. 'jquery',
  151. 'jquery-ui-core',
  152. 'jquery-ui-position',
  153. ), Popup_Maker::$VER, true );
  154. } else {
  155. wp_register_script( 'popup-maker-site', self::$js_url . 'site' . self::$suffix . '.js?defer', array(
  156. 'jquery',
  157. 'jquery-ui-core',
  158. 'jquery-ui-position',
  159. ), Popup_Maker::$VER, true );
  160. }
  161. if ( popmake_get_option( 'enable_easy_modal_compatibility_mode', false ) ) {
  162. wp_register_script( 'popup-maker-easy-modal-importer-site', self::$js_url . 'popup-maker-easy-modal-importer-site' . self::$suffix . '?defer', array( 'popup-maker-site' ), POPMAKE_VERSION, true );
  163. }
  164. self::localize_scripts();
  165. }
  166. /**
  167. * Localize scripts if enqueued.
  168. */
  169. public static function localize_scripts() {
  170. $site_home_path = parse_url( home_url() );
  171. $site_home_path = isset( $site_home_path['path'] ) ? $site_home_path['path'] : '/';
  172. wp_localize_script( 'popup-maker-site', 'pum_vars', apply_filters( 'pum_vars', array(
  173. 'version' => Popup_Maker::$VER,
  174. 'pm_dir_url' => Popup_Maker::$URL,
  175. 'ajaxurl' => admin_url( 'admin-ajax.php' ),
  176. 'restapi' => function_exists( 'rest_url' ) ? esc_url_raw( rest_url( 'pum/v1' ) ) : false,
  177. 'rest_nonce' => is_user_logged_in() ? wp_create_nonce( 'wp_rest' ) : null,
  178. 'default_theme' => (string) pum_get_default_theme_id(),
  179. 'debug_mode' => Popup_Maker::debug_mode(),
  180. 'disable_tracking' => popmake_get_option( 'disable_popup_open_tracking' ),
  181. 'home_url' => trailingslashit( $site_home_path ),
  182. 'message_position' => 'top',
  183. 'core_sub_forms_enabled' => ! PUM_Newsletters::$disabled,
  184. 'popups' => array(),
  185. ) ) );
  186. if ( Popup_Maker::debug_mode() || isset( $_GET['pum_debug'] ) ) {
  187. wp_localize_script( 'popup-maker-site', 'pum_debug_vars', apply_filters( 'pum_debug_vars', array(
  188. 'debug_mode_enabled' => __( 'Popup Maker', 'popup-maker' ) . ': ' . __( 'Debug Mode Enabled', 'popup-maker' ),
  189. 'debug_started_at' => __( 'Debug started at:', 'popup-maker' ),
  190. 'debug_more_info' => sprintf( __( 'For more information on how to use this information visit %s', 'popup-maker' ), 'https://docs.wppopupmaker.com/?utm_medium=js-debug-info&utm_campaign=contextual-help&utm_source=browser-console&utm_content=more-info' ),
  191. 'global_info' => __( 'Global Information', 'popup-maker' ),
  192. 'localized_vars' => __( 'Localized variables', 'popup-maker' ),
  193. 'popups_initializing' => __( 'Popups Initializing', 'popup-maker' ),
  194. 'popups_initialized' => __( 'Popups Initialized', 'popup-maker' ),
  195. 'single_popup_label' => __( 'Popup: #', 'popup-maker' ),
  196. 'theme_id' => __( 'Theme ID: ', 'popup-maker' ),
  197. 'label_method_call' => __( 'Method Call:', 'popup-maker' ),
  198. 'label_method_args' => __( 'Method Arguments:', 'popup-maker' ),
  199. 'label_popup_settings' => __( 'Settings', 'popup-maker' ),
  200. 'label_triggers' => __( 'Triggers', 'popup-maker' ),
  201. 'label_cookies' => __( 'Cookies', 'popup-maker' ),
  202. 'label_delay' => __( 'Delay:', 'popup-maker' ),
  203. 'label_conditions' => __( 'Conditions', 'popup-maker' ),
  204. 'label_cookie' => __( 'Cookie:', 'popup-maker' ),
  205. 'label_settings' => __( 'Settings:', 'popup-maker' ),
  206. 'label_selector' => __( 'Selector:', 'popup-maker' ),
  207. 'label_mobile_disabled' => __( 'Mobile Disabled:', 'popup-maker' ),
  208. 'label_tablet_disabled' => __( 'Tablet Disabled:', 'popup-maker' ),
  209. 'label_event' => __( 'Event: %s', 'popup-maker' ),
  210. 'triggers' => PUM_Triggers::instance()->dropdown_list(),
  211. 'cookies' => PUM_Cookies::instance()->dropdown_list(),
  212. ) ) );
  213. }
  214. /* Here for backward compatibility. */
  215. wp_localize_script( 'popup-maker-site', 'pum_sub_vars', array(
  216. 'ajaxurl' => admin_url( 'admin-ajax.php' ),
  217. 'message_position' => 'top',
  218. ) );
  219. }
  220. /**
  221. * Localize late script vars if enqueued.
  222. */
  223. public static function late_localize_scripts() {
  224. // If scripts not rendered, localize these vars. Otherwise echo them manually.
  225. if ( ! wp_script_is( 'popup-maker-site', 'done' ) ) {
  226. wp_localize_script( 'popup-maker-site', 'pum_popups', self::get_popup_settings() );
  227. } else {
  228. echo "<script type='text/javascript'>";
  229. echo 'window.pum_popups = ' . PUM_Utils_Array::safe_json_encode( self::get_popup_settings() ) . ';';
  230. // Backward compatibility fill.
  231. echo 'window.pum_vars = window.pum_vars || {}; window.pum_vars.popups = window.pum_popups;';
  232. echo "</script>";
  233. }
  234. }
  235. /**
  236. * Gets public settings for each popup for a global JS variable.
  237. *
  238. * @return array
  239. */
  240. public static function get_popup_settings() {
  241. $loaded = PUM_Site_Popups::get_loaded_popups();
  242. $settings = array();
  243. $current_popup = pum()->current_popup;
  244. if ( $loaded->have_posts() ) {
  245. while ( $loaded->have_posts() ) : $loaded->next_post();
  246. pum()->current_popup = $loaded->post;
  247. $popup = pum_get_popup( $loaded->post->ID );
  248. // Set the key to the CSS id of this popup for easy lookup.
  249. $settings[ 'pum-' . $popup->ID ] = $popup->get_public_settings();
  250. endwhile;
  251. pum()->current_popup = $current_popup;
  252. }
  253. return $settings;
  254. }
  255. /**
  256. * Register CSS.
  257. */
  258. public static function register_styles() {
  259. self::$styles_registered = true;
  260. if ( PUM_AssetCache::enabled() && false !== self::$cache_url ) {
  261. $cached = get_option( 'pum-has-cached-css' );
  262. if ( ! $cached || self::$debug ) {
  263. PUM_AssetCache::cache_css();
  264. $cached = get_option( 'pum-has-cached-css' );
  265. }
  266. wp_register_style( 'popup-maker-site', self::$cache_url . '/' . PUM_AssetCache::generate_cache_filename( 'pum-site-styles' ) . '.css?generated=' . $cached, array(), Popup_Maker::$VER );
  267. } else {
  268. wp_register_style( 'popup-maker-site', self::$css_url . 'pum-site' . ( is_rtl() ? '-rtl' : '' ) . self::$suffix . '.css', array(), Popup_Maker::$VER );
  269. self::inline_styles();
  270. }
  271. }
  272. /**
  273. * Render popup inline styles.
  274. */
  275. public static function inline_styles() {
  276. if ( ( current_action() == 'wp_head' && popmake_get_option( 'disable_popup_theme_styles', false ) ) || ( current_action() == 'admin_head' && ! popmake_is_admin_popup_page() ) ) {
  277. return;
  278. }
  279. wp_add_inline_style( 'popup-maker-site', PUM_AssetCache::inline_css() );
  280. }
  281. /**
  282. * Defers loading of scripts with ?defer parameter in url.
  283. *
  284. * @param string $url URL being cleaned
  285. *
  286. * @return string $url
  287. */
  288. public static function defer_js_url( $url ) {
  289. if ( false === strpos( $url, '.js?defer' ) ) {
  290. // not our file
  291. return $url;
  292. }
  293. return "$url' defer='defer";
  294. }
  295. /**
  296. *
  297. */
  298. public static function check_force_script_loading() {
  299. global $wp_query;
  300. if ( ! empty( $wp_query->post ) && has_shortcode( $wp_query->post->post_content, 'popup' ) || ( defined( "POPMAKE_FORCE_SCRIPTS" ) && POPMAKE_FORCE_SCRIPTS ) ) {
  301. wp_enqueue_script( 'popup-maker-site' );
  302. wp_enqueue_style( 'popup-maker-site' );
  303. }
  304. }
  305. }