← All changes
|
modules/image-loading-optimization/module.php
+37
-20
4.2.2
→
3.20.0-dev2
View file →
| @@ -1,15 +1,21 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Elementor\Modules\ImageLoadingOptimization; |
| 3 | 3 | |
| 4 | 4 | use Elementor\Core\Base\Module as BaseModule; |
| 5 | +use Elementor\Core\Experiments\Manager as Experiments_Manager; | |
| 5 | 6 | |
| 6 | 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | - exit; // Exit if accessed directly. | |
| 8 | + exit; // Exit if accessed directly | |
| 8 | 9 | } |
| 9 | 10 | |
| 10 | 11 | class Module extends BaseModule { |
| 11 | 12 | /** |
| 13 | + * @var string The experiment name. | |
| 14 | + */ | |
| 15 | + const EXPERIMENT_NAME = 'e_image_loading_optimization'; | |
| 16 | + | |
| 17 | + /** | |
| 12 | 18 | * @var int Minimum square-pixels threshold. |
| 13 | 19 | */ |
| 14 | 20 | private $min_priority_img_pixels = 50000; |
| 15 | 21 | |
| @@ -30,15 +36,33 @@ | ||
| 30 | 36 | return 'image-loading-optimization'; |
| 31 | 37 | } |
| 32 | 38 | |
| 33 | 39 | /** |
| 40 | + * Get experimental data. | |
| 41 | + * | |
| 42 | + * @return array Experimental settings. | |
| 43 | + */ | |
| 44 | + public static function get_experimental_data() { | |
| 45 | + return [ | |
| 46 | + 'name' => static::EXPERIMENT_NAME, | |
| 47 | + 'title' => esc_html__( 'Optimize Image Loading', 'elementor' ), | |
| 48 | + 'tag' => esc_html__( 'Performance', 'elementor' ), | |
| 49 | + 'description' => sprintf( | |
| 50 | + /* translators: 1: fetchpriority attribute, 2: lazy loading attribute. */ | |
| 51 | + esc_html__( 'Applying %1$s on LCP image and %2$s on images below the fold to improve performance scores.', 'elementor' ), | |
| 52 | + '<code>fetchpriority="high"</code>', | |
| 53 | + '<code>loading="lazy"</code>' | |
| 54 | + ), | |
| 55 | + 'generator_tag' => true, | |
| 56 | + 'release_status' => Experiments_Manager::RELEASE_STATUS_STABLE, | |
| 57 | + 'default' => Experiments_Manager::STATE_ACTIVE, | |
| 58 | + ]; | |
| 59 | + } | |
| 60 | + | |
| 61 | + /** | |
| 34 | 62 | * Constructor. |
| 35 | 63 | */ |
| 36 | 64 | public function __construct() { |
| 37 | - if ( ! static::is_optimized_image_loading_enabled() ) { | |
| 38 | - return; | |
| 39 | - } | |
| 40 | - | |
| 41 | 65 | parent::__construct(); |
| 42 | 66 | |
| 43 | 67 | // Stop wp core logic. |
| 44 | 68 | add_action( 'init', [ $this, 'stop_core_fetchpriority_high_logic' ] ); |
| @@ -51,20 +75,11 @@ | ||
| 51 | 75 | add_filter( 'the_content', [ $this, 'flush_header_buffer' ], 0 ); |
| 52 | 76 | |
| 53 | 77 | // Run optimization logic on content. |
| 54 | 78 | add_filter( 'wp_content_img_tag', [ $this, 'loading_optimization_image' ] ); |
| 55 | - } | |
| 56 | 79 | |
| 57 | - /** | |
| 58 | - * Check whether the "Optimized Image Loading" settings is enabled. | |
| 59 | - * | |
| 60 | - * The 'optimized_image_loading' option can be enabled/disabled from the Elementor settings. | |
| 61 | - * | |
| 62 | - * @since 3.21.0 | |
| 63 | - * @access private | |
| 64 | - */ | |
| 65 | - private static function is_optimized_image_loading_enabled(): bool { | |
| 66 | - return '1' === get_option( 'elementor_optimized_image_loading', '1' ); | |
| 80 | + // Run optimization logic on footer. Flushing of footer buffer will be handled by PHP script end default logic. | |
| 81 | + add_action( 'get_footer', [ $this, 'set_buffer' ] ); | |
| 67 | 82 | } |
| 68 | 83 | |
| 69 | 84 | /** |
| 70 | 85 | * Stop WordPress core fetchpriority logic by setting the wp_high_priority_element_flag flag to false. |
| @@ -69,9 +84,12 @@ | ||
| 69 | 84 | /** |
| 70 | 85 | * Stop WordPress core fetchpriority logic by setting the wp_high_priority_element_flag flag to false. |
| 71 | 86 | */ |
| 72 | 87 | public function stop_core_fetchpriority_high_logic() { |
| 73 | - wp_high_priority_element_flag( false ); | |
| 88 | + // wp_high_priority_element_flag was only introduced in 6.3.0 | |
| 89 | + if ( function_exists( 'wp_high_priority_element_flag' ) ) { | |
| 90 | + wp_high_priority_element_flag( false ); | |
| 91 | + } | |
| 74 | 92 | } |
| 75 | 93 | |
| 76 | 94 | /** |
| 77 | 95 | * Set buffer to handle header and footer content. |
| @@ -239,9 +257,8 @@ | ||
| 239 | 257 | __FUNCTION__, |
| 240 | 258 | esc_html__( 'An image should not be lazy-loaded and marked as high priority at the same time.', 'elementor' ), |
| 241 | 259 | '' |
| 242 | 260 | ); |
| 243 | - | |
| 244 | 261 | /* |
| 245 | 262 | * Set `fetchpriority` here for backward-compatibility as we should |
| 246 | 263 | * not override what a developer decided, even though it seems |
| 247 | 264 | * incorrect. |
| @@ -312,10 +329,10 @@ | ||
| 312 | 329 | |
| 313 | 330 | /** |
| 314 | 331 | * Determines whether to add `fetchpriority='high'` to loading attributes. |
| 315 | 332 | * |
| 316 | - * @param array $loading_attrs Array of the loading optimization attributes for the element. | |
| 317 | - * @param array $attr Array of the attributes for the element. | |
| 333 | + * @param array $loading_attrs Array of the loading optimization attributes for the element. | |
| 334 | + * @param array $attr Array of the attributes for the element. | |
| 318 | 335 | * @return array Updated loading optimization attributes for the element. |
| 319 | 336 | */ |
| 320 | 337 | private function maybe_add_fetchpriority_high_attr( $loading_attrs, $attr ) { |
| 321 | 338 | if ( isset( $attr['fetchpriority'] ) ) { |