Skip to content

Commit 90d5246

Browse files
Merge pull request #641 from contactashish13/issue-637
Allow apostrophes in charts
2 parents 78222a4 + 74060a1 commit 90d5246

File tree

6 files changed

+43
-12
lines changed

6 files changed

+43
-12
lines changed

classes/Visualizer/Module.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public function _getDataAs( $chart_id, $type ) {
190190
$settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
191191
$rows = array();
192192
$series = get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true );
193-
$data = unserialize( $chart->post_content );
193+
$data = self::get_chart_data( $chart, $type, false );
194194
if ( ! empty( $series ) ) {
195195
$row = array();
196196
foreach ( $series as $array ) {
@@ -698,4 +698,30 @@ public static final function get_features_for_license( $plan ) {
698698
}
699699
}
700700

701+
/**
702+
* Gets the chart content after common manipulations.
703+
*/
704+
public static function get_chart_data( $chart, $type, $run_filter = true ) {
705+
// change HTML entities
706+
$data = unserialize( html_entity_decode( $chart->post_content ) );
707+
$altered = array();
708+
foreach ( $data as $index => $array ) {
709+
if ( ! is_array( $index ) ) {
710+
foreach ( $array as &$datum ) {
711+
if ( is_string( $datum ) ) {
712+
$datum = stripslashes( $datum );
713+
}
714+
}
715+
$altered[ $index ] = $array;
716+
}
717+
}
718+
// if something goes wrong and the end result is empty, be safe and use the original data
719+
if ( empty( $altered ) ) {
720+
$altered = $data;
721+
}
722+
if ( $run_filter ) {
723+
return apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, $altered, $chart->ID, $type );
724+
}
725+
return $altered;
726+
}
701727
}

classes/Visualizer/Module/Admin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ public function renderLibraryPage() {
878878
}
879879

880880
$series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
881-
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( $chart->post_content ) ), $chart->ID, $type );
881+
$data = self::get_chart_data( $chart, $type );
882882

883883
$library = $this->load_chart_type( $chart->ID );
884884

classes/Visualizer/Module/Chart.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ private function _getChartArray( WP_Post $chart = null ) {
358358
}
359359
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
360360
$series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
361-
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type );
361+
$data = self::get_chart_data( $chart, $type );
362362
$library = $this->load_chart_type( $chart->ID );
363363

364364
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
@@ -887,10 +887,11 @@ private function handleCSVasString( $data, $editor_type ) {
887887
}
888888
// don't use fpucsv here because we need to just dump the data
889889
// minus the empty rows
890-
// and if any row contains ' or ", fputcsv will mess it up
891890
// because fputcsv needs to tokenize
892-
// and let's standardize the CSV enclosure
893-
$row = str_replace( "'", VISUALIZER_CSV_ENCLOSURE, $row );
891+
// we can standardize the CSV enclosure here and replace all ' with "
892+
// we can assume that if there are an even number of ' they should be changed to "
893+
// but that will screw up words like fo'c'sle
894+
// so here let's just assume ' will NOT be used for enclosures
894895
fwrite( $handle, $row );
895896
fwrite( $handle, PHP_EOL );
896897
}

classes/Visualizer/Module/Frontend.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public function renderChart( $atts ) {
253253
}
254254

255255
// handle data filter hooks
256-
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( $chart->post_content ) ), $chart->ID, $type );
256+
$data = self::get_chart_data( $chart, $type );
257257
if ( ! empty( $atts['data'] ) ) {
258258
$data = apply_filters( $atts['data'], $data, $chart->ID, $type );
259259
}

classes/Visualizer/Render/Layout.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ public static function _renderJsonScreen( $args ) {
141141
$root = get_post_meta( $id, Visualizer_Plugin::CF_JSON_ROOT, true );
142142
$paging = get_post_meta( $id, Visualizer_Plugin::CF_JSON_PAGING, true );
143143
$headers = get_post_meta( $id, Visualizer_Plugin::CF_JSON_HEADERS, true );
144-
if ( empty( $headers['method'] ) ) {
144+
if ( empty( $headers ) ) {
145+
$headers = array();
146+
}
147+
if ( $headers && empty( $headers['method'] ) ) {
145148
$headers['method'] = 'get';
146149
}
147150
$methods = apply_filters( 'visualizer_json_request_methods', array( 'GET', 'POST' ) );
@@ -174,7 +177,7 @@ class="visualizer-input json-form-element">
174177
<div>
175178
<select name="method" class="json-form-element">
176179
<?php foreach ( $methods as $method ) { ?>
177-
<option value="<?php echo $method; ?>" <?php selected( $headers['method'], $method ); ?>><?php echo $method; ?></option>
180+
<option value="<?php echo $method; ?>" <?php $headers && selected( $headers['method'], $method ); ?>><?php echo $method; ?></option>
178181
<?php } ?>
179182
</select>
180183
</div>
@@ -339,7 +342,7 @@ public static function _renderTextEditor( $args ) {
339342
$csv = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA_AS, array(), $chart_id, 'csv-display' );
340343
$data = '';
341344
if ( ! empty( $csv ) && isset( $csv['string'] ) ) {
342-
$data = str_replace( PHP_EOL, "\n", $csv['string'] );
345+
$data = str_replace( PHP_EOL, "\n", stripslashes( $csv['string'] ) );
343346
}
344347
?>
345348
<div class="viz-simple-editor-type viz-text-editor">
@@ -368,7 +371,7 @@ public static function _renderEditorTable( $args ) {
368371
}
369372
$chart = get_post( $chart_id );
370373
$type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true );
371-
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( str_replace( "'", "\'", html_entity_decode( $chart->post_content ) ) ), $type );
374+
$data = Visualizer_Module::get_chart_data( $chart, $type );
372375
} else {
373376
$headers = array_keys( $data[0] );
374377
}

classes/Visualizer/Source.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ protected function _normalizeData( $data ) {
237237
}
238238
break;
239239
case 'string':
240-
$data[ $i ] = $this->toUTF8( $data[ $i ] );
240+
// if a ' is provided, strip the backslash
241+
$data[ $i ] = stripslashes( $this->toUTF8( $data[ $i ] ) );
241242
break;
242243
}
243244
}

0 commit comments

Comments
 (0)