PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 2.1.4
Visualizer – Tables & Charts Manager with Built-in AI Generator v2.1.4
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 All 149 releases
← All changes | classes/Visualizer/Source/Csv.php +24 -78 4.0.32.1.4 View file →
@@ -46,10 +46,10 @@
46 46 *
47 47 * @access public
48 48 * @param string $filename The path to the file.
49 49 */
50 - public function __construct( $filename = '' ) {
51 - $this->_filename = trim( (string) $filename );
50 + public function __construct( $filename = null ) {
51 + $this->_filename = trim( $filename );
52 52 }
53 53
54 54 /**
55 55 * Fetches series information.
@@ -61,49 +61,33 @@
61 61 */
62 62 private function _fetchSeries( &$handle ) {
63 63 // read column titles
64 64 $labels = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
65 - $types = null;
65 + $labels = array_map( 'utf8_encode', $labels );
66 + // read series types
67 + $types = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
66 68
67 - if ( false !== strpos( $this->_filename, 'tqx=out:csv' ) ) {
68 - $attributes = $this->_fetchSeriesForGoogleQueryLanguage( $labels );
69 - if ( ! $attributes['abort'] ) {
70 - $labels = $attributes['labels'];
71 - $types = $attributes['types'];
72 - }
73 - }
74 -
75 - if ( is_null( $types ) ) {
76 - // read series types
77 - $types = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
78 - }
79 -
80 - if ( ! is_array( $labels ) || ! is_array( $types ) ) {
81 - $this->_error = esc_html__( 'File should have a heading row (1st row) and a data type row (2nd row). Please try again.', 'visualizer' );
82 - return false;
83 - }
84 -
85 - $labels = array_filter( $labels );
86 - $types = array_filter( $types );
87 -
88 69 if ( ! $labels || ! $types ) {
89 - $this->_error = esc_html__( 'File should have a heading row (1st row) and a data type row (2nd row). Please try again.', 'visualizer' );
90 70 return false;
91 71 }
92 72
73 + // if no types were setup, re read labels and empty types array
93 74 $types = array_map( 'trim', $types );
94 75 if ( ! self::_validateTypes( $types ) ) {
95 - $this->_error = esc_html__( 'Invalid data types detected in the data type row (2nd row). Please try again.', 'visualizer' );
96 - return false;
76 + // re open the file
77 + fclose( $handle );
78 + $handle = $this->_get_file_handle();
79 +
80 + // re read the labels and empty types array
81 + $labels = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
82 + $labels = array_map( 'utf8_encode', $labels );
83 + $types = array();
97 84 }
98 85
99 86 for ( $i = 0, $len = count( $labels ); $i < $len; $i++ ) {
100 - $default_type = $i === 0 ? 'string' : 'number';
101 -
102 - $labels[ $i ] = $this->toUTF8( $labels[ $i ] );
103 -
87 + $default_type = $i == 0 ? 'string' : 'number';
104 88 $this->_series[] = array(
105 - 'label' => sanitize_text_field( wp_strip_all_tags( $labels[ $i ] ) ),
89 + 'label' => $labels[ $i ],
106 90 'type' => isset( $types[ $i ] ) ? $types[ $i ] : $default_type,
107 91 );
108 92 }
109 93
@@ -119,10 +103,15 @@
119 103 * @param string $filename Optional file name to get handle. If omitted, $_filename is used.
120 104 * @return resource File handle resource on success, otherwise FALSE.
121 105 */
122 106 protected function _get_file_handle( $filename = false ) {
107 + // set line endings auto detect mode
108 + ini_set( 'auto_detect_line_endings', true );
123 109 // open file and return handle
124 - return fopen( $filename ? $filename : $this->_filename, 'rb' );
110 + $fc = iconv( 'ISO-8859-1', 'utf-8', file_get_contents( $filename ? $filename : $this->_filename ) );
111 + $tmp = tempnam( sys_get_temp_dir(), rand() );
112 + file_put_contents( $tmp, $fc );
113 + return fopen( $tmp, 'rb' );
125 114 }
126 115
127 116 /**
128 117 * Fetches information from source, parses it and builds series and data arrays.
@@ -134,9 +123,8 @@
134 123 */
135 124 public function fetch() {
136 125 // if filename is empty return false
137 126 if ( empty( $this->_filename ) ) {
138 - $this->_error = esc_html__( 'No file provided. Please try again.', 'visualizer' );
139 127 return false;
140 128 }
141 129
142 130 // read file and fill arrays
@@ -147,12 +135,11 @@
147 135 return false;
148 136 }
149 137
150 138 // fetch data
151 - $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
152 - while ( $data !== false ) {
139 + while ( ( $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ) ) !== false ) {
140 +
153 141 $this->_data[] = $this->_normalizeData( $data );
154 - $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
155 142 }
156 143
157 144 // close file handle
158 145 fclose( $handle );
@@ -172,46 +159,5 @@
172 159 public function getSourceName() {
173 160 return __CLASS__;
174 161 }
175 162
176 - /**
177 - * Adds support for QueryLanguage https://developers.google.com/chart/interactive/docs/querylanguage
178 - * where the user can provide something like /gviz/tq?tq=select%20A%2C%20B%20&tqx=out:csv after the URL of the raw spreadsheet
179 - * to get the subset of data specified by the query
180 - * this will conflate the heading and the type into one value viz. for heading XXX and type string, the value will become "XXX string"
181 - * so we need to split them apart logically
182 - * also the $types variable now contains the first row because the header and the type got conflated
183 - */
184 - private function _fetchSeriesForGoogleQueryLanguage( $labels, $types = array() ) {
185 - $new_labels = array();
186 - $new_types = array();
187 - $abort = false;
188 - foreach ( $labels as $label ) {
189 - // get the index of the last space
190 - $index = strrpos( $label, ' ' );
191 - if ( $index === false ) {
192 - // no space here? something has gone wrong; abort the entire process.
193 - $abort = true;
194 - break;
195 - }
196 - $type = trim( substr( $label, $index + 1 ) );
197 - if ( ! self::_validateTypes( array( $type ) ) ) {
198 - // some other data type? abort the entire process.
199 - $abort = true;
200 - break;
201 - }
202 - $label = substr( $label, 0, $index );
203 - $new_labels[] = $label;
204 - $new_types[] = $type;
205 - }
206 - if ( ! $abort ) {
207 - $labels = $new_labels;
208 - $types = $new_types;
209 - }
210 -
211 - return array(
212 - 'abort' => $abort,
213 - 'labels' => $labels,
214 - 'types' => $types,
215 - );
216 - }
217 163 }