PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.4.3
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.4.3
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.3, at classes/Visualizer/Gutenberg/Block.php

768 lines 23.4 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
524 $chart_type = sanitize_text_field( $data['visualizer-chart-type'] );
525 $source_type = sanitize_text_field( $data['visualizer-source'] );
526
527 update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_TYPE, $chart_type );
528 update_post_meta( $data['id'], Visualizer_Plugin::CF_SOURCE, $source_type );
529 update_post_meta( $data['id'], Visualizer_Plugin::CF_DEFAULT_DATA, $data['visualizer-default-data'] );
530 update_post_meta( $data['id'], Visualizer_Plugin::CF_SERIES, $data['visualizer-series'] );
531 update_post_meta( $data['id'], Visualizer_Plugin::CF_SETTINGS, $data['visualizer-settings'] );
532
533 if ( $data['visualizer-chart-url'] && $data['visualizer-chart-schedule'] ) {
534 $chart_url = esc_url_raw( $data['visualizer-chart-url'] );
535 $chart_schedule = intval( $data['visualizer-chart-schedule'] );
536 update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $chart_url );
537 apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $chart_url, $chart_schedule );
538 } else {
539 delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL );
540 apply_filters( 'visualizer_pro_remove_schedule', $data['id'] );
541 }
542
543 if ( $source_type === 'Visualizer_Source_Query' ) {
544 $db_schedule = intval( $data['visualizer-db-schedule'] );
545 $db_query = $data['visualizer-db-query'];
546 update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE, $db_schedule );
547 update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY, stripslashes( $db_query ) );
548 } else {
549 delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE );
550 delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY );
551 }
552
553 if ( $source_type === 'Visualizer_Source_Json' ) {
554 $json_schedule = intval( $data['visualizer-json-schedule'] );
555 $json_url = esc_url_raw( $data['visualizer-json-url'] );
556 $json_headers = esc_url_raw( $data['visualizer-json-headers'] );
557 $json_root = $data['visualizer-json-root'];
558 $json_paging = $data['visualizer-json-paging'];
559
560 update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE, $json_schedule );
561 update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL, $json_url );
562 update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS, $json_headers );
563 update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT, $json_root );
564
565 if ( ! empty( $json_paging ) ) {
566 update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING, $json_paging );
567 } else {
568 delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING );
569 }
570 } else {
571 delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE );
572 delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL );
573 delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS );
574 delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT );
575 delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING );
576 }
577
578 if ( Visualizer_Module::is_pro() ) {
579 update_post_meta( $data['id'], Visualizer_PRO::CF_PERMISSIONS, $data['visualizer-permissions'] );
580 }
581
582 if ( $data['visualizer-chart-url'] ) {
583 $chart_url = esc_url_raw( $data['visualizer-chart-url'] );
584 $content['source'] = $chart_url;
585 $content['data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] );
586 } else {
587 $content = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] );
588 }
589
590 $chart = array(
591 'ID' => $data['id'],
592 'post_content' => serialize( $content ),
593 );
594
595 wp_update_post( $chart );
596
597 $revisions = wp_get_post_revisions( $data['id'], array( 'order' => 'ASC' ) );
598
599 if ( count( $revisions ) > 1 ) {
600 $revision_ids = array_keys( $revisions );
601
602 // delete all revisions.
603 foreach ( $revision_ids as $id ) {
604 wp_delete_post_revision( $id );
605 }
606 }
607
608 return new \WP_REST_Response( array( 'success' => sprintf( 'Chart updated' ) ) );
609 }
610 }
611
612 /**
613 * Format chart data.
614 */
615 public function format_chart_data( $data, $series ) {
616 foreach ( $series as $i => $row ) {
617 // if no value exists for the seires, then add null
618 if ( ! isset( $series[ $i ] ) ) {
619 $series[ $i ] = null;
620 }
621
622 if ( is_null( $series[ $i ] ) ) {
623 continue;
624 }
625
626 if ( $row['type'] === 'number' ) {
627 foreach ( $data as $o => $col ) {
628 $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null );
629 }
630 }
631
632 if ( $row['type'] === 'boolean' ) {
633 foreach ( $data as $o => $col ) {
634 $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null;
635 }
636 }
637
638 if ( $row['type'] === 'timeofday' ) {
639 foreach ( $data as $o => $col ) {
640 $date = new DateTime( '1984-03-16T' . $col[ $i ] );
641 if ( $date ) {
642 $data[ $o ][ $i ] = array(
643 intval( $date->format( 'H' ) ),
644 intval( $date->format( 'i' ) ),
645 intval( $date->format( 's' ) ),
646 0,
647 );
648 }
649 }
650 }
651
652 if ( $row['type'] === 'string' ) {
653 foreach ( $data as $o => $col ) {
654 $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] );
655 }
656 }
657 }
658
659 return $data;
660 }
661
662 /**
663 * Use toUTF8 function
664 */
665 public function toUTF8( $datum ) {
666 if ( ! function_exists( 'mb_detect_encoding' ) || mb_detect_encoding( $datum ) !== 'ASCII' ) {
667 $datum = \ForceUTF8\Encoding::toUTF8( $datum );
668 }
669 return $datum;
670 }
671
672 /**
673 * Handle remote CSV data
674 */
675 public function upload_csv_data( $data ) {
676 if ( ! current_user_can( 'edit_posts' ) ) {
677 return false;
678 }
679
680 if ( $data['url'] && ! is_wp_error( $data['url'] ) && filter_var( $data['url'], FILTER_VALIDATE_URL ) ) {
681 $source = new Visualizer_Source_Csv_Remote( $data['url'] );
682 if ( $source->fetch() ) {
683 $temp = $source->getData();
684 if ( is_string( $temp ) && is_array( unserialize( $temp ) ) ) {
685 $content['series'] = $source->getSeries();
686 $content['data'] = $source->getRawData();
687 return $content;
688 } else {
689 return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) );
690 }
691 } else {
692 return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) );
693 }
694 } else {
695 return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) );
696 }
697 }
698
699 /**
700 * Get permission data
701 */
702 public function get_permission_data( $data ) {
703 if ( ! current_user_can( 'edit_posts' ) ) {
704 return false;
705 }
706
707 $options = array();
708 switch ( $data['type'] ) {
709 case 'users':
710 $query = new WP_User_Query(
711 array(
712 'number' => 1000,
713 'orderby' => 'display_name',
714 'fields' => array( 'ID', 'display_name' ),
715 'count_total' => false,
716 )
717 );
718 $users = $query->get_results();
719 if ( ! empty( $users ) ) {
720 $i = 0;
721 foreach ( $users as $user ) {
722 $options[ $i ]['value'] = $user->ID;
723 $options[ $i ]['label'] = $user->display_name;
724 $i++;
725 }
726 }
727 break;
728 case 'roles':
729 if ( ! function_exists( 'get_editable_roles' ) ) {
730 require_once ABSPATH . 'wp-admin/includes/user.php';
731 }
732 $roles = get_editable_roles();
733 if ( ! empty( $roles ) ) {
734 $i = 0;
735 foreach ( get_editable_roles() as $name => $info ) {
736 $options[ $i ]['value'] = $name;
737 $options[ $i ]['label'] = $name;
738 $i++;
739 }
740 }
741 break;
742 }
743 return $options;
744 }
745
746 /**
747 * Filter Rest Query
748 */
749 public function add_rest_query_vars( $args, \WP_REST_Request $request ) {
750 if ( isset( $request['meta_key'] ) && isset( $request['meta_value'] ) ) {
751 $args['meta_query'] = array(
752 'relation' => 'OR',
753 array(
754 'key' => $request->get_param( 'meta_key' ),
755 'value' => $request->get_param( 'meta_value' ),
756 'compare' => '!=',
757 ),
758 array(
759 'key' => $request->get_param( 'meta_key' ),
760 'value' => $request->get_param( 'meta_value' ),
761 'compare' => 'NOT EXISTS',
762 ),
763 );
764 }
765 return $args;
766 }
767 }
768