PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 2.1.1
Visualizer – Tables & Charts Manager with Built-in AI Generator v2.1.1
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 +28 -93 4.0.62.1.1 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,31 @@
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 + // read series types
66 + $types = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
66 67
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 68 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 69 return false;
91 70 }
92 71
72 + // if no types were setup, re read labels and empty types array
93 73 $types = array_map( 'trim', $types );
94 74 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;
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();
97 82 }
98 83
99 84 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 -
85 + $default_type = $i == 0 ? 'string' : 'number';
104 86 $this->_series[] = array(
105 - 'label' => sanitize_text_field( wp_strip_all_tags( $labels[ $i ] ) ),
87 + 'label' => $labels[ $i ],
106 88 'type' => isset( $types[ $i ] ) ? $types[ $i ] : $default_type,
107 89 );
108 90 }
109 91
@@ -116,11 +98,13 @@
116 98 * @since 1.4.2
117 99 *
118 100 * @access protected
119 101 * @param string $filename Optional file name to get handle. If omitted, $_filename is used.
120 - * @return resource|false File handle resource on success, otherwise FALSE.
102 + * @return resource File handle resource on success, otherwise FALSE.
121 103 */
122 104 protected function _get_file_handle( $filename = false ) {
105 + // set line endings auto detect mode
106 + ini_set( 'auto_detect_line_endings', true );
123 107 // open file and return handle
124 108 return fopen( $filename ? $filename : $this->_filename, 'rb' );
125 109 }
126 110
@@ -134,37 +118,29 @@
134 118 */
135 119 public function fetch() {
136 120 // if filename is empty return false
137 121 if ( empty( $this->_filename ) ) {
138 - $this->_error = esc_html__( 'No file provided. Please try again.', 'visualizer' );
139 122 return false;
140 123 }
141 124
142 125 // read file and fill arrays
143 126 $handle = $this->_get_file_handle();
144 - if ( ! $handle ) {
145 - if ( empty( $this->_error ) ) {
146 - $this->_error = esc_html__( 'The file could not be opened. Please try again.', 'visualizer' );
127 + if ( $handle ) {
128 + // fetch series
129 + if ( ! $this->_fetchSeries( $handle ) ) {
130 + return false;
147 131 }
148 - return false;
149 - }
150 132
151 - // fetch series
152 - if ( ! $this->_fetchSeries( $handle ) ) {
133 + // fetch data
134 + while ( ( $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ) ) !== false ) {
135 +
136 + $this->_data[] = $this->_normalizeData( $data );
137 + }
138 +
139 + // close file handle
153 140 fclose( $handle );
154 - return false;
155 141 }
156 142
157 - // fetch data
158 - $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
159 - while ( $data !== false ) {
160 - $this->_data[] = $this->_normalizeData( $data );
161 - $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
162 - }
163 -
164 - // close file handle
165 - fclose( $handle );
166 -
167 143 return true;
168 144 }
169 145
170 146 /**
@@ -178,46 +154,5 @@
178 154 public function getSourceName() {
179 155 return __CLASS__;
180 156 }
181 157
182 - /**
183 - * Adds support for QueryLanguage https://developers.google.com/chart/interactive/docs/querylanguage
184 - * 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
185 - * to get the subset of data specified by the query
186 - * this will conflate the heading and the type into one value viz. for heading XXX and type string, the value will become "XXX string"
187 - * so we need to split them apart logically
188 - * also the $types variable now contains the first row because the header and the type got conflated
189 - */
190 - private function _fetchSeriesForGoogleQueryLanguage( $labels, $types = array() ) {
191 - $new_labels = array();
192 - $new_types = array();
193 - $abort = false;
194 - foreach ( $labels as $label ) {
195 - // get the index of the last space
196 - $index = strrpos( $label, ' ' );
197 - if ( $index === false ) {
198 - // no space here? something has gone wrong; abort the entire process.
199 - $abort = true;
200 - break;
201 - }
202 - $type = trim( substr( $label, $index + 1 ) );
203 - if ( ! self::_validateTypes( array( $type ) ) ) {
204 - // some other data type? abort the entire process.
205 - $abort = true;
206 - break;
207 - }
208 - $label = substr( $label, 0, $index );
209 - $new_labels[] = $label;
210 - $new_types[] = $type;
211 - }
212 - if ( ! $abort ) {
213 - $labels = $new_labels;
214 - $types = $new_types;
215 - }
216 -
217 - return array(
218 - 'abort' => $abort,
219 - 'labels' => $labels,
220 - 'types' => $types,
221 - );
222 - }
223 158 }