芝麻web文件管理V1.00
编辑当前文件:/home/royashxg/www/wp-content/plugins/elementor/modules/image-loading-optimization/module.php
filter_images( $buffer ); } /** * Check for image in the content provided and apply optimization logic on them. * * @param string $content Content to be analyzed. * @return string Content with optimized images. */ private function filter_images( $content ) { return preg_replace_callback( '/
]+>/', function ( $matches ) { return $this->loading_optimization_image( $matches[0] ); }, $content ); } /** * Apply loading optimization logic on the image. * * @param mixed $image Original image tag. * @return string Optimized image. */ public function loading_optimization_image( $image ) { if ( isset( self::$image_visited[ $image ] ) ) { return self::$image_visited[ $image ]; } $optimized_image = $this->add_loading_optimization_attrs( $image ); self::$image_visited[ $image ] = $optimized_image; return $optimized_image; } /** * Adds optimization attributes to an `img` HTML tag. * * @param string $image The HTML `img` tag where the attribute should be added. * @return string Converted `img` tag with optimization attributes added. */ private function add_loading_optimization_attrs( $image ) { $width = preg_match( '/ width=["\']([0-9]+)["\']/', $image, $match_width ) ? (int) $match_width[1] : null; $height = preg_match( '/ height=["\']([0-9]+)["\']/', $image, $match_height ) ? (int) $match_height[1] : null; $loading_val = preg_match( '/ loading=["\']([A-Za-z]+)["\']/', $image, $match_loading ) ? $match_loading[1] : null; $fetchpriority_val = preg_match( '/ fetchpriority=["\']([A-Za-z]+)["\']/', $image, $match_fetchpriority ) ? $match_fetchpriority[1] : null; // Images should have height and dimension width for the loading optimization attributes to be added. if ( ! str_contains( $image, ' width="' ) || ! str_contains( $image, ' height="' ) ) { return $image; } $optimization_attrs = $this->get_loading_optimization_attributes( [ 'width' => $width, 'height' => $height, 'loading' => $loading_val, 'fetchpriority' => $fetchpriority_val, ] ); if ( ! empty( $optimization_attrs['fetchpriority'] ) ) { $image = str_replace( '
increase_content_media_count( 0 ); $increase_count = true; if ( $content_media_count < $this->omit_threshold ) { $maybe_in_viewport = true; } else { $maybe_in_viewport = false; } } if ( $maybe_in_viewport ) { $loading_attrs = $this->maybe_add_fetchpriority_high_attr( $loading_attrs, $attr ); } else { $loading_attrs['loading'] = 'lazy'; } if ( $increase_count ) { $this->increase_content_media_count(); } elseif ( $maybe_increase_count ) { if ( $this->get_min_priority_img_pixels() <= $attr['width'] * $attr['height'] ) { $this->increase_content_media_count(); } } return $loading_attrs; } /** * Helper to get the minimum threshold for number of pixels an image needs to have to be considered "priority". * * @return int The minimum number of pixels (width * height). Default is 50000. */ private function get_min_priority_img_pixels() { /** * Filter the minimum pixel threshold used to determine if an image should have fetchpriority="high" applied. * * @see https://developer.wordpress.org/reference/hooks/wp_min_priority_img_pixels/ * * @param int $pixels The minimum number of pixels (with * height). * @return int The filtered value. */ return apply_filters( 'elementor/image-loading-optimization/min_priority_img_pixels', $this->min_priority_img_pixels ); } /** * Keeps a count of media image. * * @param int $amount Amount by which count must be increased. * @return int current image count. */ private function increase_content_media_count( $amount = 1 ) { static $content_media_count = 0; $content_media_count += $amount; return $content_media_count; } /** * Determines whether to add `fetchpriority='high'` to loading attributes. * * @param array $loading_attrs Array of the loading optimization attributes for the element. * @param array $attr Array of the attributes for the element. * @return array Updated loading optimization attributes for the element. */ private function maybe_add_fetchpriority_high_attr( $loading_attrs, $attr ) { if ( isset( $attr['fetchpriority'] ) ) { if ( 'high' === $attr['fetchpriority'] ) { $loading_attrs['fetchpriority'] = 'high'; $this->high_priority_element_flag( false ); } return $loading_attrs; } // Lazy-loading and `fetchpriority="high"` are mutually exclusive. if ( isset( $loading_attrs['loading'] ) && 'lazy' === $loading_attrs['loading'] ) { return $loading_attrs; } if ( ! $this->high_priority_element_flag() ) { return $loading_attrs; } if ( $this->get_min_priority_img_pixels() <= $attr['width'] * $attr['height'] ) { $loading_attrs['fetchpriority'] = 'high'; $this->high_priority_element_flag( false ); } return $loading_attrs; } /** * Accesses a flag that indicates if an element is a possible candidate for `fetchpriority='high'`. * * @param bool $value Optional. Used to change the static variable. Default null. * @return bool Returns true if high-priority element was marked already, otherwise false. */ private function high_priority_element_flag( $value = null ) { static $high_priority_element = true; if ( is_bool( $value ) ) { $high_priority_element = $value; } return $high_priority_element; } }