| @@ -76,8 +76,13 @@ | ||
| 76 | 76 | // read series types |
| 77 | 77 | $types = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ); |
| 78 | 78 | } |
| 79 | 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 | + | |
| 80 | 85 | $labels = array_filter( $labels ); |
| 81 | 86 | $types = array_filter( $types ); |
| 82 | 87 | |
| 83 | 88 | if ( ! $labels || ! $types ) { |
| @@ -96,9 +101,9 @@ | ||
| 96 | 101 | |
| 97 | 102 | $labels[ $i ] = $this->toUTF8( $labels[ $i ] ); |
| 98 | 103 | |
| 99 | 104 | $this->_series[] = array( |
| 100 | - 'label' => $labels[ $i ], | |
| 105 | + 'label' => sanitize_text_field( wp_strip_all_tags( $labels[ $i ] ) ), | |
| 101 | 106 | 'type' => isset( $types[ $i ] ) ? $types[ $i ] : $default_type, |
| 102 | 107 | ); |
| 103 | 108 | } |
| 104 | 109 | |
| @@ -111,9 +116,9 @@ | ||
| 111 | 116 | * @since 1.4.2 |
| 112 | 117 | * |
| 113 | 118 | * @access protected |
| 114 | 119 | * @param string $filename Optional file name to get handle. If omitted, $_filename is used. |
| 115 | - * @return resource File handle resource on success, otherwise FALSE. | |
| 120 | + * @return resource|false File handle resource on success, otherwise FALSE. | |
| 116 | 121 | */ |
| 117 | 122 | protected function _get_file_handle( $filename = false ) { |
| 118 | 123 | // open file and return handle |
| 119 | 124 | return fopen( $filename ? $filename : $this->_filename, 'rb' ); |
| @@ -135,23 +140,30 @@ | ||
| 135 | 140 | } |
| 136 | 141 | |
| 137 | 142 | // read file and fill arrays |
| 138 | 143 | $handle = $this->_get_file_handle(); |
| 139 | - if ( $handle ) { | |
| 140 | - // fetch series | |
| 141 | - if ( ! $this->_fetchSeries( $handle ) ) { | |
| 142 | - return false; | |
| 144 | + if ( ! $handle ) { | |
| 145 | + if ( empty( $this->_error ) ) { | |
| 146 | + $this->_error = esc_html__( 'The file could not be opened. Please try again.', 'visualizer' ); | |
| 143 | 147 | } |
| 148 | + return false; | |
| 149 | + } | |
| 144 | 150 | |
| 145 | - // fetch data | |
| 146 | - // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition | |
| 147 | - while ( ( $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ) ) !== false ) { | |
| 148 | - $this->_data[] = $this->_normalizeData( $data ); | |
| 149 | - } | |
| 151 | + // fetch series | |
| 152 | + if ( ! $this->_fetchSeries( $handle ) ) { | |
| 153 | + fclose( $handle ); | |
| 154 | + return false; | |
| 155 | + } | |
| 150 | 156 | |
| 151 | - // close file handle | |
| 152 | - fclose( $handle ); | |
| 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 ); | |
| 153 | 162 | } |
| 163 | + | |
| 164 | + // close file handle | |
| 165 | + fclose( $handle ); | |
| 154 | 166 | |
| 155 | 167 | return true; |
| 156 | 168 | } |
| 157 | 169 | |