| 1 |
<?php |
| 2 |
/** |
| 3 |
* Divi 5 Widgets Manager entry point. |
| 4 |
* |
| 5 |
* Loaded by Wpfnl_Widgets_Manager when Divi 5 is detected. |
| 6 |
* Mirrors the diviModules Manager interface so the widget system can |
| 7 |
* instantiate it via the same naming convention. |
| 8 |
* |
| 9 |
* @package WPFunnels\Widgets\DiviModulesV5 |
| 10 |
* @since 2.9.1 |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace WPFunnels\Widgets\DiviModulesV5; |
| 14 |
|
| 15 |
final class Manager { |
| 16 |
|
| 17 |
private static $_instance = null; |
| 18 |
|
| 19 |
public static function instance() { |
| 20 |
if ( is_null( self::$_instance ) ) { |
| 21 |
self::$_instance = new self(); |
| 22 |
} |
| 23 |
return self::$_instance; |
| 24 |
} |
| 25 |
|
| 26 |
/** @var string Path to the divi-5 modules directory */ |
| 27 |
private string $d5_dir; |
| 28 |
|
| 29 |
public function __construct() { |
| 30 |
$this->d5_dir = plugin_dir_path( __FILE__ ) |
| 31 |
. '../divi-modules/divi-5/'; |
| 32 |
|
| 33 |
$this->init(); |
| 34 |
} |
| 35 |
|
| 36 |
private function init(): void { |
| 37 |
$modules_file = $this->d5_dir . 'modules/Modules.php'; |
| 38 |
if ( file_exists( $modules_file ) ) { |
| 39 |
require_once $modules_file; |
| 40 |
} |
| 41 |
|
| 42 |
$this->enqueue_vb_bundle(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Enqueue the Visual Builder JS bundle when the Divi 5 VB is active. |
| 47 |
*/ |
| 48 |
private function enqueue_vb_bundle(): void { |
| 49 |
// Fires inside PackageBuildManager::enqueue_scripts() — app-window only, |
| 50 |
// before divi-module-library is queued, so our addAction listener is |
| 51 |
// registered before Divi fires divi.moduleLibrary.registerModuleLibraryStore.after. |
| 52 |
add_action( 'divi_visual_builder_assets_before_enqueue_app_window_scripts', function () { |
| 53 |
$build_dir = $this->d5_dir . 'visual-builder/build/'; |
| 54 |
$build_url = plugins_url( |
| 55 |
'includes/core/widgets/divi-modules/divi-5/visual-builder/build/', |
| 56 |
\WPFNL_FILE |
| 57 |
); |
| 58 |
|
| 59 |
$js_file = $build_dir . 'wpfnl-divi5-vb.js'; |
| 60 |
if ( ! file_exists( $js_file ) ) { |
| 61 |
// Without this bundle Divi 5 never calls registerModule(), so the |
| 62 |
// WPFunnels modules are missing from the module library with no |
| 63 |
// visible error — it looks like a Divi incompatibility. Surface it |
| 64 |
// so the real cause (an unbuilt bundle) is diagnosable. |
| 65 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 66 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 67 |
error_log( |
| 68 |
'WPFunnels: Divi 5 Visual Builder bundle missing at ' . $js_file |
| 69 |
. ' — run `npm ci && npm run build` in' |
| 70 |
. ' includes/core/widgets/divi-modules/divi-5/visual-builder/.' |
| 71 |
. ' WPFunnels modules will not appear in the Divi 5 builder.' |
| 72 |
); |
| 73 |
} |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
wp_enqueue_script( |
| 78 |
'wpfnl-divi5-vb', |
| 79 |
$build_url . 'wpfnl-divi5-vb.js', |
| 80 |
[ 'divi-vendor-react', 'divi-vendor-wp-hooks', 'divi-module', 'divi-rest' ], |
| 81 |
\WPFNL_VERSION . '.' . (string) filemtime( $js_file ), |
| 82 |
true |
| 83 |
); |
| 84 |
|
| 85 |
// Load WPFunnels public CSS in the VB canvas so checkout/optin layouts render correctly. |
| 86 |
$public_css = \WPFNL_DIR_URL . 'public/assets/css/wpfnl-public.css'; |
| 87 |
wp_enqueue_style( 'wpfnl-public', $public_css, [], \WPFNL_VERSION ); |
| 88 |
|
| 89 |
wp_localize_script( |
| 90 |
'wpfnl-divi5-vb', |
| 91 |
'wpfnlDiviVBData', |
| 92 |
[ |
| 93 |
'postId' => get_the_ID(), |
| 94 |
'restUrl' => rest_url( 'wpfnl/v1' ), |
| 95 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 96 |
'funnels' => $this->get_funnel_options(), |
| 97 |
'mailmintForms' => $this->get_mailmint_forms_options(), |
| 98 |
'mmEnabled' => $this->is_mailmint_enabled(), |
| 99 |
'mmLists' => $this->get_mm_lists_options(), |
| 100 |
'mmTags' => $this->get_mm_tags_options(), |
| 101 |
] |
| 102 |
); |
| 103 |
} ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Mail Mint forms map for the OptIn module's "Mail Mint Form" select. |
| 108 |
* |
| 109 |
* @return array<string,string> |
| 110 |
*/ |
| 111 |
private function get_mailmint_forms_options(): array { |
| 112 |
$options = [ '' => __( 'Select a form', 'wpfnl' ) ]; |
| 113 |
if ( class_exists( '\\WPFunnels\\Widgets\\Wpfnl_Widgets_Manager' ) ) { |
| 114 |
$forms = \WPFunnels\Widgets\Wpfnl_Widgets_Manager::get_mailmint_forms(); |
| 115 |
if ( is_array( $forms ) ) { |
| 116 |
foreach ( $forms as $id => $title ) { |
| 117 |
$options[ (string) $id ] = (string) $title; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
return $options; |
| 122 |
} |
| 123 |
|
| 124 |
private function is_mailmint_enabled(): bool { |
| 125 |
return class_exists( '\\WPFunnels\\Integrations\\Helper' ) |
| 126 |
&& \WPFunnels\Integrations\Helper::maybe_enabled(); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* @return array<string,string> |
| 131 |
*/ |
| 132 |
private function get_mm_lists_options(): array { |
| 133 |
if ( ! $this->is_mailmint_enabled() ) { |
| 134 |
return [ '' => __( 'Select list', 'wpfnl' ) ]; |
| 135 |
} |
| 136 |
$lists = \WPFunnels\Integrations\Helper::get_lists(); |
| 137 |
return is_array( $lists ) ? array_map( 'strval', $lists ) : [ '' => __( 'Select list', 'wpfnl' ) ]; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @return array<string,string> |
| 142 |
*/ |
| 143 |
private function get_mm_tags_options(): array { |
| 144 |
if ( ! $this->is_mailmint_enabled() ) { |
| 145 |
return [ '' => __( 'Select tag', 'wpfnl' ) ]; |
| 146 |
} |
| 147 |
$tags = \WPFunnels\Integrations\Helper::get_tags(); |
| 148 |
return is_array( $tags ) ? array_map( 'strval', $tags ) : [ '' => __( 'Select tag', 'wpfnl' ) ]; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Build the funnel options map used by the Next Step Button "Another Funnel" |
| 153 |
* dropdown in the Divi 5 Visual Builder. |
| 154 |
* |
| 155 |
* @return array<string,string> URL => Title (first entry is the empty placeholder). |
| 156 |
*/ |
| 157 |
private function get_funnel_options(): array { |
| 158 |
if ( ! class_exists( '\\WPFunnels\\Wpfnl_functions' ) ) { |
| 159 |
return [ '' => __( 'Select funnel', 'wpfnl' ) ]; |
| 160 |
} |
| 161 |
$raw = \WPFunnels\Wpfnl_functions::get_funnel_list(); |
| 162 |
$options = []; |
| 163 |
foreach ( $raw as $url => $title ) { |
| 164 |
$options[ trim( (string) $url ) ] = (string) $title; |
| 165 |
} |
| 166 |
if ( ! isset( $options[''] ) ) { |
| 167 |
$options = array_merge( [ '' => __( 'Select funnel', 'wpfnl' ) ], $options ); |
| 168 |
} |
| 169 |
return $options; |
| 170 |
} |
| 171 |
} |