PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.7.9
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.7.9
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 3.10.4 All 148 releases
← All changes | classes/Visualizer/Source/Csv.php +64 -12 3.0.93.7.9 View file →
@@ -61,29 +61,39 @@
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 - // read series types
66 - $types = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
65 + $types = null;
67 66
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 + $labels = array_filter( $labels );
81 + $types = array_filter( $types );
82 +
68 83 if ( ! $labels || ! $types ) {
84 + $this->_error = esc_html__( 'File should have a heading row (1st row) and a data type row (2nd row). Please try again.', 'visualizer' );
69 85 return false;
70 86 }
71 87
72 - // if no types were setup, re read labels and empty types array
73 88 $types = array_map( 'trim', $types );
74 89 if ( ! self::_validateTypes( $types ) ) {
75 - // re open the file
76 - fclose( $handle );
77 - $handle = $this->_get_file_handle();
78 -
79 - // re read the labels and empty types array
80 - $labels = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
81 - $types = array();
90 + $this->_error = esc_html__( 'Invalid data types detected in the data type row (2nd row). Please try again.', 'visualizer' );
91 + return false;
82 92 }
83 93
84 94 for ( $i = 0, $len = count( $labels ); $i < $len; $i++ ) {
85 - $default_type = $i == 0 ? 'string' : 'number';
95 + $default_type = $i === 0 ? 'string' : 'number';
86 96
87 97 $labels[ $i ] = $this->toUTF8( $labels[ $i ] );
88 98
89 99 $this->_series[] = array(
@@ -121,8 +131,9 @@
121 131 */
122 132 public function fetch() {
123 133 // if filename is empty return false
124 134 if ( empty( $this->_filename ) ) {
135 + $this->_error = esc_html__( 'No file provided. Please try again.', 'visualizer' );
125 136 return false;
126 137 }
127 138
128 139 // read file and fill arrays
@@ -133,10 +144,10 @@
133 144 return false;
134 145 }
135 146
136 147 // fetch data
148 + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
137 149 while ( ( $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ) ) !== false ) {
138 -
139 150 $this->_data[] = $this->_normalizeData( $data );
140 151 }
141 152
142 153 // close file handle
@@ -157,5 +168,46 @@
157 168 public function getSourceName() {
158 169 return __CLASS__;
159 170 }
160 171
172 + /**
173 + * Adds support for QueryLanguage https://developers.google.com/chart/interactive/docs/querylanguage
174 + * 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
175 + * to get the subset of data specified by the query
176 + * this will conflate the heading and the type into one value viz. for heading XXX and type string, the value will become "XXX string"
177 + * so we need to split them apart logically
178 + * also the $types variable now contains the first row because the header and the type got conflated
179 + */
180 + private function _fetchSeriesForGoogleQueryLanguage( $labels, $types = array() ) {
181 + $new_labels = array();
182 + $new_types = array();
183 + $abort = false;
184 + foreach ( $labels as $label ) {
185 + // get the index of the last space
186 + $index = strrpos( $label, ' ' );
187 + if ( $index === false ) {
188 + // no space here? something has gone wrong; abort the entire process.
189 + $abort = true;
190 + break;
191 + }
192 + $type = trim( substr( $label, $index + 1 ) );
193 + if ( ! self::_validateTypes( array( $type ) ) ) {
194 + // some other data type? abort the entire process.
195 + $abort = true;
196 + break;
197 + }
198 + $label = substr( $label, 0, $index );
199 + $new_labels[] = $label;
200 + $new_types[] = $type;
201 + }
202 + if ( ! $abort ) {
203 + $labels = $new_labels;
204 + $types = $new_types;
205 + }
206 +
207 + return array(
208 + 'abort' => $abort,
209 + 'labels' => $labels,
210 + 'types' => $types,
211 + );
212 + }
161 213 }