PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.2.0
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.2.0
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 3.10.4 All 148 releases
← All changes | classes/Visualizer/Source.php +143 -5 3.0.93.2.0 View file →
@@ -57,8 +57,16 @@
57 57 */
58 58 protected $_series = array();
59 59
60 60 /**
61 + * The error message.
62 + *
63 + * @access protected
64 + * @var string
65 + */
66 + protected $_error;
67 +
68 + /**
61 69 * Return allowed types
62 70 *
63 71 * @since 1.0.1
64 72 *
@@ -83,9 +91,9 @@
83 91 * @return boolean TRUE if sereis types are valid, otherwise FALSE.
84 92 */
85 93 protected static function _validateTypes( $types ) {
86 94 foreach ( $types as $type ) {
87 - if ( ! in_array( $type, self::$allowed_types ) ) {
95 + if ( ! in_array( $type, self::$allowed_types, true ) ) {
88 96 return false;
89 97 }
90 98 }
91 99
@@ -193,9 +201,8 @@
193 201 * @return array Normalized row of data.
194 202 */
195 203 protected function _normalizeData( $data ) {
196 204 // normalize values
197 - // error_log(print_r($data,true));
198 205 foreach ( $this->_series as $i => $series ) {
199 206 // if no value exists for the seires, then add null
200 207 if ( ! isset( $data[ $i ] ) ) {
201 208 $data[ $i ] = null;
@@ -207,9 +214,9 @@
207 214 case 'number':
208 215 $data[ $i ] = ( is_numeric( $data[ $i ] ) ) ? floatval( $data[ $i ] ) : ( is_numeric( str_replace( ',', '', $data[ $i ] ) ) ? floatval( str_replace( ',', '', $data[ $i ] ) ) : null );
209 216 break;
210 217 case 'boolean':
211 - $data[ $i ] = ! empty( $data[ $i ] ) ? filter_validate( $data[ $i ], FILTER_VALIDATE_BOOLEAN ) : null;
218 + $data[ $i ] = ! empty( $data[ $i ] ) ? filter_var( $data[ $i ], FILTER_VALIDATE_BOOLEAN ) : null;
212 219 break;
213 220 case 'timeofday':
214 221 $date = new DateTime( '1984-03-16T' . $data[ $i ] );
215 222 if ( $date ) {
@@ -220,8 +227,15 @@
220 227 0,
221 228 );
222 229 }
223 230 break;
231 + case 'datetime':
232 + // let's check if the date is a Unix epoch
233 + $value = DateTime::createFromFormat( 'U', $data[ $i ] );
234 + if ( $value !== false && ! is_wp_error( $value ) ) {
235 + $data[ $i ] = $value->format( 'Y-m-d H:i:s' );
236 + }
237 + break;
224 238 case 'string':
225 239 $data[ $i ] = $this->toUTF8( $data[ $i ] );
226 240 break;
227 241 }
@@ -226,10 +240,9 @@
226 240 break;
227 241 }
228 242 }
229 243
230 - // error_log(print_r($data,true));
231 - return $data;
244 + return apply_filters( 'visualizer_format_data', $data, $this->_series );
232 245 }
233 246
234 247
235 248 /**
@@ -246,6 +259,131 @@
246 259 $datum = \ForceUTF8\Encoding::toUTF8( $datum );
247 260 }
248 261 return $datum;
249 262 }
263 +
264 + /**
265 + * Determines the formats of date/time columns.
266 + *
267 + * @access public
268 + *
269 + * @param array $series The actual array of series.
270 + * @param array $data The actual array of data.
271 + *
272 + * @return array
273 + */
274 + public static final function get_date_formats_if_exists( $series, $data ) {
275 + $date_formats = array();
276 + $types = array();
277 + $index = 0;
278 + foreach ( $series as $column ) {
279 + if ( in_array( $column['type'], array( 'date', 'datetime', 'timeofday' ), true ) ) {
280 + $types[] = array( 'index' => $index, 'type' => $column['type'] );
281 + }
282 + $index++;
283 + }
284 +
285 + if ( ! $types ) {
286 + return $date_formats;
287 + }
288 +
289 + $random = $data;
290 + // let's randomly pick 5 data points instead of cycling through the entire data set.
291 + if ( count( $data ) > 5 ) {
292 + $random = array();
293 + for ( $x = 0; $x < 5; $x++ ) {
294 + $random[] = $data[ rand( 0, count( $data ) - 1 ) ];
295 + }
296 + }
297 +
298 + foreach ( $types as $type ) {
299 + $formats = array();
300 + foreach ( $random as $datum ) {
301 + $f = self::determine_date_format( $datum[ $type['index'] ], $type['type'] );
302 + if ( $f ) {
303 + $formats[] = $f;
304 + }
305 + }
306 + // if there are multiple formats, use the most frequent format.
307 + $formats = array_filter( $formats );
308 + if ( $formats ) {
309 + $formats = array_count_values( $formats );
310 + arsort( $formats );
311 + $formats = array_keys( $formats );
312 + $final_format = reset( $formats );
313 + // we have determined the PHP format; now we have to change this into the JS format where m = MM, d = DD etc.
314 + $date_formats[] = array( 'index' => $type['index'], 'format' => str_replace( array( 'Y', 'm', 'd', 'H', 'i', 's' ), array( 'YYYY', 'MM', 'DD', 'HH', 'mm', 'ss' ), $final_format ) );
315 + }
316 + }
317 + return $date_formats;
318 + }
319 +
320 + /**
321 + * Determines the date/time format of the given string.
322 + *
323 + * @access private
324 + *
325 + * @param string $value The string.
326 + * @param string $type 'date', 'timeofday' or 'datetime'.
327 + *
328 + * @return string|null
329 + */
330 + private static final function determine_date_format( $value, $type ) {
331 + if ( version_compare( phpversion(), '5.3.0', '<' ) ) {
332 + return null;
333 + }
334 +
335 + $formats = array(
336 + 'Y/m/d',
337 + 'Y-m-d',
338 + 'm/d/Y',
339 + 'm-d-Y',
340 + 'd-m-Y',
341 + 'd/m/Y',
342 + );
343 +
344 + switch ( $type ) {
345 + case 'datetime':
346 + $formats = array_merge(
347 + $formats, array(
348 + 'U',
349 + 'Y/m/d H:i:s',
350 + 'Y-m-d H:i:s',
351 + 'm/d/Y H:i:s',
352 + 'm-d-Y H:i:s',
353 + )
354 + );
355 + break;
356 + case 'timeofday':
357 + $formats = array_merge(
358 + $formats, array(
359 + 'H:i:s',
360 + 'H:i',
361 + )
362 + );
363 + break;
364 + }
365 +
366 + $formats = apply_filters( 'visualizer_date_formats', $formats, $type );
367 +
368 + foreach ( $formats as $format ) {
369 + $return = DateTime::createFromFormat( $format, $value );
370 + if ( $return !== false && ! is_wp_error( $return ) ) {
371 + return $format;
372 + }
373 + }
374 + // invalid format
375 + return null;
376 + }
377 +
378 + /**
379 + * Returns the error, if any.
380 + *
381 + * @access public
382 + * @return string
383 + */
384 + public function get_error() {
385 + return $this->_error;
386 + }
387 +
250 388
251 389 }