| @@ -526,8 +526,14 @@ | ||
| 526 | 526 | wp_redirect( add_query_arg( 'chart', (int) $chart_id ) ); |
| 527 | 527 | defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit(); |
| 528 | 528 | } |
| 529 | 529 | |
| 530 | + if ( isset( $_POST['chart-img'] ) && ! empty( $_POST['chart-img'] ) ) { | |
| 531 | + $attachment_id = $this->save_chart_image( $_POST['chart-img'], $chart_id ); | |
| 532 | + if ( $attachment_id ) { | |
| 533 | + update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $attachment_id ); | |
| 534 | + } | |
| 535 | + } | |
| 530 | 536 | $lib = $this->load_chart_type( $chart_id ); |
| 531 | 537 | |
| 532 | 538 | // the alpha color picker (RGBA) is not supported by google. |
| 533 | 539 | $color_picker_dep = 'wp-color-picker'; |
| @@ -911,8 +917,16 @@ | ||
| 911 | 917 | foreach ( $values as $row ) { |
| 912 | 918 | if ( empty( $row ) ) { |
| 913 | 919 | continue; |
| 914 | 920 | } |
| 921 | + $row = explode( ',', $row ); | |
| 922 | + $row = array_map( | |
| 923 | + function( $r ) { | |
| 924 | + return '' === $r ? ' ' : $r; | |
| 925 | + }, | |
| 926 | + $row | |
| 927 | + ); | |
| 928 | + $row = implode( ',', $row ); | |
| 915 | 929 | // don't use fpucsv here because we need to just dump the data |
| 916 | 930 | // minus the empty rows |
| 917 | 931 | // because fputcsv needs to tokenize |
| 918 | 932 | // we can standardize the CSV enclosure here and replace all ' with " |
| @@ -1415,6 +1429,50 @@ | ||
| 1415 | 1429 | |
| 1416 | 1430 | if ( ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ) ) { |
| 1417 | 1431 | defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit(); |
| 1418 | 1432 | } |
| 1433 | + } | |
| 1434 | + | |
| 1435 | + /** | |
| 1436 | + * Save chart image. | |
| 1437 | + * | |
| 1438 | + * @param string $base64_img Chart image. | |
| 1439 | + * @param int $chart_id Chart ID. | |
| 1440 | + * @return attachment ID | |
| 1441 | + */ | |
| 1442 | + public function save_chart_image( $base64_img, $chart_id ) { | |
| 1443 | + // Upload dir. | |
| 1444 | + $upload_dir = wp_upload_dir(); | |
| 1445 | + $upload_path = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR; | |
| 1446 | + | |
| 1447 | + $img = str_replace( 'data:image/png;base64,', '', $base64_img ); | |
| 1448 | + $img = str_replace( ' ', '+', $img ); | |
| 1449 | + $decoded = base64_decode( $img ); | |
| 1450 | + $filename = 'visualization-' . $chart_id . '.png'; | |
| 1451 | + $file_type = 'image/png'; | |
| 1452 | + $hashed_filename = $filename; | |
| 1453 | + | |
| 1454 | + // Delete old chart image. | |
| 1455 | + $old_attachment_id = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true ); | |
| 1456 | + if ( $old_attachment_id ) { | |
| 1457 | + wp_delete_attachment( $old_attachment_id, true ); | |
| 1458 | + } | |
| 1459 | + | |
| 1460 | + // Save the image in the uploads directory. | |
| 1461 | + require_once ABSPATH . '/wp-admin/includes/file.php'; | |
| 1462 | + \WP_Filesystem(); | |
| 1463 | + global $wp_filesystem; | |
| 1464 | + $upload_file = $wp_filesystem->put_contents( $upload_path . $hashed_filename, $decoded ); | |
| 1465 | + | |
| 1466 | + // Insert new chart image. | |
| 1467 | + $attachment = array( | |
| 1468 | + 'post_mime_type' => $file_type, | |
| 1469 | + 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $hashed_filename ) ), | |
| 1470 | + 'post_content' => '', | |
| 1471 | + 'post_status' => 'inherit', | |
| 1472 | + 'guid' => $upload_dir['url'] . '/' . basename( $hashed_filename ), | |
| 1473 | + ); | |
| 1474 | + | |
| 1475 | + $attach_id = wp_insert_attachment( $attachment, $upload_dir['path'] . '/' . $hashed_filename ); | |
| 1476 | + return $attach_id; | |
| 1419 | 1477 | } |
| 1420 | 1478 | } |