| @@ -65,8 +65,9 @@ | ||
| 65 | 65 | $this->_plugin = $plugin; |
| 66 | 66 | |
| 67 | 67 | $this->_addFilter( Visualizer_Plugin::FILTER_UNDO_REVISIONS, 'undoRevisions', 10, 2 ); |
| 68 | 68 | $this->_addFilter( Visualizer_Plugin::FILTER_HANDLE_REVISIONS, 'handleExistingRevisions', 10, 2 ); |
| 69 | + $this->_addFilter( Visualizer_Plugin::FILTER_GET_CHART_DATA_AS, 'getDataAs', 10, 3 ); | |
| 69 | 70 | |
| 70 | 71 | } |
| 71 | 72 | |
| 72 | 73 | /** |
| @@ -147,8 +148,17 @@ | ||
| 147 | 148 | return $this; |
| 148 | 149 | } |
| 149 | 150 | |
| 150 | 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 | + /** | |
| 151 | 161 | * Extracts the data for a chart and prepares it for the given type. |
| 152 | 162 | * |
| 153 | 163 | * @access public |
| 154 | 164 | * @param int $chart_id The chart id. |
| @@ -158,9 +168,9 @@ | ||
| 158 | 168 | $final = null; |
| 159 | 169 | $success = false; |
| 160 | 170 | if ( $chart_id ) { |
| 161 | 171 | $chart = get_post( $chart_id ); |
| 162 | - $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER; | |
| 172 | + $success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER; | |
| 163 | 173 | } |
| 164 | 174 | if ( $success ) { |
| 165 | 175 | $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ); |
| 166 | 176 | $rows = array(); |
| @@ -195,10 +205,22 @@ | ||
| 195 | 205 | } |
| 196 | 206 | } |
| 197 | 207 | } |
| 198 | 208 | |
| 199 | - $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 | + } | |
| 200 | 220 | |
| 221 | + $filename = $title; | |
| 222 | + | |
| 201 | 223 | switch ( $type ) { |
| 202 | 224 | case 'csv': |
| 203 | 225 | $final = $this->_getCSV( $rows, $filename ); |
| 204 | 226 | break; |
| @@ -222,16 +244,18 @@ | ||
| 222 | 244 | */ |
| 223 | 245 | private function _getCSV( $rows, $filename ) { |
| 224 | 246 | $filename .= '.csv'; |
| 225 | 247 | |
| 248 | + $bom = chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ); | |
| 226 | 249 | $fp = tmpfile(); |
| 227 | 250 | // support for MS Excel |
| 228 | - fprintf( $fp, $bom = ( chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ) ) ); | |
| 251 | + fprintf( $fp, $bom ); | |
| 229 | 252 | foreach ( $rows as $row ) { |
| 230 | 253 | fputcsv( $fp, $row ); |
| 231 | 254 | } |
| 232 | 255 | rewind( $fp ); |
| 233 | 256 | $csv = ''; |
| 257 | + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition | |
| 234 | 258 | while ( ( $array = fgetcsv( $fp ) ) !== false ) { |
| 235 | 259 | if ( strlen( $csv ) > 0 ) { |
| 236 | 260 | $csv .= PHP_EOL; |
| 237 | 261 | } |
| @@ -241,8 +265,9 @@ | ||
| 241 | 265 | |
| 242 | 266 | return array( |
| 243 | 267 | 'csv' => $csv, |
| 244 | 268 | 'name' => $filename, |
| 269 | + 'string' => str_replace( $bom, '', $csv ), | |
| 245 | 270 | ); |
| 246 | 271 | } |
| 247 | 272 | |
| 248 | 273 | /** |
| @@ -252,30 +277,36 @@ | ||
| 252 | 277 | * @param array $rows The array of data. |
| 253 | 278 | * @param string $filename The name of the file to use. |
| 254 | 279 | */ |
| 255 | 280 | private function _getExcel( $rows, $filename ) { |
| 256 | - // 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 | |
| 257 | 282 | $chart = substr( $filename, 0, 30 ); |
| 258 | 283 | $filename .= '.xlsx'; |
| 259 | 284 | |
| 285 | + $vendor_file = VISUALIZER_ABSPATH . '/vendor/autoload.php'; | |
| 286 | + if ( is_readable( $vendor_file ) ) { | |
| 287 | + include_once( $vendor_file ); | |
| 288 | + } | |
| 289 | + | |
| 260 | 290 | $xlsData = ''; |
| 261 | - if ( class_exists( 'PHPExcel' ) ) { | |
| 262 | - $doc = new PHPExcel(); | |
| 291 | + if ( class_exists( 'PhpOffice\PhpSpreadsheet\Spreadsheet' ) ) { | |
| 292 | + $doc = new PhpOffice\PhpSpreadsheet\Spreadsheet(); | |
| 263 | 293 | $doc->getActiveSheet()->fromArray( $rows, null, 'A1' ); |
| 264 | 294 | $doc->getActiveSheet()->setTitle( sanitize_title( $chart ) ); |
| 265 | 295 | $doc = apply_filters( 'visualizer_excel_doc', $doc ); |
| 266 | - $writer = PHPExcel_IOFactory::createWriter( $doc, 'Excel2007' ); | |
| 296 | + $writer = PhpOffice\PhpSpreadsheet\IOFactory::createWriter( $doc, 'Xlsx' ); | |
| 267 | 297 | ob_start(); |
| 268 | 298 | $writer->save( 'php://output' ); |
| 269 | 299 | $xlsData = ob_get_contents(); |
| 270 | 300 | ob_end_clean(); |
| 271 | 301 | } else { |
| 272 | - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'Class PHPExcel does not exist!', 'error', __FILE__, __LINE__ ); | |
| 273 | - 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!' ); | |
| 274 | 304 | } |
| 275 | 305 | return array( |
| 276 | 306 | 'csv' => 'data:application/vnd.ms-excel;base64,' . base64_encode( $xlsData ), |
| 277 | 307 | 'name' => $filename, |
| 308 | + 'raw' => base64_encode( $xlsData ), | |
| 278 | 309 | ); |
| 279 | 310 | } |
| 280 | 311 | |
| 281 | 312 | /** |
| @@ -305,8 +336,14 @@ | ||
| 305 | 336 | |
| 306 | 337 | $table = '<table class="visualizer-print">'; |
| 307 | 338 | $index = 0; |
| 308 | 339 | foreach ( $rows as $row ) { |
| 340 | + // skip the data type row. | |
| 341 | + if ( 1 === $index ) { | |
| 342 | + $index++; | |
| 343 | + continue; | |
| 344 | + } | |
| 345 | + | |
| 309 | 346 | $table .= '<tr>'; |
| 310 | 347 | foreach ( $row as $col ) { |
| 311 | 348 | if ( $index === 0 ) { |
| 312 | 349 | $table .= '<th>' . $col . '</th>'; |
| @@ -413,11 +450,16 @@ | ||
| 413 | 450 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 414 | 451 | WP_Filesystem(); |
| 415 | 452 | global $wp_filesystem; |
| 416 | 453 | |
| 417 | - $dir = $wp_filesystem->wp_content_dir() . 'uploads/visualizer'; | |
| 418 | - $file = $wp_filesystem->wp_content_dir() . 'uploads/visualizer/customization.js'; | |
| 454 | + $multisite_arg = '/'; | |
| 455 | + if ( is_multisite() && ! is_main_site() ) { | |
| 456 | + $multisite_arg = '/sites/' . get_current_blog_id() . '/'; | |
| 457 | + } | |
| 419 | 458 | |
| 459 | + $dir = $wp_filesystem->wp_content_dir() . 'uploads' . $multisite_arg . 'visualizer'; | |
| 460 | + $file = $wp_filesystem->wp_content_dir() . 'uploads' . $multisite_arg . 'visualizer/customization.js'; | |
| 461 | + | |
| 420 | 462 | if ( $wp_filesystem->is_readable( $file ) ) { |
| 421 | 463 | return $specific; |
| 422 | 464 | } |
| 423 | 465 | |
| @@ -426,8 +468,9 @@ | ||
| 426 | 468 | return $default; |
| 427 | 469 | } |
| 428 | 470 | |
| 429 | 471 | if ( ! $wp_filesystem->exists( $dir ) ) { |
| 472 | + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found | |
| 430 | 473 | if ( ( $done = $wp_filesystem->mkdir( $dir ) ) === false ) { |
| 431 | 474 | do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to create directory %s', $dir ), 'error', __FILE__, __LINE__ ); |
| 432 | 475 | return $default; |
| 433 | 476 | } |
| @@ -435,8 +478,9 @@ | ||
| 435 | 478 | |
| 436 | 479 | // if file does not exist, copy. |
| 437 | 480 | if ( ! $wp_filesystem->exists( $file ) ) { |
| 438 | 481 | $src = str_replace( ABSPATH, $wp_filesystem->abspath(), VISUALIZER_ABSPATH . '/js/customization.js' ); |
| 482 | + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found | |
| 439 | 483 | if ( ( $done = $wp_filesystem->copy( $src, $file ) ) === false ) { |
| 440 | 484 | do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to copy file %s to %s', $src, $file ), 'error', __FILE__, __LINE__ ); |
| 441 | 485 | return $default; |
| 442 | 486 | } |
| @@ -448,18 +492,18 @@ | ||
| 448 | 492 | /** |
| 449 | 493 | * Load the class for the given chart's chart type so that its assets can be loaded. |
| 450 | 494 | */ |
| 451 | 495 | protected function load_chart_type( $chart_id ) { |
| 452 | - $type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true ); | |
| 453 | - $name = 'Visualizer_Render_Sidebar_Type_' . ucwords( $type ); | |
| 496 | + $name = $this->load_chart_class_name( $chart_id ); | |
| 454 | 497 | $class = null; |
| 455 | 498 | if ( class_exists( $name ) || true === apply_filters( 'visualizer_load_chart', false, $name ) ) { |
| 456 | 499 | $class = new $name; |
| 457 | 500 | } |
| 458 | 501 | |
| 459 | - if ( is_null( $class ) && VISUALIZER_PRO ) { | |
| 502 | + if ( is_null( $class ) && Visualizer_Module::is_pro() ) { | |
| 460 | 503 | // lets see if this type exists in pro. New Lite(3.1.0+) & old Pro(1.8.0-). |
| 461 | - $class = apply_filters( 'visualizer_pro_chart_type_sidebar', null, array( 'type' => $type, 'settings' => get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ) ) ); | |
| 504 | + $type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true ); | |
| 505 | + $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 ) ) ); | |
| 462 | 506 | } |
| 463 | 507 | |
| 464 | 508 | return is_null( $class ) ? null : $class->getLibrary(); |
| 465 | 509 | } |
| @@ -464,8 +508,26 @@ | ||
| 464 | 508 | return is_null( $class ) ? null : $class->getLibrary(); |
| 465 | 509 | } |
| 466 | 510 | |
| 467 | 511 | /** |
| 512 | + * Returns the class name for the given chart's chart type. | |
| 513 | + */ | |
| 514 | + protected function load_chart_class_name( $chart_id ) { | |
| 515 | + $type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true ); | |
| 516 | + $lib = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, true ); | |
| 517 | + | |
| 518 | + // backward compatibility. | |
| 519 | + if ( empty( $lib ) ) { | |
| 520 | + $lib = 'GoogleCharts'; | |
| 521 | + if ( 'dataTable' === $type ) { | |
| 522 | + $lib = 'DataTable'; | |
| 523 | + } | |
| 524 | + } | |
| 525 | + $name = 'Visualizer_Render_Sidebar_Type_' . $lib . '_' . ucwords( $type ); | |
| 526 | + return $name; | |
| 527 | + } | |
| 528 | + | |
| 529 | + /** | |
| 468 | 530 | * Generates the inline CSS to apply to the chart and adds these classes to the settings. |
| 469 | 531 | * |
| 470 | 532 | * @access public |
| 471 | 533 | * @param int $id The id of the chart. |
| @@ -486,9 +548,9 @@ | ||
| 486 | 548 | foreach ( $element as $property => $value ) { |
| 487 | 549 | $attributes[] = $this->handle_css_property( $property, $value ); |
| 488 | 550 | } |
| 489 | 551 | $class_name = $id . $name; |
| 490 | - $properties = implode( '; ', array_filter( $attributes ) ); | |
| 552 | + $properties = implode( ' !important; ', array_filter( $attributes ) ); | |
| 491 | 553 | if ( ! empty( $properties ) ) { |
| 492 | 554 | $css .= '.' . $class_name . ' {' . $properties . ' !important;}'; |
| 493 | 555 | $classes[ $name ] = $class_name; |
| 494 | 556 | } |
| @@ -540,7 +602,42 @@ | ||
| 540 | 602 | ); |
| 541 | 603 | |
| 542 | 604 | $q = new WP_Query( $args ); |
| 543 | 605 | return $q->found_posts > 0; |
| 606 | + } | |
| 607 | + | |
| 608 | + /** | |
| 609 | + * Determines how many charts have been created. | |
| 610 | + */ | |
| 611 | + protected static function numberOfCharts() { | |
| 612 | + $args = array( | |
| 613 | + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, | |
| 614 | + 'fields' => 'ids', | |
| 615 | + 'post_status' => 'publish', | |
| 616 | + 'posts_per_page' => 300, | |
| 617 | + ); | |
| 618 | + | |
| 619 | + $q = new WP_Query( $args ); | |
| 620 | + return $q->found_posts; | |
| 621 | + } | |
| 622 | + | |
| 623 | + /** | |
| 624 | + * Checks if the PRO version is active. | |
| 625 | + * | |
| 626 | + * @since 3.3.0 | |
| 627 | + */ | |
| 628 | + public static function is_pro() { | |
| 629 | + // versions of pro before 1.9.0 will use the constant VISUALIZER_PRO | |
| 630 | + // versions of pro 1.9.0 onwards will use the filter | |
| 631 | + return apply_filters( 'visualizer_is_pro', VISUALIZER_PRO ); | |
| 632 | + } | |
| 633 | + | |
| 634 | + /** | |
| 635 | + * Checks if the PRO version is older than a particular version. | |
| 636 | + * | |
| 637 | + * @since 3.3.0 | |
| 638 | + */ | |
| 639 | + public static function is_pro_older_than( $version ) { | |
| 640 | + return version_compare( VISUALIZER_PRO_VERSION, $version, '<' ); | |
| 544 | 641 | } |
| 545 | 642 | |
| 546 | 643 | } |