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