PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 3.13.2
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v3.13.2
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / includes / core / widgets / diviModulesV5 / class-manager.php

class-manager.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.13.2, at includes/core/widgets/diviModulesV5/class-manager.php

177 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Must list every Divi package the bundle externalises — see the
78 // `externals` map in visual-builder/webpack.config.js. An external
79 // left out here only works while Divi happens to print that package
80 // first, and silently throws the moment a Divi update reorders them.
81 // Deliberately excludes divi-module-library: we must print *before*
82 // it so we still catch the registerModuleLibraryStore.after action.
83 wp_enqueue_script(
84 'wpfnl-divi5-vb',
85 $build_url . 'wpfnl-divi5-vb.js',
86 [ 'divi-vendor-react', 'divi-vendor-wp-hooks', 'divi-module', 'divi-rest', 'divi-icon-library' ],
87 \WPFNL_VERSION . '.' . (string) filemtime( $js_file ),
88 true
89 );
90
91 // Load WPFunnels public CSS in the VB canvas so checkout/optin layouts render correctly.
92 $public_css = \WPFNL_DIR_URL . 'public/assets/css/wpfnl-public.css';
93 wp_enqueue_style( 'wpfnl-public', $public_css, [], \WPFNL_VERSION );
94
95 wp_localize_script(
96 'wpfnl-divi5-vb',
97 'wpfnlDiviVBData',
98 [
99 'postId' => get_the_ID(),
100 'restUrl' => rest_url( 'wpfnl/v1' ),
101 'nonce' => wp_create_nonce( 'wp_rest' ),
102 'funnels' => $this->get_funnel_options(),
103 'mailmintForms' => $this->get_mailmint_forms_options(),
104 'mmEnabled' => $this->is_mailmint_enabled(),
105 'mmLists' => $this->get_mm_lists_options(),
106 'mmTags' => $this->get_mm_tags_options(),
107 ]
108 );
109 } );
110 }
111
112 /**
113 * Mail Mint forms map for the OptIn module's "Mail Mint Form" select.
114 *
115 * @return array<string,string>
116 */
117 private function get_mailmint_forms_options(): array {
118 $options = [ '' => __( 'Select a form', 'wpfnl' ) ];
119 if ( class_exists( '\\WPFunnels\\Widgets\\Wpfnl_Widgets_Manager' ) ) {
120 $forms = \WPFunnels\Widgets\Wpfnl_Widgets_Manager::get_mailmint_forms();
121 if ( is_array( $forms ) ) {
122 foreach ( $forms as $id => $title ) {
123 $options[ (string) $id ] = (string) $title;
124 }
125 }
126 }
127 return $options;
128 }
129
130 private function is_mailmint_enabled(): bool {
131 return class_exists( '\\WPFunnels\\Integrations\\Helper' )
132 && \WPFunnels\Integrations\Helper::maybe_enabled();
133 }
134
135 /**
136 * @return array<string,string>
137 */
138 private function get_mm_lists_options(): array {
139 if ( ! $this->is_mailmint_enabled() ) {
140 return [ '' => __( 'Select list', 'wpfnl' ) ];
141 }
142 $lists = \WPFunnels\Integrations\Helper::get_lists();
143 return is_array( $lists ) ? array_map( 'strval', $lists ) : [ '' => __( 'Select list', 'wpfnl' ) ];
144 }
145
146 /**
147 * @return array<string,string>
148 */
149 private function get_mm_tags_options(): array {
150 if ( ! $this->is_mailmint_enabled() ) {
151 return [ '' => __( 'Select tag', 'wpfnl' ) ];
152 }
153 $tags = \WPFunnels\Integrations\Helper::get_tags();
154 return is_array( $tags ) ? array_map( 'strval', $tags ) : [ '' => __( 'Select tag', 'wpfnl' ) ];
155 }
156
157 /**
158 * Build the funnel options map used by the Next Step Button "Another Funnel"
159 * dropdown in the Divi 5 Visual Builder.
160 *
161 * @return array<string,string> URL => Title (first entry is the empty placeholder).
162 */
163 private function get_funnel_options(): array {
164 if ( ! class_exists( '\\WPFunnels\\Wpfnl_functions' ) ) {
165 return [ '' => __( 'Select funnel', 'wpfnl' ) ];
166 }
167 $raw = \WPFunnels\Wpfnl_functions::get_funnel_list();
168 $options = [];
169 foreach ( $raw as $url => $title ) {
170 $options[ trim( (string) $url ) ] = (string) $title;
171 }
172 if ( ! isset( $options[''] ) ) {
173 $options = array_merge( [ '' => __( 'Select funnel', 'wpfnl' ) ], $options );
174 }
175 return $options;
176 }
177 }