PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.2.0
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.2.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
← All changes | classes/Visualizer/Module.php +255 -8 3.0.63.2.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();
@@ -218,16 +232,18 @@
218 232 */
219 233 private function _getCSV( $rows, $filename ) {
220 234 $filename .= '.csv';
221 235
236 + $bom = chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF );
222 237 $fp = tmpfile();
223 238 // support for MS Excel
224 - fprintf( $fp, $bom = ( chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ) ) );
239 + fprintf( $fp, $bom );
225 240 foreach ( $rows as $row ) {
226 241 fputcsv( $fp, $row );
227 242 }
228 243 rewind( $fp );
229 244 $csv = '';
245 + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
230 246 while ( ( $array = fgetcsv( $fp ) ) !== false ) {
231 247 if ( strlen( $csv ) > 0 ) {
232 248 $csv .= PHP_EOL;
233 249 }
@@ -237,8 +253,9 @@
237 253
238 254 return array(
239 255 'csv' => $csv,
240 256 'name' => $filename,
257 + 'string' => str_replace( $bom, '', $csv ),
241 258 );
242 259 }
243 260
244 261 /**
@@ -248,30 +265,36 @@
248 265 * @param array $rows The array of data.
249 266 * @param string $filename The name of the file to use.
250 267 */
251 268 private function _getExcel( $rows, $filename ) {
252 - // PHPExcel does not like sheet names longer than 31 characters.
269 + // PHPExcel did not like sheet names longer than 31 characters and we will assume the same with PhpSpreadsheet
253 270 $chart = substr( $filename, 0, 30 );
254 271 $filename .= '.xlsx';
255 272
273 + $vendor_file = VISUALIZER_ABSPATH . '/vendor/autoload.php';
274 + if ( is_readable( $vendor_file ) ) {
275 + include_once( $vendor_file );
276 + }
277 +
256 278 $xlsData = '';
257 - if ( class_exists( 'PHPExcel' ) ) {
258 - $doc = new PHPExcel();
279 + if ( class_exists( 'PhpOffice\PhpSpreadsheet\Spreadsheet' ) ) {
280 + $doc = new PhpOffice\PhpSpreadsheet\Spreadsheet();
259 281 $doc->getActiveSheet()->fromArray( $rows, null, 'A1' );
260 282 $doc->getActiveSheet()->setTitle( sanitize_title( $chart ) );
261 283 $doc = apply_filters( 'visualizer_excel_doc', $doc );
262 - $writer = PHPExcel_IOFactory::createWriter( $doc, 'Excel2007' );
284 + $writer = PhpOffice\PhpSpreadsheet\IOFactory::createWriter( $doc, 'Xlsx' );
263 285 ob_start();
264 286 $writer->save( 'php://output' );
265 287 $xlsData = ob_get_contents();
266 288 ob_end_clean();
267 289 } 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!' );
290 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'Class PhpOffice\PhpSpreadsheet\Spreadsheet does not exist!', 'error', __FILE__, __LINE__ );
291 + error_log( 'Class PhpOffice\PhpSpreadsheet\Spreadsheet does not exist!' );
270 292 }
271 293 return array(
272 294 'csv' => 'data:application/vnd.ms-excel;base64,' . base64_encode( $xlsData ),
273 295 'name' => $filename,
296 + 'raw' => base64_encode( $xlsData ),
274 297 );
275 298 }
276 299
277 300 /**
@@ -301,8 +324,14 @@
301 324
302 325 $table = '<table class="visualizer-print">';
303 326 $index = 0;
304 327 foreach ( $rows as $row ) {
328 + // skip the data type row.
329 + if ( 1 === $index ) {
330 + $index++;
331 + continue;
332 + }
333 +
305 334 $table .= '<tr>';
306 335 foreach ( $row as $col ) {
307 336 if ( $index === 0 ) {
308 337 $table .= '<th>' . $col . '</th>';
@@ -320,7 +349,225 @@
320 349 </html>';
321 350 return array(
322 351 'csv' => $html,
323 352 );
353 + }
354 +
355 + /**
356 + * Undo revisions for the chart, and if necessary, restore the earliest version.
357 + *
358 + * @return bool If any revisions were found.
359 + */
360 + public final function undoRevisions( $chart_id, $restore = false ) {
361 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'undoRevisions for %d with%s restore', $chart_id, ( $restore ? '' : 'out' ) ), 'debug', __FILE__, __LINE__ );
362 +
363 + $revisions = wp_get_post_revisions( $chart_id, array( 'order' => 'ASC' ) );
364 + if ( count( $revisions ) > 1 ) {
365 + $revision_ids = array_keys( $revisions );
366 +
367 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'found %d revisions = %s', count( $revisions ), print_r( $revision_ids, true ) ), 'debug', __FILE__, __LINE__ );
368 +
369 + // when we restore, a new revision is likely to be created. so, let's disable revisions for the time being.
370 + add_filter( 'wp_revisions_to_keep', '__return_false' );
371 +
372 + if ( $restore ) {
373 + // restore to the oldest one i.e. the first one.
374 + wp_restore_post_revision( array_shift( $revision_ids ) );
375 + }
376 +
377 + // delete all revisions.
378 + foreach ( $revision_ids as $id ) {
379 + wp_delete_post_revision( $id );
380 + }
381 +
382 + return true;
383 + }
384 + return false;
385 + }
386 +
387 + /**
388 + * If existing revisions exist for the chart, restore the earliest version and then create a new revision to initiate editing.
389 + */
390 + public final function handleExistingRevisions( $chart_id, $chart ) {
391 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'handleExistingRevisions for %d', $chart_id ), 'debug', __FILE__, __LINE__ );
392 +
393 + // undo revisions.
394 + $revisions_found = $this->undoRevisions( $chart_id, true );
395 +
396 + // create revision for the edit action.
397 + wp_save_post_revision( $chart_id );
398 +
399 + if ( $revisions_found ) {
400 + // fetch chart data again in case it was updated by an earlier revision.
401 + $chart = get_post( $chart_id );
402 + }
403 + return $chart;
404 + }
405 +
406 + /**
407 + * Returns the language of the locale.
408 + *
409 + * @access protected
410 + */
411 + protected function get_language() {
412 + $locale = get_locale();
413 + if ( empty( $locale ) ) {
414 + return '';
415 + }
416 + $array = explode( '_', $locale );
417 + if ( count( $array ) < 2 ) {
418 + return '';
419 + }
420 + return reset( $array );
421 + }
422 +
423 + /**
424 + * Gets/creates the JS where user-specific customizations can be/have been added.
425 + */
426 + protected function get_user_customization_js() {
427 + // use this as the JS file in case we are not able to create the file in uploads.
428 + $default = VISUALIZER_ABSURL . 'js/customization.js';
429 +
430 + $uploads = wp_get_upload_dir();
431 + $specific = $uploads['baseurl'] . '/visualizer/customization.js';
432 +
433 + // for testing on user sites (before we send them the correctly customized file).
434 + if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) {
435 + return $default;
436 + }
437 +
438 + require_once( ABSPATH . 'wp-admin/includes/file.php' );
439 + WP_Filesystem();
440 + global $wp_filesystem;
441 +
442 + $dir = $wp_filesystem->wp_content_dir() . 'uploads/visualizer';
443 + $file = $wp_filesystem->wp_content_dir() . 'uploads/visualizer/customization.js';
444 +
445 + if ( $wp_filesystem->is_readable( $file ) ) {
446 + return $specific;
447 + }
448 +
449 + if ( $wp_filesystem->exists( $file ) && ! $wp_filesystem->is_readable( $file ) ) {
450 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to read file %s', $file ), 'error', __FILE__, __LINE__ );
451 + return $default;
452 + }
453 +
454 + if ( ! $wp_filesystem->exists( $dir ) ) {
455 + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
456 + if ( ( $done = $wp_filesystem->mkdir( $dir ) ) === false ) {
457 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to create directory %s', $dir ), 'error', __FILE__, __LINE__ );
458 + return $default;
459 + }
460 + }
461 +
462 + // if file does not exist, copy.
463 + if ( ! $wp_filesystem->exists( $file ) ) {
464 + $src = str_replace( ABSPATH, $wp_filesystem->abspath(), VISUALIZER_ABSPATH . '/js/customization.js' );
465 + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
466 + if ( ( $done = $wp_filesystem->copy( $src, $file ) ) === false ) {
467 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to copy file %s to %s', $src, $file ), 'error', __FILE__, __LINE__ );
468 + return $default;
469 + }
470 + }
471 +
472 + return $specific;
473 + }
474 +
475 + /**
476 + * Load the class for the given chart's chart type so that its assets can be loaded.
477 + */
478 + protected function load_chart_type( $chart_id ) {
479 + $type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true );
480 + $name = 'Visualizer_Render_Sidebar_Type_' . ucwords( $type );
481 + $class = null;
482 + if ( class_exists( $name ) || true === apply_filters( 'visualizer_load_chart', false, $name ) ) {
483 + $class = new $name;
484 + }
485 +
486 + if ( is_null( $class ) && VISUALIZER_PRO ) {
487 + // lets see if this type exists in pro. New Lite(3.1.0+) & old Pro(1.8.0-).
488 + $class = apply_filters( 'visualizer_pro_chart_type_sidebar', null, array( 'type' => $type, 'settings' => get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ) ) );
489 + }
490 +
491 + return is_null( $class ) ? null : $class->getLibrary();
492 + }
493 +
494 + /**
495 + * Generates the inline CSS to apply to the chart and adds these classes to the settings.
496 + *
497 + * @access public
498 + * @param int $id The id of the chart.
499 + * @param array $settings The settings of the chart.
500 + */
501 + protected function get_inline_custom_css( $id, $settings ) {
502 + $css = '';
503 +
504 + $arguments = array( '', $settings );
505 + if ( ! isset( $settings['customcss'] ) ) {
506 + return $arguments;
507 + }
508 +
509 + $classes = array();
510 + $css = '<style type="text/css" name="visualizer-custom-css" id="customcss-' . $id . '">';
511 + foreach ( $settings['customcss'] as $name => $element ) {
512 + $attributes = array();
513 + foreach ( $element as $property => $value ) {
514 + $attributes[] = $this->handle_css_property( $property, $value );
515 + }
516 + $class_name = $id . $name;
517 + $properties = implode( '; ', array_filter( $attributes ) );
518 + if ( ! empty( $properties ) ) {
519 + $css .= '.' . $class_name . ' {' . $properties . ' !important;}';
520 + $classes[ $name ] = $class_name;
521 + }
522 + }
523 + $css .= '</style>';
524 +
525 + $settings['cssClassNames'] = $classes;
526 +
527 + $arguments = array( $css, $settings );
528 + apply_filters_ref_array( 'visualizer_inline_css', array( &$arguments ) );
529 +
530 + return $arguments;
531 + }
532 +
533 + /**
534 + * Handles CSS properties that might need special syntax.
535 + *
536 + * @access private
537 + * @param string $property The name of the css property.
538 + * @param string $value The value of the css property.
539 + */
540 + private function handle_css_property( $property, $value ) {
541 + if ( empty( $property ) || empty( $value ) ) {
542 + return '';
543 + }
544 +
545 + switch ( $property ) {
546 + case 'transform':
547 + $value = 'rotate(' . $value . 'deg)';
548 + break;
549 + }
550 + return $property . ': ' . $value;
551 + }
552 +
553 + /**
554 + * Determines if charts have been created of the particular chart type.
555 + */
556 + protected static function hasChartType( $type ) {
557 + $args = array(
558 + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
559 + 'fields' => 'ids',
560 + 'post_status' => 'publish',
561 + 'meta_query' => array(
562 + array(
563 + 'key' => Visualizer_Plugin::CF_CHART_TYPE,
564 + 'value' => $type,
565 + ),
566 + ),
567 + );
568 +
569 + $q = new WP_Query( $args );
570 + return $q->found_posts > 0;
324 571 }
325 572
326 573 }