← All changes
|
modules/image-loading-optimization/module.php
+41
-20
4.2.2
→
3.17.0-dev3
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,37 @@ | ||
| 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 | + 'new_site' => [ | |
| 56 | + 'default_active' => true, | |
| 57 | + 'minimum_installation_version' => '3.17.0', | |
| 58 | + ], | |
| 59 | + 'generator_tag' => true, | |
| 60 | + 'release_status' => Experiments_Manager::RELEASE_STATUS_BETA, | |
| 61 | + 'default' => Experiments_Manager::STATE_INACTIVE, | |
| 62 | + ]; | |
| 63 | + } | |
| 64 | + | |
| 65 | + /** | |
| 34 | 66 | * Constructor. |
| 35 | 67 | */ |
| 36 | 68 | public function __construct() { |
| 37 | - if ( ! static::is_optimized_image_loading_enabled() ) { | |
| 38 | - return; | |
| 39 | - } | |
| 40 | - | |
| 41 | 69 | parent::__construct(); |
| 42 | 70 | |
| 43 | 71 | // Stop wp core logic. |
| 44 | 72 | add_action( 'init', [ $this, 'stop_core_fetchpriority_high_logic' ] ); |
| @@ -51,20 +79,11 @@ | ||
| 51 | 79 | add_filter( 'the_content', [ $this, 'flush_header_buffer' ], 0 ); |
| 52 | 80 | |
| 53 | 81 | // Run optimization logic on content. |
| 54 | 82 | add_filter( 'wp_content_img_tag', [ $this, 'loading_optimization_image' ] ); |
| 55 | - } | |
| 56 | 83 | |
| 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' ); | |
| 84 | + // Run optimization logic on footer. Flushing of footer buffer will be handled by PHP script end default logic. | |
| 85 | + add_action( 'get_footer', [ $this, 'set_buffer' ] ); | |
| 67 | 86 | } |
| 68 | 87 | |
| 69 | 88 | /** |
| 70 | 89 | * Stop WordPress core fetchpriority logic by setting the wp_high_priority_element_flag flag to false. |
| @@ -69,9 +88,12 @@ | ||
| 69 | 88 | /** |
| 70 | 89 | * Stop WordPress core fetchpriority logic by setting the wp_high_priority_element_flag flag to false. |
| 71 | 90 | */ |
| 72 | 91 | public function stop_core_fetchpriority_high_logic() { |
| 73 | - wp_high_priority_element_flag( false ); | |
| 92 | + // wp_high_priority_element_flag was only introduced in 6.3.0 | |
| 93 | + if ( function_exists( 'wp_high_priority_element_flag' ) ) { | |
| 94 | + wp_high_priority_element_flag( false ); | |
| 95 | + } | |
| 74 | 96 | } |
| 75 | 97 | |
| 76 | 98 | /** |
| 77 | 99 | * Set buffer to handle header and footer content. |
| @@ -239,9 +261,8 @@ | ||
| 239 | 261 | __FUNCTION__, |
| 240 | 262 | esc_html__( 'An image should not be lazy-loaded and marked as high priority at the same time.', 'elementor' ), |
| 241 | 263 | '' |
| 242 | 264 | ); |
| 243 | - | |
| 244 | 265 | /* |
| 245 | 266 | * Set `fetchpriority` here for backward-compatibility as we should |
| 246 | 267 | * not override what a developer decided, even though it seems |
| 247 | 268 | * incorrect. |
| @@ -312,10 +333,10 @@ | ||
| 312 | 333 | |
| 313 | 334 | /** |
| 314 | 335 | * Determines whether to add `fetchpriority='high'` to loading attributes. |
| 315 | 336 | * |
| 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. | |
| 337 | + * @param array $loading_attrs Array of the loading optimization attributes for the element. | |
| 338 | + * @param array $attr Array of the attributes for the element. | |
| 318 | 339 | * @return array Updated loading optimization attributes for the element. |
| 319 | 340 | */ |
| 320 | 341 | private function maybe_add_fetchpriority_high_attr( $loading_attrs, $attr ) { |
| 321 | 342 | if ( isset( $attr['fetchpriority'] ) ) { |