@@ -162,7 +162,7 @@ public function getJsonData() {
162
162
wp_send_json_error ();
163
163
}
164
164
165
- $ data = Visualizer_Render_Layout::show ( 'editor-table ' , $ data , $ chart_id , 'viz-json-table ' );
165
+ $ data = Visualizer_Render_Layout::show ( 'editor-table ' , $ data , $ chart_id , 'viz-json-table ' , false , false );
166
166
wp_send_json_success ( array ( 'table ' => $ data , 'root ' => $ params ['root ' ], 'url ' => $ params ['url ' ], 'paging ' => $ source ->getPaginationElements () ) );
167
167
}
168
168
@@ -482,11 +482,14 @@ public function renderChartPages() {
482
482
Visualizer_Plugin::VERSION ,
483
483
true
484
484
);
485
+ wp_register_script ( 'visualizer-editor-simple ' , VISUALIZER_ABSURL . 'js/simple-editor.js ' , array ( 'jquery ' ), Visualizer_Plugin::VERSION , true );
486
+
485
487
// added by Ash/Upwork
486
488
if ( VISUALIZER_PRO ) {
487
489
global $ Visualizer_Pro ;
488
490
$ Visualizer_Pro ->_addScriptsAndStyles ();
489
491
}
492
+
490
493
// dispatch pages
491
494
$ this ->_chart = get_post ( $ chart_id );
492
495
$ tab = isset ( $ _GET ['tab ' ] ) && ! empty ( $ _GET ['tab ' ] ) ? $ _GET ['tab ' ] : 'visualizer ' ;
@@ -624,6 +627,23 @@ private function _handleDataAndSettingsPage() {
624
627
wp_enqueue_script ( 'visualizer-chosen ' );
625
628
wp_enqueue_script ( 'visualizer-render ' );
626
629
630
+ if ( ! VISUALIZER_PRO ) {
631
+ wp_enqueue_script ( 'visualizer-editor-simple ' );
632
+ wp_localize_script (
633
+ 'visualizer-editor-simple ' ,
634
+ 'visualizer1 ' ,
635
+ array (
636
+ 'ajax ' => array (
637
+ 'url ' => admin_url ( 'admin-ajax.php ' ),
638
+ 'nonces ' => array (
639
+ ),
640
+ 'actions ' => array (
641
+ ),
642
+ ),
643
+ )
644
+ );
645
+ }
646
+
627
647
$ table_col_mapping = $ this ->loadCodeEditorAssets ();
628
648
629
649
wp_localize_script (
@@ -741,7 +761,105 @@ private function _handleTypesPage() {
741
761
public function renderFlattrScript () {
742
762
echo '' ;
743
763
}
744
- // changed by Ash/Upwork
764
+
765
+ /**
766
+ * Processes the CSV that is sent in the request as a string.
767
+ *
768
+ * @since 3.2.0
769
+ */
770
+ private function handleCSVasString ( $ data ) {
771
+ $ source = null ;
772
+ if ( VISUALIZER_PRO ) {
773
+ $ source = apply_filters ( 'visualizer_pro_handle_chart_data ' , $ data , '' );
774
+ } else {
775
+ // data coming in from the text editor.
776
+ $ tmpfile = tempnam ( get_temp_dir (), Visualizer_Plugin::NAME );
777
+ $ handle = fopen ( $ tmpfile , 'w ' );
778
+ $ values = preg_split ( '/[\n\r]+/ ' , stripslashes ( trim ( $ data ) ) );
779
+ if ( $ values ) {
780
+ foreach ( $ values as $ row ) {
781
+ if ( empty ( $ row ) ) {
782
+ continue ;
783
+ }
784
+ $ columns = explode ( ', ' , $ row );
785
+ fputcsv ( $ handle , $ columns );
786
+ }
787
+ }
788
+ $ source = new Visualizer_Source_Csv ( $ tmpfile );
789
+ fclose ( $ handle );
790
+ }
791
+ return $ source ;
792
+ }
793
+
794
+ /**
795
+ * Parses the data uploaded as an HTML table.
796
+ *
797
+ * @since 3.2.0
798
+ *
799
+ * @access private
800
+ */
801
+ private function handleTabularData () {
802
+ $ csv = array ();
803
+ // the datatable mentions the headers twice, so lets remove the duplicates.
804
+ $ headers = array_unique ( array_filter ( $ _POST ['header ' ] ) );
805
+ $ types = $ _POST ['type ' ];
806
+
807
+ // capture all the indexes that correspond to excluded columns.
808
+ $ exclude = array ();
809
+ $ index = 0 ;
810
+ foreach ( $ types as $ type ) {
811
+ if ( empty ( $ type ) ) {
812
+ $ exclude [] = $ index ;
813
+ }
814
+ $ index ++;
815
+ }
816
+
817
+ // when N headers are being renamed, the number of headers increases by N
818
+ // because of the way datatable duplicates header information
819
+ // so unset the headers that have been renamed.
820
+ if ( count ( $ headers ) !== count ( $ types ) ) {
821
+ $ to = count ( $ headers );
822
+ for ( $ i = count ( $ types ); $ i < $ to ; $ i ++ ) {
823
+ unset( $ headers [ $ i + 1 ] );
824
+ }
825
+ }
826
+
827
+ $ columns = array ();
828
+ for ( $ i = 0 ; $ i < count ( $ headers ); $ i ++ ) {
829
+ if ( ! isset ( $ _POST [ 'data ' . $ i ] ) ) {
830
+ continue ;
831
+ }
832
+ $ columns [ $ i ] = $ _POST [ 'data ' . $ i ];
833
+ }
834
+
835
+ $ csv [] = $ headers ;
836
+ $ csv [] = $ types ;
837
+ for ( $ j = 0 ; $ j < count ( $ columns [0 ] ); $ j ++ ) {
838
+ $ row = array ();
839
+ for ( $ i = 0 ; $ i < count ( $ headers ); $ i ++ ) {
840
+ $ row [] = $ columns [ $ i ][ $ j ];
841
+ }
842
+ $ csv [] = $ row ;
843
+ }
844
+
845
+ $ tmpfile = tempnam ( get_temp_dir (), Visualizer_Plugin::NAME );
846
+ $ handle = fopen ( $ tmpfile , 'w ' );
847
+
848
+ if ( $ csv ) {
849
+ $ index = 0 ;
850
+ foreach ( $ csv as $ row ) {
851
+ // remove all the cells corresponding to the excluded headers.
852
+ foreach ( $ exclude as $ j ) {
853
+ unset( $ row [ $ j ] );
854
+ }
855
+ fputcsv ( $ handle , $ row );
856
+ }
857
+ }
858
+ $ source = new Visualizer_Source_Csv ( $ tmpfile );
859
+ fclose ( $ handle );
860
+ return $ source ;
861
+ }
862
+
745
863
/**
746
864
* Parses uploaded CSV file and saves new data for the chart.
747
865
*
@@ -805,7 +923,9 @@ public function uploadData() {
805
923
} elseif ( isset ( $ _FILES ['local_data ' ] ) && $ _FILES ['local_data ' ]['error ' ] == 0 ) {
806
924
$ source = new Visualizer_Source_Csv ( $ _FILES ['local_data ' ]['tmp_name ' ] );
807
925
} elseif ( isset ( $ _POST ['chart_data ' ] ) && strlen ( $ _POST ['chart_data ' ] ) > 0 ) {
808
- $ source = apply_filters ( 'visualizer_pro_handle_chart_data ' , $ _POST ['chart_data ' ], '' , $ chart_id , $ _POST );
926
+ $ source = $ this ->handleCSVasString ( $ _POST ['chart_data ' ] );
927
+ } elseif ( isset ( $ _POST ['table_data ' ] ) && 'yes ' === $ _POST ['table_data ' ] ) {
928
+ $ source = $ this ->handleTabularData ();
809
929
} else {
810
930
$ render ->message = esc_html__ ( 'CSV file with chart data was not uploaded. Please, try again. ' , 'visualizer ' );
811
931
}
0 commit comments