| @@ -1,16 +1,14 @@ | ||
| 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; | |
| 7 | 5 | use Elementor\Core\Experiments\Manager as Experiments_Manager; |
| 8 | 6 | use Elementor\Modules\AtomicWidgets\Module as AtomicWidgetsModule; |
| 9 | -use Elementor\Modules\Interactions\Cache\Interactions_Postmeta; | |
| 10 | 7 | use Elementor\Plugin; |
| 11 | 8 | use Elementor\Utils; |
| 12 | 9 | |
| 10 | + | |
| 13 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | 12 | exit; // Exit if accessed directly. |
| 15 | 13 | } |
| 16 | 14 | |
| @@ -17,15 +15,8 @@ | ||
| 17 | 15 | class Module extends BaseModule { |
| 18 | 16 | const MODULE_NAME = 'e-interactions'; |
| 19 | 17 | const EXPERIMENT_NAME = 'e_interactions'; |
| 20 | 18 | |
| 21 | - const HANDLE_MOTION_JS = 'motion-js'; | |
| 22 | - const HANDLE_SHARED_UTILS = 'elementor-interactions-shared-utils'; | |
| 23 | - const HANDLE_FRONTEND = 'elementor-interactions'; | |
| 24 | - const HANDLE_EDITOR = 'elementor-editor-interactions'; | |
| 25 | - const JS_CONFIG_OBJECT = 'ElementorInteractionsConfig'; | |
| 26 | - const SCRIPT_ID_INTERACTIONS_DATA = 'elementor-interactions-data'; | |
| 27 | - | |
| 28 | 19 | public function get_name() { |
| 29 | 20 | return self::MODULE_NAME; |
| 30 | 21 | } |
| 31 | 22 | |
| @@ -38,18 +29,8 @@ | ||
| 38 | 29 | |
| 39 | 30 | return $this->preset_animations; |
| 40 | 31 | } |
| 41 | 32 | |
| 42 | - private $frontend_handler; | |
| 43 | - | |
| 44 | - private function get_frontend_handler() { | |
| 45 | - if ( ! $this->frontend_handler ) { | |
| 46 | - $this->frontend_handler = new Interactions_Frontend_Handler( fn () => $this->get_config() ); | |
| 47 | - } | |
| 48 | - | |
| 49 | - return $this->frontend_handler; | |
| 50 | - } | |
| 51 | - | |
| 52 | 33 | public static function get_experimental_data() { |
| 53 | 34 | return [ |
| 54 | 35 | 'name' => self::EXPERIMENT_NAME, |
| 55 | 36 | 'title' => esc_html__( 'Interactions', 'elementor' ), |
| @@ -71,77 +52,44 @@ | ||
| 71 | 52 | if ( ! $this->is_experiment_active() ) { |
| 72 | 53 | return; |
| 73 | 54 | } |
| 74 | 55 | |
| 75 | - $this->register_hooks(); | |
| 76 | - } | |
| 77 | - | |
| 78 | - private function register_hooks() { | |
| 79 | 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() ); | |
| 80 | 59 | add_action( 'elementor/preview/enqueue_scripts', fn () => $this->enqueue_preview_scripts() ); |
| 81 | - | |
| 82 | - add_action( 'elementor/editor/before_enqueue_scripts', fn () => $this->enqueue_editor_scripts() ); | |
| 83 | 60 | add_action( 'elementor/editor/after_enqueue_scripts', fn () => $this->enqueue_editor_scripts() ); |
| 84 | 61 | |
| 85 | - add_filter( 'elementor/document/save/data', [ $this, 'handle_interactions' ], 10, 2 ); | |
| 86 | - 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(); | |
| 87 | 70 | |
| 88 | - // Collect interactions from documents before they render (header, footer, post content) | |
| 89 | - add_filter( 'elementor/frontend/builder_content_data', [ | |
| 90 | - $this->get_frontend_handler(), | |
| 91 | - 'collect_document_interactions', | |
| 92 | - ], 10, 2 ); | |
| 71 | + return $document_after_sanitization; | |
| 72 | + }, | |
| 73 | + 10, 2 ); | |
| 93 | 74 | |
| 94 | - // Output centralized interaction data in footer | |
| 95 | - 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 ); | |
| 96 | 78 | } |
| 97 | 79 | |
| 98 | - /** | |
| 99 | - * Sanitize and validate data before saving the document. | |
| 100 | - * | |
| 101 | - * @throws \Exception When validation fails. | |
| 102 | - * @return array | |
| 103 | - */ | |
| 104 | - public function handle_interactions( $data, $document ) { | |
| 105 | - $validation = new Validation(); | |
| 106 | - $document_after_sanitization = $validation->sanitize( $data ); | |
| 107 | - | |
| 108 | - $validation->validate(); | |
| 109 | - | |
| 110 | - $parser = new Parser( $document->get_main_id() ); | |
| 111 | - return $parser->assign_interaction_ids( $document_after_sanitization ); | |
| 112 | - } | |
| 113 | - | |
| 114 | - public function handle_interactions_cache( Document $document, $data ) { | |
| 115 | - $postmeta = new Interactions_Postmeta(); | |
| 116 | - $postmeta->process_content( $document->get_main_id(), $data ); | |
| 117 | - } | |
| 118 | - | |
| 119 | - public function get_config() { | |
| 80 | + private function get_config() { | |
| 120 | 81 | return [ |
| 121 | 82 | 'constants' => $this->get_presets()->defaults(), |
| 122 | - 'breakpoints' => $this->get_active_breakpoints(), | |
| 83 | + 'animationOptions' => $this->get_presets()->list(), | |
| 123 | 84 | ]; |
| 124 | 85 | } |
| 125 | 86 | |
| 126 | - private function get_active_breakpoints() { | |
| 127 | - $breakpoints_config = Plugin::$instance->breakpoints->get_breakpoints_config(); | |
| 128 | - $active_breakpoints = Plugin::$instance->breakpoints->get_active_breakpoints(); | |
| 129 | - | |
| 130 | - $breakpoints = []; | |
| 131 | - | |
| 132 | - foreach ( array_keys( $active_breakpoints ) as $breakpoint_label ) { | |
| 133 | - $breakpoints[ $breakpoint_label ] = $breakpoints_config[ $breakpoint_label ]; | |
| 134 | - } | |
| 135 | - | |
| 136 | - return $breakpoints; | |
| 137 | - } | |
| 138 | - | |
| 139 | 87 | private function register_frontend_scripts() { |
| 140 | 88 | $suffix = ( Utils::is_script_debug() || Utils::is_elementor_tests() ) ? '' : '.min'; |
| 141 | 89 | |
| 142 | 90 | wp_register_script( |
| 143 | - self::HANDLE_MOTION_JS, | |
| 91 | + 'motion-js', | |
| 144 | 92 | ELEMENTOR_ASSETS_URL . 'lib/motion/motion' . $suffix . '.js', |
| 145 | 93 | [], |
| 146 | 94 | '11.13.5', |
| 147 | 95 | true |
| @@ -147,29 +95,32 @@ | ||
| 147 | 95 | true |
| 148 | 96 | ); |
| 149 | 97 | |
| 150 | 98 | wp_register_script( |
| 151 | - self::HANDLE_SHARED_UTILS, | |
| 152 | - $this->get_js_assets_url( 'interactions-shared-utils' ), | |
| 153 | - [ self::HANDLE_MOTION_JS ], | |
| 99 | + 'elementor-interactions', | |
| 100 | + $this->get_js_assets_url( 'interactions' ), | |
| 101 | + [ 'motion-js' ], | |
| 154 | 102 | '1.0.0', |
| 155 | 103 | true |
| 156 | 104 | ); |
| 157 | 105 | |
| 158 | 106 | wp_register_script( |
| 159 | - self::HANDLE_FRONTEND, | |
| 160 | - $this->get_js_assets_url( 'interactions' ), | |
| 161 | - [ self::HANDLE_MOTION_JS, self::HANDLE_SHARED_UTILS ], | |
| 107 | + 'elementor-editor-interactions', | |
| 108 | + $this->get_js_assets_url( 'editor-interactions' ), | |
| 109 | + [ 'motion-js' ], | |
| 162 | 110 | '1.0.0', |
| 163 | 111 | true |
| 164 | 112 | ); |
| 113 | + } | |
| 165 | 114 | |
| 166 | - wp_register_script( | |
| 167 | - self::HANDLE_EDITOR, | |
| 168 | - $this->get_js_assets_url( 'editor-interactions' ), | |
| 169 | - [ self::HANDLE_MOTION_JS, self::HANDLE_SHARED_UTILS ], | |
| 170 | - '1.0.0', | |
| 171 | - 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() | |
| 172 | 123 | ); |
| 173 | 124 | } |
| 174 | 125 | |
| 175 | 126 | public function enqueue_editor_scripts() { |
| @@ -174,22 +125,21 @@ | ||
| 174 | 125 | |
| 175 | 126 | public function enqueue_editor_scripts() { |
| 176 | 127 | wp_add_inline_script( |
| 177 | 128 | 'elementor-common', |
| 178 | - 'window.' . self::JS_CONFIG_OBJECT . ' = ' . wp_json_encode( $this->get_config() ) . ';', | |
| 129 | + 'window.ElementorInteractionsConfig = ' . wp_json_encode( $this->get_config() ) . ';', | |
| 179 | 130 | 'before' |
| 180 | 131 | ); |
| 181 | 132 | } |
| 182 | 133 | |
| 183 | 134 | public function enqueue_preview_scripts() { |
| 184 | - wp_enqueue_script( self::HANDLE_SHARED_UTILS ); | |
| 185 | 135 | // Ensure motion-js and editor-interactions handler are available in preview iframe |
| 186 | - wp_enqueue_script( self::HANDLE_MOTION_JS ); | |
| 187 | - wp_enqueue_script( self::HANDLE_EDITOR ); | |
| 136 | + wp_enqueue_script( 'motion-js' ); | |
| 137 | + wp_enqueue_script( 'elementor-editor-interactions' ); | |
| 188 | 138 | |
| 189 | 139 | wp_localize_script( |
| 190 | - self::HANDLE_EDITOR, | |
| 191 | - self::JS_CONFIG_OBJECT, | |
| 140 | + 'elementor-editor-interactions', | |
| 141 | + 'ElementorInteractionsConfig', | |
| 192 | 142 | $this->get_config() |
| 193 | 143 | ); |
| 194 | 144 | } |
| 195 | 145 | } |