-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflowmattic-xlsx-conversion-api.php
More file actions
154 lines (129 loc) · 4.5 KB
/
Copy pathflowmattic-xlsx-conversion-api.php
File metadata and controls
154 lines (129 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* Plugin Name: FlowMattic XLSX Conversion API
* Plugin URI: https://flowmattic.com
* Description: A plugin to convert XLSX files to CSV or JSON. CSV is uploaded to the media library, and JSON is returned.
* Version: 1.0.0
* Author: FlowMattic
* Author URI: https://flowmattic.com
* Text Domain: flowmattic-xlsx-conversion-api
* Domain Path: /languages
*/
// Ensure this file is called by WordPress.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Load PhpSpreadsheet library (use composer autoload, if installed via composer).
require_once __DIR__ . '/vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
/**
* Convert XLSX file to CSV or JSON.
*
* @param string $file_url The URL of the XLSX file.
* @param string $conversion_type Either 'csv' or 'json'.
*
* @return array|string If conversion type is 'json', returns an array with the JSON output, otherwise, returns an empty array.
*/
function flowmattic_convert_xlsx( $file_url, $conversion_type ) {
// Download the XLSX file to a temporary location.
$temp_file = download_url( $file_url );
if ( is_wp_error( $temp_file ) ) {
return array( 'error' => 'Failed to download file.' );
}
// Load the XLSX file.
try {
$spreadsheet = IOFactory::load( $temp_file );
} catch ( Exception $e ) {
@unlink( $temp_file );
return array( 'error' => 'Failed to load XLSX file.' );
}
// Get the original filename without extension for naming the CSV.
$path_parts = pathinfo( $file_url );
$base_filename = $path_parts['filename']; // Filename without extension
// Handle conversion type.
if ( 'csv' === $conversion_type ) {
$csv_file = flowmattic_xlsx_to_csv( $spreadsheet, $base_filename );
if ( ! is_wp_error( $csv_file ) ) {
@unlink( $temp_file ); // Clean up temporary file.
return array( 'csv_url' => wp_get_attachment_url( $csv_file ) );
} else {
return array( 'error' => 'CSV conversion failed.' );
}
} elseif ( 'json' === $conversion_type ) {
$json_output = flowmattic_xlsx_to_json( $spreadsheet );
@unlink( $temp_file ); // Clean up temporary file.
return array( 'json_output' => wp_json_encode( $json_output ) );
}
@unlink( $temp_file ); // Clean up temporary file.
return array( 'error' => 'Invalid conversion type.' );
}
/**
* Convert XLSX to CSV and upload to the media library.
*
* @param PhpOffice\PhpSpreadsheet\Spreadsheet $spreadsheet The spreadsheet object.
* @param string $base_filename The base filename to use for the CSV file.
*
* @return int|WP_Error The attachment ID on success, or WP_Error on failure.
*/
function flowmattic_xlsx_to_csv( $spreadsheet, $base_filename ) {
// Create temporary file for CSV.
$csv_file_path = wp_tempnam( $base_filename );
$worksheet = $spreadsheet->getActiveSheet();
$data = $worksheet->toArray();
// Open CSV file for writing.
$csv_file = fopen( $csv_file_path, 'w' );
// Filter out rows that only have content in the first column, and empty rows/columns.
foreach ( $data as $row ) {
$filtered_row = array_filter( $row, function ( $cell ) {
return $cell !== null && $cell !== '';
});
// Skip rows that have only the first column filled.
if ( count( $filtered_row ) === 1 && ! empty( $row[0] ) ) {
continue; // Skip this row if only the first column is filled.
}
// Only write non-empty rows that have more than just the first column.
if ( ! empty( $filtered_row ) ) {
fputcsv( $csv_file, $filtered_row );
}
}
fclose( $csv_file );
// Prepare CSV file for upload to media library.
$file_array = array(
'name' => $base_filename . '.csv',
'tmp_name' => $csv_file_path,
);
// Upload CSV file to the media library.
$attachment_id = media_handle_sideload( $file_array, 0 );
if ( is_wp_error( $attachment_id ) ) {
@unlink( $csv_file_path ); // Clean up temporary file.
return $attachment_id;
}
return $attachment_id;
}
/**
* Convert XLSX to JSON.
*
* @param PhpOffice\PhpSpreadsheet\Spreadsheet $spreadsheet The spreadsheet object.
*
* @return array The JSON output.
*/
function flowmattic_xlsx_to_json( $spreadsheet ) {
$worksheet = $spreadsheet->getActiveSheet();
$data = $worksheet->toArray();
// Filter out empty rows and empty columns.
$filtered_data = array_map(
function( $row ) {
// Remove empty columns.
return array_filter(
$row,
function( $cell ) {
return $cell !== null && $cell !== '';
}
);
},
$data
);
// Remove rows that are completely empty.
$filtered_data = array_filter( $filtered_data, 'count' );
return array_values( $filtered_data );
}