| @@ -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. |
| @@ -61,34 +61,49 @@ | ||
| 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 | - // read series types | |
| 66 | - $types = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ); | |
| 65 | + $types = null; | |
| 67 | 66 | |
| 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 | + | |
| 68 | 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' ); | |
| 69 | 90 | return false; |
| 70 | 91 | } |
| 71 | 92 | |
| 72 | - // if no types were setup, re read labels and empty types array | |
| 73 | 93 | $types = array_map( 'trim', $types ); |
| 74 | 94 | if ( ! self::_validateTypes( $types ) ) { |
| 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(); | |
| 95 | + $this->_error = esc_html__( 'Invalid data types detected in the data type row (2nd row). Please try again.', 'visualizer' ); | |
| 96 | + return false; | |
| 82 | 97 | } |
| 83 | 98 | |
| 84 | 99 | for ( $i = 0, $len = count( $labels ); $i < $len; $i++ ) { |
| 85 | - $default_type = $i == 0 ? 'string' : 'number'; | |
| 100 | + $default_type = $i === 0 ? 'string' : 'number'; | |
| 86 | 101 | |
| 87 | 102 | $labels[ $i ] = $this->toUTF8( $labels[ $i ] ); |
| 88 | 103 | |
| 89 | 104 | $this->_series[] = array( |
| 90 | - 'label' => $labels[ $i ], | |
| 105 | + 'label' => sanitize_text_field( wp_strip_all_tags( $labels[ $i ] ) ), | |
| 91 | 106 | 'type' => isset( $types[ $i ] ) ? $types[ $i ] : $default_type, |
| 92 | 107 | ); |
| 93 | 108 | } |
| 94 | 109 | |
| @@ -101,13 +116,11 @@ | ||
| 101 | 116 | * @since 1.4.2 |
| 102 | 117 | * |
| 103 | 118 | * @access protected |
| 104 | 119 | * @param string $filename Optional file name to get handle. If omitted, $_filename is used. |
| 105 | - * @return resource File handle resource on success, otherwise FALSE. | |
| 120 | + * @return resource|false File handle resource on success, otherwise FALSE. | |
| 106 | 121 | */ |
| 107 | 122 | protected function _get_file_handle( $filename = false ) { |
| 108 | - // set line endings auto detect mode | |
| 109 | - ini_set( 'auto_detect_line_endings', true ); | |
| 110 | 123 | // open file and return handle |
| 111 | 124 | return fopen( $filename ? $filename : $this->_filename, 'rb' ); |
| 112 | 125 | } |
| 113 | 126 | |
| @@ -121,28 +134,36 @@ | ||
| 121 | 134 | */ |
| 122 | 135 | public function fetch() { |
| 123 | 136 | // if filename is empty return false |
| 124 | 137 | if ( empty( $this->_filename ) ) { |
| 138 | + $this->_error = esc_html__( 'No file provided. Please try again.', 'visualizer' ); | |
| 125 | 139 | return false; |
| 126 | 140 | } |
| 127 | 141 | |
| 128 | 142 | // read file and fill arrays |
| 129 | 143 | $handle = $this->_get_file_handle(); |
| 130 | - if ( $handle ) { | |
| 131 | - // fetch series | |
| 132 | - if ( ! $this->_fetchSeries( $handle ) ) { | |
| 133 | - 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' ); | |
| 134 | 147 | } |
| 148 | + return false; | |
| 149 | + } | |
| 135 | 150 | |
| 136 | - // fetch data | |
| 137 | - 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 | + } | |
| 138 | 156 | |
| 139 | - $this->_data[] = $this->_normalizeData( $data ); | |
| 140 | - } | |
| 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 | + } | |
| 141 | 163 | |
| 142 | - // close file handle | |
| 143 | - fclose( $handle ); | |
| 144 | - } | |
| 164 | + // close file handle | |
| 165 | + fclose( $handle ); | |
| 145 | 166 | |
| 146 | 167 | return true; |
| 147 | 168 | } |
| 148 | 169 | |
| @@ -157,5 +178,46 @@ | ||
| 157 | 178 | public function getSourceName() { |
| 158 | 179 | return __CLASS__; |
| 159 | 180 | } |
| 160 | 181 | |
| 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 | + } | |
| 161 | 223 | } |