PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.4.1
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.4.1
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 +468 -214 4.0.53.4.1 View file →
@@ -63,140 +63,69 @@
63 63 add_filter( 'rest_visualizer_query', array( $this, 'add_rest_query_vars' ), 9, 2 );
64 64 }
65 65
66 66 /**
67 - * Enqueue Gutenberg block assets.
67 + * Enqueue front end and editor JavaScript and CSS
68 68 */
69 69 public function enqueue_gutenberg_scripts() {
70 - global $pagenow;
70 + global $wp_version;
71 71
72 - $blockPath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/index.js';
73 - $stylePath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/style-index.css';
74 - $asset_path = VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/index.asset.php';
75 - if ( file_exists( $asset_path ) ) {
76 - // @phpstan-ignore-next-line
77 - $asset = require $asset_path;
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 + if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) {
78 + $version = filemtime( VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/block.js' );
78 79 } else {
79 - $asset = array(
80 - 'dependencies' => array(),
81 - 'version' => $this->version,
82 - );
80 + $version = $this->version;
83 81 }
84 82
85 - if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) {
86 - $asset['version'] = filemtime( VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/index.js' );
87 - }
83 + // Enqueue the bundled block JS file
84 + wp_enqueue_script( 'handsontable', $handsontableJS );
85 + wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, array( 'wp-api', 'handsontable', 'visualizer-datatables', 'moment' ), $version, true );
88 86
89 - if ( ! wp_script_is( 'visualizer-datatables', 'registered' ) ) {
90 - wp_register_script( 'visualizer-datatables', VISUALIZER_ABSURL . 'js/lib/datatables.min.js', array( 'jquery-ui-core' ), Visualizer_Plugin::VERSION );
91 - }
87 + $type = 'community';
92 88
93 - if ( ! wp_style_is( 'visualizer-datatables', 'registered' ) ) {
94 - wp_register_style( 'visualizer-datatables', VISUALIZER_ABSURL . 'css/lib/datatables.min.css', array(), Visualizer_Plugin::VERSION );
89 + if ( Visualizer_Module::is_pro() ) {
90 + $type = 'pro';
91 + if ( apply_filters( 'visualizer_is_business', false ) ) {
92 + $type = 'business';
93 + }
95 94 }
96 95
97 - // Enqueue the bundled block JS file
98 - $script_deps = array(
99 - 'wp-api',
100 - 'wp-blocks',
101 - 'wp-block-editor',
102 - 'wp-components',
103 - 'wp-editor',
104 - 'wp-element',
105 - 'wp-i18n',
106 - 'lodash',
107 - 'moment',
108 - 'react',
109 - 'visualizer-datatables',
110 - );
111 - if ( isset( $asset['dependencies'] ) && is_array( $asset['dependencies'] ) ) {
112 - $script_deps = array_merge( $script_deps, $asset['dependencies'] );
113 - }
114 - $script_deps = array_values( array_unique( $script_deps ) );
115 - wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, $script_deps, $asset['version'], true );
96 + $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping( null, false );
116 97
117 98 $translation_array = array(
99 + 'isPro' => $type,
100 + 'proTeaser' => Visualizer_Plugin::PRO_TEASER_URL,
101 + 'absurl' => VISUALIZER_ABSURL,
102 + 'charts' => Visualizer_Module_Admin::_getChartTypesLocalized(),
118 103 'adminPage' => menu_page_url( 'visualizer', false ),
119 - 'createChart' => add_query_arg( array( 'action' => 'visualizer-create-chart', 'library' => 'yes', 'type' => '', 'chart-library' => '', 'tab' => 'visualizer' ), admin_url( 'admin-ajax.php' ) ),
120 - 'chartsPerPage' => defined( 'TI_E2E_TESTING' ) ? 20 : 6,
121 - 'isFullSiteEditor' => 'site-editor.php' === $pagenow,
122 - /* translators: %1$s: opening tag, %2$s: closing tag */
123 - 'chartEditUrl' => admin_url( 'admin-ajax.php' ),
104 + 'sqlTable' => $table_col_mapping,
124 105 );
125 106 wp_localize_script( 'visualizer-gutenberg-block', 'visualizerLocalize', $translation_array );
126 107
127 - $d3_renderer_asset = VISUALIZER_ABSPATH . '/classes/Visualizer/D3Renderer/build/index.asset.php';
128 - if ( file_exists( $d3_renderer_asset ) && ! wp_script_is( 'visualizer-d3-renderer', 'registered' ) ) {
129 - // @phpstan-ignore-next-line
130 - $d3_asset = include $d3_renderer_asset;
131 - wp_register_script(
132 - 'visualizer-d3-renderer',
133 - VISUALIZER_ABSURL . 'classes/Visualizer/D3Renderer/build/index.js',
134 - array_merge( $d3_asset['dependencies'], array( 'jquery' ) ),
135 - $d3_asset['version'],
136 - true
137 - );
138 - }
139 - if ( wp_script_is( 'visualizer-d3-renderer', 'registered' ) ) {
140 - wp_enqueue_script( 'visualizer-d3-renderer' );
141 - wp_localize_script(
142 - 'visualizer-d3-renderer',
143 - 'vizD3Renderer',
144 - array(
145 - 'iframeJsUrl' => VISUALIZER_ABSURL . 'classes/Visualizer/D3Renderer/build/iframe.js',
146 - )
147 - );
148 - }
108 + // Enqueue frontend and editor block styles
109 + wp_enqueue_style( 'handsontable', $handsontableCSS );
110 + wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, array( 'visualizer-datatables' ), $version );
149 111
150 - // Enqueue frontend and editor block styles
151 - wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, array( 'visualizer-datatables' ), $asset['version'] );
112 + if ( version_compare( $wp_version, '4.9.0', '>' ) ) {
152 113
153 - // Enqueue ChartBuilder (AI Builder) so the D3 edit modal is available in the block editor.
154 - $chart_builder_asset = VISUALIZER_ABSPATH . '/classes/Visualizer/ChartBuilder/build/index.asset.php';
155 - if ( file_exists( $chart_builder_asset ) && ! wp_script_is( 'visualizer-chart-builder', 'enqueued' ) ) {
156 - /**
157 - * Ignore missing build asset in source checkout.
158 - *
159 - * @phpstan-ignore-next-line
160 - */
161 - $cb_asset = include $chart_builder_asset;
162 - wp_enqueue_script(
163 - 'visualizer-chart-builder',
164 - VISUALIZER_ABSURL . 'classes/Visualizer/ChartBuilder/build/index.js',
165 - $cb_asset['dependencies'],
166 - $cb_asset['version'],
167 - true
168 - );
169 - wp_enqueue_style(
170 - 'visualizer-chart-builder',
171 - VISUALIZER_ABSURL . 'classes/Visualizer/ChartBuilder/build/style-index.css',
172 - array(),
173 - $cb_asset['version']
174 - );
175 - $chart_builder_css = VISUALIZER_ABSPATH . '/classes/Visualizer/ChartBuilder/build/index.css';
176 - if ( file_exists( $chart_builder_css ) ) {
177 - wp_enqueue_style(
178 - 'visualizer-chart-builder-runtime',
179 - VISUALIZER_ABSURL . 'classes/Visualizer/ChartBuilder/build/index.css',
180 - array( 'visualizer-chart-builder' ),
181 - $cb_asset['version']
182 - );
183 - }
184 - wp_localize_script(
185 - 'visualizer-chart-builder',
186 - 'vizAIBuilder',
114 + wp_enqueue_code_editor(
187 115 array(
188 - 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
189 - 'nonce' => wp_create_nonce( 'visualizer-ai-builder' ),
190 - 'isPro' => Visualizer_Module::is_pro(),
116 + 'type' => 'sql',
117 + 'codemirror' => array(
118 + 'autofocus' => true,
119 + 'lineWrapping' => true,
120 + 'dragDrop' => false,
121 + 'matchBrackets' => true,
122 + 'autoCloseBrackets' => true,
123 + 'extraKeys' => array( 'Ctrl-Space' => 'autocomplete' ),
124 + 'hintOptions' => array( 'tables' => $table_col_mapping ),
125 + ),
191 126 )
192 127 );
193 - // Inject the mount point the ChartBuilder React app needs.
194 - wp_add_inline_script(
195 - 'visualizer-chart-builder',
196 - 'document.body.insertAdjacentHTML("beforeend","<div id=\"viz-chart-builder-root\"></div>");',
197 - 'before'
198 - );
199 128 }
200 129 }
201 130 /**
202 131 * Hook server side rendering into render callback
@@ -208,11 +137,8 @@
208 137 'attributes' => array(
209 138 'id' => array(
210 139 'type' => 'number',
211 140 ),
212 - 'lazy' => array(
213 - 'type' => 'string',
214 - ),
215 141 ),
216 142 )
217 143 );
218 144 }
@@ -219,45 +145,16 @@
219 145
220 146 /**
221 147 * Gutenberg Block Callback Function
222 148 */
223 - public function gutenberg_block_callback( $atts ) {
224 - // no id, no fun.
225 - if ( ! isset( $atts['id'] ) ) {
226 - return '';
149 + public function gutenberg_block_callback( $attr ) {
150 + if ( isset( $attr['id'] ) ) {
151 + $id = $attr['id'];
152 + if ( empty( $id ) || $id === 'none' ) {
153 + return ''; // no id = no fun
154 + }
155 + return '[visualizer id="' . $id . '"]';
227 156 }
228 -
229 - $atts = shortcode_atts(
230 - array(
231 - 'id' => false,
232 - 'lazy' => apply_filters( 'visualizer_lazy_by_default', false, $atts['id'] ),
233 - // we are deliberating excluding the class attribute from here
234 - // as this will be handled by the custom class in Gutenberg
235 - ),
236 - $atts
237 - );
238 -
239 - // no id, no fun.
240 - if ( ! $atts['id'] ) {
241 - return '';
242 - }
243 -
244 - if ( $atts['lazy'] === '-1' || $atts['lazy'] === false ) {
245 - $atts['lazy'] = 'no';
246 - }
247 -
248 - // we don't want the chart in the editor lazy-loading.
249 - if ( is_admin() ) {
250 - unset( $atts['lazy'] );
251 - }
252 -
253 - $shortcode = '[visualizer';
254 - foreach ( $atts as $name => $value ) {
255 - $shortcode .= sprintf( ' %s="%s"', $name, $value );
256 - }
257 - $shortcode .= ']';
258 -
259 - return $shortcode;
260 157 }
261 158
262 159 /**
263 160 * Hook server side rendering into render callback
@@ -269,8 +166,125 @@
269 166 array(
270 167 'get_callback' => array( $this, 'get_visualizer_data' ),
271 168 )
272 169 );
170 +
171 + register_rest_route(
172 + 'visualizer/v' . VISUALIZER_REST_VERSION,
173 + '/get-query-data',
174 + array(
175 + 'methods' => 'GET',
176 + 'callback' => array( $this, 'get_query_data' ),
177 + 'permission_callback' => function () {
178 + return current_user_can( 'edit_posts' );
179 + },
180 + )
181 + );
182 +
183 + register_rest_route(
184 + 'visualizer/v' . VISUALIZER_REST_VERSION,
185 + '/get-json-root',
186 + array(
187 + 'methods' => 'GET',
188 + 'callback' => array( $this, 'get_json_root_data' ),
189 + 'args' => array(
190 + 'url' => array(
191 + 'sanitize_callback' => 'esc_url_raw',
192 + ),
193 + ),
194 + 'permission_callback' => function () {
195 + return current_user_can( 'edit_posts' );
196 + },
197 + )
198 + );
199 +
200 + register_rest_route(
201 + 'visualizer/v' . VISUALIZER_REST_VERSION,
202 + '/get-json-data',
203 + array(
204 + 'methods' => 'GET',
205 + 'callback' => array( $this, 'get_json_data' ),
206 + 'args' => array(
207 + 'url' => array(
208 + 'sanitize_callback' => 'esc_url_raw',
209 + ),
210 + 'chart' => array(
211 + 'sanitize_callback' => 'absint',
212 + ),
213 + ),
214 + 'permission_callback' => function () {
215 + return current_user_can( 'edit_posts' );
216 + },
217 + )
218 + );
219 +
220 + register_rest_route(
221 + 'visualizer/v' . VISUALIZER_REST_VERSION,
222 + '/set-json-data',
223 + array(
224 + 'methods' => 'GET',
225 + 'callback' => array( $this, 'set_json_data' ),
226 + 'args' => array(
227 + 'url' => array(
228 + 'sanitize_callback' => 'esc_url_raw',
229 + ),
230 + ),
231 + 'permission_callback' => function () {
232 + return current_user_can( 'edit_posts' );
233 + },
234 + )
235 + );
236 +
237 + register_rest_route(
238 + 'visualizer/v' . VISUALIZER_REST_VERSION,
239 + '/update-chart',
240 + array(
241 + 'methods' => 'POST',
242 + 'callback' => array( $this, 'update_chart_data' ),
243 + 'args' => array(
244 + 'id' => 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 + '/upload-data',
257 + array(
258 + 'methods' => 'POST',
259 + 'callback' => array( $this, 'upload_csv_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,
273 + '/get-permission-data',
274 + array(
275 + 'methods' => 'GET',
276 + 'callback' => array( $this, 'get_permission_data' ),
277 + 'args' => array(
278 + 'type' => array(
279 + 'sanitize_callback' => 'sanitize_text_field',
280 + ),
281 + ),
282 + 'permission_callback' => function () {
283 + return current_user_can( 'edit_posts' );
284 + },
285 + )
286 + );
273 287 }
274 288
275 289 /**
276 290 * Get Post Meta Fields
@@ -286,11 +300,8 @@
286 300 $data['visualizer-chart-type'] = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_TYPE, true );
287 301
288 302 $library = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_LIBRARY, true );
289 303 $data['visualizer-chart-library'] = $library;
290 - if ( 'd3' === $library ) {
291 - $data['visualizer-d3-code'] = get_post_meta( $post_id, Visualizer_Module_AIBuilder::CF_D3_CODE, true );
292 - }
293 304
294 305 $data['visualizer-source'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SOURCE, true );
295 306
296 307 $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true );
@@ -296,11 +307,9 @@
296 307 $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true );
297 308
298 309 // faetch and update settings
299 310 $data['visualizer-settings'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SETTINGS, true );
300 - if ( empty( $data['visualizer-settings']['pagination'] ) ) {
301 - $data['visualizer-settings']['pageSize'] = '';
302 - }
311 +
303 312 // handle series filter hooks
304 313 $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'] );
305 314
306 315 // handle settings filter hooks
@@ -308,18 +317,8 @@
308 317
309 318 // handle data filter hooks
310 319 $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'] );
311 320
312 - // we are going to format only for tabular charts, because we are not sure of the effect on others.
313 - // this is to solve the case where boolean data shows up as all-ticks on gutenberg.
314 - if ( in_array( $data['visualizer-chart-type'], array( 'tabular' ), true ) ) {
315 - $data['visualizer-data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] );
316 - }
317 -
318 - if ( ! isset( $data['visualizer-settings']['hAxis']['format'] ) ) {
319 - $data['visualizer-settings']['hAxis']['format'] = '';
320 - }
321 -
322 321 $data['visualizer-data-exploded'] = '';
323 322 // handle annotations for google charts
324 323 if ( 'GoogleCharts' === $library ) {
325 324 // this will contain the data of both axis.
@@ -336,12 +335,8 @@
336 335 if ( ! empty( $serie['role'] ) ) {
337 336 // this series is some kind of annotation, so let's collect its index.
338 337 // the index will be +1 because the X axis value is index 0, which is being ignored.
339 338 $annotations[ 'role' . ( intval( $index ) + 1 ) ] = $serie['role'];
340 -
341 - if ( isset( $data['visualizer-series'][ intval( $index ) + 1 ] ) ) {
342 - $data['visualizer-series'][ intval( $index ) + 1 ]['role'] = $serie['role'];
343 - }
344 339 }
345 340 }
346 341 }
347 342 if ( ! empty( $annotations ) ) {
@@ -386,10 +381,10 @@
386 381 $json_root = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_ROOT, true );
387 382
388 383 $json_paging = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_PAGING, true );
389 384
390 - if ( ! empty( $import ) && $schedule >= 0 ) {
391 - $data['visualizer-chart-url'] = $import;
385 + if ( ! empty( $import ) && ! empty( $schedule ) ) {
386 + $data['visualizer-chart-url'] = $import;
392 387 $data['visualizer-chart-schedule'] = $schedule;
393 388 }
394 389
395 390 if ( ! empty( $db_schedule ) && ! empty( $db_query ) ) {
@@ -408,34 +403,218 @@
408 403 }
409 404 }
410 405
411 406 if ( Visualizer_Module::is_pro() ) {
412 - $permissions = get_post_meta( $post_id, Visualizer_Pro::CF_PERMISSIONS, true );
407 + $permissions = get_post_meta( $post_id, Visualizer_PRO::CF_PERMISSIONS, true );
413 408
414 - if ( empty( $permissions ) ) {
415 - $permissions = array(
416 - 'permissions' => array(
417 - 'read' => 'all',
418 - 'edit' => 'roles',
419 - 'edit-specific' => array( 'administrator' ),
420 - ),
421 - );
409 + if ( ! empty( $permissions ) ) {
410 + $data['visualizer-permissions'] = $permissions;
422 411 }
412 + }
423 413
424 - $data['visualizer-permissions'] = $permissions;
414 + return $data;
415 + }
416 +
417 + /**
418 + * Returns the data for the query.
419 + *
420 + * @access public
421 + */
422 + public function get_query_data( $data ) {
423 + if ( ! current_user_can( 'edit_posts' ) ) {
424 + return false;
425 425 }
426 426
427 - return $data;
427 + $source = new Visualizer_Source_Query( stripslashes( $data['query'] ) );
428 + $html = $source->fetch( true );
429 + $source->fetch( false );
430 + $name = $source->getSourceName();
431 + $series = $source->getSeries();
432 + $data = $source->getRawData();
433 + $error = '';
434 + if ( empty( $html ) ) {
435 + $error = $source->get_error();
436 + wp_send_json_error( array( 'msg' => $error ) );
437 + }
438 + wp_send_json_success( array( 'table' => $html, 'name' => $name, 'series' => $series, 'data' => $data ) );
428 439 }
429 440
430 441 /**
442 + * Returns the JSON root.
443 + *
444 + * @access public
445 + */
446 + public function get_json_root_data( $data ) {
447 + if ( ! current_user_can( 'edit_posts' ) ) {
448 + return false;
449 + }
450 +
451 + $source = new Visualizer_Source_Json( $data );
452 +
453 + $roots = $source->fetchRoots();
454 + if ( empty( $roots ) ) {
455 + wp_send_json_error( array( 'msg' => $source->get_error() ) );
456 + }
457 +
458 + wp_send_json_success( array( 'url' => $data['url'], 'roots' => $roots ) );
459 + }
460 +
461 + /**
462 + * Returns the JSON data.
463 + *
464 + * @access public
465 + */
466 + public function get_json_data( $data ) {
467 + if ( ! current_user_can( 'edit_posts' ) ) {
468 + return false;
469 + }
470 +
471 + $chart_id = $data['chart'];
472 +
473 + if ( empty( $chart_id ) ) {
474 + wp_die();
475 + }
476 +
477 + $source = new Visualizer_Source_Json( $data );
478 + $source->fetch();
479 + $table = $source->getRawData();
480 +
481 + if ( empty( $table ) ) {
482 + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) );
483 + }
484 +
485 + $table = Visualizer_Render_Layout::show( 'editor-table', $table, $chart_id, 'viz-json-table', false, false );
486 + wp_send_json_success( array( 'table' => $table, 'root' => $data['root'], 'url' => $data['url'], 'paging' => $source->getPaginationElements() ) );
487 + }
488 +
489 + /**
490 + * Set the JSON data.
491 + *
492 + * @access public
493 + */
494 + public function set_json_data( $data ) {
495 + if ( ! current_user_can( 'edit_posts' ) ) {
496 + return false;
497 + }
498 +
499 + $source = new Visualizer_Source_Json( $data );
500 +
501 + $table = $source->fetch();
502 + if ( empty( $table ) ) {
503 + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) );
504 + }
505 +
506 + $source->fetchFromEditableTable();
507 + $name = $source->getSourceName();
508 + $series = json_encode( $source->getSeries() );
509 + $data = json_encode( $source->getRawData() );
510 + wp_send_json_success( array( 'name' => $name, 'series' => $series, 'data' => $data ) );
511 + }
512 +
513 + /**
514 + * Rest Callback Method
515 + */
516 + public function update_chart_data( $data ) {
517 + if ( ! current_user_can( 'edit_posts' ) ) {
518 + return false;
519 + }
520 +
521 + if ( $data['id'] && ! is_wp_error( $data['id'] ) ) {
522 +
523 + $chart_type = sanitize_text_field( $data['visualizer-chart-type'] );
524 + $source_type = sanitize_text_field( $data['visualizer-source'] );
525 +
526 + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_TYPE, $chart_type );
527 + update_post_meta( $data['id'], Visualizer_Plugin::CF_SOURCE, $source_type );
528 + update_post_meta( $data['id'], Visualizer_Plugin::CF_DEFAULT_DATA, $data['visualizer-default-data'] );
529 + update_post_meta( $data['id'], Visualizer_Plugin::CF_SERIES, $data['visualizer-series'] );
530 + update_post_meta( $data['id'], Visualizer_Plugin::CF_SETTINGS, $data['visualizer-settings'] );
531 +
532 + if ( $data['visualizer-chart-url'] && $data['visualizer-chart-schedule'] ) {
533 + $chart_url = esc_url_raw( $data['visualizer-chart-url'] );
534 + $chart_schedule = intval( $data['visualizer-chart-schedule'] );
535 + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $chart_url );
536 + apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $chart_url, $chart_schedule );
537 + } else {
538 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL );
539 + apply_filters( 'visualizer_pro_remove_schedule', $data['id'] );
540 + }
541 +
542 + if ( $source_type === 'Visualizer_Source_Query' ) {
543 + $db_schedule = intval( $data['visualizer-db-schedule'] );
544 + $db_query = $data['visualizer-db-query'];
545 + update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE, $db_schedule );
546 + update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY, stripslashes( $db_query ) );
547 + } else {
548 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE );
549 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY );
550 + }
551 +
552 + if ( $source_type === 'Visualizer_Source_Json' ) {
553 + $json_schedule = intval( $data['visualizer-json-schedule'] );
554 + $json_url = esc_url_raw( $data['visualizer-json-url'] );
555 + $json_headers = esc_url_raw( $data['visualizer-json-headers'] );
556 + $json_root = $data['visualizer-json-root'];
557 + $json_paging = $data['visualizer-json-paging'];
558 +
559 + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE, $json_schedule );
560 + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL, $json_url );
561 + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS, $json_headers );
562 + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT, $json_root );
563 +
564 + if ( ! empty( $json_paging ) ) {
565 + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING, $json_paging );
566 + } else {
567 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING );
568 + }
569 + } else {
570 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE );
571 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL );
572 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS );
573 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT );
574 + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING );
575 + }
576 +
577 + if ( Visualizer_Module::is_pro() ) {
578 + update_post_meta( $data['id'], Visualizer_PRO::CF_PERMISSIONS, $data['visualizer-permissions'] );
579 + }
580 +
581 + if ( $data['visualizer-chart-url'] ) {
582 + $chart_url = esc_url_raw( $data['visualizer-chart-url'] );
583 + $content['source'] = $chart_url;
584 + $content['data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] );
585 + } else {
586 + $content = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] );
587 + }
588 +
589 + $chart = array(
590 + 'ID' => $data['id'],
591 + 'post_content' => serialize( $content ),
592 + );
593 +
594 + wp_update_post( $chart );
595 +
596 + $revisions = wp_get_post_revisions( $data['id'], array( 'order' => 'ASC' ) );
597 +
598 + if ( count( $revisions ) > 1 ) {
599 + $revision_ids = array_keys( $revisions );
600 +
601 + // delete all revisions.
602 + foreach ( $revision_ids as $id ) {
603 + wp_delete_post_revision( $id );
604 + }
605 + }
606 +
607 + return new \WP_REST_Response( array( 'success' => sprintf( 'Chart updated' ) ) );
608 + }
609 + }
610 +
611 + /**
431 612 * Format chart data.
432 - *
433 - * Note: No matter how tempted, don't use the similar method from Visualizer_Source. That works on a different structure.
434 613 */
435 614 public function format_chart_data( $data, $series ) {
436 615 foreach ( $series as $i => $row ) {
437 - // if no value exists for the series, then add null
616 + // if no value exists for the seires, then add null
438 617 if ( ! isset( $series[ $i ] ) ) {
439 618 $series[ $i ] = null;
440 619 }
441 620
@@ -442,38 +621,39 @@
442 621 if ( is_null( $series[ $i ] ) ) {
443 622 continue;
444 623 }
445 624
446 - switch ( $row['type'] ) {
447 - case 'number':
448 - foreach ( $data as $o => $col ) {
449 - $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null );
625 + if ( $row['type'] === 'number' ) {
626 + foreach ( $data as $o => $col ) {
627 + $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null );
628 + }
629 + }
630 +
631 + if ( $row['type'] === 'boolean' ) {
632 + foreach ( $data as $o => $col ) {
633 + $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_validate( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null;
634 + }
635 + }
636 +
637 + if ( $row['type'] === 'timeofday' ) {
638 + foreach ( $data as $o => $col ) {
639 + $date = new DateTime( '1984-03-16T' . $col[ $i ] );
640 + if ( $date ) {
641 + $data[ $o ][ $i ] = array(
642 + intval( $date->format( 'H' ) ),
643 + intval( $date->format( 'i' ) ),
644 + intval( $date->format( 's' ) ),
645 + 0,
646 + );
450 647 }
451 - break;
452 - case 'boolean':
453 - foreach ( $data as $o => $col ) {
454 - $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : false;
455 - }
456 - break;
457 - case 'timeofday':
458 - foreach ( $data as $o => $col ) {
459 - $date = new DateTime( '1984-03-16T' . $col[ $i ] );
460 - if ( $date ) {
461 - $data[ $o ][ $i ] = array(
462 - intval( $date->format( 'H' ) ),
463 - intval( $date->format( 'i' ) ),
464 - intval( $date->format( 's' ) ),
465 - 0,
466 - );
467 - }
468 - }
469 - break;
470 - case 'string':
471 - foreach ( $data as $o => $col ) {
472 - $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] );
473 - }
474 - break;
648 + }
475 649 }
650 +
651 + if ( $row['type'] === 'string' ) {
652 + foreach ( $data as $o => $col ) {
653 + $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] );
654 + }
655 + }
476 656 }
477 657
478 658 return $data;
479 659 }
@@ -485,8 +665,82 @@
485 665 if ( ! function_exists( 'mb_detect_encoding' ) || mb_detect_encoding( $datum ) !== 'ASCII' ) {
486 666 $datum = \ForceUTF8\Encoding::toUTF8( $datum );
487 667 }
488 668 return $datum;
669 + }
670 +
671 + /**
672 + * Handle remote CSV data
673 + */
674 + public function upload_csv_data( $data ) {
675 + if ( ! current_user_can( 'edit_posts' ) ) {
676 + return false;
677 + }
678 +
679 + if ( $data['url'] && ! is_wp_error( $data['url'] ) && filter_var( $data['url'], FILTER_VALIDATE_URL ) ) {
680 + $source = new Visualizer_Source_Csv_Remote( $data['url'] );
681 + if ( $source->fetch() ) {
682 + $temp = $source->getData();
683 + if ( is_string( $temp ) && is_array( unserialize( $temp ) ) ) {
684 + $content['series'] = $source->getSeries();
685 + $content['data'] = $source->getRawData();
686 + return $content;
687 + } else {
688 + return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) );
689 + }
690 + } else {
691 + return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) );
692 + }
693 + } else {
694 + return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) );
695 + }
696 + }
697 +
698 + /**
699 + * Get permission data
700 + */
701 + public function get_permission_data( $data ) {
702 + if ( ! current_user_can( 'edit_posts' ) ) {
703 + return false;
704 + }
705 +
706 + $options = array();
707 + switch ( $data['type'] ) {
708 + case 'users':
709 + $query = new WP_User_Query(
710 + array(
711 + 'number' => 1000,
712 + 'orderby' => 'display_name',
713 + 'fields' => array( 'ID', 'display_name' ),
714 + 'count_total' => false,
715 + )
716 + );
717 + $users = $query->get_results();
718 + if ( ! empty( $users ) ) {
719 + $i = 0;
720 + foreach ( $users as $user ) {
721 + $options[ $i ]['value'] = $user->ID;
722 + $options[ $i ]['label'] = $user->display_name;
723 + $i++;
724 + }
725 + }
726 + break;
727 + case 'roles':
728 + if ( ! function_exists( 'get_editable_roles' ) ) {
729 + require_once ABSPATH . 'wp-admin/includes/user.php';
730 + }
731 + $roles = get_editable_roles();
732 + if ( ! empty( $roles ) ) {
733 + $i = 0;
734 + foreach ( get_editable_roles() as $name => $info ) {
735 + $options[ $i ]['value'] = $name;
736 + $options[ $i ]['label'] = $name;
737 + $i++;
738 + }
739 + }
740 + break;
741 + }
742 + return $options;
489 743 }
490 744
491 745 /**
492 746 * Filter Rest Query