| @@ -76,26 +76,24 @@ | ||
| 76 | 76 | // read series types |
| 77 | 77 | $types = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ); |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | + $labels = array_filter( $labels ); | |
| 81 | + $types = array_filter( $types ); | |
| 82 | + | |
| 80 | 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' ); | |
| 81 | 85 | return false; |
| 82 | 86 | } |
| 83 | 87 | |
| 84 | - // if no types were setup, re read labels and empty types array | |
| 85 | 88 | $types = array_map( 'trim', $types ); |
| 86 | 89 | if ( ! self::_validateTypes( $types ) ) { |
| 87 | - // re open the file | |
| 88 | - fclose( $handle ); | |
| 89 | - $handle = $this->_get_file_handle(); | |
| 90 | - | |
| 91 | - // re read the labels and empty types array | |
| 92 | - $labels = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ); | |
| 93 | - $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; | |
| 94 | 92 | } |
| 95 | 93 | |
| 96 | 94 | for ( $i = 0, $len = count( $labels ); $i < $len; $i++ ) { |
| 97 | - $default_type = $i == 0 ? 'string' : 'number'; | |
| 95 | + $default_type = $i === 0 ? 'string' : 'number'; | |
| 98 | 96 | |
| 99 | 97 | $labels[ $i ] = $this->toUTF8( $labels[ $i ] ); |
| 100 | 98 | |
| 101 | 99 | $this->_series[] = array( |
| @@ -133,8 +131,9 @@ | ||
| 133 | 131 | */ |
| 134 | 132 | public function fetch() { |
| 135 | 133 | // if filename is empty return false |
| 136 | 134 | if ( empty( $this->_filename ) ) { |
| 135 | + $this->_error = esc_html__( 'No file provided. Please try again.', 'visualizer' ); | |
| 137 | 136 | return false; |
| 138 | 137 | } |
| 139 | 138 | |
| 140 | 139 | // read file and fill arrays |
| @@ -145,10 +144,10 @@ | ||
| 145 | 144 | return false; |
| 146 | 145 | } |
| 147 | 146 | |
| 148 | 147 | // fetch data |
| 148 | + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition | |
| 149 | 149 | while ( ( $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ) ) !== false ) { |
| 150 | - | |
| 151 | 150 | $this->_data[] = $this->_normalizeData( $data ); |
| 152 | 151 | } |
| 153 | 152 | |
| 154 | 153 | // close file handle |