| @@ -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(); |
| @@ -222,16 +232,18 @@ | ||
| 222 | 232 | */ |
| 223 | 233 | private function _getCSV( $rows, $filename ) { |
| 224 | 234 | $filename .= '.csv'; |
| 225 | 235 | |
| 236 | + $bom = chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ); | |
| 226 | 237 | $fp = tmpfile(); |
| 227 | 238 | // support for MS Excel |
| 228 | - fprintf( $fp, $bom = ( chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ) ) ); | |
| 239 | + fprintf( $fp, $bom ); | |
| 229 | 240 | foreach ( $rows as $row ) { |
| 230 | 241 | fputcsv( $fp, $row ); |
| 231 | 242 | } |
| 232 | 243 | rewind( $fp ); |
| 233 | 244 | $csv = ''; |
| 245 | + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition | |
| 234 | 246 | while ( ( $array = fgetcsv( $fp ) ) !== false ) { |
| 235 | 247 | if ( strlen( $csv ) > 0 ) { |
| 236 | 248 | $csv .= PHP_EOL; |
| 237 | 249 | } |
| @@ -241,8 +253,9 @@ | ||
| 241 | 253 | |
| 242 | 254 | return array( |
| 243 | 255 | 'csv' => $csv, |
| 244 | 256 | 'name' => $filename, |
| 257 | + 'string' => str_replace( $bom, '', $csv ), | |
| 245 | 258 | ); |
| 246 | 259 | } |
| 247 | 260 | |
| 248 | 261 | /** |
| @@ -252,30 +265,36 @@ | ||
| 252 | 265 | * @param array $rows The array of data. |
| 253 | 266 | * @param string $filename The name of the file to use. |
| 254 | 267 | */ |
| 255 | 268 | private function _getExcel( $rows, $filename ) { |
| 256 | - // 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 | |
| 257 | 270 | $chart = substr( $filename, 0, 30 ); |
| 258 | 271 | $filename .= '.xlsx'; |
| 259 | 272 | |
| 273 | + $vendor_file = VISUALIZER_ABSPATH . '/vendor/autoload.php'; | |
| 274 | + if ( is_readable( $vendor_file ) ) { | |
| 275 | + include_once( $vendor_file ); | |
| 276 | + } | |
| 277 | + | |
| 260 | 278 | $xlsData = ''; |
| 261 | - if ( class_exists( 'PHPExcel' ) ) { | |
| 262 | - $doc = new PHPExcel(); | |
| 279 | + if ( class_exists( 'PhpOffice\PhpSpreadsheet\Spreadsheet' ) ) { | |
| 280 | + $doc = new PhpOffice\PhpSpreadsheet\Spreadsheet(); | |
| 263 | 281 | $doc->getActiveSheet()->fromArray( $rows, null, 'A1' ); |
| 264 | 282 | $doc->getActiveSheet()->setTitle( sanitize_title( $chart ) ); |
| 265 | 283 | $doc = apply_filters( 'visualizer_excel_doc', $doc ); |
| 266 | - $writer = PHPExcel_IOFactory::createWriter( $doc, 'Excel2007' ); | |
| 284 | + $writer = PhpOffice\PhpSpreadsheet\IOFactory::createWriter( $doc, 'Xlsx' ); | |
| 267 | 285 | ob_start(); |
| 268 | 286 | $writer->save( 'php://output' ); |
| 269 | 287 | $xlsData = ob_get_contents(); |
| 270 | 288 | ob_end_clean(); |
| 271 | 289 | } 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!' ); | |
| 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!' ); | |
| 274 | 292 | } |
| 275 | 293 | return array( |
| 276 | 294 | 'csv' => 'data:application/vnd.ms-excel;base64,' . base64_encode( $xlsData ), |
| 277 | 295 | 'name' => $filename, |
| 296 | + 'raw' => base64_encode( $xlsData ), | |
| 278 | 297 | ); |
| 279 | 298 | } |
| 280 | 299 | |
| 281 | 300 | /** |
| @@ -305,8 +324,14 @@ | ||
| 305 | 324 | |
| 306 | 325 | $table = '<table class="visualizer-print">'; |
| 307 | 326 | $index = 0; |
| 308 | 327 | foreach ( $rows as $row ) { |
| 328 | + // skip the data type row. | |
| 329 | + if ( 1 === $index ) { | |
| 330 | + $index++; | |
| 331 | + continue; | |
| 332 | + } | |
| 333 | + | |
| 309 | 334 | $table .= '<tr>'; |
| 310 | 335 | foreach ( $row as $col ) { |
| 311 | 336 | if ( $index === 0 ) { |
| 312 | 337 | $table .= '<th>' . $col . '</th>'; |
| @@ -392,7 +417,157 @@ | ||
| 392 | 417 | if ( count( $array ) < 2 ) { |
| 393 | 418 | return ''; |
| 394 | 419 | } |
| 395 | 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; | |
| 396 | 571 | } |
| 397 | 572 | |
| 398 | 573 | } |