PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 4.0.8
Visualizer – Tables & Charts Manager with Built-in AI Generator v4.0.8
4.0.8 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 All 149 releases
← All changes | classes/Visualizer/Source/Csv.php +35 -26 3.1.34.0.8 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 = null ) {
51 - $this->_filename = trim( $filename );
50 + public function __construct( $filename = '' ) {
51 + $this->_filename = trim( (string) $filename );
52 52 }
53 53
54 54 /**
55 55 * Fetches series information.
@@ -76,31 +76,34 @@
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 +
80 88 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' );
81 90 return false;
82 91 }
83 92
84 - // if no types were setup, re read labels and empty types array
85 93 $types = array_map( 'trim', $types );
86 94 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();
95 + $this->_error = esc_html__( 'Invalid data types detected in the data type row (2nd row). Please try again.', 'visualizer' );
96 + return false;
94 97 }
95 98
96 99 for ( $i = 0, $len = count( $labels ); $i < $len; $i++ ) {
97 - $default_type = $i == 0 ? 'string' : 'number';
100 + $default_type = $i === 0 ? 'string' : 'number';
98 101
99 102 $labels[ $i ] = $this->toUTF8( $labels[ $i ] );
100 103
101 104 $this->_series[] = array(
102 - 'label' => $labels[ $i ],
105 + 'label' => sanitize_text_field( wp_strip_all_tags( $labels[ $i ] ) ),
103 106 'type' => isset( $types[ $i ] ) ? $types[ $i ] : $default_type,
104 107 );
105 108 }
106 109
@@ -113,13 +116,11 @@
113 116 * @since 1.4.2
114 117 *
115 118 * @access protected
116 119 * @param string $filename Optional file name to get handle. If omitted, $_filename is used.
117 - * @return resource File handle resource on success, otherwise FALSE.
120 + * @return resource|false File handle resource on success, otherwise FALSE.
118 121 */
119 122 protected function _get_file_handle( $filename = false ) {
120 - // set line endings auto detect mode
121 - ini_set( 'auto_detect_line_endings', true );
122 123 // open file and return handle
123 124 return fopen( $filename ? $filename : $this->_filename, 'rb' );
124 125 }
125 126
@@ -133,28 +134,36 @@
133 134 */
134 135 public function fetch() {
135 136 // if filename is empty return false
136 137 if ( empty( $this->_filename ) ) {
138 + $this->_error = esc_html__( 'No file provided. Please try again.', 'visualizer' );
137 139 return false;
138 140 }
139 141
140 142 // read file and fill arrays
141 143 $handle = $this->_get_file_handle();
142 - if ( $handle ) {
143 - // fetch series
144 - if ( ! $this->_fetchSeries( $handle ) ) {
145 - 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' );
146 147 }
148 + return false;
149 + }
147 150
148 - // fetch data
149 - while ( ( $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ) ) !== false ) {
151 + // fetch series
152 + if ( ! $this->_fetchSeries( $handle ) ) {
153 + fclose( $handle );
154 + return false;
155 + }
150 156
151 - $this->_data[] = $this->_normalizeData( $data );
152 - }
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 + }
153 163
154 - // close file handle
155 - fclose( $handle );
156 - }
164 + // close file handle
165 + fclose( $handle );
157 166
158 167 return true;
159 168 }
160 169