| 1 |
<?php |
| 2 |
namespace Elementor\Modules\Interactions; |
| 3 |
|
| 4 |
use Elementor\Core\Base\Module as BaseModule; |
| 5 |
use Elementor\Core\Experiments\Manager as Experiments_Manager; |
| 6 |
use Elementor\Modules\AtomicWidgets\Module as AtomicWidgetsModule; |
| 7 |
use Elementor\Plugin; |
| 8 |
use Elementor\Utils; |
| 9 |
|
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
class Module extends BaseModule { |
| 16 |
const MODULE_NAME = 'e-interactions'; |
| 17 |
const EXPERIMENT_NAME = 'e_interactions'; |
| 18 |
|
| 19 |
public function get_name() { |
| 20 |
return self::MODULE_NAME; |
| 21 |
} |
| 22 |
|
| 23 |
private $preset_animations; |
| 24 |
|
| 25 |
private function get_presets() { |
| 26 |
if ( ! $this->preset_animations ) { |
| 27 |
$this->preset_animations = new Presets(); |
| 28 |
} |
| 29 |
|
| 30 |
return $this->preset_animations; |
| 31 |
} |
| 32 |
|
| 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 |
]; |
| 42 |
} |
| 43 |
|
| 44 |
public function is_experiment_active() { |
| 45 |
return Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_NAME ) |
| 46 |
&& Plugin::$instance->experiments->is_feature_active( AtomicWidgetsModule::EXPERIMENT_NAME ); |
| 47 |
} |
| 48 |
|
| 49 |
public function __construct() { |
| 50 |
parent::__construct(); |
| 51 |
|
| 52 |
if ( ! $this->is_experiment_active() ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 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() ); |
| 59 |
add_action( 'elementor/preview/enqueue_scripts', fn () => $this->enqueue_preview_scripts() ); |
| 60 |
add_action( 'elementor/editor/after_enqueue_scripts', fn () => $this->enqueue_editor_scripts() ); |
| 61 |
|
| 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(); |
| 70 |
|
| 71 |
return $document_after_sanitization; |
| 72 |
}, |
| 73 |
10, 2 ); |
| 74 |
|
| 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 ); |
| 78 |
} |
| 79 |
|
| 80 |
private function get_config() { |
| 81 |
return [ |
| 82 |
'constants' => $this->get_presets()->defaults(), |
| 83 |
'animationOptions' => $this->get_presets()->list(), |
| 84 |
]; |
| 85 |
} |
| 86 |
|
| 87 |
private function register_frontend_scripts() { |
| 88 |
$suffix = ( Utils::is_script_debug() || Utils::is_elementor_tests() ) ? '' : '.min'; |
| 89 |
|
| 90 |
wp_register_script( |
| 91 |
'motion-js', |
| 92 |
ELEMENTOR_ASSETS_URL . 'lib/motion/motion' . $suffix . '.js', |
| 93 |
[], |
| 94 |
'11.13.5', |
| 95 |
true |
| 96 |
); |
| 97 |
|
| 98 |
wp_register_script( |
| 99 |
'elementor-interactions', |
| 100 |
$this->get_js_assets_url( 'interactions' ), |
| 101 |
[ 'motion-js' ], |
| 102 |
'1.0.0', |
| 103 |
true |
| 104 |
); |
| 105 |
|
| 106 |
wp_register_script( |
| 107 |
'elementor-editor-interactions', |
| 108 |
$this->get_js_assets_url( 'editor-interactions' ), |
| 109 |
[ 'motion-js' ], |
| 110 |
'1.0.0', |
| 111 |
true |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 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() |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
public function enqueue_editor_scripts() { |
| 127 |
wp_add_inline_script( |
| 128 |
'elementor-common', |
| 129 |
'window.ElementorInteractionsConfig = ' . wp_json_encode( $this->get_config() ) . ';', |
| 130 |
'before' |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
public function enqueue_preview_scripts() { |
| 135 |
// Ensure motion-js and editor-interactions handler are available in preview iframe |
| 136 |
wp_enqueue_script( 'motion-js' ); |
| 137 |
wp_enqueue_script( 'elementor-editor-interactions' ); |
| 138 |
|
| 139 |
wp_localize_script( |
| 140 |
'elementor-editor-interactions', |
| 141 |
'ElementorInteractionsConfig', |
| 142 |
$this->get_config() |
| 143 |
); |
| 144 |
} |
| 145 |
} |
| 146 |
|