PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.7.9
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.7.9
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 +138 -20 3.1.33.7.9 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
@@ -132,9 +140,12 @@
132 140 *
133 141 * @access public
134 142 * @return string The serialized array of data.
135 143 */
136 - public function getData() {
144 + public function getData( $fetch_from_editable_table = false ) {
145 + if ( $fetch_from_editable_table ) {
146 + $this->_fetchDataFromEditableTable();
147 + }
137 148 return serialize( $this->_data );
138 149 }
139 150
140 151 /**
@@ -144,9 +155,12 @@
144 155 *
145 156 * @access public
146 157 * @return array
147 158 */
148 - public function getRawData() {
159 + public function getRawData( $fetch_from_editable_table = false ) {
160 + if ( $fetch_from_editable_table ) {
161 + $this->_fetchDataFromEditableTable();
162 + }
149 163 return $this->_data;
150 164 }
151 165
152 166 /**
@@ -193,9 +207,8 @@
193 207 * @return array Normalized row of data.
194 208 */
195 209 protected function _normalizeData( $data ) {
196 210 // normalize values
197 - // error_log(print_r($data,true));
198 211 foreach ( $this->_series as $i => $series ) {
199 212 // if no value exists for the seires, then add null
200 213 if ( ! isset( $data[ $i ] ) ) {
201 214 $data[ $i ] = null;
@@ -207,9 +220,10 @@
207 220 case 'number':
208 221 $data[ $i ] = ( is_numeric( $data[ $i ] ) ) ? floatval( $data[ $i ] ) : ( is_numeric( str_replace( ',', '', $data[ $i ] ) ) ? floatval( str_replace( ',', '', $data[ $i ] ) ) : null );
209 222 break;
210 223 case 'boolean':
211 - $data[ $i ] = ! empty( $data[ $i ] ) ? filter_var( $data[ $i ], FILTER_VALIDATE_BOOLEAN ) : null;
224 + $datum = trim( strval( $data[ $i ] ) );
225 + $data[ $i ] = in_array( $datum, array( 'true', 'yes', '1' ), true ) ? 'true' : 'false';
212 226 break;
213 227 case 'timeofday':
214 228 $date = new DateTime( '1984-03-16T' . $data[ $i ] );
215 229 if ( $date ) {
@@ -220,16 +234,23 @@
220 234 0,
221 235 );
222 236 }
223 237 break;
238 + case 'datetime':
239 + // let's check if the date is a Unix epoch
240 + $value = DateTime::createFromFormat( 'U', $data[ $i ] );
241 + if ( $value !== false && ! is_wp_error( $value ) ) {
242 + $data[ $i ] = $value->format( 'Y-m-d H:i:s' );
243 + }
244 + break;
224 245 case 'string':
225 - $data[ $i ] = $this->toUTF8( $data[ $i ] );
246 + // if a ' is provided, strip the backslash
247 + $data[ $i ] = stripslashes( $this->toUTF8( $data[ $i ] ) );
226 248 break;
227 249 }
228 250 }
229 251
230 - // error_log(print_r($data,true));
231 - return $data;
252 + return apply_filters( 'visualizer_format_data', $data, $this->_series );
232 253 }
233 254
234 255
235 256 /**
@@ -262,9 +283,9 @@
262 283 $date_formats = array();
263 284 $types = array();
264 285 $index = 0;
265 286 foreach ( $series as $column ) {
266 - if ( in_array( $column['type'], array( 'date', 'datetime', 'timeofday' ) ) ) {
287 + if ( in_array( $column['type'], array( 'date', 'datetime', 'timeofday' ), true ) ) {
267 288 $types[] = array( 'index' => $index, 'type' => $column['type'] );
268 289 }
269 290 $index++;
270 291 }
@@ -277,9 +298,9 @@
277 298 // let's randomly pick 5 data points instead of cycling through the entire data set.
278 299 if ( count( $data ) > 5 ) {
279 300 $random = array();
280 301 for ( $x = 0; $x < 5; $x++ ) {
281 - $random = $data[ rand( 0, count( $data ) - 1 ) ];
302 + $random[] = $data[ rand( 0, count( $data ) - 1 ) ];
282 303 }
283 304 }
284 305
285 306 foreach ( $types as $type ) {
@@ -313,10 +334,11 @@
313 334 * @param string $type 'date', 'timeofday' or 'datetime'.
314 335 *
315 336 * @return string|null
316 337 */
317 - private static final function determine_date_format( $value, $type ) {
338 + private static function determine_date_format( $value, $type ) {
318 339 if ( version_compare( phpversion(), '5.3.0', '<' ) ) {
340 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'PHP version %s not supported', phpversion() ), 'error', __FILE__, __LINE__ );
319 341 return null;
320 342 }
321 343
322 344 $formats = array(
@@ -329,19 +351,24 @@
329 351 );
330 352
331 353 switch ( $type ) {
332 354 case 'datetime':
333 - $formats += array(
334 - 'Y/m/d H:i:s',
335 - 'Y-m-d H:i:s',
336 - 'm/d/Y H:i:s',
337 - 'm-d-Y H:i:s',
355 + $formats = array_merge(
356 + $formats, array(
357 + 'U',
358 + 'Y/m/d H:i:s',
359 + 'Y-m-d H:i:s',
360 + 'm/d/Y H:i:s',
361 + 'm-d-Y H:i:s',
362 + )
338 363 );
339 364 break;
340 365 case 'timeofday':
341 - $formats += array(
342 - 'H:i:s',
343 - 'H:i',
366 + $formats = array_merge(
367 + $formats, array(
368 + 'H:i:s',
369 + 'H:i',
370 + )
344 371 );
345 372 break;
346 373 }
347 374
@@ -348,9 +375,9 @@
348 375 $formats = apply_filters( 'visualizer_date_formats', $formats, $type );
349 376
350 377 foreach ( $formats as $format ) {
351 378 $return = DateTime::createFromFormat( $format, $value );
352 - if ( $return !== false ) {
379 + if ( $return !== false && ! is_wp_error( $return ) ) {
353 380 return $format;
354 381 }
355 382 }
356 383 // invalid format
@@ -355,6 +382,97 @@
355 382 }
356 383 // invalid format
357 384 return null;
358 385 }
386 +
387 + /**
388 + * Returns the error, if any.
389 + *
390 + * @access public
391 + * @return string
392 + */
393 + public function get_error() {
394 + return $this->_error;
395 + }
396 +
397 + /**
398 + * Fetches information from the editable table and parses it to build series and data arrays.
399 + *
400 + * @since ?
401 + *
402 + * @access public
403 + * @return boolean TRUE on success, otherwise FALSE.
404 + */
405 + public function fetchFromEditableTable() {
406 + if ( empty( $this->_args ) ) {
407 + return false;
408 + }
409 +
410 + $this->_fetchSeriesFromEditableTable();
411 + $this->_fetchDataFromEditableTable();
412 + return true;
413 + }
414 +
415 + /**
416 + * Fetches series information from the editable table. This is fetched only through the UI and not while refreshing the chart data.
417 + *
418 + * @since 1.0.0
419 + *
420 + * @access private
421 + */
422 + private function _fetchSeriesFromEditableTable() {
423 + $params = $this->_args;
424 + $headers = array_filter( $params['header'] );
425 + $types = array_filter( $params['type'] );
426 + $header_row = $type_row = array();
427 + if ( $headers ) {
428 + foreach ( $headers as $header ) {
429 + if ( ! empty( $types[ $header ] ) ) {
430 + $this->_series[] = array(
431 + 'label' => $header,
432 + 'type' => $types[ $header ],
433 + );
434 + }
435 + }
436 + }
437 +
438 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Series found for %s = %s', print_r( $this->_args, true ), print_r( $this->_series, true ) ), 'debug', __FILE__, __LINE__ );
439 +
440 + return true;
441 + }
442 +
443 +
444 + /**
445 + * Fetches data information from the editable table.
446 + *
447 + * @since 1.0.0
448 + *
449 + * @access private
450 + */
451 + private function _fetchDataFromEditableTable() {
452 + $headers = wp_list_pluck( $this->_series, 'label' );
453 + $this->fetch();
454 +
455 + $data = $this->_data;
456 + $this->_data = array();
457 +
458 + foreach ( $data as $line ) {
459 + $data_row = array();
460 + // we have to make sure we are fetching the data in the right order
461 + // in case the columns have been reordered
462 + foreach ( $headers as $header ) {
463 + $value = $line[ $header ];
464 + // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
465 + if ( in_array( $header, $headers ) ) {
466 + $data_row[] = $value;
467 + }
468 + }
469 + $this->_data[] = $this->_normalizeData( $data_row );
470 + }
471 +
472 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Data found for %s = %s', print_r( $this->_args, true ), print_r( $this->_data, true ) ), 'debug', __FILE__, __LINE__ );
473 +
474 + return true;
475 + }
476 +
359 477
360 478 }