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.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.1, at classes/Visualizer/Gutenberg/Block.php

767 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 );
106 wp_localize_script( 'visualizer-gutenberg-block', 'visualizerLocalize', $translation_array );
107
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 );
111
112 if ( version_compare( $wp_version, '4.9.0', '>' ) ) {
113
114 wp_enqueue_code_editor(
115 array(
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 ),
126 )
127 );
128 }
129 }
130 /**
131 * Hook server side rendering into render callback
132 */
133 public function register_block_type() {
134 register_block_type(
135 'visualizer/chart', array(
136 'render_callback' => array( $this, 'gutenberg_block_callback' ),
137 'attributes' => array(
138 'id' => array(
139 'type' => 'number',
140 ),
141 ),
142 )
143 );
144 }
145
146 /**
147 * Gutenberg Block Callback Function
148 */
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 . '"]';
156 }
157 }
158
159 /**
160 * Hook server side rendering into render callback
161 */
162 public function register_rest_endpoints() {
163 register_rest_field(
164 'visualizer',
165 'chart_data',
166 array(
167 'get_callback' => array( $this, 'get_visualizer_data' ),
168 )
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 );
287 }
288
289 /**
290 * Get Post Meta Fields
291 */
292 public function get_visualizer_data( $post ) {
293 if ( ! current_user_can( 'edit_posts' ) ) {
294 return false;
295 }
296
297 $data = array();
298 $post_id = $post['id'];
299
300 $data['visualizer-chart-type'] = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_TYPE, true );
301
302 $library = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_LIBRARY, true );
303 $data['visualizer-chart-library'] = $library;
304
305 $data['visualizer-source'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SOURCE, true );
306
307 $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true );
308
309 // faetch and update settings
310 $data['visualizer-settings'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SETTINGS, true );
311
312 // handle series filter hooks
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'] );
314
315 // handle settings filter hooks
316 $data['visualizer-settings'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $data['visualizer-settings'], $post_id, $data['visualizer-chart-type'] );
317
318 // handle data filter hooks
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'] );
320
321 $data['visualizer-data-exploded'] = '';
322 // handle annotations for google charts
323 if ( 'GoogleCharts' === $library ) {
324 // this will contain the data of both axis.
325 $settings = $data['visualizer-settings'];
326 // this will contain data only of Y axis.
327 $series = $data['visualizer-series'];
328 $annotations = array();
329 if ( isset( $settings['series'] ) ) {
330 foreach ( $settings['series'] as $index => $serie ) {
331 // skip X axis data.
332 if ( $index === 0 ) {
333 continue;
334 }
335 if ( ! empty( $serie['role'] ) ) {
336 // this series is some kind of annotation, so let's collect its index.
337 // the index will be +1 because the X axis value is index 0, which is being ignored.
338 $annotations[ 'role' . ( intval( $index ) + 1 ) ] = $serie['role'];
339 }
340 }
341 }
342 if ( ! empty( $annotations ) ) {
343 $exploded_data = array();
344 $series_names = array();
345 foreach ( $series as $index => $serie ) {
346 // skip X axis data.
347 if ( $index === 0 ) {
348 continue;
349 }
350 if ( array_key_exists( 'role' . $index, $annotations ) ) {
351 $series_names[] = (object) array( 'role' => $annotations[ 'role' . $index ], 'type' => $serie['type'] );
352 } else {
353 $series_names[] = $serie['label'];
354 }
355 }
356 $exploded_data[] = $series_names;
357
358 foreach ( $data['visualizer-data'] as $datum ) {
359 // skip X axis data.
360 unset( $datum[0] );
361 $exploded_data[] = $datum;
362 }
363 $data['visualizer-data-exploded'] = array( $exploded_data );
364 }
365 }
366
367 $import = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_URL, true );
368
369 $schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_SCHEDULE, true );
370
371 $db_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
372
373 $db_query = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_QUERY, true );
374
375 $json_url = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_URL, true );
376
377 $json_headers = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_HEADERS, true );
378
379 $json_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_SCHEDULE, true );
380
381 $json_root = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_ROOT, true );
382
383 $json_paging = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_PAGING, true );
384
385 if ( ! empty( $import ) && ! empty( $schedule ) ) {
386 $data['visualizer-chart-url'] = $import;
387 $data['visualizer-chart-schedule'] = $schedule;
388 }
389
390 if ( ! empty( $db_schedule ) && ! empty( $db_query ) ) {
391 $data['visualizer-db-schedule'] = $db_schedule;
392 $data['visualizer-db-query'] = $db_query;
393 }
394
395 if ( ! empty( $json_url ) ) {
396 $data['visualizer-json-schedule'] = $json_schedule;
397 $data['visualizer-json-url'] = $json_url;
398 $data['visualizer-json-headers'] = $json_headers;
399 $data['visualizer-json-root'] = $json_root;
400
401 if ( Visualizer_Module::is_pro() && ! empty( $json_paging ) ) {
402 $data['visualizer-json-paging'] = $json_paging;
403 }
404 }
405
406 if ( Visualizer_Module::is_pro() ) {
407 $permissions = get_post_meta( $post_id, Visualizer_PRO::CF_PERMISSIONS, true );
408
409 if ( ! empty( $permissions ) ) {
410 $data['visualizer-permissions'] = $permissions;
411 }
412 }
413
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 }
426
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 ) );
439 }
440
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 /**
612 * Format chart data.
613 */
614 public function format_chart_data( $data, $series ) {
615 foreach ( $series as $i => $row ) {
616 // if no value exists for the seires, then add null
617 if ( ! isset( $series[ $i ] ) ) {
618 $series[ $i ] = null;
619 }
620
621 if ( is_null( $series[ $i ] ) ) {
622 continue;
623 }
624
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 );
647 }
648 }
649 }
650
651 if ( $row['type'] === 'string' ) {
652 foreach ( $data as $o => $col ) {
653 $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] );
654 }
655 }
656 }
657
658 return $data;
659 }
660
661 /**
662 * Use toUTF8 function
663 */
664 public function toUTF8( $datum ) {
665 if ( ! function_exists( 'mb_detect_encoding' ) || mb_detect_encoding( $datum ) !== 'ASCII' ) {
666 $datum = \ForceUTF8\Encoding::toUTF8( $datum );
667 }
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;
743 }
744
745 /**
746 * Filter Rest Query
747 */
748 public function add_rest_query_vars( $args, \WP_REST_Request $request ) {
749 if ( isset( $request['meta_key'] ) && isset( $request['meta_value'] ) ) {
750 $args['meta_query'] = array(
751 'relation' => 'OR',
752 array(
753 'key' => $request->get_param( 'meta_key' ),
754 'value' => $request->get_param( 'meta_value' ),
755 'compare' => '!=',
756 ),
757 array(
758 'key' => $request->get_param( 'meta_key' ),
759 'value' => $request->get_param( 'meta_value' ),
760 'compare' => 'NOT EXISTS',
761 ),
762 );
763 }
764 return $args;
765 }
766 }
767