Skip to content

Commit 4ab460e

Browse files
Merge pull request #635 from contactashish13/issue-214
data changed in one editor to be editable in the other
2 parents 2dee0d4 + f714f47 commit 4ab460e

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

classes/Visualizer/Module.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,10 @@ public function _getDataAs( $chart_id, $type ) {
237237

238238
switch ( $type ) {
239239
case 'csv':
240-
$final = $this->_getCSV( $rows, $filename );
240+
$final = $this->_getCSV( $rows, $filename, false );
241+
break;
242+
case 'csv-display':
243+
$final = $this->_getCSV( $rows, $filename, true );
241244
break;
242245
case 'xls':
243246
$final = $this->_getExcel( $rows, $filename );
@@ -256,8 +259,9 @@ public function _getDataAs( $chart_id, $type ) {
256259
* @access private
257260
* @param array $rows The array of data.
258261
* @param string $filename The name of the file to use.
262+
* @param bool $enclose Enclose strings that have commas in them in double quotes.
259263
*/
260-
private function _getCSV( $rows, $filename ) {
264+
private function _getCSV( $rows, $filename, $enclose ) {
261265
$filename .= '.csv';
262266

263267
$bom = chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF );
@@ -274,6 +278,18 @@ private function _getCSV( $rows, $filename ) {
274278
if ( strlen( $csv ) > 0 ) {
275279
$csv .= PHP_EOL;
276280
}
281+
// if enclosure is required, check every item of this line
282+
// if a comma exists in the item, add enclosure.
283+
if ( $enclose ) {
284+
$temp_array = array();
285+
foreach ( $array as $item ) {
286+
if ( strpos( $item, ',' ) !== false ) {
287+
$item = VISUALIZER_CSV_ENCLOSURE . $item . VISUALIZER_CSV_ENCLOSURE;
288+
}
289+
$temp_array[] = $item;
290+
}
291+
$array = $temp_array;
292+
}
277293
$csv .= implode( ',', $array );
278294
}
279295
fclose( $fp );

classes/Visualizer/Module/Chart.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -885,8 +885,14 @@ private function handleCSVasString( $data, $editor_type ) {
885885
if ( empty( $row ) ) {
886886
continue;
887887
}
888-
$columns = explode( ',', $row );
889-
fputcsv( $handle, $columns );
888+
// don't use fpucsv here because we need to just dump the data
889+
// minus the empty rows
890+
// and if any row contains ' or ", fputcsv will mess it up
891+
// because fputcsv needs to tokenize
892+
// and let's standardize the CSV enclosure
893+
$row = str_replace( "'", VISUALIZER_CSV_ENCLOSURE, $row );
894+
fwrite( $handle, $row );
895+
fwrite( $handle, PHP_EOL );
890896
}
891897
}
892898
$source = new Visualizer_Source_Csv( $tmpfile );
@@ -946,7 +952,7 @@ private function handleTabularData() {
946952
for ( $j = 0; $j < count( $columns[0] ); $j++ ) {
947953
$row = array();
948954
for ( $i = 0; $i < count( $headers ); $i++ ) {
949-
$row[] = $columns[ $i ][ $j ];
955+
$row[] = sanitize_text_field( $columns[ $i ][ $j ] );
950956
}
951957
$csv[] = $row;
952958
}

classes/Visualizer/Render/Layout.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public static function _renderSimpleEditorScreen( $args ) {
336336
*/
337337
public static function _renderTextEditor( $args ) {
338338
$chart_id = $args[1];
339-
$csv = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA_AS, array(), $chart_id, 'csv' );
339+
$csv = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA_AS, array(), $chart_id, 'csv-display' );
340340
$data = '';
341341
if ( ! empty( $csv ) && isset( $csv['string'] ) ) {
342342
$data = str_replace( PHP_EOL, "\n", $csv['string'] );
@@ -368,7 +368,7 @@ public static function _renderEditorTable( $args ) {
368368
}
369369
$chart = get_post( $chart_id );
370370
$type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true );
371-
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( $chart->post_content ) ), $type );
371+
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( str_replace( "'", "\'", html_entity_decode( $chart->post_content ) ) ), $type );
372372
} else {
373373
$headers = array_keys( $data[0] );
374374
}
@@ -442,7 +442,7 @@ public static function _renderEditorTable( $args ) {
442442
}
443443
foreach ( array_values( $row ) as $value ) {
444444
if ( $editable_data ) {
445-
echo '<td><input type="text" name="data' . $index++ . '[]" value="' . esc_attr( $value ) . '"></td>';
445+
echo '<td><input type="text" name="data' . $index++ . '[]" value="' . esc_attr( stripslashes( $value ) ) . '"></td>';
446446
} else {
447447
echo '<td>' . ( is_array( $value ) ? __( 'Invalid Data', 'visualizer' ) : $value ) . '</td>';
448448
}

0 commit comments

Comments
 (0)