PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 4.0.2
Visualizer – Tables & Charts Manager with Built-in AI Generator v4.0.2
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 3.10.4 All 148 releases
visualizer / classes / Visualizer / Module / Wizard.php

Wizard.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 4.0.2, at classes/Visualizer/Module/Wizard.php

688 lines 19.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The class for handle setup wizard stuff.
4 *
5 * @category Visualizer
6 * @package Module
7 *
8 * @since 3.9
9 */
10
11 /**
12 * Setup wizard main class.
13 */
14 class Visualizer_Module_Wizard extends Visualizer_Module {
15
16 /**
17 * Store name class.
18 */
19 const NAME = __CLASS__;
20
21 /**
22 * Option name.
23 */
24 const OPTION_NAME = 'visualizer_wizard_data';
25
26 /**
27 * Wizard data
28 *
29 * @access private
30 * @var $wizard_data array
31 */
32 private $wizard_data = array();
33
34 /**
35 * Constructor.
36 *
37 * @since 3.9
38 *
39 * @access public
40 *
41 * @param Visualizer_Plugin $plugin The instance of the plugin.
42 */
43 public function __construct( Visualizer_Plugin $plugin ) {
44 parent::__construct( $plugin );
45 $this->_addFilter( 'admin_body_class', 'addWizardClasses' );
46 $this->_addAction( 'admin_action_visualizer_dismiss_wizard', 'dismissWizard' );
47 $this->_addAction( 'admin_menu', 'registerAdminMenu' );
48 $this->_addAction( 'wp_ajax_visualizer_wizard_step_process', 'visualizer_wizard_step_process' );
49 $this->wizard_data = get_option( self::OPTION_NAME, array() );
50 }
51
52 /**
53 * Registers admin menu for visualizer library.
54 *
55 * @since 1.0.0
56 *
57 * @access public
58 */
59 public function registerAdminMenu() {
60 if ( ! Visualizer_Module::is_pro() && get_option( 'visualizer_fresh_install', false ) ) {
61 $hook = add_submenu_page(
62 Visualizer_Plugin::NAME,
63 __( 'Setup Wizard', 'visualizer' ),
64 __( 'Setup Wizard', 'visualizer' ),
65 'manage_options',
66 'visualizer-setup-wizard',
67 array(
68 $this,
69 'visualizer_setup_wizard_page',
70 )
71 );
72 add_action( "load-$hook", array( $this, 'visualizer_load_setup_wizard_page' ) );
73 }
74 }
75
76
77 /**
78 * Method to register the setup wizard page.
79 *
80 * @access public
81 */
82 public function visualizer_setup_wizard_page() {
83 include VISUALIZER_ABSPATH . '/templates/setup-wizard.php';
84 }
85
86 /**
87 * Add classes to make the wizard full screen.
88 *
89 * @param string $classes Body classes.
90 * @return string
91 */
92 public static function addWizardClasses( $classes ) {
93 if ( get_option( 'visualizer_fresh_install', false ) ) {
94 $classes .= ' vz-wizard-fullscreen';
95 }
96 return trim( $classes );
97 }
98
99 /**
100 * Load setup wizard page.
101 *
102 * @access public
103 */
104 public function visualizer_load_setup_wizard_page() {
105 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
106 if ( isset( $_GET['page'] ) && 'visualizer-setup-wizard' === $_GET['page'] ) {
107 remove_all_actions( 'admin_notices' );
108 }
109 add_action( 'admin_enqueue_scripts', array( $this, 'visualizer_enqueue_setup_wizard_scripts' ) );
110 add_filter( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, array( $this, 'visualizer_filter_chart_settings' ) );
111 }
112
113 /**
114 * Enqueue setup wizard required scripts.
115 *
116 * @access public
117 */
118 public function visualizer_enqueue_setup_wizard_scripts() {
119 wp_enqueue_style( 'jquery-slick', VISUALIZER_ABSURL . 'css/lib/slick.min.css', array(), Visualizer_Plugin::VERSION );
120 wp_enqueue_style( 'jquery-smart-wizard', VISUALIZER_ABSURL . 'css/lib/smart_wizard_all.min.css', array(), Visualizer_Plugin::VERSION );
121 wp_enqueue_style( 'visualizer-setup-wizard', VISUALIZER_ABSURL . 'css/style-wizard.css', array(), Visualizer_Plugin::VERSION, 'all' );
122
123 wp_register_script( 'jquery-slick', VISUALIZER_ABSURL . 'js/lib/slick.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true );
124 wp_enqueue_script( 'jquery-smart-wizard', VISUALIZER_ABSURL . 'js/lib/jquery.smartWizard.min.js', array( 'jquery', 'jquery-slick', 'clipboard' ), Visualizer_Plugin::VERSION, true );
125 wp_enqueue_script( 'visualizer-setup-wizard', VISUALIZER_ABSURL . 'js/setup-wizard.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true );
126 wp_localize_script(
127 'visualizer-setup-wizard',
128 'visualizerSetupWizardData',
129 array(
130 'adminPage' => add_query_arg( 'page', Visualizer_Plugin::NAME, admin_url( 'admin.php' ) ),
131 'ajax' => array(
132 'url' => admin_url( 'admin-ajax.php' ),
133 'security' => wp_create_nonce( VISUALIZER_ABSPATH ),
134 ),
135 'errorMessages' => array(
136 'requiredEmail' => __( 'This field is required.', 'visualizer' ),
137 'invalidEmail' => __( 'Please enter a valid email address.', 'visualizer' ),
138 ),
139 'nextButtonText' => __( 'Next Step', 'visualizer' ),
140 'backButtonText' => __( 'Back', 'visualizer' ),
141 'draftPageButtonText' => array(
142 'firstButtonText' => __( 'Save And Continue', 'visualizer' ),
143 'secondButtonText' => __( 'Continue', 'visualizer' ),
144 ),
145 )
146 );
147 }
148
149 /**
150 * Dismiss setup wizard.
151 *
152 * @param bool $redirect_to_dashboard Redirect to dashboard.
153 * @return bool|void
154 */
155 public function dismissWizard( $redirect_to_dashboard = true ) {
156 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
157 $status = isset( $_REQUEST['status'] ) ? (int) $_REQUEST['status'] : 0;
158 update_option( 'visualizer_fresh_install', $status );
159 delete_option( 'visualizer_wizard_data' );
160 if ( false !== $redirect_to_dashboard ) {
161 wp_safe_redirect( add_query_arg( 'page', Visualizer_Plugin::NAME, admin_url( 'admin.php' ) ) );
162 exit;
163 }
164 return true;
165 }
166
167 /**
168 * Setup wizard process.
169 */
170 public function visualizer_wizard_step_process() {
171 check_ajax_referer( VISUALIZER_ABSPATH, 'security' );
172 $step = ! empty( $_POST['step'] ) ? sanitize_text_field( wp_unslash( $_POST['step'] ) ) : 1;
173 switch ( $step ) {
174 case 'step_2':
175 $this->setup_wizard_import_chart();
176 break;
177 case 'step_4':
178 $this->setup_wizard_install_plugin();
179 break;
180 case 'step_subscribe':
181 $this->setup_wizard_subscribe_process();
182 break;
183 case 'create_draft_page':
184 $this->setup_wizard_create_draft_page();
185 break;
186 default:
187 wp_send_json( array( 'status' => 0 ) );
188 break;
189 }
190 }
191
192 /**
193 * Step: 2 import chart.
194 */
195 private function setup_wizard_import_chart() {
196 // phpcs:ignore WordPress.Security.NonceVerification.Missing
197 $chart_type = ! empty( $_POST['chart_type'] ) ? sanitize_text_field( wp_unslash( $_POST['chart_type'] ) ) : '';
198 $chart_status = Visualizer_Module_Admin::checkChartStatus( $chart_type );
199 if ( ! $chart_status ) {
200 wp_send_json(
201 array(
202 'success' => 0,
203 )
204 );
205 exit;
206 }
207
208 $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $chart_type . '.csv' );
209 $source->fetch();
210 $series = $source->getSeries();
211 $response = array(
212 'success' => 2,
213 'message' => __( 'Something went wrong while importing the chart', 'visualizer' ),
214 );
215
216 $data = $source->getData();
217 $args = array(
218 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
219 'post_title' => 'Visualization',
220 'post_author' => get_current_user_id(),
221 'post_status' => 'publish',
222 'post_content' => $data,
223 );
224 $chart_id = wp_insert_post( $args );
225
226 if ( $chart_id && ! is_wp_error( $chart_id ) ) {
227 // Clear existing chart cache.
228 $cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $chart_id;
229 delete_transient( $cache_key );
230
231 update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, $chart_type );
232 update_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 1 );
233 update_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
234 update_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $series );
235 update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, '' );
236
237 $data = maybe_unserialize( $data );
238 $setting_series = array();
239 $setting_slices = array();
240 foreach ( $data as $s ) {
241 $setting_series[] = array(
242 'visibleInLegend' => '',
243 'lineWidth' => '',
244 'pointSize' => '',
245 'format' => '',
246 'curveType' => '',
247 'color' => '',
248 'role' => '',
249 );
250 $setting_slices[] = array(
251 'offset' => 0,
252 'color' => '',
253 );
254 }
255 update_post_meta(
256 $chart_id,
257 Visualizer_Plugin::CF_SETTINGS,
258 array(
259 'title' => '',
260 'titlePosition' => '',
261 'titleTextStyle' => array(
262 'color' => '#000',
263 ),
264 'legend' => array(
265 'position' => 'right',
266 'alignment' => 15,
267 'textStyle' => array(
268 'color' => '#000',
269 'text' => 'both',
270 ),
271 ),
272 'tooltip' => array(
273 'trigger' => 'focus',
274 'showColorCode' => 0,
275 'showColorCode' => 0,
276 ),
277 'animation' => array(
278 'startup' => 0,
279 'duration' => '',
280 'easing' => 'linear',
281 ),
282 'width' => '',
283 'height' => '',
284 'keepAspectRatio' => false,
285 'isStacked' => false,
286 'lazy_load_chart' => true,
287 'backgroundColor' => array(
288 'strokeWidth' => '',
289 'stroke' => '',
290 'fill' => '',
291 ),
292 'chartArea' => array(
293 'left' => '',
294 'top' => '',
295 'width' => '',
296 'height' => '',
297 ),
298 'focusTarget' => 'datum',
299 'series' => $setting_series,
300 'slices' => $setting_slices,
301 'vAxis' => array(
302 'title' => '',
303 'textPosition' => '',
304 'direction' => 1,
305 'baselineColor' => '#000',
306 'textStyle' => array(
307 'color' => '#000',
308 ),
309 'format' => '',
310 'gridlines' => array(
311 'count' => '',
312 'color' => '#ccc',
313 ),
314 'minorGridlines' => array(
315 'count' => '',
316 'color' => '',
317 ),
318 'viewWindow' => array(
319 'max' => '',
320 'min' => '',
321 ),
322 ),
323 'hAxis' => array(
324 'title' => '',
325 'textPosition' => '',
326 'direction' => 1,
327 'baselineColor' => '#000',
328 'textStyle' => array(
329 'color' => '#000',
330 ),
331 'format' => '',
332 'gridlines' => array(
333 'count' => '',
334 'color' => '#ccc',
335 ),
336 'minorGridlines' => array(
337 'count' => '',
338 'color' => '',
339 ),
340 'viewWindow' => array(
341 'max' => '',
342 'min' => '',
343 ),
344 ),
345 'customcss' => array(
346 'headerRow' => array(
347 'background-color' => '',
348 'color' => '',
349 'transform' => '',
350 ),
351 'tableRow' => array(
352 'background-color' => '',
353 'color' => '',
354 'transform' => '',
355 ),
356 'oddTableRow' => array(
357 'background-color' => '',
358 'color' => '',
359 'transform' => '',
360 ),
361 'selectedTableRow' => array(
362 'background-color' => '',
363 'color' => '',
364 'transform' => '',
365 ),
366 'hoverTableRow' => array(
367 'background-color' => '',
368 'color' => '',
369 'transform' => '',
370 ),
371 'headerCell' => array(
372 'background-color' => '',
373 'color' => '',
374 'transform' => '',
375 ),
376 'tableCell' => array(
377 'background-color' => '',
378 'color' => '',
379 'transform' => '',
380 ),
381 ),
382 )
383 );
384 $wizard_data = array(
385 'chart_type' => $chart_type,
386 'chart_id' => $chart_id,
387 );
388 $this->update_wizard_data( $wizard_data, false );
389 $response = array(
390 'success' => 1,
391 'chart_id' => $chart_id,
392 );
393 }
394 wp_send_json( $response );
395 exit;
396 }
397
398 /**
399 * Update wizard data.
400 *
401 * @param array $data Wizard data.
402 * @param bool $merge_option Merge wizard data.
403 * @return bool
404 */
405 private function update_wizard_data( $data = array(), $merge_option = true ) {
406 if ( $merge_option ) {
407 $this->wizard_data = get_option( self::OPTION_NAME, array() );
408 $data = array_merge( $this->wizard_data, $data );
409 }
410 return update_option( self::OPTION_NAME, $data );
411 }
412
413 /**
414 * Step: 3 Create draft page.
415 *
416 * @param bool $return_page_id Page ID.
417 */
418 private function setup_wizard_create_draft_page( $return_page_id = false ) {
419 $add_basic_shortcode = ! empty( $_POST['add_basic_shortcode'] ) ? sanitize_text_field( wp_unslash( $_POST['add_basic_shortcode'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
420 $add_basic_shortcode = 'true' === $add_basic_shortcode ? true : false;
421 $basic_shortcode = ! empty( $_POST['basic_shortcode'] ) ? sanitize_text_field( wp_unslash( $_POST['basic_shortcode'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
422
423 if ( ! $add_basic_shortcode ) {
424 wp_send_json(
425 array(
426 'status' => 1,
427 )
428 );
429 }
430 if ( function_exists( 'use_block_editor_for_post_type' ) && use_block_editor_for_post_type( 'page' ) ) {
431 $this->wizard_data = get_option( self::OPTION_NAME, array() );
432 if ( ! empty( $this->wizard_data['chart_id'] ) ) {
433 $block_data = array(
434 'id' => $this->wizard_data['chart_id'],
435 'lazy' => '-1',
436 'route' => 'chartSelect',
437 );
438 $basic_shortcode = '<!-- wp:visualizer/chart ' . wp_json_encode( $block_data ) . ' /-->';
439 }
440 }
441 $post_title = __( 'Visualizer Demo Page', 'visualizer' );
442 $page_id = post_exists( $post_title, '', '', 'page' );
443 $args = array(
444 'post_type' => 'page',
445 'post_title' => $post_title,
446 'post_content' => $add_basic_shortcode ? $basic_shortcode : '',
447 'post_status' => 'draft',
448 );
449 if ( ! $page_id ) {
450 $page_id = wp_insert_post( $args );
451 } else {
452 $args['ID'] = $page_id;
453 $page_id = wp_update_post( $args );
454 }
455
456 if ( $page_id ) {
457 // Delete previous meta data.
458 $meta = get_post_meta( $page_id );
459 foreach ( $meta as $key => $value ) {
460 delete_post_meta( $page_id, $key );
461 }
462 // Update wizard data.
463 $wizard_data['page_id'] = $page_id;
464 $this->update_wizard_data( $wizard_data );
465 }
466 if ( $return_page_id ) {
467 return $page_id;
468 }
469 wp_send_json(
470 array(
471 'status' => $page_id,
472 )
473 );
474 }
475
476 /**
477 * Step: 3 Install plugin.
478 */
479 private function setup_wizard_install_plugin() {
480 // phpcs:ignore WordPress.Security.NonceVerification.Missing
481 $slug = ! empty( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : '';
482 if ( empty( $slug ) ) {
483 wp_send_json(
484 array(
485 'status' => 0,
486 'message' => __( 'No plugin specified.', 'visualizer' ),
487 )
488 );
489 }
490
491 if ( ! current_user_can( 'install_plugins' ) ) {
492 wp_send_json(
493 array(
494 'status' => 0,
495 'message' => __( 'Sorry, you are not allowed to install plugins on this site.', 'visualizer' ),
496 )
497 );
498 }
499
500 if ( ! empty( $slug ) ) {
501 $wizard_data = get_option( self::OPTION_NAME, array() );
502 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
503 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
504 require_once ABSPATH . 'wp-admin/includes/plugin.php';
505
506 $api = plugins_api(
507 'plugin_information',
508 array(
509 'slug' => sanitize_key( wp_unslash( $slug ) ),
510 'fields' => array(
511 'sections' => false,
512 ),
513 )
514 );
515
516 if ( is_wp_error( $api ) ) {
517 wp_send_json(
518 array(
519 'status' => 0,
520 'message' => $api->get_error_message(),
521 )
522 );
523 }
524
525 $skin = new WP_Ajax_Upgrader_Skin();
526 $upgrader = new Plugin_Upgrader( $skin );
527 $result = $upgrader->install( $api->download_link );
528 if ( is_wp_error( $result ) ) {
529 wp_send_json(
530 array(
531 'status' => 0,
532 'message' => $api->get_error_message(),
533 )
534 );
535 } elseif ( is_wp_error( $skin->result ) ) {
536 if ( 'folder_exists' !== $skin->result->get_error_code() ) {
537 wp_send_json(
538 array(
539 'status' => 0,
540 'message' => $skin->result->get_error_message(),
541 )
542 );
543 }
544 } elseif ( $skin->get_errors()->has_errors() ) {
545 if ( 'folder_exists' !== $skin->get_error_code() ) {
546 wp_send_json(
547 array(
548 'status' => 0,
549 'message' => $skin->get_error_message(),
550 )
551 );
552 }
553 } elseif ( is_null( $result ) ) {
554 global $wp_filesystem;
555 $status = array();
556 $status['message'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.', 'visualizer' );
557
558 // Pass through the error from WP_Filesystem if one was raised.
559 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
560 $status['message'] = esc_html( $wp_filesystem->errors->get_error_message() );
561 }
562
563 wp_send_json( $status );
564 }
565
566 $installed_plugins = get_plugins( '/' . sanitize_key( wp_unslash( $slug ) ) );
567 if ( ! empty( $installed_plugins ) ) {
568 $plugin_files = array_keys( $installed_plugins );
569 $plugin_file = sanitize_key( wp_unslash( $slug ) ) . '/' . $plugin_files[0];
570 activate_plugin( $plugin_file );
571 }
572 $wizard_data_updated = false;
573 if ( 'optimole-wp' === $slug ) {
574 delete_transient( 'optml_fresh_install' );
575 // Update wizard data.
576 $wizard_data['enable_perfomance'] = true;
577 $wizard_data_updated = true;
578 }
579 if ( 'otter-blocks' === $slug ) {
580 // Update wizard data.
581 $wizard_data['enable_otter_blocks'] = true;
582 $wizard_data_updated = true;
583 // Suppress Otter's post-activation redirects so the user stays in Visualizer.
584 update_option( 'themeisle_blocks_settings_redirect', '0' );
585 update_option( 'themeisle_blocks_settings_onboarding', '0' );
586 }
587 if ( 'wp-cloudflare-page-cache' === $slug ) {
588 // Update wizard data.
589 $wizard_data['enable_page_cache'] = true;
590 $wizard_data_updated = true;
591 update_option( 'swcfpc_dashboard_redirect', false );
592 }
593 if ( $wizard_data_updated ) {
594 $this->update_wizard_data( $wizard_data );
595 }
596
597 wp_send_json(
598 array(
599 'status' => 1,
600 )
601 );
602 }
603 }
604
605 /**
606 * Step: 4 skip and subscribe process.
607 */
608 private function setup_wizard_subscribe_process() {
609 $segment = 0;
610 $wizard_data = get_option( self::OPTION_NAME, array() );
611 $chart_type = ! empty( $wizard_data['chart_type'] ) ? $wizard_data['chart_type'] : '';
612 $chart_id = ! empty( $wizard_data['chart_id'] ) ? $wizard_data['chart_id'] : '';
613 $page_id = ! empty( $wizard_data['page_id'] ) ? $wizard_data['page_id'] : '';
614 $response = array(
615 'status' => 0,
616 'redirect_to' => '',
617 'message' => '',
618 );
619
620 $with_subscribe = ! empty( $_POST['with_subscribe'] ) ? (bool) $_POST['with_subscribe'] : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
621 $email = ! empty( $_POST['email'] ) ? filter_input( INPUT_POST, 'email', FILTER_SANITIZE_EMAIL ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
622 $chart_types = array(
623 'pie' => 1,
624 'bar' => 2,
625 'line' => 3,
626 'tabular' => 4,
627 );
628 if ( $chart_type && ! empty( $chart_types[ $chart_type ] ) ) {
629 $segment = $chart_types[ $chart_type ];
630 }
631 if ( ! empty( $page_id ) ) {
632 $response = array(
633 'status' => 1,
634 'redirect_to' => get_edit_post_link( $page_id, 'db' ),
635 'message' => __( 'Redirecting to draft page', 'visualizer' ),
636 );
637 } else {
638 $response = array(
639 'status' => 1,
640 'redirect_to' => add_query_arg( 'page', 'visualizer', admin_url( 'admin.php' ) ),
641 'message' => __( 'Redirecting to visualizer dashboard', 'visualizer' ),
642 );
643 }
644
645 if ( $with_subscribe && is_email( $email ) ) {
646 wp_remote_post(
647 VISUALIZER_SUBSCRIBE_API,
648 array(
649 'timeout' => 5,
650 'blocking' => false,
651 'headers' => array(
652 'Content-Type' => 'application/json',
653 'Cache-Control' => 'no-cache',
654 'Accept' => 'application/json, */*;q=0.1',
655 ),
656 'body' => wp_json_encode(
657 array(
658 'slug' => 'visualizer',
659 'site' => home_url(),
660 'email' => $email,
661 'data' => array(
662 'segment' => $segment,
663 ),
664 )
665 ),
666 )
667 );
668 }
669
670 $this->dismissWizard( false );
671 wp_send_json( $response );
672 }
673
674 /**
675 * Filter chart setting.
676 *
677 * @param array $settings Chart settings.
678 * @return array
679 */
680 public function visualizer_filter_chart_settings( $settings ) {
681 $settings['backgroundColor'] = array(
682 'fill' => '#39c3d21a',
683 'fillOpacity' => '.1',
684 );
685 return $settings;
686 }
687 }
688