| 1 |
<?php |
| 2 |
/** |
| 3 |
* Elementor compatibility controller. |
| 4 |
* |
| 5 |
* @package ContentControl\Admin |
| 6 |
* @copyright (c) 2023 Code Atlantic LLC |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ContentControl\Controllers\Compatibility; |
| 10 |
|
| 11 |
use ContentControl\Base\Controller; |
| 12 |
|
| 13 |
/** |
| 14 |
* Elementor controller class. |
| 15 |
*/ |
| 16 |
class Elementor extends Controller { |
| 17 |
|
| 18 |
/** |
| 19 |
* Initialize widget editor UX. |
| 20 |
* |
| 21 |
* @return void |
| 22 |
*/ |
| 23 |
public function init() { |
| 24 |
add_filter( 'content_control/post_types_to_ignore', [ $this, 'post_types_to_ignore' ] ); |
| 25 |
add_filter( 'content_control/protection_is_disabled', [ $this, 'protection_is_disabled' ] ); |
| 26 |
} |
| 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 |
/** |
| 38 |
* Conditionally disable Content Control for Elementor builder. |
| 39 |
* |
| 40 |
* @param boolean $is_disabled Whether protection is disabled. |
| 41 |
* @return boolean |
| 42 |
*/ |
| 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; |
| 47 |
} |
| 48 |
|
| 49 |
return $this->elementor_builder_is_active(); |
| 50 |
} |
| 51 |
|
| 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 |
/** |
| 68 |
* Check if Elementor builder is active. |
| 69 |
* |
| 70 |
* @return boolean |
| 71 |
*/ |
| 72 |
public function elementor_builder_is_active() { |
| 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(); |
| 110 |
} |
| 111 |
} |
| 112 |
|