admin_url( 'admin-ajax.php' ), 'message_position' => 'top', ] ); } } /** * Checks the current page content for the newsletter shortcode. */ public static function enqueue_page_assets() { global $post; if ( ! empty( $post ) && isset( $post->post_content ) && has_shortcode( $post->post_content, 'pum_sub_form' ) ) { wp_enqueue_script( 'popup-maker-site' ); wp_enqueue_style( 'popup-maker-site' ); } } /** * @param int $popup_id */ public static function enqueue_popup_assets( $popup_id = 0 ) { /** * TODO Replace this with a pum_get_popup function after new Popup model is in place. * * $popup = pum_get_popup( $popup_id ); * * if ( ! pum_is_popup( $popup ) ) { * return; * } */ $popup = new PUM_Popup( $popup_id ); wp_enqueue_script( 'popup-maker-site' ); wp_enqueue_style( 'popup-maker-site' ); if ( $popup->mobile_disabled() || $popup->tablet_disabled() ) { wp_enqueue_script( 'mobile-detect' ); } /* * TODO Implement this in core $popup model & advanced targeting conditions. * * if ( $popup->has_condition( array( * 'device_is_mobile', * 'device_is_phone', * 'device_is_tablet', * 'device_is_brand', * ) ) ) { * self::enqueue_script( 'mobile-detect' ); * } */ } /** * Register JS. */ public static function register_scripts() { self::$scripts_registered = true; wp_register_script( 'mobile-detect', self::$js_url . 'vendor/mobile-detect.min.js', null, '1.3.3', true ); wp_register_script( 'iframe-resizer', self::$js_url . 'vendor/iframeResizer.min.js', [ 'jquery' ], '4.3.1', false ); if ( PUM_AssetCache::enabled() && false !== self::$cache_url ) { $cached = get_option( 'pum-has-cached-js' ); if ( ! $cached || self::$debug ) { PUM_AssetCache::cache_js(); $cached = get_option( 'pum-has-cached-js' ); } // Get base dependencies and merge with collected dependencies from bundled scripts. $base_deps = [ 'jquery', 'jquery-ui-core', 'jquery-ui-position', 'wp-hooks', ]; $bundled_deps = PUM_AssetCache::get_bundled_script_dependencies(); $merged_deps = array_unique( array_merge( $base_deps, $bundled_deps ) ); $cached_script_deps = apply_filters( 'pum_site_cached_scripts_dependencies', $merged_deps ); wp_register_script( 'popup-maker-site', self::$cache_url . '/' . PUM_AssetCache::generate_cache_filename( 'pum-site-scripts' ) . '.js?defer&generated=' . $cached, $cached_script_deps, Popup_Maker::$VER, true ); do_action( 'pum_registered_cached_scripts' ); } else { wp_register_script( 'popup-maker-site', self::$js_url . 'site.js?defer', [ 'jquery', 'jquery-ui-core', 'jquery-ui-position', ], Popup_Maker::$VER, true ); } do_action( 'pum_registered_scripts' ); if ( popmake_get_option( 'enable_easy_modal_compatibility_mode', false ) ) { wp_register_script( 'popup-maker-easy-modal-importer-site', self::$js_url . 'popup-maker-easy-modal-importer-site.js?defer', [ 'popup-maker-site' ], POPMAKE_VERSION, true ); } self::localize_scripts(); } /** * Localize scripts if enqueued. */ public static function localize_scripts() { $site_home_path = wp_parse_url( home_url() ); $site_home_path = isset( $site_home_path['path'] ) ? $site_home_path['path'] : '/'; wp_localize_script( 'popup-maker-site', 'pum_vars', apply_filters( 'pum_vars', [ 'version' => Popup_Maker::$VER, 'pm_dir_url' => Popup_Maker::$URL, 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'restapi' => function_exists( 'rest_url' ) ? esc_url_raw( rest_url( 'pum/v1' ) ) : false, 'rest_nonce' => is_user_logged_in() ? wp_create_nonce( 'wp_rest' ) : null, 'default_theme' => (string) pum_get_default_theme_id(), 'debug_mode' => Popup_Maker::debug_mode(), 'disable_tracking' => popmake_get_option( 'disable_popup_open_tracking' ), 'home_url' => trailingslashit( $site_home_path ), 'message_position' => 'top', 'core_sub_forms_enabled' => ! PUM_Newsletters::$disabled, 'popups' => [], 'cookie_domain' => apply_filters( 'pum_cookie_domain', '' ), ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( Popup_Maker::debug_mode() || isset( $_GET['pum_debug'] ) ) { wp_localize_script( 'popup-maker-site', 'pum_debug_vars', apply_filters( 'pum_debug_vars', [ 'debug_mode_enabled' => __( 'Popup Maker', 'popup-maker' ) . ': ' . __( 'Debug Mode Enabled', 'popup-maker' ), 'debug_started_at' => __( 'Debug started at:', 'popup-maker' ), 'debug_more_info' => sprintf( /* translators: 1. URL to documentation. */ __( 'For more information on how to use this information visit %s', 'popup-maker' ), 'https://wppopupmaker.com/docs/?utm_medium=js-debug-info&utm_campaign=contextual-help&utm_source=browser-console&utm_content=more-info' ), 'global_info' => __( 'Global Information', 'popup-maker' ), 'localized_vars' => __( 'Localized variables', 'popup-maker' ), 'popups_initializing' => __( 'Popups Initializing', 'popup-maker' ), 'popups_initialized' => __( 'Popups Initialized', 'popup-maker' ), 'single_popup_label' => __( 'Popup: #', 'popup-maker' ), 'theme_id' => __( 'Theme ID: ', 'popup-maker' ), 'label_method_call' => __( 'Method Call:', 'popup-maker' ), 'label_method_args' => __( 'Method Arguments:', 'popup-maker' ), 'label_popup_settings' => __( 'Settings', 'popup-maker' ), 'label_triggers' => __( 'Triggers', 'popup-maker' ), 'label_cookies' => __( 'Cookies', 'popup-maker' ), 'label_delay' => __( 'Delay:', 'popup-maker' ), 'label_conditions' => __( 'Conditions', 'popup-maker' ), 'label_cookie' => __( 'Cookie:', 'popup-maker' ), 'label_settings' => __( 'Settings:', 'popup-maker' ), 'label_selector' => __( 'Selector:', 'popup-maker' ), 'label_mobile_disabled' => __( 'Mobile Disabled:', 'popup-maker' ), 'label_tablet_disabled' => __( 'Tablet Disabled:', 'popup-maker' ), /* translators: 1. Event name. */ 'label_event' => __( 'Event: %s', 'popup-maker' ), 'triggers' => PUM_Triggers::instance()->dropdown_list(), 'cookies' => PUM_Cookies::instance()->dropdown_list(), ] ) ); } /* Here for backward compatibility. */ wp_localize_script( 'popup-maker-site', 'pum_sub_vars', [ 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'message_position' => 'top', ] ); } /** * Localize late script vars if enqueued. */ public static function late_localize_scripts() { // If scripts not rendered, localize these vars. Otherwise echo them manually. if ( ! wp_script_is( 'popup-maker-site', 'done' ) ) { wp_localize_script( 'popup-maker-site', 'pum_popups', self::get_popup_settings() ); } else { echo "'; } } /** * Gets public settings for each popup for a global JS variable. * * @return array */ public static function get_popup_settings() { $popups = \PopupMaker\plugin()->get_controller( 'Frontend\Popups' )->get_loaded_popups(); $settings = []; // Get current popup to restore later. $current_popup = \PopupMaker\get_current_popup(); if ( $popups ) { foreach ( $popups as $popup ) { \PopupMaker\set_current_popup( $popup ); // Set the key to the CSS id of this popup for easy lookup. $settings[ 'pum-' . $popup->ID ] = $popup->get_public_settings(); } // Restore current popup. \PopupMaker\set_current_popup( $current_popup ); } return $settings; } /** * Register CSS. */ public static function register_styles() { self::$styles_registered = true; if ( PUM_AssetCache::enabled() && false !== self::$cache_url ) { $cached = get_option( 'pum-has-cached-css' ); if ( ! $cached || self::$debug ) { PUM_AssetCache::cache_css(); $cached = get_option( 'pum-has-cached-css' ); } // Get collected dependencies from bundled styles. $bundled_style_deps = PUM_AssetCache::get_bundled_style_dependencies(); $cached_style_deps = apply_filters( 'pum_site_cached_styles_dependencies', $bundled_style_deps ); wp_register_style( 'popup-maker-site', self::$cache_url . '/' . PUM_AssetCache::generate_cache_filename( 'pum-site-styles' ) . '.css?generated=' . $cached, $cached_style_deps, Popup_Maker::$VER ); do_action( 'pum_registered_cached_styles' ); } else { wp_register_style( 'popup-maker-site', self::$css_url . 'site' . ( is_rtl() ? '-rtl' : '' ) . '.css', [], Popup_Maker::$VER ); self::inline_styles(); } do_action( 'pum_registered_styles' ); } /** * Render popup inline styles. */ public static function inline_styles() { if ( ( current_action() === 'wp_head' && popmake_get_option( 'disable_popup_theme_styles', false ) ) || ( current_action() === 'admin_head' && ! popmake_is_admin_popup_page() ) ) { return; } wp_add_inline_style( 'popup-maker-site', PUM_AssetCache::inline_css() ); } /** * Defers loading of scripts with ?defer parameter in url. * * @param string $url URL being cleaned * * @return string $url */ public static function defer_js_url( $url ) { if ( false === strpos( $url, '.js?defer' ) ) { // not our file return $url; } return "$url' defer='defer"; } /** * */ public static function check_force_script_loading() { global $wp_query; if ( ( ! empty( $wp_query->post ) && has_shortcode( $wp_query->post->post_content, 'popup' ) ) || ( defined( 'POPMAKE_FORCE_SCRIPTS' ) && POPMAKE_FORCE_SCRIPTS ) ) { wp_enqueue_script( 'popup-maker-site' ); wp_enqueue_style( 'popup-maker-site' ); } } }