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

628 lines 17.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 $args = array(
216 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
217 'post_title' => 'Visualization',
218 'post_author' => get_current_user_id(),
219 'post_status' => 'publish',
220 'post_content' => $source->getData(),
221 );
222 $chart_id = wp_insert_post( $args );
223
224 if ( $chart_id && ! is_wp_error( $chart_id ) ) {
225 // Clear existing chart cache.
226 $cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $chart_id;
227 delete_transient( $cache_key );
228
229 update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, $chart_type );
230 update_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 1 );
231 update_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
232 update_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $series );
233 update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, '' );
234
235 $setting_series = array();
236 foreach ( $series as $s ) {
237 $setting_series[] = array(
238 'visibleInLegend' => '',
239 'lineWidth' => '',
240 'pointSize' => '',
241 'format' => '',
242 'curveType' => '',
243 'color' => '',
244 'role' => '',
245 );
246 }
247 update_post_meta(
248 $chart_id,
249 Visualizer_Plugin::CF_SETTINGS,
250 array(
251 'title' => '',
252 'titlePosition' => '',
253 'titleTextStyle' => array(
254 'color' => '#000',
255 ),
256 'legend' => array(
257 'position' => 'right',
258 'alignment' => 15,
259 'textStyle' => array(
260 'color' => '#000',
261 'text' => 'both',
262 ),
263 ),
264 'tooltip' => array(
265 'trigger' => 'focus',
266 'showColorCode' => 0,
267 'showColorCode' => 0,
268 ),
269 'animation' => array(
270 'startup' => 0,
271 'duration' => '',
272 'easing' => 'linear',
273 ),
274 'width' => '',
275 'height' => '',
276 'keepAspectRatio' => false,
277 'isStacked' => false,
278 'lazy_load_chart' => true,
279 'backgroundColor' => array(
280 'strokeWidth' => '',
281 'stroke' => '',
282 'fill' => '',
283 ),
284 'chartArea' => array(
285 'left' => '',
286 'top' => '',
287 'width' => '',
288 'height' => '',
289 ),
290 'focusTarget' => 'datum',
291 'series' => $setting_series,
292 'slices' => array(),
293 'vAxis' => array(
294 'title' => '',
295 'textPosition' => '',
296 'direction' => 1,
297 'baselineColor' => '#000',
298 'textStyle' => array(
299 'color' => '#000',
300 ),
301 'format' => '',
302 'gridlines' => array(
303 'count' => '',
304 'color' => '#ccc',
305 ),
306 'minorGridlines' => array(
307 'count' => '',
308 'color' => '',
309 ),
310 'viewWindow' => array(
311 'max' => '',
312 'min' => '',
313 ),
314 ),
315 'hAxis' => array(
316 'title' => '',
317 'textPosition' => '',
318 'direction' => 1,
319 'baselineColor' => '#000',
320 'textStyle' => array(
321 'color' => '#000',
322 ),
323 'format' => '',
324 'gridlines' => array(
325 'count' => '',
326 'color' => '#ccc',
327 ),
328 'minorGridlines' => array(
329 'count' => '',
330 'color' => '',
331 ),
332 'viewWindow' => array(
333 'max' => '',
334 'min' => '',
335 ),
336 ),
337 )
338 );
339 $wizard_data = array(
340 'chart_type' => $chart_type,
341 'chart_id' => $chart_id,
342 );
343 $this->update_wizard_data( $wizard_data, false );
344 $response = array(
345 'success' => 1,
346 );
347 }
348 wp_send_json( $response );
349 exit;
350 }
351
352 /**
353 * Update wizard data.
354 *
355 * @param array $data Wizard data.
356 * @param bool $merge_option Merge wizard data.
357 * @return bool
358 */
359 private function update_wizard_data( $data = array(), $merge_option = true ) {
360 if ( $merge_option ) {
361 $this->wizard_data = get_option( self::OPTION_NAME, array() );
362 $data = array_merge( $this->wizard_data, $data );
363 }
364 return update_option( self::OPTION_NAME, $data );
365 }
366
367 /**
368 * Step: 3 Create draft page.
369 *
370 * @param bool $return_page_id Page ID.
371 */
372 private function setup_wizard_create_draft_page( $return_page_id = false ) {
373 $add_basic_shortcode = ! empty( $_POST['add_basic_shortcode'] ) ? sanitize_text_field( wp_unslash( $_POST['add_basic_shortcode'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
374 $add_basic_shortcode = 'true' === $add_basic_shortcode ? true : false;
375 $basic_shortcode = ! empty( $_POST['basic_shortcode'] ) ? filter_input( INPUT_POST, 'basic_shortcode', FILTER_SANITIZE_STRING ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
376
377 if ( ! $add_basic_shortcode ) {
378 wp_send_json(
379 array(
380 'status' => 1,
381 )
382 );
383 }
384 if ( function_exists( 'use_block_editor_for_post_type' ) && use_block_editor_for_post_type( 'page' ) ) {
385 $this->wizard_data = get_option( self::OPTION_NAME, array() );
386 if ( ! empty( $this->wizard_data['chart_id'] ) ) {
387 $block_data = array(
388 'id' => $this->wizard_data['chart_id'],
389 'lazy' => '-1',
390 'route' => 'chartSelect',
391 );
392 $basic_shortcode = '<!-- wp:visualizer/chart ' . wp_json_encode( $block_data ) . ' /-->';
393 }
394 }
395 $post_title = __( 'Visualizer Demo Page', 'visualizer' );
396 $page_id = post_exists( $post_title, '', '', 'page' );
397 $args = array(
398 'post_type' => 'page',
399 'post_title' => $post_title,
400 'post_content' => $add_basic_shortcode ? $basic_shortcode : '',
401 'post_status' => 'draft',
402 );
403 if ( ! $page_id ) {
404 $page_id = wp_insert_post( $args );
405 } else {
406 $args['ID'] = $page_id;
407 $page_id = wp_update_post( $args );
408 }
409
410 if ( $page_id ) {
411 // Delete previous meta data.
412 $meta = get_post_meta( $page_id );
413 foreach ( $meta as $key => $value ) {
414 delete_post_meta( $page_id, $key );
415 }
416 // Update wizard data.
417 $wizard_data['page_id'] = $page_id;
418 $this->update_wizard_data( $wizard_data );
419 }
420 if ( $return_page_id ) {
421 return $page_id;
422 }
423 wp_send_json(
424 array(
425 'status' => $page_id,
426 )
427 );
428 }
429
430 /**
431 * Step: 3 Install plugin.
432 */
433 private function setup_wizard_install_plugin() {
434 // phpcs:ignore WordPress.Security.NonceVerification.Missing
435 $slug = ! empty( $_POST['slug'] ) ? filter_input( INPUT_POST, 'slug', FILTER_SANITIZE_STRING ) : '';
436 if ( empty( $slug ) ) {
437 wp_send_json(
438 array(
439 'status' => 0,
440 'message' => __( 'No plugin specified.', 'visualizer' ),
441 )
442 );
443 }
444
445 if ( ! current_user_can( 'install_plugins' ) ) {
446 wp_send_json(
447 array(
448 'status' => 0,
449 'message' => __( 'Sorry, you are not allowed to install plugins on this site.', 'visualizer' ),
450 )
451 );
452 }
453
454 if ( ! empty( $slug ) ) {
455 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
456 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
457
458 $api = plugins_api(
459 'plugin_information',
460 array(
461 'slug' => sanitize_key( wp_unslash( $slug ) ),
462 'fields' => array(
463 'sections' => false,
464 ),
465 )
466 );
467
468 if ( is_wp_error( $api ) ) {
469 wp_send_json(
470 array(
471 'status' => 0,
472 'message' => $api->get_error_message(),
473 )
474 );
475 }
476
477 $skin = new WP_Ajax_Upgrader_Skin();
478 $upgrader = new Plugin_Upgrader( $skin );
479 $result = $upgrader->install( $api->download_link );
480 if ( is_wp_error( $result ) ) {
481 wp_send_json(
482 array(
483 'status' => 0,
484 'message' => $api->get_error_message(),
485 )
486 );
487 } elseif ( is_wp_error( $skin->result ) ) {
488 if ( 'folder_exists' !== $skin->result->get_error_code() ) {
489 wp_send_json(
490 array(
491 'status' => 0,
492 'message' => $skin->result->get_error_message(),
493 )
494 );
495 }
496 } elseif ( $skin->get_errors()->has_errors() ) {
497 if ( 'folder_exists' !== $skin->get_error_code() ) {
498 wp_send_json(
499 array(
500 'status' => 0,
501 'message' => $skin->get_error_message(),
502 )
503 );
504 }
505 } elseif ( is_null( $result ) ) {
506 global $wp_filesystem;
507 $status = array();
508 $status['message'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.', 'visualizer' );
509
510 // Pass through the error from WP_Filesystem if one was raised.
511 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
512 $status['message'] = esc_html( $wp_filesystem->errors->get_error_message() );
513 }
514
515 wp_send_json( $status );
516 }
517
518 activate_plugin( 'optimole-wp/optimole-wp.php' );
519 delete_transient( 'optml_fresh_install' );
520 // Update wizard data.
521 $wizard_data['enable_perfomance'] = true;
522 $this->update_wizard_data( $wizard_data );
523
524 wp_send_json(
525 array(
526 'status' => 1,
527 )
528 );
529 }
530 }
531
532 /**
533 * Step: 4 skip and subscribe process.
534 */
535 private function setup_wizard_subscribe_process() {
536 $segment = 0;
537 $wizard_data = get_option( self::OPTION_NAME, array() );
538 $chart_type = ! empty( $wizard_data['chart_type'] ) ? $wizard_data['chart_type'] : '';
539 $chart_id = ! empty( $wizard_data['chart_id'] ) ? $wizard_data['chart_id'] : '';
540 $page_id = ! empty( $wizard_data['page_id'] ) ? $wizard_data['page_id'] : '';
541 $response = array(
542 'status' => 0,
543 'redirect_to' => '',
544 'message' => '',
545 );
546
547 $with_subscribe = ! empty( $_POST['with_subscribe'] ) ? (bool) $_POST['with_subscribe'] : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
548 $email = ! empty( $_POST['email'] ) ? filter_input( INPUT_POST, 'email', FILTER_SANITIZE_EMAIL ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
549 $chart_types = array(
550 'pie' => 1,
551 'bar' => 2,
552 'line' => 3,
553 'tabular' => 4,
554 );
555 if ( $chart_type && ! empty( $chart_types[ $chart_type ] ) ) {
556 $segment = $chart_types[ $chart_type ];
557 }
558 if ( ! empty( $page_id ) ) {
559 $response = array(
560 'status' => 1,
561 'redirect_to' => get_edit_post_link( $page_id, 'db' ),
562 'message' => __( 'Redirecting to draft page', 'visualizer' ),
563 );
564 } else {
565 $response = array(
566 'status' => 1,
567 'redirect_to' => add_query_arg( 'page', 'visualizer', admin_url( 'admin.php' ) ),
568 'message' => __( 'Redirecting to visualizer dashboard', 'visualizer' ),
569 );
570 }
571
572 if ( $with_subscribe && is_email( $email ) ) {
573 $request_res = wp_remote_post(
574 VISUALIZER_SUBSCRIBE_API,
575 array(
576 'timeout' => 100,
577 'headers' => array(
578 'Content-Type' => 'application/json',
579 'Cache-Control' => 'no-cache',
580 'Accept' => 'application/json, */*;q=0.1',
581 ),
582 'body' => wp_json_encode(
583 array(
584 'slug' => 'visualizer',
585 'site' => home_url(),
586 'email' => $email,
587 'data' => array(
588 'segment' => $segment,
589 ),
590 )
591 ),
592 )
593 );
594 if ( ! is_wp_error( $request_res ) ) {
595 $body = json_decode( wp_remote_retrieve_body( $request_res ) );
596 if ( 'success' === $body->code ) {
597 $this->dismissWizard( false );
598 wp_send_json( $response );
599 }
600 }
601 wp_send_json(
602 array(
603 'status' => 0,
604 'redirect_to' => '',
605 'message' => '',
606 )
607 );
608 } else {
609 $this->dismissWizard( false );
610 wp_send_json( $response );
611 }
612 }
613
614 /**
615 * Filter chart setting.
616 *
617 * @param array $settings Chart settings.
618 * @return array
619 */
620 public function visualizer_filter_chart_settings( $settings ) {
621 $settings['backgroundColor'] = array(
622 'fill' => '#39c3d21a',
623 'fillOpacity' => '.1',
624 );
625 return $settings;
626 }
627 }
628