JFIF $ d d 7 * Z qn )ZP 0 !1 A "#a$2Qr D8 a Ri [f \m I ykI w0 c u F c Rı?lO7 к _f˓ [C $殷WF< _W ԣ s Kc ëI zyQy /_LK ℂ ;C",p F A : /] =H ~ , l s / 9 ć: [=/#f ; ) x { ٛE Q )~ = 𘙲r*2~ a _V = ' ku m F D } KYYC )( { * g& f `툪 r y ` = ^ c J. I ]( * `wq 1dđ #̩ ͑ 0 ; H ] u 搂 @:~ ו K L N s h } O I R* 8 :2 ! lD JV o ( 3 = M ( z Ȱ+i *N A r6K n S l ) ! J J Ӂ* % ݉?|D } d5: eP0 R; {$X' x F @ .ÊB {,WJ uQ ɲRI ;9 Q E琯 6 2fT .DU J ;*c P A\IL Nj! J ۱+ O\ ͔ ] ޒ S J ȧ c%A N ol ՎprU L Zԛe r E2= X D Xg VQeӓ k y P7 U* o m Q I s ,K `) 6\G3t?pgjr m ۛجw luGt f h 9uyP 0D;Uڽ"OXl i f $ ) &|ML 0 Z rm1 [ H XP l P R0 'G=i 2N +0 e 2 ]] 9 VT P O 7 h(F * 癈 ' = Q VZD F,d ߬ ~ T X G[ `le 69C R(!S2! P < 0 x < !1AQ "Raq 02Br #S CTb ? Ζ "]mH5WR 7 k.ۛ ! } Q~ + yԏ z |@ T20 S~ K e k *zF f^2X *( @ 8 r ? C I uI|֓>^Ex LgNU Y+{.R Ѫ τV YT D I 6 2 '8Y2 7'\T P.6 d &˦ @V q i |8 -O Ε ] ʔ U= T L 8= ;6c | !qfF3 a ů& ~$ l} 'NW U s$U k^S V : U#6w + +s &r+n ڐ { @ 29gL u " T Ù M=6 (^ "7 r}=6Yݾ lC u hquymp Ǧ G jhs ǜNl ɻ} o 7 #S6 a w 4 ! OS r D 5 7 % | ?x> L | / nD 6?/8 w # [ ) L 7 +6〼T A T g ! %5 Mm Z /c -{1_Je "| ^$ 'O & ޱմ < H vNqϡQ$ < 6 &Nݛ ֨tǻN (9 n ru~'όN sMG& ,uH Kw *Ī 5@ u6 BԒ i YN * f 0 _ GhKW u lT <nj- 0{ M L~ | /a v\ E = ?1 \ɟuq Ђ Y O a K ޡP q $ H8o Q˼EV ~ k 0 h' L Pp M -M" m^ !6l nմ 쮝C U T . ! :vMǨb h ډ Vc r @~p # !1QAa q ? AL0L x LU 4 d ; x " 16ad0 7.\ߋ 0 = P l v/ " c A Ay A|2pO8"d MV #1] = \ " ˡ ] k v I9 d ۓ & q ) ǐq E ꞅ v "P )q X &Q З non]я u ג./ ; gZ$ M QР 2M ]h ni 0 P $ێ 3CɏGjώy : m c # S_ l@ '_ \m|C 2`K _ 1 cIvGc $? PCQ S+D ? K aײ k Fr b 8 ύ NEدg ĝ U 3 6 #Ik+ v { v i ( ވ ? a e) X xy/*@Q Ǽ Ծ ) $ IWO l c G; !i k{ gOŀ L ('x T rb $ w) R $ & N1E td U3Uȉ1p M"N* (DNy d96.(jQ) X 5 c QɎMy W ? Q *!R > 6=7)Xj 5`J ] e 8%t ! + ' ! 1Q 5 !1 AQaq ё #2 "0BRb ? G t^ ## .l lQ T $ v , ,m 㵜5u bV =sY+ @d { N ! dn O < .- B; _w J t 6;Q Jd. Q c%p{ 1 ,sND dF H I0 Гo X ш e黅X ۢ F :)[ F G X ƹ / w _c MeD ,ʡc c . W D tA $ j@ : ) - #u c1< @ۗ9F ) KJ- hpP] _ x [ q B lb p ʖ w q " LFGd ƶ * s +ډ_Zc " ? % t[I P 6 J ]# =ɺ V v v CGs Gh1 > )6 |e y ? L ӣ m,4 G W Ui ` ] uJVo VDG< SB 6ϏQ @ TiU l y O U 0 kfV ~ ~}S Z @ *WU Ui ## ; s /[ = ! 7}" W N ] ' (L ! ~ y5 g9T̅J kbM' +s :S +B) v @ M j e C f j E 0 Y\Qn z G 1 д~ W o{T 9?` Rm yhsy3 !HAD ] m c1 ~ 2 L S u7x T ;j$ `} 4-> L# vzŏ I L S ֭ T { r jGK C ; b pU= - ` Bs K. S F w4 M q] Z dH S 0 ) tLg
<?php
namespace Elementor\Modules\ImageLoadingOptimization;
use Elementor\Core\Base\Module as BaseModule;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Module extends BaseModule {
/**
* @var int Minimum square-pixels threshold.
*/
private $min_priority_img_pixels = 50000;
/**
* @var int The number of content media elements to not lazy-load.
*/
private $omit_threshold = 3;
/**
* @var array Keep a track of images for which loading optimization strategy were computed.
*/
private static $image_visited = [];
/**
* Get Module name.
*/
public function get_name() {
return 'image-loading-optimization';
}
/**
* Constructor.
*/
public function __construct() {
if ( ! static::is_optimized_image_loading_enabled() ) {
return;
}
parent::__construct();
// Stop wp core logic.
add_action( 'init', [ $this, 'stop_core_fetchpriority_high_logic' ] );
add_filter( 'wp_lazy_loading_enabled', '__return_false' );
// Run optimization logic on header.
add_action( 'get_header', [ $this, 'set_buffer' ] );
// Ensure buffer is flushed (if any) before the content logic.
add_filter( 'the_content', [ $this, 'flush_header_buffer' ], 0 );
// Run optimization logic on content.
add_filter( 'wp_content_img_tag', [ $this, 'loading_optimization_image' ] );
}
/**
* Check whether the "Optimized Image Loading" settings is enabled.
*
* The 'optimized_image_loading' option can be enabled/disabled from the Elementor settings.
*
* @since 3.21.0
* @access private
*/
private static function is_optimized_image_loading_enabled(): bool {
return '1' === get_option( 'elementor_optimized_image_loading', '1' );
}
/**
* Stop WordPress core fetchpriority logic by setting the wp_high_priority_element_flag flag to false.
*/
public function stop_core_fetchpriority_high_logic() {
wp_high_priority_element_flag( false );
}
/**
* Set buffer to handle header and footer content.
*/
public function set_buffer() {
ob_start( [ $this, 'handle_buffer_content' ] );
}
/**
* This function ensure that buffer if any is flushed before the content is called.
* This function behaves more like an action than a filter.
*
* @param string $content the content.
* @return string We simply return the content from parameter.
*/
public function flush_header_buffer( $content ) {
$buffer_status = ob_get_status();
if ( ! empty( $buffer_status ) &&
1 === $buffer_status['type'] &&
get_class( $this ) . '::handle_buffer_content' === $buffer_status['name'] ) {
ob_end_flush();
}
return $content;
}
/**
* Callback to handle image optimization logic on buffered content.
*
* @param string $buffer Buffered content.
* @return string Content with optimized images.
*/
public function handle_buffer_content( $buffer ) {
return $this->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(
'/<img\s[^>]+>/',
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( '<img', '<img fetchpriority="' . esc_attr( $optimization_attrs['fetchpriority'] ) . '"', $image );
}
if ( ! empty( $optimization_attrs['loading'] ) ) {
$image = str_replace( '<img', '<img loading="' . esc_attr( $optimization_attrs['loading'] ) . '"', $image );
}
return $image;
}
/**
* Return loading Loading optimization attributes for a image with give attribute.
*
* @param array $attr Existing image attributes.
* @return array Loading optimization attributes.
*/
private function get_loading_optimization_attributes( $attr ) {
$loading_attrs = [];
// For any resources, width and height must be provided, to avoid layout shifts.
if ( ! isset( $attr['width'], $attr['height'] ) ) {
return $loading_attrs;
}
/*
* The key function logic starts here.
*/
$maybe_in_viewport = null;
$increase_count = false;
$maybe_increase_count = false;
/*
* Logic to handle a `loading` attribute that is already provided.
*
* Copied from `wp_get_loading_optimization_attributes()`.
*/
if ( isset( $attr['loading'] ) ) {
/*
* Interpret "lazy" as not in viewport. Any other value can be
* interpreted as in viewport (realistically only "eager" or `false`
* to force-omit the attribute are other potential values).
*/
if ( 'lazy' === $attr['loading'] ) {
$maybe_in_viewport = false;
} else {
$maybe_in_viewport = true;
}
}
// Logic to handle a `fetchpriority` attribute that is already provided.
$has_fetchpriority_high_attr = ( isset( $attr['fetchpriority'] ) && 'high' === $attr['fetchpriority'] );
/*
* Handle cases where a `fetchpriority="high"` has already been set.
*
* Copied from `wp_get_loading_optimization_attributes()`.
*/
if ( $has_fetchpriority_high_attr ) {
/*
* If the image was already determined to not be in the viewport (e.g.
* from an already provided `loading` attribute), trigger a warning.
* Otherwise, the value can be interpreted as in viewport, since only
* the most important in-viewport image should have `fetchpriority` set
* to "high".
*/
if ( false === $maybe_in_viewport ) {
_doing_it_wrong(
__FUNCTION__,
esc_html__( 'An image should not be lazy-loaded and marked as high priority at the same time.', 'elementor' ),
''
);
/*
* Set `fetchpriority` here for backward-compatibility as we should
* not override what a developer decided, even though it seems
* incorrect.
*/
$loading_attrs['fetchpriority'] = 'high';
} else {
$maybe_in_viewport = true;
}
}
if ( null === $maybe_in_viewport && ! is_admin() ) {
$content_media_count = $this->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;
}
}