supported_editors = array( 'elementor' => array( 'check' => 'elementor/loaded', 'init' => array($this, 'init_elementor') ), 'divi' => array( 'init' => array($this, 'init_divi') ), 'wpbakery' => array( 'init' => array($this, 'init_wpbakery') ), 'beaver' => array( 'init' => array($this, 'init_beaver') ) ); // Initialize editor integrations add_action('init', array($this, 'initialize_editors'), 20); // Enqueue common assets for all editors add_action('admin_enqueue_scripts', array($this, 'enqueue_common_assets')); add_action('wp_enqueue_scripts', array($this, 'enqueue_common_assets')); } public function initialize_editors() { foreach ($this->supported_editors as $editor => $config) { if ($this->is_editor_active($editor)) { call_user_func($config['init']); } } } private function is_editor_active($editor) { if (!isset($this->supported_editors[$editor])) { return false; } switch ($editor) { case 'elementor': if (!did_action($this->supported_editors[$editor]['check'])) { return false; } if (class_exists('\Elementor\Plugin') && isset(\Elementor\Plugin::$instance->editor)) { return \Elementor\Plugin::$instance->editor->is_edit_mode() || \Elementor\Plugin::$instance->preview->is_preview_mode(); } return false; case 'divi': // et_pb_is_pagebuilder_used() checks edit context; fall back to the // simpler function_exists guard so we don't break non-edit admin pages. return function_exists('et_builder_is_active') && (is_admin() || (function_exists('et_fb_is_enabled') && et_fb_is_enabled())); case 'wpbakery': return function_exists('vc_is_inline') && vc_is_inline(); case 'beaver': return defined('FL_BUILDER_VERSION') && class_exists('FLBuilderModel') && FLBuilderModel::is_builder_active(); default: return false; } } public function enqueue_common_assets() { // Only enqueue if we're in an editor context if (!$this->is_any_editor_active()) { return; } // Enqueue React and ReactDOM first wp_enqueue_script( 'react', CANTO_FBC_URL . 'public/assets/react.production.min.js', array(), '17.0.2', true ); wp_enqueue_script( 'react-dom', CANTO_FBC_URL . 'public/assets/react-dom.production.min.js', array('react'), '17.0.2', true ); // Enqueue vendor scripts wp_enqueue_script( 'canto-editor-vendor', CANTO_FBC_URL . 'public/assets/app.vendor.bundle.js', array('jquery', 'react', 'react-dom'), '1.0.0', true ); // Then enqueue the main bundle wp_enqueue_script( 'canto-editor-bundle', CANTO_FBC_URL . 'public/assets/app.bundle.js', array('jquery', 'canto-editor-vendor'), '1.0.0', true ); // Then enqueue our core editor integration wp_enqueue_script( 'canto-editor-core', CANTO_FBC_URL . 'assets/js/editor-core.js', array('jquery', 'canto-editor-vendor', 'canto-editor-bundle'), '1.0.0', true ); // Add configuration $config = array( 'ajaxurl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('canto_editor_nonce'), 'FBC_URL' => CANTO_FBC_URL, 'FBC_PATH' => CANTO_FBC_PATH, 'app_api' => get_option('fbc_app_api', 'canto.com'), 'subdomain' => get_option('fbc_flight_domain'), 'token' => get_option('fbc_app_token'), 'initialized' => false ); wp_localize_script('canto-editor-core', 'cantoEditorConfig', $config); // Add initialization check wp_add_inline_script('canto-editor-core', ' window.cantoEditorInitCheck = function() { return typeof React !== "undefined" && typeof ReactDOM !== "undefined" && typeof window.canto !== "undefined" && typeof window.canto.openDialog === "function"; }; ', 'before'); } private function is_any_editor_active() { foreach ($this->supported_editors as $editor => $config) { if ($this->is_editor_active($editor)) { return true; } } return false; } // Elementor Integration public function init_elementor() { require_once CANTO_FBC_PATH . 'includes/elementor/class-canto-elementor.php'; Canto_Elementor::instance(); } // Divi Integration public function init_divi() { add_action('et_builder_ready', array($this, 'load_divi_integration')); } public function load_divi_integration() { wp_enqueue_script( 'canto-divi-integration', CANTO_FBC_URL . 'assets/js/divi-integration.js', array('jquery', 'canto-editor-core'), '1.0.0', true ); } // WPBakery Integration public function init_wpbakery() { add_action('vc_after_init', array($this, 'load_wpbakery_integration')); } public function load_wpbakery_integration() { wp_enqueue_script( 'canto-wpbakery-integration', CANTO_FBC_URL . 'assets/js/wpbakery-integration.js', array('jquery', 'canto-editor-core'), '1.0.0', true ); } // Beaver Builder Integration public function init_beaver() { add_action('fl_builder_before_init', array($this, 'load_beaver_integration')); } public function load_beaver_integration() { wp_enqueue_script( 'canto-beaver-integration', CANTO_FBC_URL . 'assets/js/beaver-integration.js', array('jquery', 'canto-editor-core'), '1.0.0', true ); } }