| @@ -1,30 +1,22 @@ | ||
| 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 | - const HANDLE_MOTION_JS = 'motion-js'; | |
| 21 | - const HANDLE_SHARED_UTILS = 'elementor-interactions-shared-utils'; | |
| 22 | - const HANDLE_FRONTEND = 'elementor-interactions'; | |
| 23 | - const HANDLE_EDITOR = 'elementor-editor-interactions'; | |
| 24 | - const JS_CONFIG_OBJECT = 'ElementorInteractionsConfig'; | |
| 25 | - const SCRIPT_ID_INTERACTIONS_DATA = 'elementor-interactions-data'; | |
| 26 | - | |
| 27 | 19 | public function get_name() { |
| 28 | 20 | return self::MODULE_NAME; |
| 29 | 21 | } |
| 30 | 22 | |
| @@ -37,20 +29,22 @@ | ||
| 37 | 29 | |
| 38 | 30 | return $this->preset_animations; |
| 39 | 31 | } |
| 40 | 32 | |
| 41 | - private $frontend_handler; | |
| 42 | - | |
| 43 | - private function get_frontend_handler() { | |
| 44 | - if ( ! $this->frontend_handler ) { | |
| 45 | - $this->frontend_handler = new Interactions_Frontend_Handler( fn () => $this->get_config() ); | |
| 46 | - } | |
| 47 | - | |
| 48 | - return $this->frontend_handler; | |
| 33 | + public static function get_experimental_data() { | |
| 34 | + return [ | |
| 35 | + 'name' => self::EXPERIMENT_NAME, | |
| 36 | + 'title' => esc_html__( 'Interactions', 'elementor' ), | |
| 37 | + 'description' => esc_html__( 'Enable element interactions.', 'elementor' ), | |
| 38 | + 'hidden' => true, | |
| 39 | + 'default' => Experiments_Manager::STATE_ACTIVE, | |
| 40 | + 'release_status' => Experiments_Manager::RELEASE_STATUS_DEV, | |
| 41 | + ]; | |
| 49 | 42 | } |
| 50 | 43 | |
| 51 | 44 | public function is_experiment_active() { |
| 52 | - return Plugin::$instance->experiments->is_feature_active( AtomicWidgetsModule::EXPERIMENT_NAME ); | |
| 45 | + return Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_NAME ) | |
| 46 | + && Plugin::$instance->experiments->is_feature_active( AtomicWidgetsModule::EXPERIMENT_NAME ); | |
| 53 | 47 | } |
| 54 | 48 | |
| 55 | 49 | public function __construct() { |
| 56 | 50 | parent::__construct(); |
| @@ -58,77 +52,44 @@ | ||
| 58 | 52 | if ( ! $this->is_experiment_active() ) { |
| 59 | 53 | return; |
| 60 | 54 | } |
| 61 | 55 | |
| 62 | - $this->register_hooks(); | |
| 63 | - } | |
| 64 | - | |
| 65 | - private function register_hooks() { | |
| 66 | 56 | add_action( 'elementor/frontend/after_register_scripts', fn () => $this->register_frontend_scripts() ); |
| 57 | + add_action( 'elementor/editor/before_enqueue_scripts', fn () => $this->enqueue_editor_scripts() ); | |
| 58 | + add_action( 'elementor/frontend/before_enqueue_scripts', fn () => $this->enqueue_interactions() ); | |
| 67 | 59 | 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 | 60 | add_action( 'elementor/editor/after_enqueue_scripts', fn () => $this->enqueue_editor_scripts() ); |
| 71 | 61 | |
| 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 ); | |
| 62 | + add_filter( 'elementor/document/save/data', | |
| 63 | + /** | |
| 64 | + * @throws \Exception | |
| 65 | + */ | |
| 66 | + function( $data, $document ) { | |
| 67 | + $validation = new Validation(); | |
| 68 | + $document_after_sanitization = $validation->sanitize( $data ); | |
| 69 | + $validation->validate(); | |
| 74 | 70 | |
| 75 | - // 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 ); | |
| 71 | + return $document_after_sanitization; | |
| 72 | + }, | |
| 73 | + 10, 2 ); | |
| 80 | 74 | |
| 81 | - // Output centralized interaction data in footer | |
| 82 | - add_action( 'wp_footer', [ $this->get_frontend_handler(), 'print_interactions_data' ], 1 ); | |
| 75 | + add_filter( 'elementor/document/save/data', function( $data, $document ) { | |
| 76 | + return ( new Parser( $document->get_main_id() ) )->assign_interaction_ids( $data ); | |
| 77 | + }, 11, 2 ); | |
| 83 | 78 | } |
| 84 | 79 | |
| 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 ); | |
| 94 | - | |
| 95 | - $validation->validate(); | |
| 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 ); | |
| 104 | - } | |
| 105 | - | |
| 106 | - public function get_config() { | |
| 80 | + private function get_config() { | |
| 107 | 81 | return [ |
| 108 | 82 | 'constants' => $this->get_presets()->defaults(), |
| 109 | - 'breakpoints' => $this->get_active_breakpoints(), | |
| 83 | + 'animationOptions' => $this->get_presets()->list(), | |
| 110 | 84 | ]; |
| 111 | 85 | } |
| 112 | 86 | |
| 113 | - private function get_active_breakpoints() { | |
| 114 | - $breakpoints_config = Plugin::$instance->breakpoints->get_breakpoints_config(); | |
| 115 | - $active_breakpoints = Plugin::$instance->breakpoints->get_active_breakpoints(); | |
| 116 | - | |
| 117 | - $breakpoints = []; | |
| 118 | - | |
| 119 | - foreach ( array_keys( $active_breakpoints ) as $breakpoint_label ) { | |
| 120 | - $breakpoints[ $breakpoint_label ] = $breakpoints_config[ $breakpoint_label ]; | |
| 121 | - } | |
| 122 | - | |
| 123 | - return $breakpoints; | |
| 124 | - } | |
| 125 | - | |
| 126 | 87 | private function register_frontend_scripts() { |
| 127 | 88 | $suffix = ( Utils::is_script_debug() || Utils::is_elementor_tests() ) ? '' : '.min'; |
| 128 | 89 | |
| 129 | 90 | wp_register_script( |
| 130 | - self::HANDLE_MOTION_JS, | |
| 91 | + 'motion-js', | |
| 131 | 92 | ELEMENTOR_ASSETS_URL . 'lib/motion/motion' . $suffix . '.js', |
| 132 | 93 | [], |
| 133 | 94 | '11.13.5', |
| 134 | 95 | true |
| @@ -134,29 +95,32 @@ | ||
| 134 | 95 | true |
| 135 | 96 | ); |
| 136 | 97 | |
| 137 | 98 | wp_register_script( |
| 138 | - self::HANDLE_SHARED_UTILS, | |
| 139 | - $this->get_js_assets_url( 'interactions-shared-utils' ), | |
| 140 | - [ self::HANDLE_MOTION_JS ], | |
| 99 | + 'elementor-interactions', | |
| 100 | + $this->get_js_assets_url( 'interactions' ), | |
| 101 | + [ 'motion-js' ], | |
| 141 | 102 | '1.0.0', |
| 142 | 103 | true |
| 143 | 104 | ); |
| 144 | 105 | |
| 145 | 106 | wp_register_script( |
| 146 | - self::HANDLE_FRONTEND, | |
| 147 | - $this->get_js_assets_url( 'interactions' ), | |
| 148 | - [ self::HANDLE_MOTION_JS, self::HANDLE_SHARED_UTILS ], | |
| 107 | + 'elementor-editor-interactions', | |
| 108 | + $this->get_js_assets_url( 'editor-interactions' ), | |
| 109 | + [ 'motion-js' ], | |
| 149 | 110 | '1.0.0', |
| 150 | 111 | true |
| 151 | 112 | ); |
| 113 | + } | |
| 152 | 114 | |
| 153 | - wp_register_script( | |
| 154 | - self::HANDLE_EDITOR, | |
| 155 | - $this->get_js_assets_url( 'editor-interactions' ), | |
| 156 | - [ self::HANDLE_MOTION_JS, self::HANDLE_SHARED_UTILS ], | |
| 157 | - '1.0.0', | |
| 158 | - true | |
| 115 | + public function enqueue_interactions(): void { | |
| 116 | + wp_enqueue_script( 'motion-js' ); | |
| 117 | + wp_enqueue_script( 'elementor-interactions' ); | |
| 118 | + | |
| 119 | + wp_localize_script( | |
| 120 | + 'elementor-interactions', | |
| 121 | + 'ElementorInteractionsConfig', | |
| 122 | + $this->get_config() | |
| 159 | 123 | ); |
| 160 | 124 | } |
| 161 | 125 | |
| 162 | 126 | public function enqueue_editor_scripts() { |
| @@ -161,22 +125,21 @@ | ||
| 161 | 125 | |
| 162 | 126 | public function enqueue_editor_scripts() { |
| 163 | 127 | wp_add_inline_script( |
| 164 | 128 | 'elementor-common', |
| 165 | - 'window.' . self::JS_CONFIG_OBJECT . ' = ' . wp_json_encode( $this->get_config() ) . ';', | |
| 129 | + 'window.ElementorInteractionsConfig = ' . wp_json_encode( $this->get_config() ) . ';', | |
| 166 | 130 | 'before' |
| 167 | 131 | ); |
| 168 | 132 | } |
| 169 | 133 | |
| 170 | 134 | public function enqueue_preview_scripts() { |
| 171 | - wp_enqueue_script( self::HANDLE_SHARED_UTILS ); | |
| 172 | 135 | // Ensure motion-js and editor-interactions handler are available in preview iframe |
| 173 | - wp_enqueue_script( self::HANDLE_MOTION_JS ); | |
| 174 | - wp_enqueue_script( self::HANDLE_EDITOR ); | |
| 136 | + wp_enqueue_script( 'motion-js' ); | |
| 137 | + wp_enqueue_script( 'elementor-editor-interactions' ); | |
| 175 | 138 | |
| 176 | 139 | wp_localize_script( |
| 177 | - self::HANDLE_EDITOR, | |
| 178 | - self::JS_CONFIG_OBJECT, | |
| 140 | + 'elementor-editor-interactions', | |
| 141 | + 'ElementorInteractionsConfig', | |
| 179 | 142 | $this->get_config() |
| 180 | 143 | ); |
| 181 | 144 | } |
| 182 | 145 | } |