| @@ -20,34 +20,92 @@ | ||
| 20 | 20 | * |
| 21 | 21 | * @return void |
| 22 | 22 | */ |
| 23 | 23 | public function init() { |
| 24 | + add_filter( 'content_control/post_types_to_ignore', [ $this, 'post_types_to_ignore' ] ); | |
| 24 | 25 | add_filter( 'content_control/protection_is_disabled', [ $this, 'protection_is_disabled' ] ); |
| 25 | 26 | } |
| 26 | 27 | |
| 27 | 28 | /** |
| 29 | + * Check if controller is enabled. | |
| 30 | + * | |
| 31 | + * @return bool | |
| 32 | + */ | |
| 33 | + public function controller_enabled() { | |
| 34 | + return class_exists( '\Elementor\Plugin' ) || did_action( 'elementor/loaded' ); | |
| 35 | + } | |
| 36 | + | |
| 37 | + /** | |
| 28 | 38 | * Conditionally disable Content Control for Elementor builder. |
| 29 | 39 | * |
| 30 | - * @param boolean $protection_is_disabled Whether protection is disabled. | |
| 40 | + * @param boolean $is_disabled Whether protection is disabled. | |
| 31 | 41 | * @return boolean |
| 32 | 42 | */ |
| 33 | - public function protection_is_disabled( $protection_is_disabled ) { | |
| 34 | - if ( did_action( 'elementor/loaded' ) && $this->elementor_builder_is_active() ) { | |
| 35 | - return true; | |
| 43 | + public function protection_is_disabled( $is_disabled ) { | |
| 44 | + // If already disabled, no reason to continue. | |
| 45 | + if ( $is_disabled || ! did_action( 'elementor/loaded' ) ) { | |
| 46 | + return $is_disabled; | |
| 36 | 47 | } |
| 37 | 48 | |
| 38 | - return $protection_is_disabled; | |
| 49 | + return $this->elementor_builder_is_active(); | |
| 39 | 50 | } |
| 40 | 51 | |
| 41 | 52 | /** |
| 53 | + * Add Elementor font post type to ignored post types. | |
| 54 | + * | |
| 55 | + * @param string[] $post_types Post types to ignore. | |
| 56 | + * @return string[] | |
| 57 | + */ | |
| 58 | + public function post_types_to_ignore( $post_types ) { | |
| 59 | + $post_types[] = 'elementor_font'; | |
| 60 | + $post_types[] = 'elementor_icons'; | |
| 61 | + $post_types[] = 'elementor_library'; | |
| 62 | + $post_types[] = 'elementor_snippet'; | |
| 63 | + | |
| 64 | + return $post_types; | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 42 | 68 | * Check if Elementor builder is active. |
| 43 | 69 | * |
| 44 | 70 | * @return boolean |
| 45 | 71 | */ |
| 46 | 72 | public function elementor_builder_is_active() { |
| 47 | - return class_exists( '\Elementor\Plugin' ) && | |
| 48 | - isset( \Elementor\Plugin::$instance ) && | |
| 49 | - isset( \Elementor\Plugin::$instance->preview ) && | |
| 50 | - method_exists( \Elementor\Plugin::$instance->preview, 'is_preview_mode' ) && | |
| 51 | - \Elementor\Plugin::$instance->preview->is_preview_mode(); | |
| 73 | + // Check if this is the admin theme builder app. | |
| 74 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended Simple & direct string comparison. | |
| 75 | + if ( | |
| 76 | + is_admin() && | |
| 77 | + // Disable notices as this is a generic string comparison to prevent doing a lot of work. | |
| 78 | + // phpcs:disable WordPress.Security.NonceVerification.Recommended | |
| 79 | + ! empty( $_GET['page'] ) && | |
| 80 | + 'elementor-app' === $_GET['page'] | |
| 81 | + // phpcs:enable WordPress.Security.NonceVerification.Recommended | |
| 82 | + ) { | |
| 83 | + return true; | |
| 84 | + } | |
| 85 | + | |
| 86 | + if ( ! class_exists( '\Elementor\Plugin' ) || ! isset( \Elementor\Plugin::$instance ) ) { | |
| 87 | + return false; | |
| 88 | + } | |
| 89 | + | |
| 90 | + /** | |
| 91 | + * Elementor instance. | |
| 92 | + * | |
| 93 | + * @var \Elementor\Plugin $elementor | |
| 94 | + */ | |
| 95 | + $elementor = \Elementor\Plugin::$instance; | |
| 96 | + | |
| 97 | + /** | |
| 98 | + * Elementor preview instance. | |
| 99 | + * | |
| 100 | + * @var \Elementor\Preview|(object{is_preview_mod:\Closure}&\stdClass)|false $preview | |
| 101 | + */ | |
| 102 | + $preview = isset( $elementor->preview ) ? $elementor->preview : false; | |
| 103 | + | |
| 104 | + if ( false === $preview || ! method_exists( $preview, 'is_preview_mode' ) ) { | |
| 105 | + return false; | |
| 106 | + } | |
| 107 | + | |
| 108 | + // Check if the page builder is active. | |
| 109 | + return $preview->is_preview_mode(); | |
| 52 | 110 | } |
| 53 | 111 | } |