d5_dir = plugin_dir_path( __FILE__ ) . '../divi-modules/divi-5/'; $this->init(); } private function init(): void { $modules_file = $this->d5_dir . 'modules/Modules.php'; if ( file_exists( $modules_file ) ) { require_once $modules_file; } $this->enqueue_vb_bundle(); } /** * Enqueue the Visual Builder JS bundle when the Divi 5 VB is active. */ private function enqueue_vb_bundle(): void { // Fires inside PackageBuildManager::enqueue_scripts() — app-window only, // before divi-module-library is queued, so our addAction listener is // registered before Divi fires divi.moduleLibrary.registerModuleLibraryStore.after. add_action( 'divi_visual_builder_assets_before_enqueue_app_window_scripts', function () { $build_dir = $this->d5_dir . 'visual-builder/build/'; $build_url = plugins_url( 'includes/core/widgets/divi-modules/divi-5/visual-builder/build/', \WPFNL_FILE ); $js_file = $build_dir . 'wpfnl-divi5-vb.js'; if ( ! file_exists( $js_file ) ) { // Without this bundle Divi 5 never calls registerModule(), so the // WPFunnels modules are missing from the module library with no // visible error — it looks like a Divi incompatibility. Surface it // so the real cause (an unbuilt bundle) is diagnosable. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log( 'WPFunnels: Divi 5 Visual Builder bundle missing at ' . $js_file . ' — run `npm ci && npm run build` in' . ' includes/core/widgets/divi-modules/divi-5/visual-builder/.' . ' WPFunnels modules will not appear in the Divi 5 builder.' ); } return; } // Must list every Divi package the bundle externalises — see the // `externals` map in visual-builder/webpack.config.js. An external // left out here only works while Divi happens to print that package // first, and silently throws the moment a Divi update reorders them. // Deliberately excludes divi-module-library: we must print *before* // it so we still catch the registerModuleLibraryStore.after action. wp_enqueue_script( 'wpfnl-divi5-vb', $build_url . 'wpfnl-divi5-vb.js', [ 'divi-vendor-react', 'divi-vendor-wp-hooks', 'divi-module', 'divi-rest', 'divi-icon-library' ], \WPFNL_VERSION . '.' . (string) filemtime( $js_file ), true ); // Load WPFunnels public CSS in the VB canvas so checkout/optin layouts render correctly. $public_css = \WPFNL_DIR_URL . 'public/assets/css/wpfnl-public.css'; wp_enqueue_style( 'wpfnl-public', $public_css, [], \WPFNL_VERSION ); wp_localize_script( 'wpfnl-divi5-vb', 'wpfnlDiviVBData', [ 'postId' => get_the_ID(), 'restUrl' => rest_url( 'wpfnl/v1' ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'funnels' => $this->get_funnel_options(), 'mailmintForms' => $this->get_mailmint_forms_options(), 'mmEnabled' => $this->is_mailmint_enabled(), 'mmLists' => $this->get_mm_lists_options(), 'mmTags' => $this->get_mm_tags_options(), ] ); } ); } /** * Mail Mint forms map for the OptIn module's "Mail Mint Form" select. * * @return array */ private function get_mailmint_forms_options(): array { $options = [ '' => __( 'Select a form', 'wpfnl' ) ]; if ( class_exists( '\\WPFunnels\\Widgets\\Wpfnl_Widgets_Manager' ) ) { $forms = \WPFunnels\Widgets\Wpfnl_Widgets_Manager::get_mailmint_forms(); if ( is_array( $forms ) ) { foreach ( $forms as $id => $title ) { $options[ (string) $id ] = (string) $title; } } } return $options; } private function is_mailmint_enabled(): bool { return class_exists( '\\WPFunnels\\Integrations\\Helper' ) && \WPFunnels\Integrations\Helper::maybe_enabled(); } /** * @return array */ private function get_mm_lists_options(): array { if ( ! $this->is_mailmint_enabled() ) { return [ '' => __( 'Select list', 'wpfnl' ) ]; } $lists = \WPFunnels\Integrations\Helper::get_lists(); return is_array( $lists ) ? array_map( 'strval', $lists ) : [ '' => __( 'Select list', 'wpfnl' ) ]; } /** * @return array */ private function get_mm_tags_options(): array { if ( ! $this->is_mailmint_enabled() ) { return [ '' => __( 'Select tag', 'wpfnl' ) ]; } $tags = \WPFunnels\Integrations\Helper::get_tags(); return is_array( $tags ) ? array_map( 'strval', $tags ) : [ '' => __( 'Select tag', 'wpfnl' ) ]; } /** * Build the funnel options map used by the Next Step Button "Another Funnel" * dropdown in the Divi 5 Visual Builder. * * @return array URL => Title (first entry is the empty placeholder). */ private function get_funnel_options(): array { if ( ! class_exists( '\\WPFunnels\\Wpfnl_functions' ) ) { return [ '' => __( 'Select funnel', 'wpfnl' ) ]; } $raw = \WPFunnels\Wpfnl_functions::get_funnel_list(); $options = []; foreach ( $raw as $url => $title ) { $options[ trim( (string) $url ) ] = (string) $title; } if ( ! isset( $options[''] ) ) { $options = array_merge( [ '' => __( 'Select funnel', 'wpfnl' ) ], $options ); } return $options; } }