| @@ -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. |
| @@ -76,25 +76,22 @@ | ||
| 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 | - | |
| 85 | - $labels = array_filter( $labels ); | |
| 86 | - $types = array_filter( $types ); | |
| 87 | - | |
| 88 | 80 | 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 | 81 | return false; |
| 91 | 82 | } |
| 92 | 83 | |
| 84 | + // if no types were setup, re read labels and empty types array | |
| 93 | 85 | $types = array_map( 'trim', $types ); |
| 94 | 86 | 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; | |
| 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(); | |
| 97 | 94 | } |
| 98 | 95 | |
| 99 | 96 | for ( $i = 0, $len = count( $labels ); $i < $len; $i++ ) { |
| 100 | 97 | $default_type = $i === 0 ? 'string' : 'number'; |
| @@ -101,9 +98,9 @@ | ||
| 101 | 98 | |
| 102 | 99 | $labels[ $i ] = $this->toUTF8( $labels[ $i ] ); |
| 103 | 100 | |
| 104 | 101 | $this->_series[] = array( |
| 105 | - 'label' => sanitize_text_field( wp_strip_all_tags( $labels[ $i ] ) ), | |
| 102 | + 'label' => $labels[ $i ], | |
| 106 | 103 | 'type' => isset( $types[ $i ] ) ? $types[ $i ] : $default_type, |
| 107 | 104 | ); |
| 108 | 105 | } |
| 109 | 106 | |
| @@ -116,11 +113,13 @@ | ||
| 116 | 113 | * @since 1.4.2 |
| 117 | 114 | * |
| 118 | 115 | * @access protected |
| 119 | 116 | * @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. | |
| 117 | + * @return resource File handle resource on success, otherwise FALSE. | |
| 121 | 118 | */ |
| 122 | 119 | protected function _get_file_handle( $filename = false ) { |
| 120 | + // set line endings auto detect mode | |
| 121 | + ini_set( 'auto_detect_line_endings', true ); | |
| 123 | 122 | // open file and return handle |
| 124 | 123 | return fopen( $filename ? $filename : $this->_filename, 'rb' ); |
| 125 | 124 | } |
| 126 | 125 | |
| @@ -134,36 +133,28 @@ | ||
| 134 | 133 | */ |
| 135 | 134 | public function fetch() { |
| 136 | 135 | // if filename is empty return false |
| 137 | 136 | if ( empty( $this->_filename ) ) { |
| 138 | - $this->_error = esc_html__( 'No file provided. Please try again.', 'visualizer' ); | |
| 139 | 137 | return false; |
| 140 | 138 | } |
| 141 | 139 | |
| 142 | 140 | // read file and fill arrays |
| 143 | 141 | $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' ); | |
| 142 | + if ( $handle ) { | |
| 143 | + // fetch series | |
| 144 | + if ( ! $this->_fetchSeries( $handle ) ) { | |
| 145 | + return false; | |
| 147 | 146 | } |
| 148 | - return false; | |
| 149 | - } | |
| 150 | 147 | |
| 151 | - // fetch series | |
| 152 | - if ( ! $this->_fetchSeries( $handle ) ) { | |
| 148 | + // fetch data | |
| 149 | + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition | |
| 150 | + while ( ( $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ) ) !== false ) { | |
| 151 | + $this->_data[] = $this->_normalizeData( $data ); | |
| 152 | + } | |
| 153 | + | |
| 154 | + // close file handle | |
| 153 | 155 | fclose( $handle ); |
| 154 | - return false; | |
| 155 | 156 | } |
| 156 | - | |
| 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 | 157 | |
| 167 | 158 | return true; |
| 168 | 159 | } |
| 169 | 160 | |