PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.0
Elementor Website Builder – more than just a page builder v4.2.0
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / performance-lab / module.php

module.php in Elementor Website Builder – more than just a page builder 4.2.0, at modules/performance-lab/module.php

57 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\PerformanceLab;
3
4 use Elementor\Core\Base\Module as BaseModule;
5 use Elementor\Plugin;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly.
9 }
10
11 class Module extends BaseModule {
12
13 const PERFORMANCE_LAB_FUNCTION_NAME = 'webp_uploads_img_tag_update_mime_type';
14 const PERFORMANCE_LAB_OPTION_NAME = 'site-health/webp-support';
15
16 public function get_name() {
17 return 'performance-lab';
18 }
19
20 private function is_performance_lab_is_active() {
21 if ( function_exists( self::PERFORMANCE_LAB_FUNCTION_NAME ) ) {
22 $perflab_modules_settings = get_option( self::PERFORMANCE_LAB_OPTION_NAME, [] );
23 if ( isset( $perflab_modules_settings ) && isset( $perflab_modules_settings[ self::PERFORMANCE_LAB_OPTION_NAME ] ) &&
24 '1' === $perflab_modules_settings[ self::PERFORMANCE_LAB_OPTION_NAME ]['enabled'] ) {
25 return true;
26 }
27 }
28 return false;
29 }
30
31 private function performance_lab_get_webp_src( $attachment_id, $size, $url ) {
32 $image_object = wp_get_attachment_image_src( $attachment_id, $size );
33 $image_src = call_user_func( self::PERFORMANCE_LAB_FUNCTION_NAME, $image_object[0], 'webp', $attachment_id );
34 if ( ! empty( $image_src ) ) {
35 return $image_src;
36 }
37 return $url;
38 }
39
40 private function replace_css_with_webp( $value, $css_property, $matches ) {
41 if ( 0 === strpos( $css_property, 'background-image' ) && '{{URL}}' === $matches[0] ) {
42 $value['url'] = $this->performance_lab_get_webp_src( $value['id'], 'full', $value['url'] );
43 }
44 return $value;
45 }
46
47 public function __construct() {
48 parent::__construct();
49
50 if ( $this->is_performance_lab_is_active() ) {
51 add_filter( 'elementor/files/css/property', function( $value, $css_property, $matches ) {
52 return $this->replace_css_with_webp( $value, $css_property, $matches );
53 }, 10, 3 );
54 }
55 }
56 }
57