PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / compatibility / class-wpo-page-builder-compatibility.php

class-wpo-page-builder-compatibility.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.7.0, at compatibility/class-wpo-page-builder-compatibility.php

99 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) {
3 die('No direct access allowed');
4 }
5
6 /**
7 * Adds compatibility for Page Builder plugins.
8 */
9 class WPO_Page_Builder_Compatibility {
10
11 /**
12 * Query string parameters used by supported page builders to signal edit mode.
13 *
14 * - fl_builder: Beaver Builder
15 * - et_fb: Divi Theme Builder
16 * - ct_builder: Oxygen Builder
17 * - elementor-preview: Elementor
18 * - oxygen: Oxygen (alternative trigger)
19 */
20 const EDIT_MODE_QUERY_PARAMS = array(
21 'fl_builder',
22 'et_fb',
23 'ct_builder',
24 'elementor-preview',
25 'oxygen',
26 );
27
28 /**
29 * Path fragment that identifies Divi cached assets.
30 */
31 const DIVI_CACHE_PATH_FRAGMENT = 'et-cache';
32
33 /**
34 * Constructor.
35 */
36 private function __construct() {
37 $this->disable_webp_alter_html_in_edit_mode();
38
39 add_filter('wpo_minify_file_modification_time', array($this, 'use_file_hash_for_divi_assets'), 10, 2);
40 }
41
42 /**
43 * Returns singleton instance.
44 *
45 * @return self
46 */
47 public static function instance() {
48 static $instance = null;
49 if (null === $instance) {
50 $instance = new self();
51 }
52
53 return $instance;
54 }
55
56 /**
57 * Replaces the modification time of Divi assets with the file hash for WPO Minify.
58 *
59 * @param string $modification_time The original modification time.
60 * @param string $file_path The absolute path to the file.
61 * @return string
62 */
63 public function use_file_hash_for_divi_assets($modification_time, $file_path) {
64 if (false !== strpos($file_path, self::DIVI_CACHE_PATH_FRAGMENT)) {
65 $hash = hash_file('adler32', $file_path);
66 if ($hash) {
67 return $hash . '-h';
68 }
69 }
70
71 return $modification_time;
72 }
73
74 /**
75 * Determines whether the current page is being viewed inside a page builder's
76 * edit/preview mode (e.g. Beaver Builder, Divi, Elementor, Oxygen Builder).
77 *
78 * @return bool
79 */
80 public static function is_edit_mode(): bool {
81 foreach (self::EDIT_MODE_QUERY_PARAMS as $param) {
82 if (isset($_GET[$param])) { // phpcs:ignore WordPress.Security.NonceVerification -- Only checking for key existence, not using values.
83 return true;
84 }
85 }
86
87 return false;
88 }
89
90 /**
91 * Disables altering HTML for WebP when current page is in edit mode.
92 */
93 private function disable_webp_alter_html_in_edit_mode(): void {
94 if (self::is_edit_mode()) {
95 add_filter('wpo_disable_webp_alter_html', '__return_true');
96 }
97 }
98 }
99