first commit
This commit is contained in:
140
wp-content/plugins/elementor-pro/modules/interactions/hooks.php
Normal file
140
wp-content/plugins/elementor-pro/modules/interactions/hooks.php
Normal file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace ElementorPro\Modules\Interactions;
|
||||
|
||||
use ElementorPro\Plugin;
|
||||
use ElementorPro\Core\Utils as ProUtils;
|
||||
use Elementor\Utils;
|
||||
use Elementor\Modules\Interactions\Module as InteractionsModule;
|
||||
use Elementor\Modules\Interactions\Presets;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Hooks {
|
||||
const PACKAGES = [
|
||||
'editor-interactions-extended',
|
||||
];
|
||||
|
||||
public function register() {
|
||||
$this->register_packages()
|
||||
->register_pro_scripts()
|
||||
->replace_core_handlers();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function register_packages() {
|
||||
add_filter( 'elementor-pro/editor/v2/packages', function ( $packages ) {
|
||||
return array_merge( $packages, self::PACKAGES );
|
||||
} );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function register_pro_scripts() {
|
||||
add_action( 'elementor/frontend/after_register_scripts', [ $this, 'register_frontend_scripts' ], 20 );
|
||||
add_action( 'elementor/preview/enqueue_scripts', [ $this, 'register_editor_scripts' ], 1 );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function replace_core_handlers() {
|
||||
// Run after core's print_interactions_data (priority 1). If core enqueued its script
|
||||
// it means the page has interactions — swap it for the pro script. If core didn't
|
||||
// enqueue anything (no interactions), we skip too, preserving the load optimization.
|
||||
add_action( 'wp_footer', [ $this, 'replace_frontend_handlers' ], 2 );
|
||||
|
||||
add_action( 'elementor/preview/enqueue_scripts', function() {
|
||||
$core_module = Plugin::elementor()->modules_manager->get_modules( ProUtils::get_class_constant( InteractionsModule::class, 'MODULE_NAME', 'e-interactions' ) );
|
||||
if ( $core_module && method_exists( $core_module, 'enqueue_preview_scripts' ) ) {
|
||||
remove_action( 'elementor/preview/enqueue_scripts', [ $core_module, 'enqueue_preview_scripts' ] );
|
||||
}
|
||||
}, 1 );
|
||||
|
||||
add_action( 'elementor/preview/enqueue_scripts', [ $this, 'replace_preview_handlers' ], 20 );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function register_frontend_scripts() {
|
||||
wp_register_script(
|
||||
'elementor-interactions-pro',
|
||||
$this->get_js_assets_url( 'interactions-pro' ),
|
||||
[
|
||||
ProUtils::get_class_constant( InteractionsModule::class, 'HANDLE_MOTION_JS', 'motion-js' ),
|
||||
ProUtils::get_class_constant( InteractionsModule::class, 'HANDLE_SHARED_UTILS', 'elementor-interactions-shared-utils' ),
|
||||
],
|
||||
ELEMENTOR_PRO_VERSION,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
public function register_editor_scripts() {
|
||||
wp_register_script(
|
||||
'elementor-editor-interactions-pro',
|
||||
$this->get_js_assets_url( 'editor-interactions-pro' ),
|
||||
[
|
||||
ProUtils::get_class_constant( InteractionsModule::class, 'HANDLE_MOTION_JS', 'motion-js' ),
|
||||
ProUtils::get_class_constant( InteractionsModule::class, 'HANDLE_SHARED_UTILS', 'elementor-interactions-shared-utils' ),
|
||||
],
|
||||
ELEMENTOR_PRO_VERSION,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
public function replace_frontend_handlers() {
|
||||
$handle_frontend = ProUtils::get_class_constant( InteractionsModule::class, 'HANDLE_FRONTEND', 'elementor-interactions' );
|
||||
|
||||
// Core only enqueues its script when the page has interactions.
|
||||
// If it didn't enqueue, there's nothing to replace — skip to preserve the load optimization.
|
||||
if ( ! wp_script_is( $handle_frontend, 'enqueued' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
wp_dequeue_script( $handle_frontend );
|
||||
wp_deregister_script( $handle_frontend );
|
||||
|
||||
wp_enqueue_script( 'elementor-interactions-pro' );
|
||||
|
||||
wp_localize_script(
|
||||
'elementor-interactions-pro',
|
||||
ProUtils::get_class_constant( InteractionsModule::class, 'JS_CONFIG_OBJECT', 'ElementorInteractionsConfig' ),
|
||||
$this->get_config()
|
||||
);
|
||||
}
|
||||
|
||||
public function replace_preview_handlers() {
|
||||
$handle_editor = ProUtils::get_class_constant( InteractionsModule::class, 'HANDLE_EDITOR', 'elementor-editor-interactions' );
|
||||
wp_dequeue_script( $handle_editor );
|
||||
wp_deregister_script( $handle_editor );
|
||||
|
||||
wp_enqueue_script( 'elementor-editor-interactions-pro' );
|
||||
|
||||
wp_localize_script(
|
||||
'elementor-editor-interactions-pro',
|
||||
ProUtils::get_class_constant( InteractionsModule::class, 'JS_CONFIG_OBJECT', 'ElementorInteractionsConfig' ),
|
||||
$this->get_config()
|
||||
);
|
||||
}
|
||||
|
||||
private function get_config() {
|
||||
if ( class_exists( 'Elementor\\Modules\\Interactions\\Module' ) ) {
|
||||
$module = \Elementor\Modules\Interactions\Module::instance();
|
||||
if ( $module && is_callable( [ $module, 'get_config' ] ) ) {
|
||||
return $module->get_config();
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'constants' => class_exists( Presets::class ) ? ( new Presets() )->defaults() : [],
|
||||
'breakpoints' => [],
|
||||
];
|
||||
}
|
||||
|
||||
private function get_js_assets_url( $filename ) {
|
||||
$suffix = ( Utils::is_script_debug() || Utils::is_elementor_tests() ) ? '' : '.min';
|
||||
return ELEMENTOR_PRO_URL . 'assets/js/' . $filename . $suffix . '.js';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace ElementorPro\Modules\Interactions;
|
||||
|
||||
use ElementorPro\Plugin;
|
||||
use ElementorPro\Base\Module_Base;
|
||||
use ElementorPro\Core\Utils as ProUtils;
|
||||
use Elementor\Modules\AtomicWidgets\Module as AtomicWidgetsModule;
|
||||
use Elementor\Modules\Interactions\Module as InteractionsModule;
|
||||
use Elementor\Core\Experiments\Manager as ExperimentsManager;
|
||||
use ElementorPro\License\API;
|
||||
use ElementorPro\License\Admin;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
class Module extends Module_Base {
|
||||
const EXPERIMENT_NAME = 'e_pro_interactions';
|
||||
|
||||
public function get_name() {
|
||||
return ProUtils::get_class_constant( InteractionsModule::class, 'MODULE_NAME', 'e-interactions' );
|
||||
}
|
||||
|
||||
public static function get_experimental_data(): array {
|
||||
return [
|
||||
'name' => self::EXPERIMENT_NAME,
|
||||
'title' => esc_html__( 'Pro Interactions', 'elementor-pro' ),
|
||||
'description' => esc_html__( 'Enhanced interactions with replay support. Note: This feature requires both the "Atomic Widgets" and "Interactions" experiments to be enabled.', 'elementor-pro' ),
|
||||
'hidden' => true,
|
||||
'default' => ExperimentsManager::STATE_ACTIVE,
|
||||
'release_status' => ExperimentsManager::RELEASE_STATUS_DEV,
|
||||
];
|
||||
}
|
||||
|
||||
private function is_supported_by_current_license() {
|
||||
if ( empty( \ElementorPro\License\Admin::get_license_key() ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! API::is_license_active() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return API::is_licence_has_feature( 'pro-interactions', API::BC_VALIDATION_CALLBACK );
|
||||
}
|
||||
|
||||
private function hooks() {
|
||||
return new Hooks();
|
||||
}
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
if ( ! $this->is_experiment_active() || ! $this->is_supported_by_current_license() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->hooks()->register();
|
||||
}
|
||||
|
||||
private function is_experiment_active(): bool {
|
||||
return version_compare( ELEMENTOR_VERSION, '4.0', '>=' )
|
||||
&& class_exists( 'Elementor\\Modules\\Interactions\\Module' )
|
||||
&& Plugin::elementor()->experiments->is_feature_active( self::EXPERIMENT_NAME )
|
||||
&& Plugin::elementor()->experiments->is_feature_active( AtomicWidgetsModule::EXPERIMENT_NAME )
|
||||
&& Plugin::elementor()->experiments->is_feature_active( ProUtils::get_class_constant( InteractionsModule::class, 'EXPERIMENT_NAME', 'e_interactions' ) );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user