PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.4.4
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.4.4
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
visualizer / classes / Visualizer / Gutenberg / Block.php

Block.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.4.4, at classes/Visualizer/Gutenberg/Block.php

770 lines 23.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Copyright 2018 ThemeIsle (email : friends@themeisle.com) |
4 // +----------------------------------------------------------------------+
5 // | This program is free software; you can redistribute it and/or modify |
6 // | it under the terms of the GNU General Public License, version 2, as |
7 // | published by the Free Software Foundation. |
8 // | |
9 // | This program is distributed in the hope that it will be useful, |
10 // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 // | GNU General Public License for more details. |
13 // | |
14 // | You should have received a copy of the GNU General Public License |
15 // | along with this program; if not, write to the Free Software |
16 // | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
17 // | MA 02110-1301 USA |
18 // +----------------------------------------------------------------------+
19 // | Author: Hardeep Asrani <hardeep@themeisle.com> |
20 // +----------------------------------------------------------------------+
21 /**
22 * All the Gutenberg block related code.
23 *
24 * @category Visualizer
25 * @package Gutenberg
26 *
27 * @since 3.1.0
28 */
29 class Visualizer_Gutenberg_Block {
30
31 /**
32 * A reference to an instance of this class.
33 *
34 * @var Visualizer_Gutenberg_Block The one Visualizer_Gutenberg_Block instance.
35 */
36 private static $instance;
37
38 /**
39 * Visualizer plugin version.
40 *
41 * @var string $version The current version of the plugin.
42 */
43 protected $version;
44
45 /**
46 * Returns an instance of this class.
47 */
48 public static function get_instance() {
49 if ( null === self::$instance ) {
50 self::$instance = new Visualizer_Gutenberg_Block();
51 }
52 return self::$instance;
53 }
54
55 /**
56 * Initializes the plugin by setting filters and administration functions.
57 */
58 private function __construct() {
59 $this->version = Visualizer_Plugin::VERSION;
60 add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_gutenberg_scripts' ) );
61 add_action( 'init', array( $this, 'register_block_type' ) );
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 }
65
66 /**
67 * Enqueue front end and editor JavaScript and CSS
68 */
69 public function enqueue_gutenberg_scripts() {
70 global $wp_version;
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
77 if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) {
78 $version = filemtime( VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/block.js' );
79 } else {
80 $version = $this->version;
81 }
82
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 );
86
87 $type = 'community';
88
89 if ( Visualizer_Module::is_pro() ) {
90 $type = 'pro';
91 if ( apply_filters( 'visualizer_is_business', false ) ) {
92 $type = 'business';
93 }
94 }
95
96 $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping( null, false );
97
98 $translation_array = array(
99 'isPro' => $type,
100 'proTeaser' => Visualizer_Plugin::PRO_TEASER_URL,
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,
106 );
107 wp_localize_script( 'visualizer-gutenberg-block', 'visualizerLocalize', $translation_array );
108
109 // Enqueue frontend and editor block styles
110 wp_enqueue_style( 'handsontable', $handsontableCSS );
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 }
130 }
131 /**
132 * Hook server side rendering into render callback
133 */
134 public function register_block_type() {
135 register_block_type(
136 'visualizer/chart', array(
137 'render_callback' => array( $this, 'gutenberg_block_callback' ),
138 'attributes' => array(
139 'id' => array(
140 'type' => 'number',
141 ),
142 ),
143 )
144 );
145 }
146
147 /**
148 * Gutenberg Block Callback Function
149 */
150 public function gutenberg_block_callback( $attr ) {
151 if ( isset( $attr['id'] ) ) {
152 $id = $attr['id'];
153 if ( empty( $id ) || $id === 'none' ) {
154 return ''; // no id = no fun
155 }
156 return '[visualizer id="' . $id . '"]';
157 }
158 }
159
160 /**
161 * Hook server side rendering into render callback
162 */
163 public function register_rest_endpoints() {
164 register_rest_field(
165 'visualizer',
166 'chart_data',
167 array(
168 'get_callback' => array( $this, 'get_visualizer_data' ),
169 )
170 );
171
172 register_rest_route(
173 'visualizer/v' . VISUALIZER_REST_VERSION,
174 '/get-query-data',
175 array(
176 'methods' => 'GET',
177 'callback' => array( $this, 'get_query_data' ),
178 'permission_callback' => function () {
179 return current_user_can( 'edit_posts' );
180 },
181 )
182 );
183
184 register_rest_route(
185 'visualizer/v' . VISUALIZER_REST_VERSION,
186 '/get-json-root',
187 array(
188 'methods' => 'GET',
189 'callback' => array( $this, 'get_json_root_data' ),
190 'args' => array(
191 'url' => array(
192 'sanitize_callback' => 'esc_url_raw',
193 ),
194 ),
195 'permission_callback' => function () {
196 return current_user_can( 'edit_posts' );
197 },
198 )
199 );
200
201 register_rest_route(
202 'visualizer/v' . VISUALIZER_REST_VERSION,
203 '/get-json-data',
204 array(
205 'methods' => 'GET',
206 'callback' => array( $this, 'get_json_data' ),
207 'args' => array(
208 'url' => array(
209 'sanitize_callback' => 'esc_url_raw',
210 ),
211 'chart' => array(
212 'sanitize_callback' => 'absint',
213 ),
214 ),
215 'permission_callback' => function () {
216 return current_user_can( 'edit_posts' );
217 },
218 )
219 );
220
221 register_rest_route(
222 'visualizer/v' . VISUALIZER_REST_VERSION,
223 '/set-json-data',
224 array(
225 'methods' => 'GET',
226 'callback' => array( $this, 'set_json_data' ),
227 'args' => array(
228 'url' => array(
229 'sanitize_callback' => 'esc_url_raw',
230 ),
231 ),
232 'permission_callback' => function () {
233 return current_user_can( 'edit_posts' );
234 },
235 )
236 );
237
238 register_rest_route(
239 'visualizer/v' . VISUALIZER_REST_VERSION,
240 '/update-chart',
241 array(
242 'methods' => 'POST',
243 'callback' => array( $this, 'update_chart_data' ),
244 'args' => array(
245 'id' => array(
246 'sanitize_callback' => 'absint',
247 ),
248 ),
249 'permission_callback' => function () {
250 return current_user_can( 'edit_posts' );
251 },
252 )
253 );
254
255 register_rest_route(
256 'visualizer/v' . VISUALIZER_REST_VERSION,
257 '/upload-data',
258 array(
259 'methods' => 'POST',
260 'callback' => array( $this, 'upload_csv_data' ),
261 'args' => array(
262 'url' => array(
263 'sanitize_callback' => 'esc_url_raw',
264 ),
265 ),
266 'permission_callback' => function () {
267 return current_user_can( 'edit_posts' );
268 },
269 )
270 );
271
272 register_rest_route(
273 'visualizer/v' . VISUALIZER_REST_VERSION,
274 '/get-permission-data',
275 array(
276 'methods' => 'GET',
277 'callback' => array( $this, 'get_permission_data' ),
278 'args' => array(
279 'type' => array(
280 'sanitize_callback' => 'sanitize_text_field',
281 ),
282 ),
283 'permission_callback' => function () {
284 return current_user_can( 'edit_posts' );
285 },
286 )
287 );
288 }
289
290 /**
291 * Get Post Meta Fields
292 */
293 public function get_visualizer_data( $post ) {
294 if ( ! current_user_can( 'edit_posts' ) ) {
295 return false;
296 }
297
298 $data = array();
299 $post_id = $post['id'];
300
301 $data['visualizer-chart-type'] = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_TYPE, true );
302
303 $library = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_LIBRARY, true );
304 $data['visualizer-chart-library'] = $library;
305
306 $data['visualizer-source'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SOURCE, true );
307
308 $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true );
309
310 // faetch and update settings
311 $data['visualizer-settings'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SETTINGS, true );
312
313 // handle series filter hooks
314 $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'] );
315
316 // handle settings filter hooks
317 $data['visualizer-settings'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $data['visualizer-settings'], $post_id, $data['visualizer-chart-type'] );
318
319 // handle data filter hooks
320 $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'] );
321
322 $data['visualizer-data-exploded'] = '';
323 // handle annotations for google charts
324 if ( 'GoogleCharts' === $library ) {
325 // this will contain the data of both axis.
326 $settings = $data['visualizer-settings'];
327 // this will contain data only of Y axis.
328 $series = $data['visualizer-series'];
329 $annotations = array();
330 if ( isset( $settings['series'] ) ) {
331 foreach ( $settings['series'] as $index => $serie ) {
332 // skip X axis data.
333 if ( $index === 0 ) {
334 continue;
335 }
336 if ( ! empty( $serie['role'] ) ) {
337 // this series is some kind of annotation, so let's collect its index.
338 // the index will be +1 because the X axis value is index 0, which is being ignored.
339 $annotations[ 'role' . ( intval( $index ) + 1 ) ] = $serie['role'];
340 }
341 }
342 }
343 if ( ! empty( $annotations ) ) {
344 $exploded_data = array();
345 $series_names = array();
346 foreach ( $series as $index => $serie ) {
347 // skip X axis data.
348 if ( $index === 0 ) {
349 continue;
350 }
351 if ( array_key_exists( 'role' . $index, $annotations ) ) {
352 $series_names[] = (object) array( 'role' => $annotations[ 'role' . $index ], 'type' => $serie['type'] );
353 } else {
354 $series_names[] = $serie['label'];
355 }
356 }
357 $exploded_data[] = $series_names;
358
359 foreach ( $data['visualizer-data'] as $datum ) {
360 // skip X axis data.
361 unset( $datum[0] );
362 $exploded_data[] = $datum;
363 }
364 $data['visualizer-data-exploded'] = array( $exploded_data );
365 }
366 }
367
368 $import = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_URL, true );
369
370 $schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_SCHEDULE, true );
371
372 $db_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
373
374 $db_query = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_QUERY, true );
375
376 $json_url = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_URL, true );
377
378 $json_headers = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_HEADERS, true );
379
380 $json_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_SCHEDULE, true );
381
382 $json_root = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_ROOT, true );
383
384 $json_paging = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_PAGING, true );
385
386 if ( ! empty( $import ) && ! empty( $schedule ) ) {
387 $data['visualizer-chart-url'] = $import;
388 $data['visualizer-chart-schedule'] = $schedule;
389 }
390
391 if ( ! empty( $db_schedule ) && ! empty( $db_query ) ) {
392 $data['visualizer-db-schedule'] = $db_schedule;
393 $data['visualizer-db-query'] = $db_query;
394 }
395
396 if ( ! empty( $json_url ) ) {
397 $data['visualizer-json-schedule'] = $json_schedule;
398 $data['visualizer-json-url'] = $json_url;
399 $data['visualizer-json-headers'] = $json_headers;
400 $data['visualizer-json-root'] = $json_root;
401
402 if ( Visualizer_Module::is_pro() && ! empty( $json_paging ) ) {
403 $data['visualizer-json-paging'] = $json_paging;
404 }
405 }
406
407 if ( Visualizer_Module::is_pro() ) {
408 $permissions = get_post_meta( $post_id, Visualizer_PRO::CF_PERMISSIONS, true );
409
410 if ( ! empty( $permissions ) ) {
411 $data['visualizer-permissions'] = $permissions;
412 }
413 }
414
415 return $data;
416 }
417
418 /**
419 * Returns the data for the query.
420 *
421 * @access public
422 */
423 public function get_query_data( $data ) {
424 if ( ! current_user_can( 'edit_posts' ) ) {
425 return false;
426 }
427
428 $source = new Visualizer_Source_Query( stripslashes( $data['query'] ) );
429 $html = $source->fetch( true );
430 $source->fetch( false );
431 $name = $source->getSourceName();
432 $series = $source->getSeries();
433 $data = $source->getRawData();
434 $error = '';
435 if ( empty( $html ) ) {
436 $error = $source->get_error();
437 wp_send_json_error( array( 'msg' => $error ) );
438 }
439 wp_send_json_success( array( 'table' => $html, 'name' => $name, 'series' => $series, 'data' => $data ) );
440 }
441
442 /**
443 * Returns the JSON root.
444 *
445 * @access public
446 */
447 public function get_json_root_data( $data ) {
448 if ( ! current_user_can( 'edit_posts' ) ) {
449 return false;
450 }
451
452 $source = new Visualizer_Source_Json( $data );
453
454 $roots = $source->fetchRoots();
455 if ( empty( $roots ) ) {
456 wp_send_json_error( array( 'msg' => $source->get_error() ) );
457 }
458
459 wp_send_json_success( array( 'url' => $data['url'], 'roots' => $roots ) );
460 }
461
462 /**
463 * Returns the JSON data.
464 *
465 * @access public
466 */
467 public function get_json_data( $data ) {
468 if ( ! current_user_can( 'edit_posts' ) ) {
469 return false;
470 }
471
472 $chart_id = $data['chart'];
473
474 if ( empty( $chart_id ) ) {
475 wp_die();
476 }
477
478 $source = new Visualizer_Source_Json( $data );
479 $source->fetch();
480 $table = $source->getRawData();
481
482 if ( empty( $table ) ) {
483 wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) );
484 }
485
486 $table = Visualizer_Render_Layout::show( 'editor-table', $table, $chart_id, 'viz-json-table', false, false );
487 wp_send_json_success( array( 'table' => $table, 'root' => $data['root'], 'url' => $data['url'], 'paging' => $source->getPaginationElements() ) );
488 }
489
490 /**
491 * Set the JSON data.
492 *
493 * @access public
494 */
495 public function set_json_data( $data ) {
496 if ( ! current_user_can( 'edit_posts' ) ) {
497 return false;
498 }
499
500 $source = new Visualizer_Source_Json( $data );
501
502 $table = $source->fetch();
503 if ( empty( $table ) ) {
504 wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) );
505 }
506
507 $source->fetchFromEditableTable();
508 $name = $source->getSourceName();
509 $series = json_encode( $source->getSeries() );
510 $data = json_encode( $source->getRawData() );
511 wp_send_json_success( array( 'name' => $name, 'series' => $series, 'data' => $data ) );
512 }
513
514 /**
515 * Rest Callback Method
516 */
517 public function update_chart_data( $data ) {
518 if ( ! current_user_can( 'edit_posts' ) ) {
519 return false;
520 }
521
522 if ( $data['id'] && ! is_wp_error( $data['id'] ) ) {
523 if ( get_post_type( $data['id'] ) !== Visualizer_Plugin::CPT_VISUALIZER ) {
524 return new WP_Error( 'invalid_post_type', 'Invalid post type.' );
525 }
526 $chart_type = sanitize_text_field( $data['visualizer-chart-type'] );
527 $source_type = sanitize_text_field( $data['visualizer-source'] );
528
529 update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_TYPE, $chart_type );
530 update_post_meta( $data['id'], Visualizer_Plugin::CF_SOURCE, $source_type );
531 update_post_meta( $data['id'], Visualizer_Plugin::CF_DEFAULT_DATA, $data['visualizer-default-data'] );
532 update_post_meta( $data['id'], Visualizer_Plugin::CF_SERIES, $data['visualizer-series'] );
533 update_post_meta( $data['id'], Visualizer_Plugin::CF_SETTINGS, $data['visualizer-settings'] );
534
535 if ( $data['visualizer-chart-url'] && $data['visualizer-chart-schedule'] ) {
536 $chart_url = esc_url_raw( $data['visualizer-chart-url'] );
537 $chart_schedule = intval( $data['visualizer-chart-schedule'] );
538 update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $chart_url );
539 apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $chart_url, $chart_schedule );
540 } else {
541 delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL );
542 apply_filters( 'visualizer_pro_remove_schedule', $data['id'] );
543 }
544
545 if ( $source_type === 'Visualizer_Source_Query' ) {
546 $db_schedule = intval( $data['visualizer-db-schedule'] );
547 $db_query = $data['visualizer-db-query'];
548 update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE, $db_schedule );
549 update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY, stripslashes( $db_query ) );
550 } else {
551 delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE );
552 delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY );
553 }
554
555 if ( $source_type === 'Visualizer_Source_Json' ) {
556 $json_schedule = intval( $data['visualizer-json-schedule'] );
557 $json_url = esc_url_raw( $data['visualizer-json-url'] );
558 $json_headers = esc_url_raw( $data['visualizer-json-headers'] );
559 $json_root = $data['visualizer-json-root'];
560 $json_paging = $data['visualizer-json-paging'];
561
562 update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE, $json_schedule );
563 update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL, $json_url );
564 update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS, $json_headers );
565 update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT, $json_root );
566
567 if ( ! empty( $json_paging ) ) {
568 update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING, $json_paging );
569 } else {
570 delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING );
571 }
572 } else {
573 delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE );
574 delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL );
575 delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS );
576 delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT );
577 delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING );
578 }
579
580 if ( Visualizer_Module::is_pro() ) {
581 update_post_meta( $data['id'], Visualizer_PRO::CF_PERMISSIONS, $data['visualizer-permissions'] );
582 }
583
584 if ( $data['visualizer-chart-url'] ) {
585 $chart_url = esc_url_raw( $data['visualizer-chart-url'] );
586 $content['source'] = $chart_url;
587 $content['data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] );
588 } else {
589 $content = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] );
590 }
591
592 $chart = array(
593 'ID' => $data['id'],
594 'post_content' => serialize( $content ),
595 );
596
597 wp_update_post( $chart );
598
599 $revisions = wp_get_post_revisions( $data['id'], array( 'order' => 'ASC' ) );
600
601 if ( count( $revisions ) > 1 ) {
602 $revision_ids = array_keys( $revisions );
603
604 // delete all revisions.
605 foreach ( $revision_ids as $id ) {
606 wp_delete_post_revision( $id );
607 }
608 }
609
610 return new \WP_REST_Response( array( 'success' => sprintf( 'Chart updated' ) ) );
611 }
612 }
613
614 /**
615 * Format chart data.
616 */
617 public function format_chart_data( $data, $series ) {
618 foreach ( $series as $i => $row ) {
619 // if no value exists for the seires, then add null
620 if ( ! isset( $series[ $i ] ) ) {
621 $series[ $i ] = null;
622 }
623
624 if ( is_null( $series[ $i ] ) ) {
625 continue;
626 }
627
628 if ( $row['type'] === 'number' ) {
629 foreach ( $data as $o => $col ) {
630 $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null );
631 }
632 }
633
634 if ( $row['type'] === 'boolean' ) {
635 foreach ( $data as $o => $col ) {
636 $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null;
637 }
638 }
639
640 if ( $row['type'] === 'timeofday' ) {
641 foreach ( $data as $o => $col ) {
642 $date = new DateTime( '1984-03-16T' . $col[ $i ] );
643 if ( $date ) {
644 $data[ $o ][ $i ] = array(
645 intval( $date->format( 'H' ) ),
646 intval( $date->format( 'i' ) ),
647 intval( $date->format( 's' ) ),
648 0,
649 );
650 }
651 }
652 }
653
654 if ( $row['type'] === 'string' ) {
655 foreach ( $data as $o => $col ) {
656 $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] );
657 }
658 }
659 }
660
661 return $data;
662 }
663
664 /**
665 * Use toUTF8 function
666 */
667 public function toUTF8( $datum ) {
668 if ( ! function_exists( 'mb_detect_encoding' ) || mb_detect_encoding( $datum ) !== 'ASCII' ) {
669 $datum = \ForceUTF8\Encoding::toUTF8( $datum );
670 }
671 return $datum;
672 }
673
674 /**
675 * Handle remote CSV data
676 */
677 public function upload_csv_data( $data ) {
678 if ( ! current_user_can( 'edit_posts' ) ) {
679 return false;
680 }
681
682 if ( $data['url'] && ! is_wp_error( $data['url'] ) && filter_var( $data['url'], FILTER_VALIDATE_URL ) ) {
683 $source = new Visualizer_Source_Csv_Remote( $data['url'] );
684 if ( $source->fetch() ) {
685 $temp = $source->getData();
686 if ( is_string( $temp ) && is_array( unserialize( $temp ) ) ) {
687 $content['series'] = $source->getSeries();
688 $content['data'] = $source->getRawData();
689 return $content;
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 } else {
697 return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) );
698 }
699 }
700
701 /**
702 * Get permission data
703 */
704 public function get_permission_data( $data ) {
705 if ( ! current_user_can( 'edit_posts' ) ) {
706 return false;
707 }
708
709 $options = array();
710 switch ( $data['type'] ) {
711 case 'users':
712 $query = new WP_User_Query(
713 array(
714 'number' => 1000,
715 'orderby' => 'display_name',
716 'fields' => array( 'ID', 'display_name' ),
717 'count_total' => false,
718 )
719 );
720 $users = $query->get_results();
721 if ( ! empty( $users ) ) {
722 $i = 0;
723 foreach ( $users as $user ) {
724 $options[ $i ]['value'] = $user->ID;
725 $options[ $i ]['label'] = $user->display_name;
726 $i++;
727 }
728 }
729 break;
730 case 'roles':
731 if ( ! function_exists( 'get_editable_roles' ) ) {
732 require_once ABSPATH . 'wp-admin/includes/user.php';
733 }
734 $roles = get_editable_roles();
735 if ( ! empty( $roles ) ) {
736 $i = 0;
737 foreach ( get_editable_roles() as $name => $info ) {
738 $options[ $i ]['value'] = $name;
739 $options[ $i ]['label'] = $name;
740 $i++;
741 }
742 }
743 break;
744 }
745 return $options;
746 }
747
748 /**
749 * Filter Rest Query
750 */
751 public function add_rest_query_vars( $args, \WP_REST_Request $request ) {
752 if ( isset( $request['meta_key'] ) && isset( $request['meta_value'] ) ) {
753 $args['meta_query'] = array(
754 'relation' => 'OR',
755 array(
756 'key' => $request->get_param( 'meta_key' ),
757 'value' => $request->get_param( 'meta_value' ),
758 'compare' => '!=',
759 ),
760 array(
761 'key' => $request->get_param( 'meta_key' ),
762 'value' => $request->get_param( 'meta_value' ),
763 'compare' => 'NOT EXISTS',
764 ),
765 );
766 }
767 return $args;
768 }
769 }
770