| @@ -1,22 +1,21 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 3 | 2 | namespace Elementor\Modules\Interactions; |
| 4 | 3 | |
| 5 | 4 | use Elementor\Core\Base\Module as BaseModule; |
| 6 | -use Elementor\Core\Base\Document; | |
| 5 | +use Elementor\Core\Experiments\Manager as Experiments_Manager; | |
| 7 | 6 | use Elementor\Modules\AtomicWidgets\Module as AtomicWidgetsModule; |
| 8 | -use Elementor\Modules\Interactions\Cache\Interactions_Postmeta; | |
| 9 | 7 | use Elementor\Plugin; |
| 10 | 8 | use Elementor\Utils; |
| 11 | 9 | |
| 10 | + | |
| 12 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | 12 | exit; // Exit if accessed directly. |
| 14 | 13 | } |
| 15 | 14 | |
| 16 | 15 | class Module extends BaseModule { |
| 17 | - const EXPERIMENT_NAME = AtomicWidgetsModule::EXPERIMENT_NAME; | |
| 18 | 16 | const MODULE_NAME = 'e-interactions'; |
| 17 | + const EXPERIMENT_NAME = 'e_interactions'; | |
| 19 | 18 | |
| 20 | 19 | const HANDLE_MOTION_JS = 'motion-js'; |
| 21 | 20 | const HANDLE_SHARED_UTILS = 'elementor-interactions-shared-utils'; |
| 22 | 21 | const HANDLE_FRONTEND = 'elementor-interactions'; |
| @@ -29,8 +28,10 @@ | ||
| 29 | 28 | } |
| 30 | 29 | |
| 31 | 30 | private $preset_animations; |
| 32 | 31 | |
| 32 | + private $frontend_handler; | |
| 33 | + | |
| 33 | 34 | private function get_presets() { |
| 34 | 35 | if ( ! $this->preset_animations ) { |
| 35 | 36 | $this->preset_animations = new Presets(); |
| 36 | 37 | } |
| @@ -37,10 +38,8 @@ | ||
| 37 | 38 | |
| 38 | 39 | return $this->preset_animations; |
| 39 | 40 | } |
| 40 | 41 | |
| 41 | - private $frontend_handler; | |
| 42 | - | |
| 43 | 42 | private function get_frontend_handler() { |
| 44 | 43 | if ( ! $this->frontend_handler ) { |
| 45 | 44 | $this->frontend_handler = new Interactions_Frontend_Handler( fn () => $this->get_config() ); |
| 46 | 45 | } |
| @@ -47,10 +46,22 @@ | ||
| 47 | 46 | |
| 48 | 47 | return $this->frontend_handler; |
| 49 | 48 | } |
| 50 | 49 | |
| 50 | + public static function get_experimental_data() { | |
| 51 | + return [ | |
| 52 | + 'name' => self::EXPERIMENT_NAME, | |
| 53 | + 'title' => esc_html__( 'Interactions', 'elementor' ), | |
| 54 | + 'description' => esc_html__( 'Enable element interactions.', 'elementor' ), | |
| 55 | + 'hidden' => true, | |
| 56 | + 'default' => Experiments_Manager::STATE_ACTIVE, | |
| 57 | + 'release_status' => Experiments_Manager::RELEASE_STATUS_DEV, | |
| 58 | + ]; | |
| 59 | + } | |
| 60 | + | |
| 51 | 61 | public function is_experiment_active() { |
| 52 | - return Plugin::$instance->experiments->is_feature_active( AtomicWidgetsModule::EXPERIMENT_NAME ); | |
| 62 | + return Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_NAME ) | |
| 63 | + && Plugin::$instance->experiments->is_feature_active( AtomicWidgetsModule::EXPERIMENT_NAME ); | |
| 53 | 64 | } |
| 54 | 65 | |
| 55 | 66 | public function __construct() { |
| 56 | 67 | parent::__construct(); |
| @@ -58,50 +69,35 @@ | ||
| 58 | 69 | if ( ! $this->is_experiment_active() ) { |
| 59 | 70 | return; |
| 60 | 71 | } |
| 61 | 72 | |
| 62 | - $this->register_hooks(); | |
| 63 | - } | |
| 64 | - | |
| 65 | - private function register_hooks() { | |
| 66 | 73 | add_action( 'elementor/frontend/after_register_scripts', fn () => $this->register_frontend_scripts() ); |
| 74 | + add_action( 'elementor/editor/before_enqueue_scripts', fn () => $this->enqueue_editor_scripts() ); | |
| 67 | 75 | add_action( 'elementor/preview/enqueue_scripts', fn () => $this->enqueue_preview_scripts() ); |
| 68 | - | |
| 69 | - add_action( 'elementor/editor/before_enqueue_scripts', fn () => $this->enqueue_editor_scripts() ); | |
| 70 | 76 | add_action( 'elementor/editor/after_enqueue_scripts', fn () => $this->enqueue_editor_scripts() ); |
| 71 | 77 | |
| 72 | - add_filter( 'elementor/document/save/data', [ $this, 'handle_interactions' ], 10, 2 ); | |
| 73 | - add_action( 'elementor/document/after_save', [ $this, 'handle_interactions_cache' ], 10, 2 ); | |
| 74 | - | |
| 75 | 78 | // Collect interactions from documents before they render (header, footer, post content) |
| 76 | - add_filter( 'elementor/frontend/builder_content_data', [ | |
| 77 | - $this->get_frontend_handler(), | |
| 78 | - 'collect_document_interactions', | |
| 79 | - ], 10, 2 ); | |
| 79 | + add_filter( 'elementor/frontend/builder_content_data', [ $this->get_frontend_handler(), 'collect_document_interactions' ], 10, 2 ); | |
| 80 | 80 | |
| 81 | 81 | // Output centralized interaction data in footer |
| 82 | 82 | add_action( 'wp_footer', [ $this->get_frontend_handler(), 'print_interactions_data' ], 1 ); |
| 83 | - } | |
| 84 | 83 | |
| 85 | - /** | |
| 86 | - * Sanitize and validate data before saving the document. | |
| 87 | - * | |
| 88 | - * @throws \Exception When validation fails. | |
| 89 | - * @return array | |
| 90 | - */ | |
| 91 | - public function handle_interactions( $data, $document ) { | |
| 92 | - $validation = new Validation(); | |
| 93 | - $document_after_sanitization = $validation->sanitize( $data ); | |
| 84 | + add_filter( 'elementor/document/save/data', | |
| 85 | + /** | |
| 86 | + * @throws \Exception | |
| 87 | + */ | |
| 88 | + function( $data, $document ) { | |
| 89 | + $validation = new Validation(); | |
| 90 | + $document_after_sanitization = $validation->sanitize( $data ); | |
| 91 | + $validation->validate(); | |
| 94 | 92 | |
| 95 | - $validation->validate(); | |
| 93 | + return $document_after_sanitization; | |
| 94 | + }, | |
| 95 | + 10, 2 ); | |
| 96 | 96 | |
| 97 | - $parser = new Parser( $document->get_main_id() ); | |
| 98 | - return $parser->assign_interaction_ids( $document_after_sanitization ); | |
| 99 | - } | |
| 100 | - | |
| 101 | - public function handle_interactions_cache( Document $document, $data ) { | |
| 102 | - $postmeta = new Interactions_Postmeta(); | |
| 103 | - $postmeta->process_content( $document->get_main_id(), $data ); | |
| 97 | + add_filter( 'elementor/document/save/data', function( $data, $document ) { | |
| 98 | + return ( new Parser( $document->get_main_id() ) )->assign_interaction_ids( $data ); | |
| 99 | + }, 11, 2 ); | |
| 104 | 100 | } |
| 105 | 101 | |
| 106 | 102 | public function get_config() { |
| 107 | 103 | return [ |