PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.6.1
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.6.1
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/Gutenberg/Block.php +472 -51 3.1.33.6.1 View file →
@@ -45,9 +45,9 @@
45 45 /**
46 46 * Returns an instance of this class.
47 47 */
48 48 public static function get_instance() {
49 - if ( null == self::$instance ) {
49 + if ( null === self::$instance ) {
50 50 self::$instance = new Visualizer_Gutenberg_Block();
51 51 }
52 52 return self::$instance;
53 53 }
@@ -59,8 +59,9 @@
59 59 $this->version = Visualizer_Plugin::VERSION;
60 60 add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_gutenberg_scripts' ) );
61 61 add_action( 'init', array( $this, 'register_block_type' ) );
62 62 add_action( 'rest_api_init', array( $this, 'register_rest_endpoints' ) );
63 + add_filter( 'rest_visualizer_query', array( $this, 'add_rest_query_vars' ), 9, 2 );
63 64 }
64 65
65 66 /**
66 67 * Enqueue front end and editor JavaScript and CSS
@@ -65,8 +66,10 @@
65 66 /**
66 67 * Enqueue front end and editor JavaScript and CSS
67 68 */
68 69 public function enqueue_gutenberg_scripts() {
70 + global $wp_version;
71 +
69 72 $blockPath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.js';
70 73 $handsontableJS = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/handsontable.js';
71 74 $stylePath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.css';
72 75 $handsontableCSS = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/handsontable.css';
@@ -71,36 +74,68 @@
71 74 $stylePath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.css';
72 75 $handsontableCSS = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/handsontable.css';
73 76
74 77 if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) {
75 - $version = filemtime( VISUALIZER_ABSPATH . 'classes/Visualizer/Gutenberg/build/block.js' );
78 + $version = filemtime( VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/block.js' );
76 79 } else {
77 80 $version = $this->version;
78 81 }
79 82
83 + if ( ! wp_script_is( 'visualizer-datatables', 'registered' ) ) {
84 + wp_register_script( 'visualizer-datatables', VISUALIZER_ABSURL . 'js/lib/datatables.min.js', array( 'jquery-ui-core' ), Visualizer_Plugin::VERSION );
85 + }
86 +
87 + if ( ! wp_style_is( 'visualizer-datatables', 'registered' ) ) {
88 + wp_register_style( 'visualizer-datatables', VISUALIZER_ABSURL . 'css/lib/datatables.min.css', array(), Visualizer_Plugin::VERSION );
89 + }
90 +
80 91 // Enqueue the bundled block JS file
81 92 wp_enqueue_script( 'handsontable', $handsontableJS );
82 - wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, array( 'wp-api', 'handsontable' ), $version, true );
93 + wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, array( 'wp-api', 'handsontable', 'visualizer-datatables', 'moment' ), $version, true );
83 94
84 95 $type = 'community';
85 96
86 - if ( VISUALIZER_PRO ) {
97 + if ( Visualizer_Module::is_pro() ) {
87 98 $type = 'pro';
88 99 if ( apply_filters( 'visualizer_is_business', false ) ) {
89 - $type = 'developer';
100 + $type = 'business';
90 101 }
91 102 }
92 103
104 + $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping( null, false );
105 +
93 106 $translation_array = array(
94 107 'isPro' => $type,
95 108 'proTeaser' => Visualizer_Plugin::PRO_TEASER_URL,
96 109 'absurl' => VISUALIZER_ABSURL,
110 + 'charts' => Visualizer_Module_Admin::_getChartTypesLocalized(),
111 + 'adminPage' => menu_page_url( 'visualizer', false ),
112 + 'sqlTable' => $table_col_mapping,
113 + 'chartsPerPage' => defined( 'TI_CYPRESS_TESTING' ) ? 20 : 6,
97 114 );
98 115 wp_localize_script( 'visualizer-gutenberg-block', 'visualizerLocalize', $translation_array );
99 116
100 117 // Enqueue frontend and editor block styles
101 118 wp_enqueue_style( 'handsontable', $handsontableCSS );
102 - wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, '', $version );
119 + wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, array( 'visualizer-datatables' ), $version );
120 +
121 + if ( version_compare( $wp_version, '4.9.0', '>' ) ) {
122 +
123 + wp_enqueue_code_editor(
124 + array(
125 + 'type' => 'sql',
126 + 'codemirror' => array(
127 + 'autofocus' => true,
128 + 'lineWrapping' => true,
129 + 'dragDrop' => false,
130 + 'matchBrackets' => true,
131 + 'autoCloseBrackets' => true,
132 + 'extraKeys' => array( 'Ctrl-Space' => 'autocomplete' ),
133 + 'hintOptions' => array( 'tables' => $table_col_mapping ),
134 + ),
135 + )
136 + );
137 + }
103 138 }
104 139 /**
105 140 * Hook server side rendering into render callback
106 141 */
@@ -111,8 +146,11 @@
111 146 'attributes' => array(
112 147 'id' => array(
113 148 'type' => 'number',
114 149 ),
150 + 'lazy' => array(
151 + 'type' => 'string',
152 + ),
115 153 ),
116 154 )
117 155 );
118 156 }
@@ -119,16 +157,46 @@
119 157
120 158 /**
121 159 * Gutenberg Block Callback Function
122 160 */
123 - public function gutenberg_block_callback( $attr ) {
124 - if ( isset( $attr['id'] ) ) {
125 - $id = $attr['id'];
126 - if ( empty( $id ) || $id === 'none' ) {
127 - return ''; // no id = no fun
128 - }
129 - return '[visualizer id="' . $id . '"]';
161 + public function gutenberg_block_callback( $atts ) {
162 + // no id, no fun.
163 + if ( ! isset( $atts['id'] ) ) {
164 + return '';
130 165 }
166 +
167 + $atts = shortcode_atts(
168 + array(
169 + 'id' => false,
170 + 'lazy' => apply_filters( 'visualizer_lazy_by_default', false, $atts['id'] ),
171 + // we are deliberating excluding the class attribute from here
172 + // as this will be handled by the custom class in Gutenberg
173 + ),
174 + $atts
175 + );
176 +
177 + // no id, no fun.
178 + if ( ! $atts['id'] ) {
179 + return '';
180 + }
181 +
182 + // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
183 + if ( $atts['lazy'] == -1 || $atts['lazy'] == false ) {
184 + $atts['lazy'] = 'no';
185 + }
186 +
187 + // we don't want the chart in the editor lazy-loading.
188 + if ( is_admin() ) {
189 + unset( $atts['lazy'] );
190 + }
191 +
192 + $shortcode = '[visualizer';
193 + foreach ( $atts as $name => $value ) {
194 + $shortcode .= sprintf( ' %s="%s"', $name, $value );
195 + }
196 + $shortcode .= ']';
197 +
198 + return $shortcode;
131 199 }
132 200
133 201 /**
134 202 * Hook server side rendering into render callback
@@ -143,8 +211,74 @@
143 211 );
144 212
145 213 register_rest_route(
146 214 'visualizer/v' . VISUALIZER_REST_VERSION,
215 + '/get-query-data',
216 + array(
217 + 'methods' => 'GET',
218 + 'callback' => array( $this, 'get_query_data' ),
219 + 'permission_callback' => function () {
220 + return current_user_can( 'edit_posts' );
221 + },
222 + )
223 + );
224 +
225 + register_rest_route(
226 + 'visualizer/v' . VISUALIZER_REST_VERSION,
227 + '/get-json-root',
228 + array(
229 + 'methods' => 'GET',
230 + 'callback' => array( $this, 'get_json_root_data' ),
231 + 'args' => array(
232 + 'url' => array(
233 + 'sanitize_callback' => 'esc_url_raw',
234 + ),
235 + ),
236 + 'permission_callback' => function () {
237 + return current_user_can( 'edit_posts' );
238 + },
239 + )
240 + );
241 +
242 + register_rest_route(
243 + 'visualizer/v' . VISUALIZER_REST_VERSION,
244 + '/get-json-data',
245 + array(
246 + 'methods' => 'GET',
247 + 'callback' => array( $this, 'get_json_data' ),
248 + 'args' => array(
249 + 'url' => array(
250 + 'sanitize_callback' => 'esc_url_raw',
251 + ),
252 + 'chart' => array(
253 + 'sanitize_callback' => 'absint',
254 + ),
255 + ),
256 + 'permission_callback' => function () {
257 + return current_user_can( 'edit_posts' );
258 + },
259 + )
260 + );
261 +
262 + register_rest_route(
263 + 'visualizer/v' . VISUALIZER_REST_VERSION,
264 + '/set-json-data',
265 + array(
266 + 'methods' => 'GET',
267 + 'callback' => array( $this, 'set_json_data' ),
268 + 'args' => array(
269 + 'url' => array(
270 + 'sanitize_callback' => 'esc_url_raw',
271 + ),
272 + ),
273 + 'permission_callback' => function () {
274 + return current_user_can( 'edit_posts' );
275 + },
276 + )
277 + );
278 +
279 + register_rest_route(
280 + 'visualizer/v' . VISUALIZER_REST_VERSION,
147 281 '/update-chart',
148 282 array(
149 283 'methods' => 'POST',
150 284 'callback' => array( $this, 'update_chart_data' ),
@@ -152,8 +286,11 @@
152 286 'id' => array(
153 287 'sanitize_callback' => 'absint',
154 288 ),
155 289 ),
290 + 'permission_callback' => function () {
291 + return current_user_can( 'edit_posts' );
292 + },
156 293 )
157 294 );
158 295
159 296 register_rest_route(
@@ -166,8 +303,11 @@
166 303 'url' => array(
167 304 'sanitize_callback' => 'esc_url_raw',
168 305 ),
169 306 ),
307 + 'permission_callback' => function () {
308 + return current_user_can( 'edit_posts' );
309 + },
170 310 )
171 311 );
172 312
173 313 register_rest_route(
@@ -180,8 +320,11 @@
180 320 'type' => array(
181 321 'sanitize_callback' => 'sanitize_text_field',
182 322 ),
183 323 ),
324 + 'permission_callback' => function () {
325 + return current_user_can( 'edit_posts' );
326 + },
184 327 )
185 328 );
186 329 }
187 330
@@ -188,13 +331,20 @@
188 331 /**
189 332 * Get Post Meta Fields
190 333 */
191 334 public function get_visualizer_data( $post ) {
335 + if ( ! current_user_can( 'edit_posts' ) ) {
336 + return false;
337 + }
338 +
192 339 $data = array();
193 340 $post_id = $post['id'];
194 341
195 342 $data['visualizer-chart-type'] = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_TYPE, true );
196 343
344 + $library = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_LIBRARY, true );
345 + $data['visualizer-chart-library'] = $library;
346 +
197 347 $data['visualizer-source'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SOURCE, true );
198 348
199 349 $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true );
200 350
@@ -209,23 +359,116 @@
209 359
210 360 // handle data filter hooks
211 361 $data['visualizer-data'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( get_the_content( $post_id ) ) ), $post_id, $data['visualizer-chart-type'] );
212 362
363 + // we are going to format only for tabular charts, because we are not sure of the effect on others.
364 + // this is to solve the case where boolean data shows up as all-ticks on gutenberg.
365 + if ( in_array( $data['visualizer-chart-type'], array( 'tabular' ), true ) ) {
366 + $data['visualizer-data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] );
367 + }
368 +
369 + $data['visualizer-data-exploded'] = '';
370 + // handle annotations for google charts
371 + if ( 'GoogleCharts' === $library ) {
372 + // this will contain the data of both axis.
373 + $settings = $data['visualizer-settings'];
374 + // this will contain data only of Y axis.
375 + $series = $data['visualizer-series'];
376 + $annotations = array();
377 + if ( isset( $settings['series'] ) ) {
378 + foreach ( $settings['series'] as $index => $serie ) {
379 + // skip X axis data.
380 + if ( $index === 0 ) {
381 + continue;
382 + }
383 + if ( ! empty( $serie['role'] ) ) {
384 + // this series is some kind of annotation, so let's collect its index.
385 + // the index will be +1 because the X axis value is index 0, which is being ignored.
386 + $annotations[ 'role' . ( intval( $index ) + 1 ) ] = $serie['role'];
387 +
388 + if ( isset( $data['visualizer-series'][ intval( $index ) + 1 ] ) ) {
389 + $data['visualizer-series'][ intval( $index ) + 1 ]['role'] = $serie['role'];
390 + }
391 + }
392 + }
393 + }
394 + if ( ! empty( $annotations ) ) {
395 + $exploded_data = array();
396 + $series_names = array();
397 + foreach ( $series as $index => $serie ) {
398 + // skip X axis data.
399 + if ( $index === 0 ) {
400 + continue;
401 + }
402 + if ( array_key_exists( 'role' . $index, $annotations ) ) {
403 + $series_names[] = (object) array( 'role' => $annotations[ 'role' . $index ], 'type' => $serie['type'] );
404 + } else {
405 + $series_names[] = $serie['label'];
406 + }
407 + }
408 + $exploded_data[] = $series_names;
409 +
410 + foreach ( $data['visualizer-data'] as $datum ) {
411 + // skip X axis data.
412 + unset( $datum[0] );
413 + $exploded_data[] = $datum;
414 + }
415 + $data['visualizer-data-exploded'] = array( $exploded_data );
416 + }
417 + }
418 +
213 419 $import = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_URL, true );
214 420
215 421 $schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_SCHEDULE, true );
216 422
423 + $db_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
424 +
425 + $db_query = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_QUERY, true );
426 +
427 + $json_url = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_URL, true );
428 +
429 + $json_headers = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_HEADERS, true );
430 +
431 + $json_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_SCHEDULE, true );
432 +
433 + $json_root = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_ROOT, true );
434 +
435 + $json_paging = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_PAGING, true );
436 +
217 437 if ( ! empty( $import ) && ! empty( $schedule ) ) {
218 438 $data['visualizer-chart-url'] = $import;
219 439 $data['visualizer-chart-schedule'] = $schedule;
220 440 }
221 441
222 - if ( VISUALIZER_PRO ) {
442 + if ( ! empty( $db_schedule ) && ! empty( $db_query ) ) {
443 + $data['visualizer-db-schedule'] = $db_schedule;
444 + $data['visualizer-db-query'] = $db_query;
445 + }
446 +
447 + if ( ! empty( $json_url ) ) {
448 + $data['visualizer-json-schedule'] = $json_schedule;
449 + $data['visualizer-json-url'] = $json_url;
450 + $data['visualizer-json-headers'] = $json_headers;
451 + $data['visualizer-json-root'] = $json_root;
452 +
453 + if ( Visualizer_Module::is_pro() && ! empty( $json_paging ) ) {
454 + $data['visualizer-json-paging'] = $json_paging;
455 + }
456 + }
457 +
458 + if ( Visualizer_Module::is_pro() ) {
223 459 $permissions = get_post_meta( $post_id, Visualizer_PRO::CF_PERMISSIONS, true );
224 460
225 - if ( ! empty( $permissions ) ) {
226 - $data['visualizer-permissions'] = $permissions;
461 + if ( empty( $permissions ) ) {
462 + $permissions = array( 'permissions' => array(
463 + 'read' => 'all',
464 + 'edit' => 'roles',
465 + 'edit-specific' => array( 'administrator' ),
466 + ),
467 + );
227 468 }
469 +
470 + $data['visualizer-permissions'] = $permissions;
228 471 }
229 472
230 473 return $data;
231 474 }
@@ -230,33 +473,181 @@
230 473 return $data;
231 474 }
232 475
233 476 /**
477 + * Returns the data for the query.
478 + *
479 + * @access public
480 + */
481 + public function get_query_data( $data ) {
482 + if ( ! current_user_can( 'edit_posts' ) ) {
483 + return false;
484 + }
485 +
486 + $source = new Visualizer_Source_Query( stripslashes( $data['query'] ) );
487 + $html = $source->fetch( true );
488 + $source->fetch( false );
489 + $name = $source->getSourceName();
490 + $series = $source->getSeries();
491 + $data = $source->getRawData();
492 + $error = '';
493 + if ( empty( $html ) ) {
494 + $error = $source->get_error();
495 + wp_send_json_error( array( 'msg' => $error ) );
496 + }
497 + wp_send_json_success( array( 'table' => $html, 'name' => $name, 'series' => $series, 'data' => $data ) );
498 + }
499 +
500 + /**
501 + * Returns the JSON root.
502 + *
503 + * @access public
504 + */
505 + public function get_json_root_data( $data ) {
506 + if ( ! current_user_can( 'edit_posts' ) ) {
507 + return false;
508 + }
509 +
510 + $source = new Visualizer_Source_Json( $data );
511 +
512 + $roots = $source->fetchRoots();
513 + if ( empty( $roots ) ) {
514 + wp_send_json_error( array( 'msg' => $source->get_error() ) );
515 + }
516 +
517 + wp_send_json_success( array( 'url' => $data['url'], 'roots' => $roots ) );
518 + }
519 +
520 + /**
521 + * Returns the JSON data.
522 + *
523 + * @access public
524 + */
525 + public function get_json_data( $data ) {
526 + if ( ! current_user_can( 'edit_posts' ) ) {
527 + return false;
528 + }
529 +
530 + $chart_id = $data['chart'];
531 +
532 + if ( empty( $chart_id ) ) {
533 + wp_die();
534 + }
535 +
536 + $source = new Visualizer_Source_Json( $data );
537 + $source->fetch();
538 + $table = $source->getRawData();
539 +
540 + if ( empty( $table ) ) {
541 + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) );
542 + }
543 +
544 + $table = Visualizer_Render_Layout::show( 'editor-table', $table, $chart_id, 'viz-json-table', false, false );
545 + wp_send_json_success( array( 'table' => $table, 'root' => $data['root'], 'url' => $data['url'], 'paging' => $source->getPaginationElements() ) );
546 + }
547 +
548 + /**
549 + * Set the JSON data.
550 + *
551 + * @access public
552 + */
553 + public function set_json_data( $data ) {
554 + if ( ! current_user_can( 'edit_posts' ) ) {
555 + return false;
556 + }
557 +
558 + $source = new Visualizer_Source_Json( $data );
559 +
560 + $table = $source->fetch();
561 + if ( empty( $table ) ) {
562 + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) );
563 + }
564 +
565 + $source->fetchFromEditableTable();
566 + $name = $source->getSourceName();
567 + $series = json_encode( $source->getSeries() );
568 + $data = json_encode( $source->getRawData() );
569 + wp_send_json_success( array( 'name' => $name, 'series' => $series, 'data' => $data ) );
570 + }
571 +
572 + /**
234 573 * Rest Callback Method
235 574 */
236 575 public function update_chart_data( $data ) {
576 + if ( ! current_user_can( 'edit_posts' ) ) {
577 + return false;
578 + }
579 +
237 580 if ( $data['id'] && ! is_wp_error( $data['id'] ) ) {
581 + if ( get_post_type( $data['id'] ) !== Visualizer_Plugin::CPT_VISUALIZER ) {
582 + return new WP_Error( 'invalid_post_type', 'Invalid post type.' );
583 + }
584 + $chart_type = sanitize_text_field( $data['visualizer-chart-type'] );
585 + $source_type = sanitize_text_field( $data['visualizer-source'] );
238 586
239 - update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_TYPE, $data['visualizer-chart-type'] );
240 - update_post_meta( $data['id'], Visualizer_Plugin::CF_SOURCE, $data['visualizer-source'] );
587 + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_TYPE, $chart_type );
588 + update_post_meta( $data['id'], Visualizer_Plugin::CF_SOURCE, $source_type );
241 589 update_post_meta( $data['id'], Visualizer_Plugin::CF_DEFAULT_DATA, $data['visualizer-default-data'] );
242 590 update_post_meta( $data['id'], Visualizer_Plugin::CF_SERIES, $data['visualizer-series'] );
243 591 update_post_meta( $data['id'], Visualizer_Plugin::CF_SETTINGS, $data['visualizer-settings'] );
244 592
245 593 if ( $data['visualizer-chart-url'] && $data['visualizer-chart-schedule'] ) {
246 - update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $data['visualizer-chart-url'] );
247 - apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $data['visualizer-chart-url'], $data['visualizer-chart-schedule'] );
594 + $chart_url = esc_url_raw( $data['visualizer-chart-url'] );
595 + $chart_schedule = intval( $data['visualizer-chart-schedule'] );
596 + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $chart_url );
597 + apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $chart_url, $chart_schedule );
248 598 } else {
249 599 delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL );
250 600 apply_filters( 'visualizer_pro_remove_schedule', $data['id'] );
251 601 }
252 602
253 - if ( VISUALIZER_PRO ) {
603 + // let's check if this is not an external db chart
604 + // as there is no support for that in the block editor interface
605 + $external_params = get_post_meta( $data['id'], Visualizer_Plugin::CF_REMOTE_DB_PARAMS, true );
606 + if ( empty( $external_params ) ) {
607 + if ( $source_type === 'Visualizer_Source_Query' ) {
608 + $db_schedule = intval( $data['visualizer-db-schedule'] );
609 + $db_query = $data['visualizer-db-query'];
610 + update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE, $db_schedule );
611 + update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY, stripslashes( $db_query ) );
612 + } else {
613 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE );
614 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY );
615 + }
616 + }
617 +
618 + if ( $source_type === 'Visualizer_Source_Json' ) {
619 + $json_schedule = intval( $data['visualizer-json-schedule'] );
620 + $json_url = esc_url_raw( $data['visualizer-json-url'] );
621 + $json_headers = esc_url_raw( $data['visualizer-json-headers'] );
622 + $json_root = $data['visualizer-json-root'];
623 + $json_paging = $data['visualizer-json-paging'];
624 +
625 + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE, $json_schedule );
626 + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL, $json_url );
627 + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS, $json_headers );
628 + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT, $json_root );
629 +
630 + if ( ! empty( $json_paging ) ) {
631 + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING, $json_paging );
632 + } else {
633 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING );
634 + }
635 + } else {
636 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE );
637 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL );
638 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS );
639 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT );
640 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING );
641 + }
642 +
643 + if ( Visualizer_Module::is_pro() ) {
254 644 update_post_meta( $data['id'], Visualizer_PRO::CF_PERMISSIONS, $data['visualizer-permissions'] );
255 645 }
256 646
257 647 if ( $data['visualizer-chart-url'] ) {
258 - $content['source'] = $data['visualizer-chart-url'];
648 + $chart_url = esc_url_raw( $data['visualizer-chart-url'] );
649 + $content['source'] = $chart_url;
259 650 $content['data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] );
260 651 } else {
261 652 $content = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] );
262 653 }
@@ -284,8 +675,10 @@
284 675 }
285 676
286 677 /**
287 678 * Format chart data.
679 + *
680 + * Note: No matter how tempted, don't use the similar method from Visualizer_Source. That works on a different structure.
288 681 */
289 682 public function format_chart_data( $data, $series ) {
290 683 foreach ( $series as $i => $row ) {
291 684 // if no value exists for the seires, then add null
@@ -296,39 +689,38 @@
296 689 if ( is_null( $series[ $i ] ) ) {
297 690 continue;
298 691 }
299 692
300 - if ( $row['type'] == 'number' ) {
301 - foreach ( $data as $o => $col ) {
302 - $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null );
303 - }
304 - }
305 -
306 - if ( $row['type'] == 'boolean' ) {
307 - foreach ( $data as $o => $col ) {
308 - $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_validate( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null;
309 - }
310 - }
311 -
312 - if ( $row['type'] == 'timeofday' ) {
313 - foreach ( $data as $o => $col ) {
314 - $date = new DateTime( '1984-03-16T' . $col[ $i ] );
315 - if ( $date ) {
316 - $data[ $o ][ $i ] = array(
317 - intval( $date->format( 'H' ) ),
318 - intval( $date->format( 'i' ) ),
319 - intval( $date->format( 's' ) ),
320 - 0,
321 - );
693 + switch ( $row['type'] ) {
694 + case 'number':
695 + foreach ( $data as $o => $col ) {
696 + $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null );
322 697 }
323 - }
698 + break;
699 + case 'boolean':
700 + foreach ( $data as $o => $col ) {
701 + $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : false;
702 + }
703 + break;
704 + case 'timeofday':
705 + foreach ( $data as $o => $col ) {
706 + $date = new DateTime( '1984-03-16T' . $col[ $i ] );
707 + if ( $date ) {
708 + $data[ $o ][ $i ] = array(
709 + intval( $date->format( 'H' ) ),
710 + intval( $date->format( 'i' ) ),
711 + intval( $date->format( 's' ) ),
712 + 0,
713 + );
714 + }
715 + }
716 + break;
717 + case 'string':
718 + foreach ( $data as $o => $col ) {
719 + $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] );
720 + }
721 + break;
324 722 }
325 -
326 - if ( $row['type'] == 'string' ) {
327 - foreach ( $data as $o => $col ) {
328 - $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] );
329 - }
330 - }
331 723 }
332 724
333 725 return $data;
334 726 }
@@ -346,8 +738,12 @@
346 738 /**
347 739 * Handle remote CSV data
348 740 */
349 741 public function upload_csv_data( $data ) {
742 + if ( ! current_user_can( 'edit_posts' ) ) {
743 + return false;
744 + }
745 +
350 746 if ( $data['url'] && ! is_wp_error( $data['url'] ) && filter_var( $data['url'], FILTER_VALIDATE_URL ) ) {
351 747 $source = new Visualizer_Source_Csv_Remote( $data['url'] );
352 748 if ( $source->fetch() ) {
353 749 $temp = $source->getData();
@@ -369,8 +765,12 @@
369 765 /**
370 766 * Get permission data
371 767 */
372 768 public function get_permission_data( $data ) {
769 + if ( ! current_user_can( 'edit_posts' ) ) {
770 + return false;
771 + }
772 +
373 773 $options = array();
374 774 switch ( $data['type'] ) {
375 775 case 'users':
376 776 $query = new WP_User_Query(
@@ -408,5 +808,26 @@
408 808 }
409 809 return $options;
410 810 }
411 811
812 + /**
813 + * Filter Rest Query
814 + */
815 + public function add_rest_query_vars( $args, \WP_REST_Request $request ) {
816 + if ( isset( $request['meta_key'] ) && isset( $request['meta_value'] ) ) {
817 + $args['meta_query'] = array(
818 + 'relation' => 'OR',
819 + array(
820 + 'key' => $request->get_param( 'meta_key' ),
821 + 'value' => $request->get_param( 'meta_value' ),
822 + 'compare' => '!=',
823 + ),
824 + array(
825 + 'key' => $request->get_param( 'meta_key' ),
826 + 'value' => $request->get_param( 'meta_value' ),
827 + 'compare' => 'NOT EXISTS',
828 + ),
829 + );
830 + }
831 + return $args;
832 + }
412 833 }