is_bot() ) { return; } $options = new \AjaxPress\Options(); $settings = $options->get_settings( true ); if ( ! empty( $settings['disable_for_admins'] ) && current_user_can( 'manage_options' ) ) { return; } if ( ! apply_filters( 'ajaxpress_navigation', true ) ) { return; } if ( apply_filters( 'ajaxpress_keep_block_navigation', false ) ) { return; } wp_dequeue_script_module( '@wordpress/block-library/query/view' ); wp_dequeue_script_module( '@wordpress/interactivity-router' ); } /** * Enqueue frontend scripts. * * @return void */ public function enqueue_scripts() { // Skip for bots/crawlers. if ( $this->is_bot() ) { return; } $options = new \AjaxPress\Options(); $settings = $options->get_settings( true ); // Skip SPA for logged-in admins if setting is enabled. if ( ! empty( $settings['disable_for_admins'] ) && current_user_can( 'manage_options' ) ) { return; } // Enqueue CSS. wp_enqueue_style( 'ajaxpress', AJAXPRESS_URL . 'public/css/ajaxpress.min.css', array(), AJAXPRESS_VERSION ); // Inline CSS. $inline_css = $this->get_inline_css(); wp_add_inline_style( 'ajaxpress', $inline_css ); // Enqueue JS in footer. wp_enqueue_script( 'ajaxpress', AJAXPRESS_URL . 'public/js/ajaxpress.min.js', array(), AJAXPRESS_VERSION, true ); // Inline script. $inline_script = $this->get_inline_script(); wp_localize_script( 'ajaxpress', 'ajaxpress_vars', $inline_script ); } /** * Enqueue the SPA on wp-login.php. * * The wp_enqueue_scripts hook does not fire on wp-login.php, so without * this the login screen is the one place on the site that never gets * AjaxPress and always hard-reloads. Loading the same bundle makes it a * normal SPA page: the form posts through the navigation iframe, and a * failed attempt re-renders without a reload. * * Opt out with the `ajaxpress_ajax_login` filter, or by turning on the * full page reload setting for auth forms. * * @return bool */ public function enqueue_login_scripts() { if ( $this->is_bot() ) { return false; } // The interim-login / auth-check modal loads wp-login.php inside a // small iframe in wp-admin. Wrapping that in the SPA (and // intercepting its submit) breaks the modal's own flow - it must // stay plain WordPress. if ( isset( $_GET['interim-login'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended return false; } $options = new \AjaxPress\Options(); $settings = $options->get_settings( true ); // Master SPA switch also governs the login screen. if ( empty( $settings['enable_navigation'] ) ) { return false; } // Respect the opt-in full page reload for auth forms. if ( ! empty( $settings['bypass_auth_forms'] ) ) { return false; } if ( ! apply_filters( 'ajaxpress_ajax_login', true ) ) { return false; } wp_enqueue_style( 'ajaxpress', AJAXPRESS_URL . 'public/css/ajaxpress.min.css', array(), AJAXPRESS_VERSION ); wp_add_inline_style( 'ajaxpress', $this->get_inline_css() ); wp_enqueue_script( 'ajaxpress', AJAXPRESS_URL . 'public/js/ajaxpress.min.js', array(), AJAXPRESS_VERSION, true ); wp_localize_script( 'ajaxpress', 'ajaxpress_vars', $this->get_inline_script() ); return true; } /** * Get Inline Script. * * @return array */ public function get_inline_script() { $options = new \AjaxPress\Options(); $license = get_option( 'ajaxpress_license', [] ); $settings = $options->get_settings( true ); // The frontend only ever needs a validity flag. The full license // record (key, plan, activation counts) stays admin-side: anyone // can read frontend page source, and a leaked key can consume the // customer's activation seats. $is_license_active = ! empty( $license['key'] ) && 'active' === ( isset( $license['status'] ) ? $license['status'] : '' ); $script = array( 'rest' => array( 'url' => rest_url('ajaxpress'), 'nonce' => wp_create_nonce('wp_rest'), ), 'site' => array( 'url' => get_site_url(), 'name' => get_bloginfo('name'), 'language' => get_bloginfo('language'), ), 'plugin' => array( 'url' => plugin_dir_url(AJAXPRESS), 'version' => AJAXPRESS_VERSION, ), 'settings' => $settings, 'navigation' => apply_filters( 'ajaxpress_navigation', true ), 'user_logged_in' => is_user_logged_in(), 'license' => array( 'valid' => $is_license_active, ), 'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG, ); /** * Filters the variables handed to the frontend bundle as ajaxpress_vars. * * @param array $script Frontend variables. */ return apply_filters( 'ajaxpress_frontend_vars', $script ); } /** * Get Inline CSS. * * @return string */ public function get_inline_css() { $settings = Options::get_instance()->get_settings( true ); /** * Filters the cursor and animation timing written into the inline CSS. * * @param array $vars { cursor: CSS cursor, duration: seconds }. * @param array $settings Public settings. */ $vars = apply_filters( 'ajaxpress_inline_css_vars', array( 'cursor' => 'wait', 'duration' => 0.3, ), $settings ); $css = wp_sprintf( ' :root { --ajaxpress-cursor-mode: %s; --ajaxpress-animation-duration: %ss; }', $vars['cursor'], $vars['duration'] ); // Content animations. $css .= ' .ajaxpress-animate-fade { animation: ajaxpress-fade var(--ajaxpress-animation-duration) ease-in; } .ajaxpress-animate-slide-up { animation: ajaxpress-slide-up var(--ajaxpress-animation-duration) ease-out; } .ajaxpress-animate-slide-down { animation: ajaxpress-slide-down var(--ajaxpress-animation-duration) ease-out; } @keyframes ajaxpress-fade { from { opacity: 0; } to { opacity: 1; } } @keyframes ajaxpress-slide-up { from { transform: translateY(30px); opacity: 0; } to { transform: translateY(0); opacity: 1; } } @keyframes ajaxpress-slide-down { from { transform: translateY(-30px); opacity: 0; } to { transform: translateY(0); opacity: 1; } } .ajaxpress-animate-cursor { cursor: var(--ajaxpress-cursor-mode) !important; } .ajaxpress-animate-cursor * { cursor: var(--ajaxpress-cursor-mode) !important; } '; return apply_filters( 'ajaxpress_inline_css', $css ); } /** * Check if current request is from a bot/crawler. * * @return bool */ private function is_bot() { if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) { return false; } $ua = strtolower( sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) ); $bots = array( 'googlebot', 'bingbot', 'slurp', 'duckduckbot', 'baiduspider', 'yandexbot', 'facebookexternalhit', 'linkedinbot', 'twitterbot', 'applebot', 'semrushbot', 'ahrefsbot', 'mj12bot', 'dotbot', 'petalbot', 'bytespider', ); foreach ( $bots as $bot ) { if ( strpos( $ua, $bot ) !== false ) { return true; } } return false; } } // Start. Enqueues::start(); }