← All changes
|
modules/image-loading-optimization/module.php
+35
-21
4.2.0-dev2
→
3.20.2
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' ] ); |
| @@ -54,24 +78,15 @@ | ||
| 54 | 78 | add_filter( 'wp_content_img_tag', [ $this, 'loading_optimization_image' ] ); |
| 55 | 79 | } |
| 56 | 80 | |
| 57 | 81 | /** |
| 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' ); | |
| 67 | - } | |
| 68 | - | |
| 69 | - /** | |
| 70 | 82 | * Stop WordPress core fetchpriority logic by setting the wp_high_priority_element_flag flag to false. |
| 71 | 83 | */ |
| 72 | 84 | public function stop_core_fetchpriority_high_logic() { |
| 73 | - wp_high_priority_element_flag( false ); | |
| 85 | + // wp_high_priority_element_flag was only introduced in 6.3.0 | |
| 86 | + if ( function_exists( 'wp_high_priority_element_flag' ) ) { | |
| 87 | + wp_high_priority_element_flag( false ); | |
| 88 | + } | |
| 74 | 89 | } |
| 75 | 90 | |
| 76 | 91 | /** |
| 77 | 92 | * Set buffer to handle header and footer content. |
| @@ -239,9 +254,8 @@ | ||
| 239 | 254 | __FUNCTION__, |
| 240 | 255 | esc_html__( 'An image should not be lazy-loaded and marked as high priority at the same time.', 'elementor' ), |
| 241 | 256 | '' |
| 242 | 257 | ); |
| 243 | - | |
| 244 | 258 | /* |
| 245 | 259 | * Set `fetchpriority` here for backward-compatibility as we should |
| 246 | 260 | * not override what a developer decided, even though it seems |
| 247 | 261 | * incorrect. |
| @@ -312,10 +326,10 @@ | ||
| 312 | 326 | |
| 313 | 327 | /** |
| 314 | 328 | * Determines whether to add `fetchpriority='high'` to loading attributes. |
| 315 | 329 | * |
| 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. | |
| 330 | + * @param array $loading_attrs Array of the loading optimization attributes for the element. | |
| 331 | + * @param array $attr Array of the attributes for the element. | |
| 318 | 332 | * @return array Updated loading optimization attributes for the element. |
| 319 | 333 | */ |
| 320 | 334 | private function maybe_add_fetchpriority_high_attr( $loading_attrs, $attr ) { |
| 321 | 335 | if ( isset( $attr['fetchpriority'] ) ) { |