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

635 lines 18.0 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 )
345 );
346 $wizard_data = array(
347 'chart_type' => $chart_type,
348 'chart_id' => $chart_id,
349 );
350 $this->update_wizard_data( $wizard_data, false );
351 $response = array(
352 'success' => 1,
353 );
354 }
355 wp_send_json( $response );
356 exit;
357 }
358
359 /**
360 * Update wizard data.
361 *
362 * @param array $data Wizard data.
363 * @param bool $merge_option Merge wizard data.
364 * @return bool
365 */
366 private function update_wizard_data( $data = array(), $merge_option = true ) {
367 if ( $merge_option ) {
368 $this->wizard_data = get_option( self::OPTION_NAME, array() );
369 $data = array_merge( $this->wizard_data, $data );
370 }
371 return update_option( self::OPTION_NAME, $data );
372 }
373
374 /**
375 * Step: 3 Create draft page.
376 *
377 * @param bool $return_page_id Page ID.
378 */
379 private function setup_wizard_create_draft_page( $return_page_id = false ) {
380 $add_basic_shortcode = ! empty( $_POST['add_basic_shortcode'] ) ? sanitize_text_field( wp_unslash( $_POST['add_basic_shortcode'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
381 $add_basic_shortcode = 'true' === $add_basic_shortcode ? true : false;
382 $basic_shortcode = ! empty( $_POST['basic_shortcode'] ) ? filter_input( INPUT_POST, 'basic_shortcode', FILTER_SANITIZE_STRING ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
383
384 if ( ! $add_basic_shortcode ) {
385 wp_send_json(
386 array(
387 'status' => 1,
388 )
389 );
390 }
391 if ( function_exists( 'use_block_editor_for_post_type' ) && use_block_editor_for_post_type( 'page' ) ) {
392 $this->wizard_data = get_option( self::OPTION_NAME, array() );
393 if ( ! empty( $this->wizard_data['chart_id'] ) ) {
394 $block_data = array(
395 'id' => $this->wizard_data['chart_id'],
396 'lazy' => '-1',
397 'route' => 'chartSelect',
398 );
399 $basic_shortcode = '<!-- wp:visualizer/chart ' . wp_json_encode( $block_data ) . ' /-->';
400 }
401 }
402 $post_title = __( 'Visualizer Demo Page', 'visualizer' );
403 $page_id = post_exists( $post_title, '', '', 'page' );
404 $args = array(
405 'post_type' => 'page',
406 'post_title' => $post_title,
407 'post_content' => $add_basic_shortcode ? $basic_shortcode : '',
408 'post_status' => 'draft',
409 );
410 if ( ! $page_id ) {
411 $page_id = wp_insert_post( $args );
412 } else {
413 $args['ID'] = $page_id;
414 $page_id = wp_update_post( $args );
415 }
416
417 if ( $page_id ) {
418 // Delete previous meta data.
419 $meta = get_post_meta( $page_id );
420 foreach ( $meta as $key => $value ) {
421 delete_post_meta( $page_id, $key );
422 }
423 // Update wizard data.
424 $wizard_data['page_id'] = $page_id;
425 $this->update_wizard_data( $wizard_data );
426 }
427 if ( $return_page_id ) {
428 return $page_id;
429 }
430 wp_send_json(
431 array(
432 'status' => $page_id,
433 )
434 );
435 }
436
437 /**
438 * Step: 3 Install plugin.
439 */
440 private function setup_wizard_install_plugin() {
441 // phpcs:ignore WordPress.Security.NonceVerification.Missing
442 $slug = ! empty( $_POST['slug'] ) ? filter_input( INPUT_POST, 'slug', FILTER_SANITIZE_STRING ) : '';
443 if ( empty( $slug ) ) {
444 wp_send_json(
445 array(
446 'status' => 0,
447 'message' => __( 'No plugin specified.', 'visualizer' ),
448 )
449 );
450 }
451
452 if ( ! current_user_can( 'install_plugins' ) ) {
453 wp_send_json(
454 array(
455 'status' => 0,
456 'message' => __( 'Sorry, you are not allowed to install plugins on this site.', 'visualizer' ),
457 )
458 );
459 }
460
461 if ( ! empty( $slug ) ) {
462 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
463 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
464
465 $api = plugins_api(
466 'plugin_information',
467 array(
468 'slug' => sanitize_key( wp_unslash( $slug ) ),
469 'fields' => array(
470 'sections' => false,
471 ),
472 )
473 );
474
475 if ( is_wp_error( $api ) ) {
476 wp_send_json(
477 array(
478 'status' => 0,
479 'message' => $api->get_error_message(),
480 )
481 );
482 }
483
484 $skin = new WP_Ajax_Upgrader_Skin();
485 $upgrader = new Plugin_Upgrader( $skin );
486 $result = $upgrader->install( $api->download_link );
487 if ( is_wp_error( $result ) ) {
488 wp_send_json(
489 array(
490 'status' => 0,
491 'message' => $api->get_error_message(),
492 )
493 );
494 } elseif ( is_wp_error( $skin->result ) ) {
495 if ( 'folder_exists' !== $skin->result->get_error_code() ) {
496 wp_send_json(
497 array(
498 'status' => 0,
499 'message' => $skin->result->get_error_message(),
500 )
501 );
502 }
503 } elseif ( $skin->get_errors()->has_errors() ) {
504 if ( 'folder_exists' !== $skin->get_error_code() ) {
505 wp_send_json(
506 array(
507 'status' => 0,
508 'message' => $skin->get_error_message(),
509 )
510 );
511 }
512 } elseif ( is_null( $result ) ) {
513 global $wp_filesystem;
514 $status = array();
515 $status['message'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.', 'visualizer' );
516
517 // Pass through the error from WP_Filesystem if one was raised.
518 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
519 $status['message'] = esc_html( $wp_filesystem->errors->get_error_message() );
520 }
521
522 wp_send_json( $status );
523 }
524
525 activate_plugin( 'optimole-wp/optimole-wp.php' );
526 delete_transient( 'optml_fresh_install' );
527 // Update wizard data.
528 $wizard_data['enable_perfomance'] = true;
529 $this->update_wizard_data( $wizard_data );
530
531 wp_send_json(
532 array(
533 'status' => 1,
534 )
535 );
536 }
537 }
538
539 /**
540 * Step: 4 skip and subscribe process.
541 */
542 private function setup_wizard_subscribe_process() {
543 $segment = 0;
544 $wizard_data = get_option( self::OPTION_NAME, array() );
545 $chart_type = ! empty( $wizard_data['chart_type'] ) ? $wizard_data['chart_type'] : '';
546 $chart_id = ! empty( $wizard_data['chart_id'] ) ? $wizard_data['chart_id'] : '';
547 $page_id = ! empty( $wizard_data['page_id'] ) ? $wizard_data['page_id'] : '';
548 $response = array(
549 'status' => 0,
550 'redirect_to' => '',
551 'message' => '',
552 );
553
554 $with_subscribe = ! empty( $_POST['with_subscribe'] ) ? (bool) $_POST['with_subscribe'] : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
555 $email = ! empty( $_POST['email'] ) ? filter_input( INPUT_POST, 'email', FILTER_SANITIZE_EMAIL ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
556 $chart_types = array(
557 'pie' => 1,
558 'bar' => 2,
559 'line' => 3,
560 'tabular' => 4,
561 );
562 if ( $chart_type && ! empty( $chart_types[ $chart_type ] ) ) {
563 $segment = $chart_types[ $chart_type ];
564 }
565 if ( ! empty( $page_id ) ) {
566 $response = array(
567 'status' => 1,
568 'redirect_to' => get_edit_post_link( $page_id, 'db' ),
569 'message' => __( 'Redirecting to draft page', 'visualizer' ),
570 );
571 } else {
572 $response = array(
573 'status' => 1,
574 'redirect_to' => add_query_arg( 'page', 'visualizer', admin_url( 'admin.php' ) ),
575 'message' => __( 'Redirecting to visualizer dashboard', 'visualizer' ),
576 );
577 }
578
579 if ( $with_subscribe && is_email( $email ) ) {
580 $request_res = wp_remote_post(
581 VISUALIZER_SUBSCRIBE_API,
582 array(
583 'timeout' => 100,
584 'headers' => array(
585 'Content-Type' => 'application/json',
586 'Cache-Control' => 'no-cache',
587 'Accept' => 'application/json, */*;q=0.1',
588 ),
589 'body' => wp_json_encode(
590 array(
591 'slug' => 'visualizer',
592 'site' => home_url(),
593 'email' => $email,
594 'data' => array(
595 'segment' => $segment,
596 ),
597 )
598 ),
599 )
600 );
601 if ( ! is_wp_error( $request_res ) ) {
602 $body = json_decode( wp_remote_retrieve_body( $request_res ) );
603 if ( 'success' === $body->code ) {
604 $this->dismissWizard( false );
605 wp_send_json( $response );
606 }
607 }
608 wp_send_json(
609 array(
610 'status' => 0,
611 'redirect_to' => '',
612 'message' => '',
613 )
614 );
615 } else {
616 $this->dismissWizard( false );
617 wp_send_json( $response );
618 }
619 }
620
621 /**
622 * Filter chart setting.
623 *
624 * @param array $settings Chart settings.
625 * @return array
626 */
627 public function visualizer_filter_chart_settings( $settings ) {
628 $settings['backgroundColor'] = array(
629 'fill' => '#39c3d21a',
630 'fillOpacity' => '.1',
631 );
632 return $settings;
633 }
634 }
635