PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.3.0
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.3.0
4.0.8 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 All 149 releases
← All changes | classes/Visualizer/Module.php +305 -9 3.0.73.3.0 View file →
@@ -62,8 +62,13 @@
62 62 global $wpdb;
63 63
64 64 $this->_wpdb = $wpdb;
65 65 $this->_plugin = $plugin;
66 +
67 + $this->_addFilter( Visualizer_Plugin::FILTER_UNDO_REVISIONS, 'undoRevisions', 10, 2 );
68 + $this->_addFilter( Visualizer_Plugin::FILTER_HANDLE_REVISIONS, 'handleExistingRevisions', 10, 2 );
69 + $this->_addFilter( Visualizer_Plugin::FILTER_GET_CHART_DATA_AS, 'getDataAs', 10, 3 );
70 +
66 71 }
67 72
68 73 /**
69 74 * Registers an action hook.
@@ -143,8 +148,17 @@
143 148 return $this;
144 149 }
145 150
146 151 /**
152 + * A wrapper around the actual function _getDataAs. This function is invoked as a filter.
153 + *
154 + * @since 3.2.0
155 + */
156 + public function getDataAs( $final, $chart_id, $type ) {
157 + return $this->_getDataAs( $chart_id, $type );
158 + }
159 +
160 + /**
147 161 * Extracts the data for a chart and prepares it for the given type.
148 162 *
149 163 * @access public
150 164 * @param int $chart_id The chart id.
@@ -154,9 +168,9 @@
154 168 $final = null;
155 169 $success = false;
156 170 if ( $chart_id ) {
157 171 $chart = get_post( $chart_id );
158 - $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
172 + $success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER;
159 173 }
160 174 if ( $success ) {
161 175 $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
162 176 $rows = array();
@@ -191,10 +205,22 @@
191 205 }
192 206 }
193 207 }
194 208
195 - $filename = isset( $settings['title'] ) && ! empty( $settings['title'] ) ? $settings['title'] : 'visualizer#' . $chart_id;
209 + $title = 'visualizer#' . $chart_id;
210 + if ( ! empty( $settings['title'] ) ) {
211 + $title = $settings['title'];
212 + }
213 + // for ChartJS, title is an array.
214 + if ( is_array( $title ) && isset( $title['text'] ) ) {
215 + $title = $title['text'];
216 + }
217 + if ( empty( $title ) ) {
218 + $title = 'visualizer#' . $chart_id;
219 + }
196 220
221 + $filename = $title;
222 +
197 223 switch ( $type ) {
198 224 case 'csv':
199 225 $final = $this->_getCSV( $rows, $filename );
200 226 break;
@@ -218,16 +244,18 @@
218 244 */
219 245 private function _getCSV( $rows, $filename ) {
220 246 $filename .= '.csv';
221 247
248 + $bom = chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF );
222 249 $fp = tmpfile();
223 250 // support for MS Excel
224 - fprintf( $fp, $bom = ( chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ) ) );
251 + fprintf( $fp, $bom );
225 252 foreach ( $rows as $row ) {
226 253 fputcsv( $fp, $row );
227 254 }
228 255 rewind( $fp );
229 256 $csv = '';
257 + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
230 258 while ( ( $array = fgetcsv( $fp ) ) !== false ) {
231 259 if ( strlen( $csv ) > 0 ) {
232 260 $csv .= PHP_EOL;
233 261 }
@@ -237,8 +265,9 @@
237 265
238 266 return array(
239 267 'csv' => $csv,
240 268 'name' => $filename,
269 + 'string' => str_replace( $bom, '', $csv ),
241 270 );
242 271 }
243 272
244 273 /**
@@ -248,30 +277,36 @@
248 277 * @param array $rows The array of data.
249 278 * @param string $filename The name of the file to use.
250 279 */
251 280 private function _getExcel( $rows, $filename ) {
252 - // PHPExcel does not like sheet names longer than 31 characters.
281 + // PHPExcel did not like sheet names longer than 31 characters and we will assume the same with PhpSpreadsheet
253 282 $chart = substr( $filename, 0, 30 );
254 283 $filename .= '.xlsx';
255 284
285 + $vendor_file = VISUALIZER_ABSPATH . '/vendor/autoload.php';
286 + if ( is_readable( $vendor_file ) ) {
287 + include_once( $vendor_file );
288 + }
289 +
256 290 $xlsData = '';
257 - if ( class_exists( 'PHPExcel' ) ) {
258 - $doc = new PHPExcel();
291 + if ( class_exists( 'PhpOffice\PhpSpreadsheet\Spreadsheet' ) ) {
292 + $doc = new PhpOffice\PhpSpreadsheet\Spreadsheet();
259 293 $doc->getActiveSheet()->fromArray( $rows, null, 'A1' );
260 294 $doc->getActiveSheet()->setTitle( sanitize_title( $chart ) );
261 295 $doc = apply_filters( 'visualizer_excel_doc', $doc );
262 - $writer = PHPExcel_IOFactory::createWriter( $doc, 'Excel2007' );
296 + $writer = PhpOffice\PhpSpreadsheet\IOFactory::createWriter( $doc, 'Xlsx' );
263 297 ob_start();
264 298 $writer->save( 'php://output' );
265 299 $xlsData = ob_get_contents();
266 300 ob_end_clean();
267 301 } else {
268 - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'Class PHPExcel does not exist!', 'error', __FILE__, __LINE__ );
269 - error_log( 'Class PHPExcel does not exist!' );
302 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'Class PhpOffice\PhpSpreadsheet\Spreadsheet does not exist!', 'error', __FILE__, __LINE__ );
303 + error_log( 'Class PhpOffice\PhpSpreadsheet\Spreadsheet does not exist!' );
270 304 }
271 305 return array(
272 306 'csv' => 'data:application/vnd.ms-excel;base64,' . base64_encode( $xlsData ),
273 307 'name' => $filename,
308 + 'raw' => base64_encode( $xlsData ),
274 309 );
275 310 }
276 311
277 312 /**
@@ -301,8 +336,14 @@
301 336
302 337 $table = '<table class="visualizer-print">';
303 338 $index = 0;
304 339 foreach ( $rows as $row ) {
340 + // skip the data type row.
341 + if ( 1 === $index ) {
342 + $index++;
343 + continue;
344 + }
345 +
305 346 $table .= '<tr>';
306 347 foreach ( $row as $col ) {
307 348 if ( $index === 0 ) {
308 349 $table .= '<th>' . $col . '</th>';
@@ -323,8 +364,59 @@
323 364 );
324 365 }
325 366
326 367 /**
368 + * Undo revisions for the chart, and if necessary, restore the earliest version.
369 + *
370 + * @return bool If any revisions were found.
371 + */
372 + public final function undoRevisions( $chart_id, $restore = false ) {
373 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'undoRevisions for %d with%s restore', $chart_id, ( $restore ? '' : 'out' ) ), 'debug', __FILE__, __LINE__ );
374 +
375 + $revisions = wp_get_post_revisions( $chart_id, array( 'order' => 'ASC' ) );
376 + if ( count( $revisions ) > 1 ) {
377 + $revision_ids = array_keys( $revisions );
378 +
379 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'found %d revisions = %s', count( $revisions ), print_r( $revision_ids, true ) ), 'debug', __FILE__, __LINE__ );
380 +
381 + // when we restore, a new revision is likely to be created. so, let's disable revisions for the time being.
382 + add_filter( 'wp_revisions_to_keep', '__return_false' );
383 +
384 + if ( $restore ) {
385 + // restore to the oldest one i.e. the first one.
386 + wp_restore_post_revision( array_shift( $revision_ids ) );
387 + }
388 +
389 + // delete all revisions.
390 + foreach ( $revision_ids as $id ) {
391 + wp_delete_post_revision( $id );
392 + }
393 +
394 + return true;
395 + }
396 + return false;
397 + }
398 +
399 + /**
400 + * If existing revisions exist for the chart, restore the earliest version and then create a new revision to initiate editing.
401 + */
402 + public final function handleExistingRevisions( $chart_id, $chart ) {
403 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'handleExistingRevisions for %d', $chart_id ), 'debug', __FILE__, __LINE__ );
404 +
405 + // undo revisions.
406 + $revisions_found = $this->undoRevisions( $chart_id, true );
407 +
408 + // create revision for the edit action.
409 + wp_save_post_revision( $chart_id );
410 +
411 + if ( $revisions_found ) {
412 + // fetch chart data again in case it was updated by an earlier revision.
413 + $chart = get_post( $chart_id );
414 + }
415 + return $chart;
416 + }
417 +
418 + /**
327 419 * Returns the language of the locale.
328 420 *
329 421 * @access protected
330 422 */
@@ -338,5 +430,209 @@
338 430 return '';
339 431 }
340 432 return reset( $array );
341 433 }
434 +
435 + /**
436 + * Gets/creates the JS where user-specific customizations can be/have been added.
437 + */
438 + protected function get_user_customization_js() {
439 + // use this as the JS file in case we are not able to create the file in uploads.
440 + $default = VISUALIZER_ABSURL . 'js/customization.js';
441 +
442 + $uploads = wp_get_upload_dir();
443 + $specific = $uploads['baseurl'] . '/visualizer/customization.js';
444 +
445 + // for testing on user sites (before we send them the correctly customized file).
446 + if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) {
447 + return $default;
448 + }
449 +
450 + require_once( ABSPATH . 'wp-admin/includes/file.php' );
451 + WP_Filesystem();
452 + global $wp_filesystem;
453 +
454 + $dir = $wp_filesystem->wp_content_dir() . 'uploads/visualizer';
455 + $file = $wp_filesystem->wp_content_dir() . 'uploads/visualizer/customization.js';
456 +
457 + if ( $wp_filesystem->is_readable( $file ) ) {
458 + return $specific;
459 + }
460 +
461 + if ( $wp_filesystem->exists( $file ) && ! $wp_filesystem->is_readable( $file ) ) {
462 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to read file %s', $file ), 'error', __FILE__, __LINE__ );
463 + return $default;
464 + }
465 +
466 + if ( ! $wp_filesystem->exists( $dir ) ) {
467 + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
468 + if ( ( $done = $wp_filesystem->mkdir( $dir ) ) === false ) {
469 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to create directory %s', $dir ), 'error', __FILE__, __LINE__ );
470 + return $default;
471 + }
472 + }
473 +
474 + // if file does not exist, copy.
475 + if ( ! $wp_filesystem->exists( $file ) ) {
476 + $src = str_replace( ABSPATH, $wp_filesystem->abspath(), VISUALIZER_ABSPATH . '/js/customization.js' );
477 + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
478 + if ( ( $done = $wp_filesystem->copy( $src, $file ) ) === false ) {
479 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to copy file %s to %s', $src, $file ), 'error', __FILE__, __LINE__ );
480 + return $default;
481 + }
482 + }
483 +
484 + return $specific;
485 + }
486 +
487 + /**
488 + * Load the class for the given chart's chart type so that its assets can be loaded.
489 + */
490 + protected function load_chart_type( $chart_id ) {
491 + $name = $this->load_chart_class_name( $chart_id );
492 + $class = null;
493 + if ( class_exists( $name ) || true === apply_filters( 'visualizer_load_chart', false, $name ) ) {
494 + $class = new $name;
495 + }
496 +
497 + if ( is_null( $class ) && Visualizer_Module::is_pro() ) {
498 + // lets see if this type exists in pro. New Lite(3.1.0+) & old Pro(1.8.0-).
499 + $type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true );
500 + $class = apply_filters( 'visualizer_pro_chart_type_sidebar', null, array( 'id' => $chart_id, 'type' => $type, 'settings' => get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ) ) );
501 + }
502 +
503 + return is_null( $class ) ? null : $class->getLibrary();
504 + }
505 +
506 + /**
507 + * Returns the class name for the given chart's chart type.
508 + */
509 + protected function load_chart_class_name( $chart_id ) {
510 + $type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true );
511 + $lib = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, true );
512 +
513 + // backward compatibility.
514 + if ( empty( $lib ) ) {
515 + $lib = 'GoogleCharts';
516 + if ( 'dataTable' === $type ) {
517 + $lib = 'DataTable';
518 + }
519 + }
520 + $name = 'Visualizer_Render_Sidebar_Type_' . $lib . '_' . ucwords( $type );
521 + return $name;
522 + }
523 +
524 + /**
525 + * Generates the inline CSS to apply to the chart and adds these classes to the settings.
526 + *
527 + * @access public
528 + * @param int $id The id of the chart.
529 + * @param array $settings The settings of the chart.
530 + */
531 + protected function get_inline_custom_css( $id, $settings ) {
532 + $css = '';
533 +
534 + $arguments = array( '', $settings );
535 + if ( ! isset( $settings['customcss'] ) ) {
536 + return $arguments;
537 + }
538 +
539 + $classes = array();
540 + $css = '<style type="text/css" name="visualizer-custom-css" id="customcss-' . $id . '">';
541 + foreach ( $settings['customcss'] as $name => $element ) {
542 + $attributes = array();
543 + foreach ( $element as $property => $value ) {
544 + $attributes[] = $this->handle_css_property( $property, $value );
545 + }
546 + $class_name = $id . $name;
547 + $properties = implode( ' !important; ', array_filter( $attributes ) );
548 + if ( ! empty( $properties ) ) {
549 + $css .= '.' . $class_name . ' {' . $properties . ' !important;}';
550 + $classes[ $name ] = $class_name;
551 + }
552 + }
553 + $css .= '</style>';
554 +
555 + $settings['cssClassNames'] = $classes;
556 +
557 + $arguments = array( $css, $settings );
558 + apply_filters_ref_array( 'visualizer_inline_css', array( &$arguments ) );
559 +
560 + return $arguments;
561 + }
562 +
563 + /**
564 + * Handles CSS properties that might need special syntax.
565 + *
566 + * @access private
567 + * @param string $property The name of the css property.
568 + * @param string $value The value of the css property.
569 + */
570 + private function handle_css_property( $property, $value ) {
571 + if ( empty( $property ) || empty( $value ) ) {
572 + return '';
573 + }
574 +
575 + switch ( $property ) {
576 + case 'transform':
577 + $value = 'rotate(' . $value . 'deg)';
578 + break;
579 + }
580 + return $property . ': ' . $value;
581 + }
582 +
583 + /**
584 + * Determines if charts have been created of the particular chart type.
585 + */
586 + protected static function hasChartType( $type ) {
587 + $args = array(
588 + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
589 + 'fields' => 'ids',
590 + 'post_status' => 'publish',
591 + 'meta_query' => array(
592 + array(
593 + 'key' => Visualizer_Plugin::CF_CHART_TYPE,
594 + 'value' => $type,
595 + ),
596 + ),
597 + );
598 +
599 + $q = new WP_Query( $args );
600 + return $q->found_posts > 0;
601 + }
602 +
603 + /**
604 + * Determines how many charts have been created.
605 + */
606 + protected static function numberOfCharts() {
607 + $args = array(
608 + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
609 + 'fields' => 'ids',
610 + 'post_status' => 'publish',
611 + 'posts_per_page' => 300,
612 + );
613 +
614 + $q = new WP_Query( $args );
615 + return $q->found_posts;
616 + }
617 +
618 + /**
619 + * Checks if the PRO version is active.
620 + *
621 + * @since 3.3.0
622 + */
623 + public static function is_pro() {
624 + // versions of pro before 1.9.0 will use the constant VISUALIZER_PRO
625 + // versions of pro 1.9.0 onwards will use the filter
626 + return apply_filters( 'visualizer_is_pro', VISUALIZER_PRO );
627 + }
628 +
629 + /**
630 + * Checks if the PRO version is older than a particular version.
631 + *
632 + * @since 3.3.0
633 + */
634 + public static function is_pro_older_than( $version ) {
635 + return version_compare( VISUALIZER_PRO_VERSION, $version, '<' );
636 + }
637 +
342 638 }