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

529 lines 15.4 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 $response = array(
210 'success' => 2,
211 'message' => __( 'Something went wrong while importing the chart', 'visualizer' ),
212 );
213
214 $args = array(
215 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
216 'post_title' => 'Visualization',
217 'post_author' => get_current_user_id(),
218 'post_status' => 'publish',
219 'post_content' => $source->getData(),
220 );
221 $chart_id = wp_insert_post( $args );
222
223 if ( $chart_id && ! is_wp_error( $chart_id ) ) {
224 // Clear existing chart cache.
225 $cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $chart_id;
226 delete_transient( $cache_key );
227
228 update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, $chart_type );
229 update_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 1 );
230 update_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
231 update_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
232 update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, '' );
233 update_post_meta(
234 $chart_id,
235 Visualizer_Plugin::CF_SETTINGS,
236 array(
237 'focusTarget' => 'datum',
238 )
239 );
240 $wizard_data = array(
241 'chart_type' => $chart_type,
242 'chart_id' => $chart_id,
243 );
244 $this->update_wizard_data( $wizard_data, false );
245 $response = array(
246 'success' => 1,
247 );
248 }
249 wp_send_json( $response );
250 exit;
251 }
252
253 /**
254 * Update wizard data.
255 *
256 * @param array $data Wizard data.
257 * @param bool $merge_option Merge wizard data.
258 * @return bool
259 */
260 private function update_wizard_data( $data = array(), $merge_option = true ) {
261 if ( $merge_option ) {
262 $this->wizard_data = get_option( self::OPTION_NAME, array() );
263 $data = array_merge( $this->wizard_data, $data );
264 }
265 return update_option( self::OPTION_NAME, $data );
266 }
267
268 /**
269 * Step: 3 Create draft page.
270 *
271 * @param bool $return_page_id Page ID.
272 */
273 private function setup_wizard_create_draft_page( $return_page_id = false ) {
274 $add_basic_shortcode = ! empty( $_POST['add_basic_shortcode'] ) ? sanitize_text_field( wp_unslash( $_POST['add_basic_shortcode'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
275 $add_basic_shortcode = 'true' === $add_basic_shortcode ? true : false;
276 $basic_shortcode = ! empty( $_POST['basic_shortcode'] ) ? filter_input( INPUT_POST, 'basic_shortcode', FILTER_SANITIZE_STRING ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
277
278 if ( ! $add_basic_shortcode ) {
279 wp_send_json(
280 array(
281 'status' => 1,
282 )
283 );
284 }
285 if ( function_exists( 'use_block_editor_for_post_type' ) && use_block_editor_for_post_type( 'page' ) ) {
286 $this->wizard_data = get_option( self::OPTION_NAME, array() );
287 if ( ! empty( $this->wizard_data['chart_id'] ) ) {
288 $block_data = array(
289 'id' => $this->wizard_data['chart_id'],
290 'lazy' => '-1',
291 'route' => 'chartSelect',
292 );
293 $basic_shortcode = '<!-- wp:visualizer/chart ' . wp_json_encode( $block_data ) . ' /-->';
294 }
295 }
296 $post_title = __( 'Visualizer Demo Page', 'visualizer' );
297 $page_id = post_exists( $post_title, '', '', 'page' );
298 $args = array(
299 'post_type' => 'page',
300 'post_title' => $post_title,
301 'post_content' => $add_basic_shortcode ? $basic_shortcode : '',
302 'post_status' => 'draft',
303 );
304 if ( ! $page_id ) {
305 $page_id = wp_insert_post( $args );
306 } else {
307 $args['ID'] = $page_id;
308 $page_id = wp_update_post( $args );
309 }
310
311 if ( $page_id ) {
312 // Delete previous meta data.
313 $meta = get_post_meta( $page_id );
314 foreach ( $meta as $key => $value ) {
315 delete_post_meta( $page_id, $key );
316 }
317 // Update wizard data.
318 $wizard_data['page_id'] = $page_id;
319 $this->update_wizard_data( $wizard_data );
320 }
321 if ( $return_page_id ) {
322 return $page_id;
323 }
324 wp_send_json(
325 array(
326 'status' => $page_id,
327 )
328 );
329 }
330
331 /**
332 * Step: 3 Install plugin.
333 */
334 private function setup_wizard_install_plugin() {
335 // phpcs:ignore WordPress.Security.NonceVerification.Missing
336 $slug = ! empty( $_POST['slug'] ) ? filter_input( INPUT_POST, 'slug', FILTER_SANITIZE_STRING ) : '';
337 if ( empty( $slug ) ) {
338 wp_send_json(
339 array(
340 'status' => 0,
341 'message' => __( 'No plugin specified.', 'visualizer' ),
342 )
343 );
344 }
345
346 if ( ! current_user_can( 'install_plugins' ) ) {
347 wp_send_json(
348 array(
349 'status' => 0,
350 'message' => __( 'Sorry, you are not allowed to install plugins on this site.', 'visualizer' ),
351 )
352 );
353 }
354
355 if ( ! empty( $slug ) ) {
356 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
357 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
358
359 $api = plugins_api(
360 'plugin_information',
361 array(
362 'slug' => sanitize_key( wp_unslash( $slug ) ),
363 'fields' => array(
364 'sections' => false,
365 ),
366 )
367 );
368
369 if ( is_wp_error( $api ) ) {
370 wp_send_json(
371 array(
372 'status' => 0,
373 'message' => $api->get_error_message(),
374 )
375 );
376 }
377
378 $skin = new WP_Ajax_Upgrader_Skin();
379 $upgrader = new Plugin_Upgrader( $skin );
380 $result = $upgrader->install( $api->download_link );
381 if ( is_wp_error( $result ) ) {
382 wp_send_json(
383 array(
384 'status' => 0,
385 'message' => $api->get_error_message(),
386 )
387 );
388 } elseif ( is_wp_error( $skin->result ) ) {
389 if ( 'folder_exists' !== $skin->result->get_error_code() ) {
390 wp_send_json(
391 array(
392 'status' => 0,
393 'message' => $skin->result->get_error_message(),
394 )
395 );
396 }
397 } elseif ( $skin->get_errors()->has_errors() ) {
398 if ( 'folder_exists' !== $skin->get_error_code() ) {
399 wp_send_json(
400 array(
401 'status' => 0,
402 'message' => $skin->get_error_message(),
403 )
404 );
405 }
406 } elseif ( is_null( $result ) ) {
407 global $wp_filesystem;
408 $status = array();
409 $status['message'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.', 'visualizer' );
410
411 // Pass through the error from WP_Filesystem if one was raised.
412 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
413 $status['message'] = esc_html( $wp_filesystem->errors->get_error_message() );
414 }
415
416 wp_send_json( $status );
417 }
418
419 activate_plugin( 'optimole-wp/optimole-wp.php' );
420 delete_transient( 'optml_fresh_install' );
421 // Update wizard data.
422 $wizard_data['enable_perfomance'] = true;
423 $this->update_wizard_data( $wizard_data );
424
425 wp_send_json(
426 array(
427 'status' => 1,
428 )
429 );
430 }
431 }
432
433 /**
434 * Step: 4 skip and subscribe process.
435 */
436 private function setup_wizard_subscribe_process() {
437 $segment = 0;
438 $wizard_data = get_option( self::OPTION_NAME, array() );
439 $chart_type = ! empty( $wizard_data['chart_type'] ) ? $wizard_data['chart_type'] : '';
440 $chart_id = ! empty( $wizard_data['chart_id'] ) ? $wizard_data['chart_id'] : '';
441 $page_id = ! empty( $wizard_data['page_id'] ) ? $wizard_data['page_id'] : '';
442 $response = array(
443 'status' => 0,
444 'redirect_to' => '',
445 'message' => '',
446 );
447
448 $with_subscribe = ! empty( $_POST['with_subscribe'] ) ? (bool) $_POST['with_subscribe'] : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
449 $email = ! empty( $_POST['email'] ) ? filter_input( INPUT_POST, 'email', FILTER_SANITIZE_EMAIL ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
450 $chart_types = array(
451 'pie' => 1,
452 'bar' => 2,
453 'line' => 3,
454 'tabular' => 4,
455 );
456 if ( $chart_type && ! empty( $chart_types[ $chart_type ] ) ) {
457 $segment = $chart_types[ $chart_type ];
458 }
459 if ( ! empty( $page_id ) ) {
460 $response = array(
461 'status' => 1,
462 'redirect_to' => get_edit_post_link( $page_id, 'db' ),
463 'message' => __( 'Redirecting to draft page', 'visualizer' ),
464 );
465 } else {
466 $response = array(
467 'status' => 1,
468 'redirect_to' => add_query_arg( 'page', 'visualizer', admin_url( 'admin.php' ) ),
469 'message' => __( 'Redirecting to visualizer dashboard', 'visualizer' ),
470 );
471 }
472
473 if ( $with_subscribe && is_email( $email ) ) {
474 $request_res = wp_remote_post(
475 VISUALIZER_SUBSCRIBE_API,
476 array(
477 'timeout' => 100,
478 'headers' => array(
479 'Content-Type' => 'application/json',
480 'Cache-Control' => 'no-cache',
481 'Accept' => 'application/json, */*;q=0.1',
482 ),
483 'body' => wp_json_encode(
484 array(
485 'slug' => 'visualizer',
486 'site' => home_url(),
487 'email' => $email,
488 'data' => array(
489 'segment' => $segment,
490 ),
491 )
492 ),
493 )
494 );
495 if ( ! is_wp_error( $request_res ) ) {
496 $body = json_decode( wp_remote_retrieve_body( $request_res ) );
497 if ( 'success' === $body->code ) {
498 $this->dismissWizard( false );
499 wp_send_json( $response );
500 }
501 }
502 wp_send_json(
503 array(
504 'status' => 0,
505 'redirect_to' => '',
506 'message' => '',
507 )
508 );
509 } else {
510 $this->dismissWizard( false );
511 wp_send_json( $response );
512 }
513 }
514
515 /**
516 * Filter chart setting.
517 *
518 * @param array $settings Chart settings.
519 * @return array
520 */
521 public function visualizer_filter_chart_settings( $settings ) {
522 $settings['backgroundColor'] = array(
523 'fill' => '#39c3d21a',
524 'fillOpacity' => '.1',
525 );
526 return $settings;
527 }
528 }
529