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