← All changes
|
compatibility/class-wpo-page-builder-compatibility.php
+58
-7
3.3.1
→
4.7.0
View file →
| @@ -8,21 +8,47 @@ | ||
| 8 | 8 | */ |
| 9 | 9 | class WPO_Page_Builder_Compatibility { |
| 10 | 10 | |
| 11 | 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 | + /** | |
| 12 | 34 | * Constructor. |
| 13 | 35 | */ |
| 14 | 36 | private function __construct() { |
| 15 | 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); | |
| 16 | 40 | } |
| 17 | 41 | |
| 18 | 42 | /** |
| 19 | 43 | * Returns singleton instance. |
| 44 | + * | |
| 45 | + * @return self | |
| 20 | 46 | */ |
| 21 | 47 | public static function instance() { |
| 22 | 48 | static $instance = null; |
| 23 | - if (null == $instance) { | |
| 24 | - $instance = new static; | |
| 49 | + if (null === $instance) { | |
| 50 | + $instance = new self(); | |
| 25 | 51 | } |
| 26 | 52 | |
| 27 | 53 | return $instance; |
| 28 | 54 | } |
| @@ -27,21 +53,46 @@ | ||
| 27 | 53 | return $instance; |
| 28 | 54 | } |
| 29 | 55 | |
| 30 | 56 | /** |
| 31 | - * Checks if current page is in Page Builder edit mode. | |
| 57 | + * Replaces the modification time of Divi assets with the file hash for WPO Minify. | |
| 32 | 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 | + * | |
| 33 | 78 | * @return bool |
| 34 | 79 | */ |
| 35 | - private function is_edit_mode() { | |
| 36 | - return isset($_GET['fl_builder']) || isset($_GET['et_fb']); | |
| 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; | |
| 37 | 88 | } |
| 38 | 89 | |
| 39 | 90 | /** |
| 40 | 91 | * Disables altering HTML for WebP when current page is in edit mode. |
| 41 | 92 | */ |
| 42 | - private function disable_webp_alter_html_in_edit_mode() { | |
| 43 | - if ($this->is_edit_mode()) { | |
| 93 | + private function disable_webp_alter_html_in_edit_mode(): void { | |
| 94 | + if (self::is_edit_mode()) { | |
| 44 | 95 | add_filter('wpo_disable_webp_alter_html', '__return_true'); |
| 45 | 96 | } |
| 46 | 97 | } |
| 47 | 98 | } |