mes() ) { $args = [ 'youtube' => $this->options->get( 'lazyload_youtube' ), ]; $html = $this->iframe->lazyloadIframes( $html, $buffer, $args ); } if ( $this->can_lazyload_images() ) { if ( ! $this->is_native_images() ) { $html = $this->image->lazyloadPictures( $html, $buffer ); $buffer = $this->hide_scripts( $html ); $buffer = $this->hide_noscripts( $buffer ); } $html = $this->image->lazyloadImages( $html, $buffer, $this->is_native_images() ); /** * Filters the application of lazyload on background images * * @since 3.3 * * @param bool $lazyload True to apply, false otherwise. */ if ( apply_filters( 'rocket_lazyload_background_images', true ) ) { $html = $this->image->lazyloadBackgroundImages( $html, $buffer ); } } return $html; } /** * Applies lazyload on responsive images attributes srcset and sizes * * @since 3.3 * * @param string $html Image HTML. * @return string */ public function lazyload_responsive( $html ) { if ( $this->is_native_images() ) { return $html; } return $this->image->lazyloadResponsiveAttributes( $html ); } /** * Applies lazyload on WordPress smilies * * @since 3.3 * * @return void */ public function lazyload_smilies() { if ( ! $this->should_lazyload() ) { return; } if ( ! $this->options->get( 'lazyload' ) ) { return; } $filters = [ 'the_content' => 10, 'the_excerpt' => 10, 'comment_text' => 20, ]; foreach ( $filters as $filter => $prio ) { if ( ! has_filter( $filter ) ) { continue; } remove_filter( $filter, 'convert_smilies', $prio ); add_filter( $filter, [ $this->image, 'convertSmilies' ], $prio ); } } /** * Prevents lazyload if the option is unchecked on the WP Rocket options metabox for a post * * @since 3.3 * * @return void */ public function deactivate_lazyload_on_specific_posts() { if ( is_rocket_post_excluded_option( 'lazyload' ) ) { add_filter( 'do_rocket_lazyload', '__return_false' ); } if ( is_rocket_post_excluded_option( 'lazyload_iframes' ) ) { add_filter( 'do_rocket_lazyload_iframes', '__return_false' ); } } /** * Disable WP core lazyload if our images lazyload is active * * @since 3.5 * * @param bool $value Current value for the enabling variable. * @param string $tag_name The tag name. * * @return bool */ public function maybe_disable_core_lazyload( $value, $tag_name ) { if ( false === $value || rocket_bypass() ) { return $value; } if ( 'img' === $tag_name ) { return ! (bool) $this->can_lazyload_images(); } if ( 'iframe' === $tag_name ) { return ! (bool) $this->can_lazyload_iframes(); } return $value; } /** * Adds the exclusions from the options to the exclusions arrays * * @since 3.8 * * @param array $exclusions Array of excluded patterns. * @return array */ public function add_exclusions( $exclusions ): array { if ( ! is_array( $exclusions ) ) { $exclusions = []; } $exclude_lazyload = $this->options->get( 'exclude_lazyload', [] ); if ( empty( $exclude_lazyload ) ) { return $exclusions; } return array_unique( array_merge( $exclusions, $exclude_lazyload ) ); } /** * Checks if we can lazyload images. * * @since 3.3 * * @return boolean */ private function can_lazyload_images() { if ( ! $this->options->get( 'lazyload', 0 ) ) { return false; } /** * Filters the lazyload application on images * * @since 2.0 * * @param bool $do_rocket_lazyload True to apply lazyload, false otherwise. */ return apply_filters( 'do_rocket_lazyload', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals } /** * Checks if we can lazyload iframes * * @since 3.3 * * @return boolean */ private function can_lazyload_iframes() { if ( ! $this->options->get( 'lazyload_iframes', 0 ) ) { return false; } /** * Filters the lazyload application on iframes * * @since 2.0 * * @param bool $do_rocket_lazyload_iframes True to apply lazyload, false otherwise. */ return apply_filters( 'do_rocket_lazyload_iframes', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals } /** * Checks if native lazyload is enabled for images * * @since 3.10.2 * * @return bool */ private function is_native_images(): bool { /** * Filters the use of native lazyload for images * * @since 3.10.2 * * @param bool $use_native True to use native lazyload for images, false otherwise. */ return (bool) apply_filters( 'rocket_use_native_lazyload_images', false ); } }