PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.4.5
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.4.5
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/Gutenberg/Block.php +417 -25 3.1.03.4.5 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,13 +66,15 @@
65 66 /**
66 67 * Enqueue front end and editor JavaScript and CSS
67 68 */
68 69 public function enqueue_gutenberg_scripts() {
69 - $blockPath = VISUALIZER_ABSURL . '/classes/Visualizer/Gutenberg/build/block.js';
70 - $handsontableJS = VISUALIZER_ABSURL . '/classes/Visualizer/Gutenberg/build/handsontable.js';
71 - $stylePath = VISUALIZER_ABSURL . '/classes/Visualizer/Gutenberg/build/block.css';
72 - $handsontableCSS = VISUALIZER_ABSURL . '/classes/Visualizer/Gutenberg/build/handsontable.css';
70 + global $wp_version;
73 71
72 + $blockPath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.js';
73 + $handsontableJS = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/handsontable.js';
74 + $stylePath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.css';
75 + $handsontableCSS = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/handsontable.css';
76 +
74 77 if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) {
75 78 $version = filemtime( VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/block.js' );
76 79 } else {
77 80 $version = $this->version;
@@ -78,13 +81,13 @@
78 81 }
79 82
80 83 // Enqueue the bundled block JS file
81 84 wp_enqueue_script( 'handsontable', $handsontableJS );
82 - wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, array( 'wp-api', 'handsontable' ), $version );
85 + wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, array( 'wp-api', 'handsontable', 'visualizer-datatables', 'moment' ), $version, true );
83 86
84 87 $type = 'community';
85 88
86 - if ( VISUALIZER_PRO ) {
89 + if ( Visualizer_Module::is_pro() ) {
87 90 $type = 'pro';
88 91 if ( apply_filters( 'visualizer_is_business', false ) ) {
89 92 $type = 'business';
90 93 }
@@ -89,18 +92,42 @@
89 92 $type = 'business';
90 93 }
91 94 }
92 95
96 + $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping( null, false );
97 +
93 98 $translation_array = array(
94 99 'isPro' => $type,
95 100 'proTeaser' => Visualizer_Plugin::PRO_TEASER_URL,
96 101 'absurl' => VISUALIZER_ABSURL,
102 + 'charts' => Visualizer_Module_Admin::_getChartTypesLocalized(),
103 + 'adminPage' => menu_page_url( 'visualizer', false ),
104 + 'sqlTable' => $table_col_mapping,
105 + 'chartsPerPage' => defined( 'TI_CYPRESS_TESTING' ) ? 20 : 6,
97 106 );
98 107 wp_localize_script( 'visualizer-gutenberg-block', 'visualizerLocalize', $translation_array );
99 108
100 109 // Enqueue frontend and editor block styles
101 110 wp_enqueue_style( 'handsontable', $handsontableCSS );
102 - wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, '', $version );
111 + wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, array( 'visualizer-datatables' ), $version );
112 +
113 + if ( version_compare( $wp_version, '4.9.0', '>' ) ) {
114 +
115 + wp_enqueue_code_editor(
116 + array(
117 + 'type' => 'sql',
118 + 'codemirror' => array(
119 + 'autofocus' => true,
120 + 'lineWrapping' => true,
121 + 'dragDrop' => false,
122 + 'matchBrackets' => true,
123 + 'autoCloseBrackets' => true,
124 + 'extraKeys' => array( 'Ctrl-Space' => 'autocomplete' ),
125 + 'hintOptions' => array( 'tables' => $table_col_mapping ),
126 + ),
127 + )
128 + );
129 + }
103 130 }
104 131 /**
105 132 * Hook server side rendering into render callback
106 133 */
@@ -111,8 +138,11 @@
111 138 'attributes' => array(
112 139 'id' => array(
113 140 'type' => 'number',
114 141 ),
142 + 'lazy' => array(
143 + 'type' => 'string',
144 + ),
115 145 ),
116 146 )
117 147 );
118 148 }
@@ -119,14 +149,46 @@
119 149
120 150 /**
121 151 * Gutenberg Block Callback Function
122 152 */
123 - public function gutenberg_block_callback( $attr ) {
124 - $id = $attr['id'];
125 - if ( empty( $id ) || $id === 'none' ) {
126 - return ''; // no id = no fun
153 + public function gutenberg_block_callback( $atts ) {
154 + // no id, no fun.
155 + if ( ! isset( $atts['id'] ) ) {
156 + return '';
127 157 }
128 - return '[visualizer id="' . $id . '"]';
158 +
159 + $atts = shortcode_atts(
160 + array(
161 + 'id' => false,
162 + 'lazy' => apply_filters( 'visualizer_lazy_by_default', false, $atts['id'] ),
163 + // we are deliberating excluding the class attribute from here
164 + // as this will be handled by the custom class in Gutenberg
165 + ),
166 + $atts
167 + );
168 +
169 + // no id, no fun.
170 + if ( ! $atts['id'] ) {
171 + return '';
172 + }
173 +
174 + // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
175 + if ( $atts['lazy'] == -1 || $atts['lazy'] == false ) {
176 + $atts['lazy'] = 'no';
177 + }
178 +
179 + // we don't want the chart in the editor lazy-loading.
180 + if ( is_admin() ) {
181 + unset( $atts['lazy'] );
182 + }
183 +
184 + $shortcode = '[visualizer';
185 + foreach ( $atts as $name => $value ) {
186 + $shortcode .= sprintf( ' %s="%s"', $name, $value );
187 + }
188 + $shortcode .= ']';
189 +
190 + return $shortcode;
129 191 }
130 192
131 193 /**
132 194 * Hook server side rendering into render callback
@@ -141,8 +203,74 @@
141 203 );
142 204
143 205 register_rest_route(
144 206 'visualizer/v' . VISUALIZER_REST_VERSION,
207 + '/get-query-data',
208 + array(
209 + 'methods' => 'GET',
210 + 'callback' => array( $this, 'get_query_data' ),
211 + 'permission_callback' => function () {
212 + return current_user_can( 'edit_posts' );
213 + },
214 + )
215 + );
216 +
217 + register_rest_route(
218 + 'visualizer/v' . VISUALIZER_REST_VERSION,
219 + '/get-json-root',
220 + array(
221 + 'methods' => 'GET',
222 + 'callback' => array( $this, 'get_json_root_data' ),
223 + 'args' => array(
224 + 'url' => array(
225 + 'sanitize_callback' => 'esc_url_raw',
226 + ),
227 + ),
228 + 'permission_callback' => function () {
229 + return current_user_can( 'edit_posts' );
230 + },
231 + )
232 + );
233 +
234 + register_rest_route(
235 + 'visualizer/v' . VISUALIZER_REST_VERSION,
236 + '/get-json-data',
237 + array(
238 + 'methods' => 'GET',
239 + 'callback' => array( $this, 'get_json_data' ),
240 + 'args' => array(
241 + 'url' => array(
242 + 'sanitize_callback' => 'esc_url_raw',
243 + ),
244 + 'chart' => array(
245 + 'sanitize_callback' => 'absint',
246 + ),
247 + ),
248 + 'permission_callback' => function () {
249 + return current_user_can( 'edit_posts' );
250 + },
251 + )
252 + );
253 +
254 + register_rest_route(
255 + 'visualizer/v' . VISUALIZER_REST_VERSION,
256 + '/set-json-data',
257 + array(
258 + 'methods' => 'GET',
259 + 'callback' => array( $this, 'set_json_data' ),
260 + 'args' => array(
261 + 'url' => array(
262 + 'sanitize_callback' => 'esc_url_raw',
263 + ),
264 + ),
265 + 'permission_callback' => function () {
266 + return current_user_can( 'edit_posts' );
267 + },
268 + )
269 + );
270 +
271 + register_rest_route(
272 + 'visualizer/v' . VISUALIZER_REST_VERSION,
145 273 '/update-chart',
146 274 array(
147 275 'methods' => 'POST',
148 276 'callback' => array( $this, 'update_chart_data' ),
@@ -150,8 +278,11 @@
150 278 'id' => array(
151 279 'sanitize_callback' => 'absint',
152 280 ),
153 281 ),
282 + 'permission_callback' => function () {
283 + return current_user_can( 'edit_posts' );
284 + },
154 285 )
155 286 );
156 287
157 288 register_rest_route(
@@ -164,8 +295,11 @@
164 295 'url' => array(
165 296 'sanitize_callback' => 'esc_url_raw',
166 297 ),
167 298 ),
299 + 'permission_callback' => function () {
300 + return current_user_can( 'edit_posts' );
301 + },
168 302 )
169 303 );
170 304
171 305 register_rest_route(
@@ -178,8 +312,11 @@
178 312 'type' => array(
179 313 'sanitize_callback' => 'sanitize_text_field',
180 314 ),
181 315 ),
316 + 'permission_callback' => function () {
317 + return current_user_can( 'edit_posts' );
318 + },
182 319 )
183 320 );
184 321 }
185 322
@@ -186,13 +323,20 @@
186 323 /**
187 324 * Get Post Meta Fields
188 325 */
189 326 public function get_visualizer_data( $post ) {
327 + if ( ! current_user_can( 'edit_posts' ) ) {
328 + return false;
329 + }
330 +
190 331 $data = array();
191 332 $post_id = $post['id'];
192 333
193 334 $data['visualizer-chart-type'] = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_TYPE, true );
194 335
336 + $library = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_LIBRARY, true );
337 + $data['visualizer-chart-library'] = $library;
338 +
195 339 $data['visualizer-source'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SOURCE, true );
196 340
197 341 $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true );
198 342
@@ -207,18 +351,94 @@
207 351
208 352 // handle data filter hooks
209 353 $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'] );
210 354
355 + $data['visualizer-data-exploded'] = '';
356 + // handle annotations for google charts
357 + if ( 'GoogleCharts' === $library ) {
358 + // this will contain the data of both axis.
359 + $settings = $data['visualizer-settings'];
360 + // this will contain data only of Y axis.
361 + $series = $data['visualizer-series'];
362 + $annotations = array();
363 + if ( isset( $settings['series'] ) ) {
364 + foreach ( $settings['series'] as $index => $serie ) {
365 + // skip X axis data.
366 + if ( $index === 0 ) {
367 + continue;
368 + }
369 + if ( ! empty( $serie['role'] ) ) {
370 + // this series is some kind of annotation, so let's collect its index.
371 + // the index will be +1 because the X axis value is index 0, which is being ignored.
372 + $annotations[ 'role' . ( intval( $index ) + 1 ) ] = $serie['role'];
373 + }
374 + }
375 + }
376 + if ( ! empty( $annotations ) ) {
377 + $exploded_data = array();
378 + $series_names = array();
379 + foreach ( $series as $index => $serie ) {
380 + // skip X axis data.
381 + if ( $index === 0 ) {
382 + continue;
383 + }
384 + if ( array_key_exists( 'role' . $index, $annotations ) ) {
385 + $series_names[] = (object) array( 'role' => $annotations[ 'role' . $index ], 'type' => $serie['type'] );
386 + } else {
387 + $series_names[] = $serie['label'];
388 + }
389 + }
390 + $exploded_data[] = $series_names;
391 +
392 + foreach ( $data['visualizer-data'] as $datum ) {
393 + // skip X axis data.
394 + unset( $datum[0] );
395 + $exploded_data[] = $datum;
396 + }
397 + $data['visualizer-data-exploded'] = array( $exploded_data );
398 + }
399 + }
400 +
211 401 $import = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_URL, true );
212 402
213 403 $schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_SCHEDULE, true );
214 404
405 + $db_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
406 +
407 + $db_query = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_QUERY, true );
408 +
409 + $json_url = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_URL, true );
410 +
411 + $json_headers = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_HEADERS, true );
412 +
413 + $json_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_SCHEDULE, true );
414 +
415 + $json_root = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_ROOT, true );
416 +
417 + $json_paging = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_PAGING, true );
418 +
215 419 if ( ! empty( $import ) && ! empty( $schedule ) ) {
216 420 $data['visualizer-chart-url'] = $import;
217 421 $data['visualizer-chart-schedule'] = $schedule;
218 422 }
219 423
220 - if ( VISUALIZER_PRO ) {
424 + if ( ! empty( $db_schedule ) && ! empty( $db_query ) ) {
425 + $data['visualizer-db-schedule'] = $db_schedule;
426 + $data['visualizer-db-query'] = $db_query;
427 + }
428 +
429 + if ( ! empty( $json_url ) ) {
430 + $data['visualizer-json-schedule'] = $json_schedule;
431 + $data['visualizer-json-url'] = $json_url;
432 + $data['visualizer-json-headers'] = $json_headers;
433 + $data['visualizer-json-root'] = $json_root;
434 +
435 + if ( Visualizer_Module::is_pro() && ! empty( $json_paging ) ) {
436 + $data['visualizer-json-paging'] = $json_paging;
437 + }
438 + }
439 +
440 + if ( Visualizer_Module::is_pro() ) {
221 441 $permissions = get_post_meta( $post_id, Visualizer_PRO::CF_PERMISSIONS, true );
222 442
223 443 if ( ! empty( $permissions ) ) {
224 444 $data['visualizer-permissions'] = $permissions;
@@ -228,33 +448,176 @@
228 448 return $data;
229 449 }
230 450
231 451 /**
452 + * Returns the data for the query.
453 + *
454 + * @access public
455 + */
456 + public function get_query_data( $data ) {
457 + if ( ! current_user_can( 'edit_posts' ) ) {
458 + return false;
459 + }
460 +
461 + $source = new Visualizer_Source_Query( stripslashes( $data['query'] ) );
462 + $html = $source->fetch( true );
463 + $source->fetch( false );
464 + $name = $source->getSourceName();
465 + $series = $source->getSeries();
466 + $data = $source->getRawData();
467 + $error = '';
468 + if ( empty( $html ) ) {
469 + $error = $source->get_error();
470 + wp_send_json_error( array( 'msg' => $error ) );
471 + }
472 + wp_send_json_success( array( 'table' => $html, 'name' => $name, 'series' => $series, 'data' => $data ) );
473 + }
474 +
475 + /**
476 + * Returns the JSON root.
477 + *
478 + * @access public
479 + */
480 + public function get_json_root_data( $data ) {
481 + if ( ! current_user_can( 'edit_posts' ) ) {
482 + return false;
483 + }
484 +
485 + $source = new Visualizer_Source_Json( $data );
486 +
487 + $roots = $source->fetchRoots();
488 + if ( empty( $roots ) ) {
489 + wp_send_json_error( array( 'msg' => $source->get_error() ) );
490 + }
491 +
492 + wp_send_json_success( array( 'url' => $data['url'], 'roots' => $roots ) );
493 + }
494 +
495 + /**
496 + * Returns the JSON data.
497 + *
498 + * @access public
499 + */
500 + public function get_json_data( $data ) {
501 + if ( ! current_user_can( 'edit_posts' ) ) {
502 + return false;
503 + }
504 +
505 + $chart_id = $data['chart'];
506 +
507 + if ( empty( $chart_id ) ) {
508 + wp_die();
509 + }
510 +
511 + $source = new Visualizer_Source_Json( $data );
512 + $source->fetch();
513 + $table = $source->getRawData();
514 +
515 + if ( empty( $table ) ) {
516 + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) );
517 + }
518 +
519 + $table = Visualizer_Render_Layout::show( 'editor-table', $table, $chart_id, 'viz-json-table', false, false );
520 + wp_send_json_success( array( 'table' => $table, 'root' => $data['root'], 'url' => $data['url'], 'paging' => $source->getPaginationElements() ) );
521 + }
522 +
523 + /**
524 + * Set the JSON data.
525 + *
526 + * @access public
527 + */
528 + public function set_json_data( $data ) {
529 + if ( ! current_user_can( 'edit_posts' ) ) {
530 + return false;
531 + }
532 +
533 + $source = new Visualizer_Source_Json( $data );
534 +
535 + $table = $source->fetch();
536 + if ( empty( $table ) ) {
537 + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) );
538 + }
539 +
540 + $source->fetchFromEditableTable();
541 + $name = $source->getSourceName();
542 + $series = json_encode( $source->getSeries() );
543 + $data = json_encode( $source->getRawData() );
544 + wp_send_json_success( array( 'name' => $name, 'series' => $series, 'data' => $data ) );
545 + }
546 +
547 + /**
232 548 * Rest Callback Method
233 549 */
234 550 public function update_chart_data( $data ) {
551 + if ( ! current_user_can( 'edit_posts' ) ) {
552 + return false;
553 + }
554 +
235 555 if ( $data['id'] && ! is_wp_error( $data['id'] ) ) {
556 + if ( get_post_type( $data['id'] ) !== Visualizer_Plugin::CPT_VISUALIZER ) {
557 + return new WP_Error( 'invalid_post_type', 'Invalid post type.' );
558 + }
559 + $chart_type = sanitize_text_field( $data['visualizer-chart-type'] );
560 + $source_type = sanitize_text_field( $data['visualizer-source'] );
236 561
237 - update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_TYPE, $data['visualizer-chart-type'] );
238 - update_post_meta( $data['id'], Visualizer_Plugin::CF_SOURCE, $data['visualizer-source'] );
562 + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_TYPE, $chart_type );
563 + update_post_meta( $data['id'], Visualizer_Plugin::CF_SOURCE, $source_type );
239 564 update_post_meta( $data['id'], Visualizer_Plugin::CF_DEFAULT_DATA, $data['visualizer-default-data'] );
240 565 update_post_meta( $data['id'], Visualizer_Plugin::CF_SERIES, $data['visualizer-series'] );
241 566 update_post_meta( $data['id'], Visualizer_Plugin::CF_SETTINGS, $data['visualizer-settings'] );
242 567
243 568 if ( $data['visualizer-chart-url'] && $data['visualizer-chart-schedule'] ) {
244 - update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $data['visualizer-chart-url'] );
245 - apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $data['visualizer-chart-url'], $data['visualizer-chart-schedule'] );
569 + $chart_url = esc_url_raw( $data['visualizer-chart-url'] );
570 + $chart_schedule = intval( $data['visualizer-chart-schedule'] );
571 + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $chart_url );
572 + apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $chart_url, $chart_schedule );
246 573 } else {
247 574 delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL );
248 575 apply_filters( 'visualizer_pro_remove_schedule', $data['id'] );
249 576 }
250 577
251 - if ( VISUALIZER_PRO ) {
578 + if ( $source_type === 'Visualizer_Source_Query' ) {
579 + $db_schedule = intval( $data['visualizer-db-schedule'] );
580 + $db_query = $data['visualizer-db-query'];
581 + update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE, $db_schedule );
582 + update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY, stripslashes( $db_query ) );
583 + } else {
584 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE );
585 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY );
586 + }
587 +
588 + if ( $source_type === 'Visualizer_Source_Json' ) {
589 + $json_schedule = intval( $data['visualizer-json-schedule'] );
590 + $json_url = esc_url_raw( $data['visualizer-json-url'] );
591 + $json_headers = esc_url_raw( $data['visualizer-json-headers'] );
592 + $json_root = $data['visualizer-json-root'];
593 + $json_paging = $data['visualizer-json-paging'];
594 +
595 + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE, $json_schedule );
596 + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL, $json_url );
597 + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS, $json_headers );
598 + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT, $json_root );
599 +
600 + if ( ! empty( $json_paging ) ) {
601 + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING, $json_paging );
602 + } else {
603 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING );
604 + }
605 + } else {
606 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE );
607 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL );
608 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS );
609 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT );
610 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING );
611 + }
612 +
613 + if ( Visualizer_Module::is_pro() ) {
252 614 update_post_meta( $data['id'], Visualizer_PRO::CF_PERMISSIONS, $data['visualizer-permissions'] );
253 615 }
254 616
255 617 if ( $data['visualizer-chart-url'] ) {
256 - $content['source'] = $data['visualizer-chart-url'];
618 + $chart_url = esc_url_raw( $data['visualizer-chart-url'] );
619 + $content['source'] = $chart_url;
257 620 $content['data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] );
258 621 } else {
259 622 $content = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] );
260 623 }
@@ -294,21 +657,21 @@
294 657 if ( is_null( $series[ $i ] ) ) {
295 658 continue;
296 659 }
297 660
298 - if ( $row['type'] == 'number' ) {
661 + if ( $row['type'] === 'number' ) {
299 662 foreach ( $data as $o => $col ) {
300 663 $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null );
301 664 }
302 665 }
303 666
304 - if ( $row['type'] == 'boolean' ) {
667 + if ( $row['type'] === 'boolean' ) {
305 668 foreach ( $data as $o => $col ) {
306 - $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_validate( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null;
669 + $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null;
307 670 }
308 671 }
309 672
310 - if ( $row['type'] == 'timeofday' ) {
673 + if ( $row['type'] === 'timeofday' ) {
311 674 foreach ( $data as $o => $col ) {
312 675 $date = new DateTime( '1984-03-16T' . $col[ $i ] );
313 676 if ( $date ) {
314 677 $data[ $o ][ $i ] = array(
@@ -320,9 +683,9 @@
320 683 }
321 684 }
322 685 }
323 686
324 - if ( $row['type'] == 'string' ) {
687 + if ( $row['type'] === 'string' ) {
325 688 foreach ( $data as $o => $col ) {
326 689 $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] );
327 690 }
328 691 }
@@ -344,8 +707,12 @@
344 707 /**
345 708 * Handle remote CSV data
346 709 */
347 710 public function upload_csv_data( $data ) {
711 + if ( ! current_user_can( 'edit_posts' ) ) {
712 + return false;
713 + }
714 +
348 715 if ( $data['url'] && ! is_wp_error( $data['url'] ) && filter_var( $data['url'], FILTER_VALIDATE_URL ) ) {
349 716 $source = new Visualizer_Source_Csv_Remote( $data['url'] );
350 717 if ( $source->fetch() ) {
351 718 $temp = $source->getData();
@@ -367,8 +734,12 @@
367 734 /**
368 735 * Get permission data
369 736 */
370 737 public function get_permission_data( $data ) {
738 + if ( ! current_user_can( 'edit_posts' ) ) {
739 + return false;
740 + }
741 +
371 742 $options = array();
372 743 switch ( $data['type'] ) {
373 744 case 'users':
374 745 $query = new WP_User_Query(
@@ -406,5 +777,26 @@
406 777 }
407 778 return $options;
408 779 }
409 780
781 + /**
782 + * Filter Rest Query
783 + */
784 + public function add_rest_query_vars( $args, \WP_REST_Request $request ) {
785 + if ( isset( $request['meta_key'] ) && isset( $request['meta_value'] ) ) {
786 + $args['meta_query'] = array(
787 + 'relation' => 'OR',
788 + array(
789 + 'key' => $request->get_param( 'meta_key' ),
790 + 'value' => $request->get_param( 'meta_value' ),
791 + 'compare' => '!=',
792 + ),
793 + array(
794 + 'key' => $request->get_param( 'meta_key' ),
795 + 'value' => $request->get_param( 'meta_value' ),
796 + 'compare' => 'NOT EXISTS',
797 + ),
798 + );
799 + }
800 + return $args;
801 + }
410 802 }