PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.11.7
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.11.7
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 3.11.7, at classes/Visualizer/Module/Wizard.php

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