PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / trunk
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce vtrunk
3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 All 160 releases
cartflows / wizard / inc / wizard-core.php

wizard-core.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce trunk, at wizard/inc/wizard-core.php

478 lines 14.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CartFlows Admin Menu.
4 *
5 * @package CartFlows
6 */
7
8 namespace CartflowsAdmin\Wizard\Inc;
9
10 // Exit if accessed directly.
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 /**
16 * Class Admin_Menu.
17 */
18 class WizardCore {
19
20 /**
21 * Instance
22 *
23 * @access private
24 * @var object Class object.
25 * @since 1.0.0
26 */
27 private static $instance;
28
29 /**
30 * Initiator
31 *
32 * @since 1.0.0
33 * @return object initialized object of class.
34 */
35 public static function get_instance() {
36 if ( ! isset( self::$instance ) ) {
37 self::$instance = new self();
38 }
39 return self::$instance;
40 }
41
42 /**
43 * Constructor.
44 */
45 public function __construct() {
46
47 if ( apply_filters( 'cartflows_enable_setup_wizard', true ) && current_user_can( 'manage_options' ) ) {
48
49 add_action( 'admin_menu', array( $this, 'admin_menus' ) );
50 add_action( 'admin_init', array( $this, 'setup_wizard' ) );
51 add_action( 'admin_init', array( $this, 'hide_notices' ) );
52 add_action( 'admin_notices', array( $this, 'show_setup_wizard' ) );
53 add_action( 'woocommerce_installed', array( $this, 'disable_woo_setup_redirect' ) );
54 // We are hiding admin bar intentionally for setup wizard.
55 add_filter( 'show_admin_bar', '__return_false', 1 ); //phpcs:ignore WordPressVIPMinimum.UserExperience.AdminBarRemoval.RemovalDetected
56
57 add_action( 'init', array( $this, 'load_scripts' ) );
58 add_action( 'admin_print_styles', array( $this, 'load_admin_media_styles' ) );
59
60 add_action( 'admin_init', array( $this, 'redirect_to_onboarding' ) );
61 add_filter( 'before_cp_load_popup', array( $this, 'hide_convert_pro_popups' ), 10, 1 ); // Disable the render of Convert Pro popups in onboarding wizard.
62 }
63 }
64
65 /**
66 * Redirect to onboarding if required.
67 *
68 * @return void
69 * @since 2.0.0
70 */
71 public function redirect_to_onboarding() {
72
73 if ( ! get_option( 'wcf_start_onboarding', false ) ) {
74 return;
75 }
76
77 if ( '1' === get_option( 'wcf_setup_complete', false ) || '1' === get_option( 'wcf_setup_skipped', false ) ) {
78 return;
79 }
80
81 delete_option( 'wcf_start_onboarding' );
82 wp_safe_redirect( esc_url_raw( admin_url( 'index.php?page=cartflow-setup' ) ) );
83 exit();
84 }
85
86
87 /**
88 * Load styles.
89 */
90 public function load_admin_media_styles() {
91
92 $ary_libs = array(
93 'common',
94 'forms',
95 );
96 $admin_media_styles_url = add_query_arg(
97 array(
98 'c' => 0,
99 'dir' => ! is_rtl() ? 'ltr' : 'rtl',
100 'load[]' => implode( ',', $ary_libs ),
101 'ver' => 'you_wp_version',
102 ),
103 admin_url() . 'load-styles.php'
104 );
105 echo "<link rel='stylesheet' id='admin_styles_for_media-css' href='" . esc_url( $admin_media_styles_url ) . "' type='text/css' media='all' />";
106 }
107
108 /**
109 * Load media.
110 */
111 public function load_scripts() {
112
113 add_action( 'wp_enqueue_scripts', array( $this, 'load_media_script' ) );
114 }
115
116 /**
117 * Load WP media script on init.
118 */
119 public function load_media_script() {
120 wp_enqueue_media();
121 }
122
123 /**
124 * Hide a notice if the GET variable is set.
125 */
126 public function hide_notices() {
127
128 if ( ! isset( $_GET['wcf-hide-notice'] ) ) {
129 return;
130 }
131
132 $wcf_hide_notice = isset( $_GET['wcf-hide-notice'] ) ? sanitize_text_field( wp_unslash( $_GET['wcf-hide-notice'] ) ) : '';
133 $_wcf_notice_nonce = isset( $_GET['_wcf_notice_nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wcf_notice_nonce'] ) ) : '';
134
135 if ( $wcf_hide_notice && $_wcf_notice_nonce && wp_verify_nonce( sanitize_text_field( wp_unslash( $_wcf_notice_nonce ) ), 'wcf_hide_notices_nonce' ) ) {
136 update_option( 'wcf_setup_skipped', true );
137 }
138 }
139
140 /**
141 * Disable the woo redirect for new setup.
142 */
143 public function disable_woo_setup_redirect() {
144
145 delete_transient( '_wc_activation_redirect' );
146 }
147
148 /**
149 * Show action links on the plugin screen.
150 *
151 * @since 1.0.0
152 * @return void
153 */
154 public function show_setup_wizard() {
155
156 $screen = get_current_screen();
157 $screen_id = $screen ? $screen->id : '';
158 $allowed_screens = array(
159 'cartflows_page_cartflows_settings',
160 'edit-cartflows_flow',
161 'dashboard',
162 'plugins',
163 );
164
165 if ( ! in_array( $screen_id, $allowed_screens, true ) ) {
166 return;
167 }
168
169 $status = get_option( 'wcf_setup_complete', false );
170 $skip_setup = get_option( 'wcf_setup_skipped', false );
171
172 if ( false === $status && ! $skip_setup ) { ?>
173 <div class="notice notice-info wcf-notice">
174 <p><b><?php esc_html_e( 'Thanks for installing and using CartFlows!', 'cartflows' ); ?></b></p>
175 <p><?php esc_html_e( 'It is easy to use the CartFlows. Please use the setup wizard to quick start setup.', 'cartflows' ); ?></p>
176 <p>
177 <a href="<?php echo esc_url( admin_url( 'index.php?page=cartflow-setup' ) ); ?>" class="button button-primary"> <?php esc_html_e( 'Start Wizard', 'cartflows' ); ?></a>
178 <a class="button-secondary" href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'wcf-hide-notice', 'install' ), 'wcf_hide_notices_nonce', '_wcf_notice_nonce' ) ); ?>"><?php esc_html_e( 'Skip Setup', 'cartflows' ); ?></a>
179 </p>
180 </div>
181 <?php
182 }
183 }
184
185 /**
186 * Add admin menus/screens.
187 */
188 public function admin_menus() {
189
190 // Ignoring phpcs nonce rule as we are calling this function on admin action.
191 if ( empty( $_GET['page'] ) || 'cartflow-setup' !== $_GET['page'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
192 return;
193 }
194
195 add_dashboard_page( '', '', 'manage_options', 'cartflow-setup', '' );
196 }
197
198 /**
199 * Show the setup wizard.
200 */
201 public function setup_wizard() {
202
203 // Ignoring phpcs nonce rule as we are calling this function on admin action.
204 if ( empty( $_GET['page'] ) || 'cartflow-setup' !== $_GET['page'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
205 return;
206 }
207 $this->load_required_scripts();
208 $this->localize_vars();
209
210 // Diable loading of Query Monitor in footer.
211 add_filter( 'qm/dispatch/html', '__return_false' );
212
213 ob_start();
214 include_once CARTFLOWS_DIR . 'wizard/views/wizard-base.php';
215 }
216
217 /**
218 * Load scripts.
219 */
220 public function load_required_scripts() {
221 $handle = 'cartflows-wizard';
222 $build_path = CARTFLOWS_DIR . 'wizard/assets/build/';
223 $build_url = CARTFLOWS_URL . 'wizard/assets/build/';
224 $script_asset_path = $build_path . 'wizard-app.asset.php';
225 $script_info = file_exists( $script_asset_path )
226 ? include $script_asset_path
227 : array(
228 'dependencies' => array(),
229 'version' => CARTFLOWS_VER,
230 );
231
232 $script_dep = array_merge( $script_info['dependencies'], array( 'updates' ) );
233
234 wp_register_script(
235 $handle,
236 $build_url . 'wizard-app.js',
237 $script_dep,
238 $script_info['version'],
239 true
240 );
241
242 wp_register_style(
243 $handle,
244 $build_url . 'wizard-app.css',
245 array(),
246 CARTFLOWS_VER
247 );
248
249 wp_enqueue_script( $handle );
250 wp_enqueue_style( $handle );
251 wp_style_add_data( $handle, 'rtl', 'replace' );
252
253 wp_register_script(
254 'cartflows-setup-helper',
255 CARTFLOWS_URL . 'wizard/assets/js/helper.js',
256 array( 'jquery', 'wp-util', 'updates', 'media-upload' ),
257 CARTFLOWS_VER,
258 true
259 );
260 wp_enqueue_script( 'cartflows-setup-helper' );
261 wp_enqueue_media();
262 wp_enqueue_script( 'jquery-ui' );
263 }
264
265 /**
266 * Save usage tracking Settings.
267 */
268 public function save_usage_tracking_option() {
269
270 if ( ! current_user_can( 'manage_options' ) ) {
271 return;
272 }
273
274 check_ajax_referer( 'wcf-usage-tracking-option', 'security' );
275
276 $allow_usage_tracking = isset( $_POST['allow_usage_tracking'] ) && 'true' == $_POST['allow_usage_tracking'] ? 'yes' : 'no';
277
278 $usage_tracking = get_site_option( 'cf_usage_optin' );
279
280 if ( ( false === $usage_tracking ) || $allow_usage_tracking !== $usage_tracking ) {
281 update_site_option( 'cf_usage_optin', $allow_usage_tracking );
282 }
283
284 wp_send_json_success( get_site_option( 'cf_usage_optin' ) );
285 }
286
287 /**
288 * Redirect the user to create his first flow depending on which UI he is using.
289 */
290 public function get_final_page_link() {
291
292 $default_url = add_query_arg(
293 array(
294 'page' => CARTFLOWS_SLUG,
295 'path' => 'flows',
296 ),
297 admin_url()
298 );
299
300 return $default_url;
301 }
302
303
304 /**
305 * Localize variables in admin.
306 */
307 public function localize_vars() {
308
309 $vars = array();
310
311 $plugins = array(
312 array(
313 'name' => 'WooCommerce',
314 'slug' => 'woocommerce',
315 'status' => $this->get_plugin_status( 'woocommerce/woocommerce.php' ),
316 ),
317 array(
318 'name' => 'Cart Abandonment',
319 'slug' => 'woo-cart-abandonment-recovery',
320 'status' => $this->get_plugin_status( 'woo-cart-abandonment-recovery/woo-cart-abandonment-recovery.php' ),
321 ),
322 array(
323 'name' => 'Modern cart',
324 'slug' => 'modern-cart',
325 'status' => $this->get_plugin_status( 'modern-cart/modern-cart.php' ),
326 ),
327 array(
328 'name' => 'WooCommerce Payments',
329 'slug' => 'woocommerce-payments',
330 'status' => $this->get_plugin_status( 'woocommerce-payments/woocommerce-payments.php' ),
331 ),
332 array(
333 'name' => 'Spectra',
334 'slug' => 'ultimate-addons-for-gutenberg',
335 'status' => $this->get_plugin_status( 'ultimate-addons-for-gutenberg/ultimate-addons-for-gutenberg.php' ),
336 ),
337 );
338
339 $installed_plugins = get_plugins();
340
341 $page_builders = array(
342 'elementor' => array(
343 'slug' => 'elementor',
344 'init' => 'elementor/elementor.php',
345 'active' => is_plugin_active( 'elementor/elementor.php' ) ? 'yes' : 'no',
346 'install' => isset( $installed_plugins['elementor/elementor.php'] ) ? 'yes' : 'no',
347 ),
348 'beaver-builder' => array(
349 'slug' => 'beaver-builder-lite-version',
350 'init' => 'beaver-builder-lite-version/fl-builder.php',
351 'active' => is_plugin_active( 'beaver-builder-lite-version/fl-builder.php' ) ? 'yes' : 'no',
352 'install' => isset( $installed_plugins['beaver-builder-lite-version/fl-builder.php'] ) ? 'yes' : 'no',
353 ),
354 'divi' => array(
355 'slug' => 'divi',
356 'init' => 'divi',
357 'active' => 'yes',
358 'install' => 'NA', // Removed the Support of DIVI theme that is why it is set as NA.
359 ),
360 'gutenberg' => array(
361 'slug' => 'ultimate-addons-for-gutenberg',
362 'init' => 'ultimate-addons-for-gutenberg/ultimate-addons-for-gutenberg.php',
363 'active' => is_plugin_active( 'ultimate-addons-for-gutenberg/ultimate-addons-for-gutenberg.php' ) ? 'yes' : 'no',
364 'install' => isset( $installed_plugins['ultimate-addons-for-gutenberg/ultimate-addons-for-gutenberg.php'] ) ? 'yes' : 'no',
365 ),
366 'bricks-builder' => array(
367 'slug' => 'bricks',
368 'init' => 'bricks',
369 'active' => 'yes',
370 'install' => 'NA', // Bricks is an paid theme that is why, the install option is set to NA. As this will be enabled on the user's website before the CartFlows Install.
371 ),
372 // Intentionally installing the GB plugin when the other option is selected.
373 'other' => array(
374 'slug' => 'ultimate-addons-for-gutenberg',
375 'init' => 'ultimate-addons-for-gutenberg/ultimate-addons-for-gutenberg.php',
376 'active' => is_plugin_active( 'ultimate-addons-for-gutenberg/ultimate-addons-for-gutenberg.php' ) ? 'yes' : 'no',
377 'install' => isset( $installed_plugins['ultimate-addons-for-gutenberg/ultimate-addons-for-gutenberg.php'] ) ? 'yes' : 'no',
378 ),
379 );
380
381 $current_user = wp_get_current_user();
382
383 $vars = array(
384 'current_user_name' => ! empty( $current_user->user_firstname ) ? $current_user->user_firstname : $current_user->display_name,
385 'current_user_email' => ! empty( $current_user->user_email ) ? $current_user->user_email : '',
386 'plugins' => $plugins,
387 'page_builders' => $page_builders,
388 'active_page_builder' => WizardHelper::get_active_supported_builder( $page_builders ),
389 'ajax_url' => admin_url( 'admin-ajax.php' ),
390 'admin_url' => admin_url( 'admin.php' ),
391 'admin_base_url' => admin_url(),
392 'admin_index_url' => admin_url( 'index.php' ),
393 'default_page_builder' => \Cartflows_Helper::get_common_settings()['default_page_builder'],
394 'site_logo' => $this->get_site_logo_url(),
395 'template_import_errors' => array(
396 'api_errors' => array(
397 'title' => __( 'Oops!! Unexpected error occoured', 'cartflows' ),
398 'msg' => __( 'Import template API call failed. Please reload the page and try again!', 'cartflows' ),
399 ),
400 ),
401 'is_pro' => _is_cartflows_pro(),
402 'cf_pro_type' => defined( 'CARTFLOWS_PRO_PLUGIN_TYPE' ) ? CARTFLOWS_PRO_PLUGIN_TYPE : 'free',
403 'woocommerce_status' => $this->get_plugin_status( 'woocommerce/woocommerce.php' ),
404 );
405
406 $vars = apply_filters( 'cartflows_admin_wizard_localized_vars', $vars );
407
408 wp_localize_script( 'cartflows-wizard', 'cartflows_wizard', $vars );
409 }
410
411 /**
412 * Get customizer/site logo URL.
413 *
414 * @since 1.10.0
415 *
416 * @return $image_url Site logo URL
417 */
418 public function get_site_logo_url() {
419
420 $logo = get_theme_mod( 'custom_logo' );
421 $site_logo = '';
422
423 if ( ! empty( $logo ) ) {
424
425 $image = wp_get_attachment_image_src( $logo, 'full' );
426
427 $site_logo = array(
428 'id' => $logo,
429 'url' => $image[0] ? $image[0] : '',
430 'width' => $image[1] ? $image[1] : '',
431 'height' => $image[2] ? $image[2] : '',
432 );
433 }
434
435 return $site_logo;
436 }
437
438 /**
439 * Get plugin status
440 *
441 * @since 1.1.4
442 *
443 * @param string $plugin_init_file Plguin init file.
444 * @return mixed
445 */
446 public function get_plugin_status( $plugin_init_file ) {
447
448 $installed_plugins = get_plugins();
449
450 if ( ! isset( $installed_plugins[ $plugin_init_file ] ) ) {
451 return 'not-installed';
452 } elseif ( is_plugin_active( $plugin_init_file ) ) {
453 return 'active';
454 } else {
455 return 'inactive';
456 }
457 }
458
459 /**
460 * Function to hide Convert Pro popups during the onboarding wizard process.
461 *
462 * This function checks if the current page is the CartFlows setup page and if so, it sets the load parameter to false, effectively hiding the Convert Pro popups.
463 *
464 * @param bool $load The initial load state of the Convert Pro popups.
465 * @return bool The modified load state of the Convert Pro popups.
466 */
467 public function hide_convert_pro_popups( $load ) {
468
469 if ( ! empty( $_GET['page'] ) && 'cartflow-setup' === $_GET['page'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
470 $load = false;
471 }
472
473 return $load;
474 }
475 }
476
477 WizardCore::get_instance();
478