| @@ -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>'; |
| @@ -426,8 +451,9 @@ | ||
| 426 | 451 | return $default; |
| 427 | 452 | } |
| 428 | 453 | |
| 429 | 454 | if ( ! $wp_filesystem->exists( $dir ) ) { |
| 455 | + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found | |
| 430 | 456 | if ( ( $done = $wp_filesystem->mkdir( $dir ) ) === false ) { |
| 431 | 457 | do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to create directory %s', $dir ), 'error', __FILE__, __LINE__ ); |
| 432 | 458 | return $default; |
| 433 | 459 | } |
| @@ -435,8 +461,9 @@ | ||
| 435 | 461 | |
| 436 | 462 | // if file does not exist, copy. |
| 437 | 463 | if ( ! $wp_filesystem->exists( $file ) ) { |
| 438 | 464 | $src = str_replace( ABSPATH, $wp_filesystem->abspath(), VISUALIZER_ABSPATH . '/js/customization.js' ); |
| 465 | + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found | |
| 439 | 466 | if ( ( $done = $wp_filesystem->copy( $src, $file ) ) === false ) { |
| 440 | 467 | do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to copy file %s to %s', $src, $file ), 'error', __FILE__, __LINE__ ); |
| 441 | 468 | return $default; |
| 442 | 469 | } |