options = get_option( 'aio_options', array() ); $this->site_host = strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ); $this->home_path_prefix = rtrim( (string) wp_parse_url( home_url( '/' ), PHP_URL_PATH ), '/' ); if ( ! empty( $this->options['optEnableSmartLcp'] ) ) { add_action( 'wp_head', array( $this, 'output_lcp_featured_preload' ), 1 ); } if ( ! empty( $this->options['optCriticalCss'] ) ) { add_action( 'wp_head', array( $this, 'output_critical_css' ), 2 ); } if ( ! empty( $this->options['optEnableInstantPage'] ) ) { add_action( 'wp_footer', array( $this, 'output_instant_page_prefetch' ), 99 ); } if ( $this->needs_html_buffer() ) { add_action( 'template_redirect', array( $this, 'start_html_buffer' ), 1 ); } } private function needs_html_buffer() { $keys = array( 'optEnableSmartLcp', 'opt-perf-lazyload', 'optLazyLoadBg', 'optEnableAutoImageDims', 'optEnableWebpRewrite', 'optEnableFontSwap', 'optAsyncGoogleFonts', 'optSelfHostGoogleFonts', 'optEnableAsyncCss', 'optEnableYoutubeFacade', 'optEnableDelayJS', 'optEnableCdn', 'optEnableMinify', ); foreach ( $keys as $key ) { if ( ! empty( $this->options[ $key ] ) ) { return true; } } return false; } private function is_optimizable_request() { if ( is_admin() || aio_is_speed_safe_mode() ) { return false; } if ( ( function_exists( 'wp_doing_ajax' ) && wp_doing_ajax() ) || ( function_exists( 'wp_doing_cron' ) && wp_doing_cron() ) ) { return false; } if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { return false; } if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) { return false; } if ( is_feed() || is_preview() || is_customize_preview() || is_robots() || is_trackback() ) { return false; } if ( get_query_var( 'aio_sitemap' ) ) { return false; } if ( is_user_logged_in() ) { return false; } return true; } public function output_lcp_featured_preload() { if ( is_admin() || aio_is_speed_safe_mode() || ! is_singular() ) { return; } $post_id = get_queried_object_id(); if ( ! $post_id ) { return; } $thumb_id = 0; $size = 'large'; $sizes = ''; $is_section_bg = false; if ( is_page() ) { $post_obj = get_post( $post_id ); $content = $post_obj ? (string) $post_obj->post_content : ''; if ( preg_match( '/\[section\b([^\]]*)\bbg=["\']?(\d+)["\']?([^\]]*)\]/i', substr( $content, 0, 1500 ), $m ) ) { $thumb_id = (int) $m[2]; $sec_attrs = $m[1] . ' ' . $m[3]; $size = 'full'; if ( preg_match( '/\bbg_size=["\']?([a-z0-9_-]+)["\']?/i', $sec_attrs, $sm ) && strtolower( $sm[1] ) !== 'original' ) { $size = strtolower( $sm[1] ); } $is_section_bg = true; } else { return; } } else { if ( ! has_post_thumbnail( $post_id ) ) { return; } $thumb_id = (int) get_post_thumbnail_id( $post_id ); } if ( ! $thumb_id ) { return; } $src = wp_get_attachment_image_url( $thumb_id, $size ); if ( empty( $src ) ) { return; } $srcset = $is_section_bg ? '' : wp_get_attachment_image_srcset( $thumb_id, $size ); if ( ! $is_section_bg && '' === $sizes ) { $sizes = (string) wp_get_attachment_image_sizes( $thumb_id, $size ); } if ( ! empty( $this->options['optEnableWebpRewrite'] ) || ! empty( $this->options['opt-perf-webp'] ) ) { $src = $this->maybe_rewrite_url_to_webp( $src ); if ( ! empty( $srcset ) ) { $srcset = $this->maybe_rewrite_srcset_to_webp( $srcset ); } } echo '' . "\n"; } /** * Output user-configured Above-the-Fold Critical CSS early in
. */ public function output_critical_css() { if ( ! $this->is_optimizable_request() ) { return; } $css = trim( (string) $this->options['optCriticalCss'] ); if ( $css === '' ) { return; } // Prevent any accidental breakout inside user CSS $safe_css = preg_replace( '#\s*style#i', '', $css ); echo '' . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } public function output_instant_page_prefetch() { if ( ! $this->is_optimizable_request() ) { return; } $rules = array( 'prefetch' => array( array( 'source' => 'document', 'where' => array( 'and' => array( array( 'href_matches' => '/*' ), array( 'not' => array( 'href_matches' => array( '/wp-admin/*', '/wp-login.php*', '/*add-to-cart=*', '/*remove_item=*', '/*logout*', '/cart/*', '/checkout/*', '/my-account/*', '/gio-hang/*', '/thanh-toan/*', '/tai-khoan/*', '/*\\?*', ), ), ), array( 'not' => array( 'selector_matches' => '.no-prefetch, [download], [rel~=nofollow], .add_to_cart_button, .ajax_add_to_cart', ), ), ), ), 'eagerness' => 'moderate', ), ), ); echo '' . "\n"; ?> is_optimizable_request() ) { ob_start( array( $this, 'transform_html' ) ); } } /** * Main single-pass HTML transformation callback. */ public function transform_html( $html ) { if ( empty( $html ) || strlen( $html ) < 200 || stripos( $html, 'options['optEnableFontSwap'] ); $async_font_on = ! empty( $this->options['optAsyncGoogleFonts'] ); $self_host_font_on = ! empty( $this->options['optSelfHostGoogleFonts'] ) && class_exists( 'AIO_Font_Optimizer' ); $async_css_on = ! empty( $this->options['optEnableAsyncCss'] ); $yt_facade_on = ! empty( $this->options['optEnableYoutubeFacade'] ); $smart_lcp_on = ! empty( $this->options['optEnableSmartLcp'] ); $lazyload_on = ! empty( $this->options['opt-perf-lazyload'] ); $lazy_bg_on = ! empty( $this->options['optLazyLoadBg'] ); $auto_dims_on = ! empty( $this->options['optEnableAutoImageDims'] ); $webp_rewrite_on = ! empty( $this->options['optEnableWebpRewrite'] ) || ! empty( $this->options['opt-perf-webp'] ); $delay_js_on = ! empty( $this->options['optEnableDelayJS'] ); $cdn_on = ! empty( $this->options['optEnableCdn'] ) && ! empty( $this->options['txtCdnUrl'] ); $minify_on = ! empty( $this->options['optEnableMinify'] ); $yt_count = 0; $bg_lazy_count = 0; $font_detected = false; $font_preloads = array(); // 1. Protect '; if ( stripos( $working, '' ) !== false ) { $working = str_ireplace( '', $yt_css . "\n", $working ); } else { $working = $yt_css . "\n" . $working; } if ( stripos( $working, '