| @@ -65,9 +65,25 @@ | ||
| 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 ); | |
| 70 | + $this->_addAction( 'pre_get_posts', 'PreGetPosts' ); | |
| 71 | + register_shutdown_function( array( $this, 'onShutdown' ) ); | |
| 72 | + } | |
| 69 | 73 | |
| 74 | + /** | |
| 75 | + * Register a shutdown hook to catch fatal errors. | |
| 76 | + * | |
| 77 | + * @since ? | |
| 78 | + * | |
| 79 | + * @access public | |
| 80 | + */ | |
| 81 | + public function onShutdown() { | |
| 82 | + $error = error_get_last(); | |
| 83 | + if ( $error && $error['type'] === E_ERROR && false !== strpos( $error['file'], 'Visualizer/' ) ) { | |
| 84 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Critical error %s', print_r( $error, true ) ), 'error', __FILE__, __LINE__ ); | |
| 85 | + } | |
| 70 | 86 | } |
| 71 | 87 | |
| 72 | 88 | /** |
| 73 | 89 | * Registers an action hook. |
| @@ -96,18 +112,18 @@ | ||
| 96 | 112 | * @access public |
| 97 | 113 | * @param string $tag The name of the AJAX action to which the $method is hooked. |
| 98 | 114 | * @param string $method Optional. The name of the method to be called. If the name of the method is not provided, tag name will be used as method name. |
| 99 | 115 | * @param bool $methodClass The root of the method. |
| 100 | - * @param boolean $private Optional. Determines if we should register hook for logged in users. | |
| 101 | - * @param boolean $public Optional. Determines if we should register hook for not logged in users. | |
| 116 | + * @param boolean $logged_in Optional. Determines if we should register hook for logged in users. | |
| 117 | + * @param boolean $logged_out Optional. Determines if we should register hook for not logged in users. | |
| 102 | 118 | * @return Visualizer_Module |
| 103 | 119 | */ |
| 104 | - protected function _addAjaxAction( $tag, $method = '', $methodClass = null, $private = true, $public = false ) { | |
| 105 | - if ( $private ) { | |
| 120 | + protected function _addAjaxAction( $tag, $method = '', $methodClass = null, $logged_in = true, $logged_out = false ) { | |
| 121 | + if ( $logged_in ) { | |
| 106 | 122 | $this->_addAction( 'wp_ajax_' . $tag, $method, $methodClass ); |
| 107 | 123 | } |
| 108 | 124 | |
| 109 | - if ( $public ) { | |
| 125 | + if ( $logged_out ) { | |
| 110 | 126 | $this->_addAction( 'wp_ajax_nopriv_' . $tag, $method, $methodClass ); |
| 111 | 127 | } |
| 112 | 128 | |
| 113 | 129 | return $this; |
| @@ -120,9 +136,9 @@ | ||
| 120 | 136 | * @uses add_filter() To register filter hook. |
| 121 | 137 | * |
| 122 | 138 | * @access protected |
| 123 | 139 | * @param string $tag The name of the filter to hook the $method to. |
| 124 | - * @param type $method The name of the method to be called when the filter is applied. | |
| 140 | + * @param string $method The name of the method to be called when the filter is applied. | |
| 125 | 141 | * @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action. |
| 126 | 142 | * @param int $accepted_args optional. The number of arguments the function accept (default 1). |
| 127 | 143 | * @return Visualizer_Module |
| 128 | 144 | */ |
| @@ -147,8 +163,17 @@ | ||
| 147 | 163 | return $this; |
| 148 | 164 | } |
| 149 | 165 | |
| 150 | 166 | /** |
| 167 | + * A wrapper around the actual function _getDataAs. This function is invoked as a filter. | |
| 168 | + * | |
| 169 | + * @since 3.2.0 | |
| 170 | + */ | |
| 171 | + public function getDataAs( $data, $chart_id, $type ) { | |
| 172 | + return $this->_getDataAs( $chart_id, $type ); | |
| 173 | + } | |
| 174 | + | |
| 175 | + /** | |
| 151 | 176 | * Extracts the data for a chart and prepares it for the given type. |
| 152 | 177 | * |
| 153 | 178 | * @access public |
| 154 | 179 | * @param int $chart_id The chart id. |
| @@ -158,15 +183,15 @@ | ||
| 158 | 183 | $final = null; |
| 159 | 184 | $success = false; |
| 160 | 185 | if ( $chart_id ) { |
| 161 | 186 | $chart = get_post( $chart_id ); |
| 162 | - $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER; | |
| 187 | + $success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER; | |
| 163 | 188 | } |
| 164 | 189 | if ( $success ) { |
| 165 | 190 | $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ); |
| 166 | 191 | $rows = array(); |
| 167 | 192 | $series = get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true ); |
| 168 | - $data = unserialize( $chart->post_content ); | |
| 193 | + $data = self::get_chart_data( $chart, $type, false ); | |
| 169 | 194 | if ( ! empty( $series ) ) { |
| 170 | 195 | $row = array(); |
| 171 | 196 | foreach ( $series as $array ) { |
| 172 | 197 | $row[] = $array['label']; |
| @@ -195,14 +220,28 @@ | ||
| 195 | 220 | } |
| 196 | 221 | } |
| 197 | 222 | } |
| 198 | 223 | |
| 199 | - $filename = isset( $settings['title'] ) && ! empty( $settings['title'] ) ? $settings['title'] : 'visualizer#' . $chart_id; | |
| 224 | + $title = 'visualizer#' . $chart_id; | |
| 225 | + if ( ! empty( $settings['title'] ) ) { | |
| 226 | + $title = $settings['title']; | |
| 227 | + } | |
| 228 | + // for ChartJS, title is an array. | |
| 229 | + if ( is_array( $title ) && isset( $title['text'] ) ) { | |
| 230 | + $title = $title['text']; | |
| 231 | + } | |
| 232 | + if ( empty( $title ) ) { | |
| 233 | + $title = 'visualizer#' . $chart_id; | |
| 234 | + } | |
| 200 | 235 | |
| 236 | + $filename = $title; | |
| 201 | 237 | switch ( $type ) { |
| 202 | 238 | case 'csv': |
| 203 | - $final = $this->_getCSV( $rows, $filename ); | |
| 239 | + $final = $this->_getCSV( $rows, $filename, false ); | |
| 204 | 240 | break; |
| 241 | + case 'csv-display': | |
| 242 | + $final = $this->_getCSV( $rows, $filename, true ); | |
| 243 | + break; | |
| 205 | 244 | case 'xls': |
| 206 | 245 | $final = $this->_getExcel( $rows, $filename ); |
| 207 | 246 | break; |
| 208 | 247 | case 'print': |
| @@ -207,8 +246,11 @@ | ||
| 207 | 246 | break; |
| 208 | 247 | case 'print': |
| 209 | 248 | $final = $this->_getHTML( $rows ); |
| 210 | 249 | break; |
| 250 | + case 'image': | |
| 251 | + $final = $this->_getImage( $chart ); | |
| 252 | + break; | |
| 211 | 253 | } |
| 212 | 254 | } |
| 213 | 255 | return $final; |
| 214 | 256 | } |
| @@ -218,25 +260,55 @@ | ||
| 218 | 260 | * |
| 219 | 261 | * @access private |
| 220 | 262 | * @param array $rows The array of data. |
| 221 | 263 | * @param string $filename The name of the file to use. |
| 264 | + * @param bool $enclose Enclose strings that have commas in them in double quotes. | |
| 222 | 265 | */ |
| 223 | - private function _getCSV( $rows, $filename ) { | |
| 266 | + private function _getCSV( $rows, $filename, $enclose ) { | |
| 224 | 267 | $filename .= '.csv'; |
| 225 | 268 | |
| 226 | - $fp = tmpfile(); | |
| 269 | + $bom = chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ); | |
| 270 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged | |
| 271 | + $fp = function_exists( 'tmpfile' ) ? @tmpfile() : null; | |
| 272 | + if ( ! $fp ) { | |
| 273 | + if ( ! function_exists( 'wp_tempnam' ) ) { | |
| 274 | + require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 275 | + } | |
| 276 | + $fp = fopen( wp_tempnam(), 'w+' ); | |
| 277 | + } | |
| 278 | + if ( ! $fp ) { | |
| 279 | + return array( 'csv' => '', 'name' => $filename, 'string' => '' ); | |
| 280 | + } | |
| 281 | + if ( ! apply_filters( 'vizualizer_export_include_series_type', true ) ) { | |
| 282 | + unset( $rows[1] ); | |
| 283 | + $rows = array_values( $rows ); | |
| 284 | + } | |
| 227 | 285 | // support for MS Excel |
| 228 | - fprintf( $fp, $bom = ( chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ) ) ); | |
| 286 | + fprintf( $fp, $bom ); | |
| 229 | 287 | foreach ( $rows as $row ) { |
| 230 | 288 | fputcsv( $fp, $row ); |
| 231 | 289 | } |
| 232 | 290 | rewind( $fp ); |
| 233 | 291 | $csv = ''; |
| 234 | - while ( ( $array = fgetcsv( $fp ) ) !== false ) { | |
| 292 | + $array = fgetcsv( $fp ); | |
| 293 | + while ( $array !== false ) { | |
| 235 | 294 | if ( strlen( $csv ) > 0 ) { |
| 236 | 295 | $csv .= PHP_EOL; |
| 237 | 296 | } |
| 238 | - $csv .= implode( ',', $array ); | |
| 297 | + // if enclosure is required, check every item of this line | |
| 298 | + // if a comma exists in the item, add enclosure. | |
| 299 | + if ( $enclose ) { | |
| 300 | + $temp_array = array(); | |
| 301 | + foreach ( $array as $item ) { | |
| 302 | + if ( strpos( $item, ',' ) !== false ) { | |
| 303 | + $item = VISUALIZER_CSV_ENCLOSURE . $item . VISUALIZER_CSV_ENCLOSURE; | |
| 304 | + } | |
| 305 | + $temp_array[] = $item; | |
| 306 | + } | |
| 307 | + $array = $temp_array; | |
| 308 | + } | |
| 309 | + $csv .= implode( ',', $array ); | |
| 310 | + $array = fgetcsv( $fp ); | |
| 239 | 311 | } |
| 240 | 312 | fclose( $fp ); |
| 241 | 313 | |
| 242 | 314 | return array( |
| @@ -241,8 +313,9 @@ | ||
| 241 | 313 | |
| 242 | 314 | return array( |
| 243 | 315 | 'csv' => $csv, |
| 244 | 316 | 'name' => $filename, |
| 317 | + 'string' => str_replace( $bom, '', $csv ), | |
| 245 | 318 | ); |
| 246 | 319 | } |
| 247 | 320 | |
| 248 | 321 | /** |
| @@ -252,35 +325,54 @@ | ||
| 252 | 325 | * @param array $rows The array of data. |
| 253 | 326 | * @param string $filename The name of the file to use. |
| 254 | 327 | */ |
| 255 | 328 | private function _getExcel( $rows, $filename ) { |
| 256 | - // PHPExcel did not like sheet names longer than 31 characters and we will assume the same with PhpSpreadsheet | |
| 257 | - $chart = substr( $filename, 0, 30 ); | |
| 258 | - $filename .= '.xlsx'; | |
| 259 | - | |
| 329 | + // OpenSpout allows for long sheet names, but let's keep the same limit for compatibility. | |
| 330 | + $chart = substr( $filename, 0, 30 ); | |
| 331 | + $filename .= '.xlsx'; | |
| 332 | + if ( ! apply_filters( 'vizualizer_export_include_series_type', true ) ) { | |
| 333 | + unset( $rows[1] ); | |
| 334 | + $rows = array_values( $rows ); | |
| 335 | + $rows = array_map( | |
| 336 | + function ( $r ) { | |
| 337 | + return array_map( 'strval', $r ); | |
| 338 | + }, | |
| 339 | + $rows | |
| 340 | + ); | |
| 341 | + } | |
| 260 | 342 | $vendor_file = VISUALIZER_ABSPATH . '/vendor/autoload.php'; |
| 261 | 343 | if ( is_readable( $vendor_file ) ) { |
| 262 | - include_once( $vendor_file ); | |
| 344 | + include_once $vendor_file; | |
| 263 | 345 | } |
| 346 | + $xlsData = ''; | |
| 347 | + if ( class_exists( 'OpenSpout\Writer\Common\Creator\WriterEntityFactory' ) ) { | |
| 348 | + try { | |
| 349 | + // Use OpenSpout to create the XLSX file in memory. | |
| 350 | + $writer = \OpenSpout\Writer\Common\Creator\WriterEntityFactory::createXLSXWriter(); | |
| 351 | + $writer->openToFile( 'php://output' ); // Open to output instead of a file. | |
| 352 | + $writer->getCurrentSheet()->setName( sanitize_title( $chart ) ); | |
| 264 | 353 | |
| 265 | - $xlsData = ''; | |
| 266 | - if ( class_exists( 'PhpOffice\PhpSpreadsheet\Spreadsheet' ) ) { | |
| 267 | - $doc = new PhpOffice\PhpSpreadsheet\Spreadsheet(); | |
| 268 | - $doc->getActiveSheet()->fromArray( $rows, null, 'A1' ); | |
| 269 | - $doc->getActiveSheet()->setTitle( sanitize_title( $chart ) ); | |
| 270 | - $doc = apply_filters( 'visualizer_excel_doc', $doc ); | |
| 271 | - $writer = PhpOffice\PhpSpreadsheet\IOFactory::createWriter( $doc, 'Xlsx' ); | |
| 272 | - ob_start(); | |
| 273 | - $writer->save( 'php://output' ); | |
| 274 | - $xlsData = ob_get_contents(); | |
| 275 | - ob_end_clean(); | |
| 354 | + // Write rows. | |
| 355 | + foreach ( $rows as $row ) { | |
| 356 | + $rowFromValues = \OpenSpout\Writer\Common\Creator\WriterEntityFactory::createRowFromArray( $row ); | |
| 357 | + $writer->addRow( $rowFromValues ); | |
| 358 | + } | |
| 359 | + | |
| 360 | + ob_start(); | |
| 361 | + $writer->close(); // Saves and closes the file in the output buffer. | |
| 362 | + $xlsData = ob_get_clean(); | |
| 363 | + } catch ( Exception $e ) { | |
| 364 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'OpenSpout writer error: ' . $e->getMessage(), 'error', __FILE__, __LINE__ ); | |
| 365 | + error_log( 'OpenSpout writer error: ' . $e->getMessage() ); | |
| 366 | + } | |
| 276 | 367 | } else { |
| 277 | - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'Class PhpOffice\PhpSpreadsheet\Spreadsheet does not exist!', 'error', __FILE__, __LINE__ ); | |
| 278 | - error_log( 'Class PhpOffice\PhpSpreadsheet\Spreadsheet does not exist!' ); | |
| 368 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'Class OpenSpout\Writer\Common\Creator\WriterEntityFactory does not exist!', 'error', __FILE__, __LINE__ ); | |
| 369 | + error_log( 'Class OpenSpout\Writer\Common\Creator\WriterEntityFactory does not exist!' ); | |
| 279 | 370 | } |
| 280 | 371 | return array( |
| 281 | 372 | 'csv' => 'data:application/vnd.ms-excel;base64,' . base64_encode( $xlsData ), |
| 282 | 373 | 'name' => $filename, |
| 374 | + 'raw' => base64_encode( $xlsData ), | |
| 283 | 375 | ); |
| 284 | 376 | } |
| 285 | 377 | |
| 286 | 378 | /** |
| @@ -310,8 +402,14 @@ | ||
| 310 | 402 | |
| 311 | 403 | $table = '<table class="visualizer-print">'; |
| 312 | 404 | $index = 0; |
| 313 | 405 | foreach ( $rows as $row ) { |
| 406 | + // skip the data type row. | |
| 407 | + if ( 1 === $index ) { | |
| 408 | + ++$index; | |
| 409 | + continue; | |
| 410 | + } | |
| 411 | + | |
| 314 | 412 | $table .= '<tr>'; |
| 315 | 413 | foreach ( $row as $col ) { |
| 316 | 414 | if ( $index === 0 ) { |
| 317 | 415 | $table .= '<th>' . $col . '</th>'; |
| @@ -319,9 +417,9 @@ | ||
| 319 | 417 | $table .= '<td>' . $col . '</td>'; |
| 320 | 418 | } |
| 321 | 419 | } |
| 322 | 420 | $table .= '</tr>'; |
| 323 | - $index++; | |
| 421 | + ++$index; | |
| 324 | 422 | } |
| 325 | 423 | $table .= '</table>'; |
| 326 | 424 | |
| 327 | 425 | $html .= apply_filters( 'visualizer_print_table', $table ) . ' |
| @@ -332,15 +430,31 @@ | ||
| 332 | 430 | ); |
| 333 | 431 | } |
| 334 | 432 | |
| 335 | 433 | /** |
| 434 | + * Disable revisions temporarily for visualizer post type. | |
| 435 | + */ | |
| 436 | + final protected function disableRevisionsTemporarily() { | |
| 437 | + add_filter( | |
| 438 | + 'wp_revisions_to_keep', function ( $num, $post ) { | |
| 439 | + if ( $post->post_type === Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 440 | + return 0; | |
| 441 | + } | |
| 442 | + return $num; | |
| 443 | + }, 10, 2 | |
| 444 | + ); | |
| 445 | + } | |
| 446 | + | |
| 447 | + /** | |
| 336 | 448 | * Undo revisions for the chart, and if necessary, restore the earliest version. |
| 337 | 449 | * |
| 338 | 450 | * @return bool If any revisions were found. |
| 339 | 451 | */ |
| 340 | - public final function undoRevisions( $chart_id, $restore = false ) { | |
| 452 | + final public function undoRevisions( $chart_id, $restore = false ) { | |
| 341 | 453 | do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'undoRevisions for %d with%s restore', $chart_id, ( $restore ? '' : 'out' ) ), 'debug', __FILE__, __LINE__ ); |
| 342 | - | |
| 454 | + if ( get_post_type( $chart_id ) !== Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 455 | + return false; | |
| 456 | + } | |
| 343 | 457 | $revisions = wp_get_post_revisions( $chart_id, array( 'order' => 'ASC' ) ); |
| 344 | 458 | if ( count( $revisions ) > 1 ) { |
| 345 | 459 | $revision_ids = array_keys( $revisions ); |
| 346 | 460 | |
| @@ -346,9 +460,9 @@ | ||
| 346 | 460 | |
| 347 | 461 | do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'found %d revisions = %s', count( $revisions ), print_r( $revision_ids, true ) ), 'debug', __FILE__, __LINE__ ); |
| 348 | 462 | |
| 349 | 463 | // when we restore, a new revision is likely to be created. so, let's disable revisions for the time being. |
| 350 | - add_filter( 'wp_revisions_to_keep', '__return_false' ); | |
| 464 | + $this->disableRevisionsTemporarily(); | |
| 351 | 465 | |
| 352 | 466 | if ( $restore ) { |
| 353 | 467 | // restore to the oldest one i.e. the first one. |
| 354 | 468 | wp_restore_post_revision( array_shift( $revision_ids ) ); |
| @@ -366,11 +480,19 @@ | ||
| 366 | 480 | |
| 367 | 481 | /** |
| 368 | 482 | * If existing revisions exist for the chart, restore the earliest version and then create a new revision to initiate editing. |
| 369 | 483 | */ |
| 370 | - public final function handleExistingRevisions( $chart_id, $chart ) { | |
| 484 | + final public function handleExistingRevisions( $chart_id, $chart ) { | |
| 485 | + | |
| 371 | 486 | do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'handleExistingRevisions for %d', $chart_id ), 'debug', __FILE__, __LINE__ ); |
| 372 | - | |
| 487 | + if ( get_post_type( $chart_id ) !== Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 488 | + return $chart_id; | |
| 489 | + } | |
| 490 | + // AI Builder (D3) charts use their own publish flow — skip the classic revision-restore | |
| 491 | + // mechanism, which would otherwise overwrite the saved title and settings. | |
| 492 | + if ( 'd3' === get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, true ) ) { | |
| 493 | + return $chart; | |
| 494 | + } | |
| 373 | 495 | // undo revisions. |
| 374 | 496 | $revisions_found = $this->undoRevisions( $chart_id, true ); |
| 375 | 497 | |
| 376 | 498 | // create revision for the edit action. |
| @@ -414,41 +536,55 @@ | ||
| 414 | 536 | if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) { |
| 415 | 537 | return $default; |
| 416 | 538 | } |
| 417 | 539 | |
| 418 | - require_once( ABSPATH . 'wp-admin/includes/file.php' ); | |
| 419 | - WP_Filesystem(); | |
| 420 | - global $wp_filesystem; | |
| 540 | + try { | |
| 541 | + require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 542 | + WP_Filesystem(); | |
| 543 | + global $wp_filesystem; | |
| 544 | + if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base' ) ) { | |
| 545 | + return $default; | |
| 546 | + } | |
| 421 | 547 | |
| 422 | - $dir = $wp_filesystem->wp_content_dir() . 'uploads/visualizer'; | |
| 423 | - $file = $wp_filesystem->wp_content_dir() . 'uploads/visualizer/customization.js'; | |
| 548 | + $multisite_arg = '/'; | |
| 549 | + if ( is_multisite() && ! is_main_site() ) { | |
| 550 | + $multisite_arg = '/sites/' . get_current_blog_id() . '/'; | |
| 551 | + } | |
| 424 | 552 | |
| 425 | - if ( $wp_filesystem->is_readable( $file ) ) { | |
| 426 | - return $specific; | |
| 427 | - } | |
| 553 | + $dir = $wp_filesystem->wp_content_dir() . 'uploads' . $multisite_arg . 'visualizer'; | |
| 554 | + $file = $wp_filesystem->wp_content_dir() . 'uploads' . $multisite_arg . 'visualizer/customization.js'; | |
| 428 | 555 | |
| 429 | - if ( $wp_filesystem->exists( $file ) && ! $wp_filesystem->is_readable( $file ) ) { | |
| 430 | - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to read file %s', $file ), 'error', __FILE__, __LINE__ ); | |
| 431 | - return $default; | |
| 432 | - } | |
| 556 | + if ( $wp_filesystem->is_readable( $file ) ) { | |
| 557 | + return $specific; | |
| 558 | + } | |
| 433 | 559 | |
| 434 | - if ( ! $wp_filesystem->exists( $dir ) ) { | |
| 435 | - if ( ( $done = $wp_filesystem->mkdir( $dir ) ) === false ) { | |
| 436 | - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to create directory %s', $dir ), 'error', __FILE__, __LINE__ ); | |
| 560 | + if ( $wp_filesystem->exists( $file ) && ! $wp_filesystem->is_readable( $file ) ) { | |
| 561 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to read file %s', $file ), 'error', __FILE__, __LINE__ ); | |
| 437 | 562 | return $default; |
| 438 | 563 | } |
| 439 | - } | |
| 440 | 564 | |
| 441 | - // if file does not exist, copy. | |
| 442 | - if ( ! $wp_filesystem->exists( $file ) ) { | |
| 443 | - $src = str_replace( ABSPATH, $wp_filesystem->abspath(), VISUALIZER_ABSPATH . '/js/customization.js' ); | |
| 444 | - if ( ( $done = $wp_filesystem->copy( $src, $file ) ) === false ) { | |
| 445 | - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to copy file %s to %s', $src, $file ), 'error', __FILE__, __LINE__ ); | |
| 446 | - return $default; | |
| 565 | + if ( ! $wp_filesystem->exists( $dir ) ) { | |
| 566 | + $done = $wp_filesystem->mkdir( $dir ); | |
| 567 | + if ( $done === false ) { | |
| 568 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to create directory %s', $dir ), 'error', __FILE__, __LINE__ ); | |
| 569 | + return $default; | |
| 570 | + } | |
| 447 | 571 | } |
| 572 | + | |
| 573 | + // if file does not exist, copy. | |
| 574 | + if ( ! $wp_filesystem->exists( $file ) ) { | |
| 575 | + $src = str_replace( ABSPATH, $wp_filesystem->abspath(), VISUALIZER_ABSPATH . '/js/customization.js' ); | |
| 576 | + $done = $wp_filesystem->copy( $src, $file ); | |
| 577 | + if ( $done === false ) { | |
| 578 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to copy file %s to %s', $src, $file ), 'error', __FILE__, __LINE__ ); | |
| 579 | + return $default; | |
| 580 | + } | |
| 581 | + } | |
| 582 | + | |
| 583 | + return $specific; | |
| 584 | + } catch ( \Throwable $e ) { | |
| 585 | + return $default; | |
| 448 | 586 | } |
| 449 | - | |
| 450 | - return $specific; | |
| 451 | 587 | } |
| 452 | 588 | |
| 453 | 589 | /** |
| 454 | 590 | * Load the class for the given chart's chart type so that its assets can be loaded. |
| @@ -453,18 +589,24 @@ | ||
| 453 | 589 | /** |
| 454 | 590 | * Load the class for the given chart's chart type so that its assets can be loaded. |
| 455 | 591 | */ |
| 456 | 592 | protected function load_chart_type( $chart_id ) { |
| 457 | - $type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true ); | |
| 458 | - $name = 'Visualizer_Render_Sidebar_Type_' . ucwords( $type ); | |
| 593 | + // D3/AI charts have no traditional type class — return the library string directly. | |
| 594 | + $lib = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, true ); | |
| 595 | + if ( 'd3' === $lib ) { | |
| 596 | + return 'd3'; | |
| 597 | + } | |
| 598 | + | |
| 599 | + $name = $this->load_chart_class_name( $chart_id ); | |
| 459 | 600 | $class = null; |
| 460 | 601 | if ( class_exists( $name ) || true === apply_filters( 'visualizer_load_chart', false, $name ) ) { |
| 461 | - $class = new $name; | |
| 602 | + $class = new $name(); | |
| 462 | 603 | } |
| 463 | 604 | |
| 464 | - if ( is_null( $class ) && VISUALIZER_PRO ) { | |
| 605 | + if ( is_null( $class ) && Visualizer_Module::is_pro() ) { | |
| 465 | 606 | // lets see if this type exists in pro. New Lite(3.1.0+) & old Pro(1.8.0-). |
| 466 | - $class = apply_filters( 'visualizer_pro_chart_type_sidebar', null, array( 'type' => $type, 'settings' => get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ) ) ); | |
| 607 | + $type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true ); | |
| 608 | + $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 ) ) ); | |
| 467 | 609 | } |
| 468 | 610 | |
| 469 | 611 | return is_null( $class ) ? null : $class->getLibrary(); |
| 470 | 612 | } |
| @@ -469,8 +611,26 @@ | ||
| 469 | 611 | return is_null( $class ) ? null : $class->getLibrary(); |
| 470 | 612 | } |
| 471 | 613 | |
| 472 | 614 | /** |
| 615 | + * Returns the class name for the given chart's chart type. | |
| 616 | + */ | |
| 617 | + protected function load_chart_class_name( $chart_id ) { | |
| 618 | + $type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true ); | |
| 619 | + $lib = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, true ); | |
| 620 | + | |
| 621 | + // backward compatibility. | |
| 622 | + if ( empty( $lib ) ) { | |
| 623 | + $lib = 'GoogleCharts'; | |
| 624 | + if ( 'dataTable' === $type ) { | |
| 625 | + $lib = 'DataTable'; | |
| 626 | + } | |
| 627 | + } | |
| 628 | + $name = 'Visualizer_Render_Sidebar_Type_' . $lib . '_' . ucwords( $type ); | |
| 629 | + return $name; | |
| 630 | + } | |
| 631 | + | |
| 632 | + /** | |
| 473 | 633 | * Generates the inline CSS to apply to the chart and adds these classes to the settings. |
| 474 | 634 | * |
| 475 | 635 | * @access public |
| 476 | 636 | * @param int $id The id of the chart. |
| @@ -478,32 +638,31 @@ | ||
| 478 | 638 | */ |
| 479 | 639 | protected function get_inline_custom_css( $id, $settings ) { |
| 480 | 640 | $css = ''; |
| 481 | 641 | |
| 482 | - $arguments = array( '', $settings ); | |
| 483 | - if ( ! isset( $settings['customcss'] ) ) { | |
| 484 | - return $arguments; | |
| 485 | - } | |
| 486 | - | |
| 487 | 642 | $classes = array(); |
| 488 | 643 | $css = '<style type="text/css" name="visualizer-custom-css" id="customcss-' . $id . '">'; |
| 489 | - foreach ( $settings['customcss'] as $name => $element ) { | |
| 490 | - $attributes = array(); | |
| 491 | - foreach ( $element as $property => $value ) { | |
| 492 | - $attributes[] = $this->handle_css_property( $property, $value ); | |
| 644 | + if ( ! empty( $settings['customcss'] ) ) { | |
| 645 | + foreach ( $settings['customcss'] as $name => $element ) { | |
| 646 | + $attributes = array(); | |
| 647 | + foreach ( $element as $property => $value ) { | |
| 648 | + $attributes[] = $this->handle_css_property( $property, $value ); | |
| 649 | + } | |
| 650 | + $class_name = $id . $name; | |
| 651 | + $properties = implode( ' !important; ', array_filter( $attributes ) ); | |
| 652 | + if ( ! empty( $properties ) ) { | |
| 653 | + $css .= wp_strip_all_tags( '.' . $class_name . ' {' . $properties . ' !important;}' ); | |
| 654 | + $classes[ $name ] = $class_name; | |
| 655 | + } | |
| 493 | 656 | } |
| 494 | - $class_name = $id . $name; | |
| 495 | - $properties = implode( '; ', array_filter( $attributes ) ); | |
| 496 | - if ( ! empty( $properties ) ) { | |
| 497 | - $css .= '.' . $class_name . ' {' . $properties . ' !important;}'; | |
| 498 | - $classes[ $name ] = $class_name; | |
| 499 | - } | |
| 657 | + $settings['cssClassNames'] = $classes; | |
| 500 | 658 | } |
| 501 | - $css .= '</style>'; | |
| 502 | 659 | |
| 503 | - $settings['cssClassNames'] = $classes; | |
| 660 | + $img_path = VISUALIZER_ABSURL . 'images'; | |
| 661 | + $css .= ".locker,.locker-loader{position:absolute;top:0;left:0;width:100%;height:100%}.locker{z-index:1000;opacity:.8;background-color:#fff;-ms-filter:\"progid:DXImageTransform.Microsoft.Alpha(Opacity=80)\";filter:alpha(opacity=80)}.locker-loader{z-index:1001;background:url($img_path/ajax-loader.gif) no-repeat center center}.dt-button{display:none!important}.visualizer-front-container.visualizer-lazy-render{content-visibility: auto;}.google-visualization-controls-categoryfilter label.google-visualization-controls-label {vertical-align: middle;}.google-visualization-controls-categoryfilter li.goog-inline-block {margin: 0 0.2em;}.google-visualization-controls-categoryfilter li {padding: 0 0.2em;}.visualizer-front-container .dataTables_scrollHeadInner{margin: 0 auto;}"; | |
| 662 | + $css .= '</style>'; | |
| 504 | 663 | |
| 505 | - $arguments = array( $css, $settings ); | |
| 664 | + $arguments = array( $css, $settings ); | |
| 506 | 665 | apply_filters_ref_array( 'visualizer_inline_css', array( &$arguments ) ); |
| 507 | 666 | |
| 508 | 667 | return $arguments; |
| 509 | 668 | } |
| @@ -547,5 +706,265 @@ | ||
| 547 | 706 | $q = new WP_Query( $args ); |
| 548 | 707 | return $q->found_posts > 0; |
| 549 | 708 | } |
| 550 | 709 | |
| 710 | + /** | |
| 711 | + * Determines how many charts have been created. | |
| 712 | + */ | |
| 713 | + protected static function numberOfCharts() { | |
| 714 | + $args = array( | |
| 715 | + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, | |
| 716 | + 'fields' => 'ids', | |
| 717 | + 'post_status' => 'publish', | |
| 718 | + 'posts_per_page' => 300, | |
| 719 | + ); | |
| 720 | + | |
| 721 | + $q = new WP_Query( $args ); | |
| 722 | + return $q->found_posts; | |
| 723 | + } | |
| 724 | + | |
| 725 | + /** | |
| 726 | + * Checks whether the current user may edit a specific chart. | |
| 727 | + * | |
| 728 | + * @param int $chart_id Chart ID. | |
| 729 | + * @return bool | |
| 730 | + */ | |
| 731 | + public static function can_edit_chart( $chart_id ) { | |
| 732 | + $chart_id = absint( $chart_id ); | |
| 733 | + if ( ! $chart_id ) { | |
| 734 | + return false; | |
| 735 | + } | |
| 736 | + | |
| 737 | + $chart = get_post( $chart_id ); | |
| 738 | + return $chart | |
| 739 | + && Visualizer_Plugin::CPT_VISUALIZER === $chart->post_type | |
| 740 | + && ( | |
| 741 | + current_user_can( 'edit_post', $chart_id ) | |
| 742 | + || ( (int) $chart->post_author === get_current_user_id() && current_user_can( 'edit_posts' ) ) | |
| 743 | + ); | |
| 744 | + } | |
| 745 | + | |
| 746 | + /** | |
| 747 | + * Checks if the PRO version is active. | |
| 748 | + * | |
| 749 | + * @since 3.3.0 | |
| 750 | + */ | |
| 751 | + public static function is_pro() { | |
| 752 | + // versions of pro before 1.9.0 will use the constant VISUALIZER_PRO | |
| 753 | + // versions of pro 1.9.0 onwards will use the filter | |
| 754 | + return apply_filters( 'visualizer_is_pro', VISUALIZER_PRO ); | |
| 755 | + } | |
| 756 | + | |
| 757 | + /** | |
| 758 | + * Checks if the PRO version is older than a particular version. | |
| 759 | + * | |
| 760 | + * @since 3.3.0 | |
| 761 | + */ | |
| 762 | + public static function is_pro_older_than( $version ) { | |
| 763 | + return version_compare( VISUALIZER_PRO_VERSION, $version, '<' ); | |
| 764 | + } | |
| 765 | + | |
| 766 | + /** | |
| 767 | + * Should we show some specific feature on the basis of the version? | |
| 768 | + * | |
| 769 | + * @since 3.4.0 | |
| 770 | + */ | |
| 771 | + public static function can_show_feature( $feature ) { | |
| 772 | + switch ( $feature ) { | |
| 773 | + case 'simple-editor': | |
| 774 | + // if user has pro but an older version, then don't load the simple editor functionality | |
| 775 | + // as the select box will not behave as expected because the pro editor's functionality will supercede. | |
| 776 | + return ! Visualizer_Module::is_pro() || ! Visualizer_Module::is_pro_older_than( '1.9.2' ); | |
| 777 | + } | |
| 778 | + return false; | |
| 779 | + } | |
| 780 | + | |
| 781 | + /** | |
| 782 | + * Gets the features for the provided license type. | |
| 783 | + */ | |
| 784 | + final public static function get_features_for_license( $plan ) { | |
| 785 | + $is_new_personal = apply_filters( 'visualizer_is_new_personal', false ); | |
| 786 | + switch ( $plan ) { | |
| 787 | + case 1: | |
| 788 | + $features = array( 'import-wp', 'import-wc-report', 'import-file', 'import-url' ); | |
| 789 | + if ( ! $is_new_personal ) { | |
| 790 | + $features[] = 'db-query'; | |
| 791 | + } | |
| 792 | + return $features; | |
| 793 | + case 2: | |
| 794 | + $features = array( 'schedule-chart', 'chart-permissions', 'import-chart', 'data-filter-configuration', 'frontend-actions' ); | |
| 795 | + if ( $is_new_personal ) { | |
| 796 | + $features[] = 'db-query'; | |
| 797 | + } | |
| 798 | + return $features; | |
| 799 | + } | |
| 800 | + } | |
| 801 | + | |
| 802 | + /** | |
| 803 | + * Safely unserialize chart/source content, blocking PHP object injection. | |
| 804 | + * | |
| 805 | + * Single guarded chokepoint shared by chart/source content sinks so the | |
| 806 | + * allowed_classes guard cannot be dropped from one call site independently. | |
| 807 | + * | |
| 808 | + * @param mixed $content The serialized content (only strings are decoded). | |
| 809 | + * @return mixed The decoded value (array for valid chart data), or false. | |
| 810 | + */ | |
| 811 | + public static function decode_content( $content ) { | |
| 812 | + if ( ! is_string( $content ) ) { | |
| 813 | + return false; | |
| 814 | + } | |
| 815 | + $value = unserialize( trim( $content ), array( 'allowed_classes' => false ) ); | |
| 816 | + if ( self::contains_references( $value ) ) { | |
| 817 | + return false; | |
| 818 | + } | |
| 819 | + return self::strip_incomplete_objects( $value ); | |
| 820 | + } | |
| 821 | + | |
| 822 | + /** | |
| 823 | + * Check decoded arrays for references before recursively processing them. | |
| 824 | + * | |
| 825 | + * Cyclic serialized arrays necessarily contain a reference. Rejecting all | |
| 826 | + * references also prevents shared references from becoming cycles later, | |
| 827 | + * so strip_incomplete_objects() cannot recurse without terminating. | |
| 828 | + * | |
| 829 | + * @param mixed $value The decoded value. | |
| 830 | + * @return bool Whether the value contains an array reference. | |
| 831 | + */ | |
| 832 | + private static function contains_references( $value ) { | |
| 833 | + if ( ! is_array( $value ) ) { | |
| 834 | + return false; | |
| 835 | + } | |
| 836 | + foreach ( array_keys( $value ) as $key ) { | |
| 837 | + if ( null !== ReflectionReference::fromArrayElement( $value, $key ) ) { | |
| 838 | + return true; | |
| 839 | + } | |
| 840 | + if ( is_array( $value[ $key ] ) && self::contains_references( $value[ $key ] ) ) { | |
| 841 | + return true; | |
| 842 | + } | |
| 843 | + } | |
| 844 | + return false; | |
| 845 | + } | |
| 846 | + | |
| 847 | + /** | |
| 848 | + * Remove the __PHP_Incomplete_Class stubs the allowed_classes guard leaves | |
| 849 | + * behind; they crash map_deep() when the decoded value is written back to | |
| 850 | + * post meta. Legitimate chart content is nested arrays/scalars only. | |
| 851 | + * | |
| 852 | + * @param mixed $value The decoded value. | |
| 853 | + * @return mixed The value without object stubs; false for a top-level stub. | |
| 854 | + */ | |
| 855 | + private static function strip_incomplete_objects( $value ) { | |
| 856 | + if ( $value instanceof __PHP_Incomplete_Class ) { | |
| 857 | + return false; | |
| 858 | + } | |
| 859 | + if ( is_array( $value ) ) { | |
| 860 | + foreach ( $value as $key => $item ) { | |
| 861 | + if ( $item instanceof __PHP_Incomplete_Class ) { | |
| 862 | + unset( $value[ $key ] ); | |
| 863 | + } elseif ( is_array( $item ) ) { | |
| 864 | + $value[ $key ] = self::strip_incomplete_objects( $item ); | |
| 865 | + } | |
| 866 | + } | |
| 867 | + } | |
| 868 | + return $value; | |
| 869 | + } | |
| 870 | + | |
| 871 | + /** | |
| 872 | + * Object-injection-safe drop-in for maybe_unserialize(). | |
| 873 | + * | |
| 874 | + * @param mixed $value Raw meta/content value. | |
| 875 | + * @return mixed The decoded value for serialized input, the value unchanged otherwise. | |
| 876 | + */ | |
| 877 | + public static function maybe_decode_content( $value ) { | |
| 878 | + return is_serialized( $value ) ? self::decode_content( $value ) : $value; | |
| 879 | + } | |
| 880 | + | |
| 881 | + /** | |
| 882 | + * Gets the chart content after common manipulations. | |
| 883 | + */ | |
| 884 | + public static function get_chart_data( $chart, $type, $run_filter = true ) { | |
| 885 | + // change HTML entities | |
| 886 | + $post_content = html_entity_decode( htmlentities( $chart->post_content ) ); | |
| 887 | + $post_content = preg_replace_callback( | |
| 888 | + '!s:(\d+):"(.*?)";!s', | |
| 889 | + function ( $matches ) { | |
| 890 | + if ( isset( $matches[2] ) ) { | |
| 891 | + return 's:' . strlen( $matches[2] ) . ':"' . $matches[2] . '";'; | |
| 892 | + } | |
| 893 | + }, | |
| 894 | + $post_content | |
| 895 | + ); | |
| 896 | + $data = self::decode_content( $post_content ); | |
| 897 | + $altered = array(); | |
| 898 | + if ( ! empty( $data ) ) { | |
| 899 | + foreach ( $data as $index => $array ) { | |
| 900 | + if ( ! is_array( $index ) && is_array( $array ) ) { | |
| 901 | + foreach ( $array as &$datum ) { | |
| 902 | + if ( is_string( $datum ) ) { | |
| 903 | + $datum = stripslashes( $datum ); | |
| 904 | + } | |
| 905 | + } | |
| 906 | + $altered[ $index ] = $array; | |
| 907 | + } | |
| 908 | + } | |
| 909 | + } | |
| 910 | + // if something goes wrong and the end result is empty, be safe and use the original data | |
| 911 | + if ( empty( $altered ) ) { | |
| 912 | + $altered = $data; | |
| 913 | + } | |
| 914 | + if ( $run_filter ) { | |
| 915 | + return apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, $altered, $chart->ID, $type ); | |
| 916 | + } | |
| 917 | + return $altered; | |
| 918 | + } | |
| 919 | + | |
| 920 | + /** | |
| 921 | + * Get chart image. | |
| 922 | + * | |
| 923 | + * @param object $chart Chart data. | |
| 924 | + * @return string | |
| 925 | + */ | |
| 926 | + public function _getImage( $chart = null ) { | |
| 927 | + $image = ''; | |
| 928 | + if ( $chart ) { | |
| 929 | + $chart_image = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ); | |
| 930 | + if ( ! empty( $chart_image ) ) { | |
| 931 | + $image = wp_get_attachment_image( $chart_image, 'full' ); | |
| 932 | + } | |
| 933 | + } | |
| 934 | + return array( | |
| 935 | + 'csv' => $image, | |
| 936 | + ); | |
| 937 | + } | |
| 938 | + | |
| 939 | + /** | |
| 940 | + * Filter chart title if visualizer post type. | |
| 941 | + * | |
| 942 | + * @param object $query WP Query object. | |
| 943 | + * @return void | |
| 944 | + */ | |
| 945 | + public function PreGetPosts( $query ) { | |
| 946 | + if ( ! $query->is_main_query() ) { | |
| 947 | + $post_type = $query->get( 'post_type' ); | |
| 948 | + if ( 'visualizer' === $post_type ) { | |
| 949 | + $this->_addFilter( Visualizer_Plugin::FILTER_CHART_TITLE, 'filterChartTitle', 10, 2 ); | |
| 950 | + } | |
| 951 | + } | |
| 952 | + } | |
| 953 | + | |
| 954 | + /** | |
| 955 | + * Filter chart title. | |
| 956 | + * | |
| 957 | + * @access public | |
| 958 | + * @param string $post_title Post title. | |
| 959 | + * @param int $post_id Post ID. | |
| 960 | + * @return string | |
| 961 | + */ | |
| 962 | + public function filterChartTitle( $post_title, $post_id ) { | |
| 963 | + $post_type = get_post_type( $post_id ); | |
| 964 | + $post_title = trim( $post_title ); | |
| 965 | + if ( 'visualizer' === $post_type && 'Visualization' === $post_title ) { | |
| 966 | + return sprintf( '%s #%d', $post_title, $post_id ); | |
| 967 | + } | |
| 968 | + return $post_title; | |
| 969 | + } | |
| 551 | 970 | } |