Skip to content

Commit 2449a40

Browse files
Merge branch '3.4.0' of https://github.com/codeinwp/visualizer into issue-214
2 parents d51bf96 + f0e7944 commit 2449a40

39 files changed

+1576
-105
lines changed

classes/Visualizer/Gutenberg/Block.php

Lines changed: 263 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ private function __construct() {
6767
* Enqueue front end and editor JavaScript and CSS
6868
*/
6969
public function enqueue_gutenberg_scripts() {
70+
global $wp_version;
71+
7072
$blockPath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.js';
7173
$handsontableJS = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/handsontable.js';
7274
$stylePath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.css';
@@ -91,18 +93,39 @@ public function enqueue_gutenberg_scripts() {
9193
}
9294
}
9395

96+
$table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping();
97+
9498
$translation_array = array(
9599
'isPro' => $type,
96100
'proTeaser' => Visualizer_Plugin::PRO_TEASER_URL,
97101
'absurl' => VISUALIZER_ABSURL,
98102
'charts' => Visualizer_Module_Admin::_getChartTypesLocalized(),
99103
'adminPage' => menu_page_url( 'visualizer', false ),
104+
'sqlTable' => $table_col_mapping,
100105
);
101106
wp_localize_script( 'visualizer-gutenberg-block', 'visualizerLocalize', $translation_array );
102107

103108
// Enqueue frontend and editor block styles
104109
wp_enqueue_style( 'handsontable', $handsontableCSS );
105110
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+
}
106129
}
107130
/**
108131
* Hook server side rendering into render callback
@@ -145,6 +168,72 @@ public function register_rest_endpoints() {
145168
)
146169
);
147170

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+
148237
register_rest_route(
149238
'visualizer/v' . VISUALIZER_REST_VERSION,
150239
'/update-chart',
@@ -232,11 +321,41 @@ public function get_visualizer_data( $post ) {
232321

233322
$schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_SCHEDULE, true );
234323

324+
$db_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
325+
326+
$db_query = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_QUERY, true );
327+
328+
$json_url = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_URL, true );
329+
330+
$json_headers = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_HEADERS, true );
331+
332+
$json_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_SCHEDULE, true );
333+
334+
$json_root = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_ROOT, true );
335+
336+
$json_paging = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_PAGING, true );
337+
235338
if ( ! empty( $import ) && ! empty( $schedule ) ) {
236339
$data['visualizer-chart-url'] = $import;
237340
$data['visualizer-chart-schedule'] = $schedule;
238341
}
239342

343+
if ( ! empty( $db_schedule ) && ! empty( $db_query ) ) {
344+
$data['visualizer-db-schedule'] = $db_schedule;
345+
$data['visualizer-db-query'] = $db_query;
346+
}
347+
348+
if ( ! empty( $json_url ) ) {
349+
$data['visualizer-json-schedule'] = $json_schedule;
350+
$data['visualizer-json-url'] = $json_url;
351+
$data['visualizer-json-headers'] = $json_headers;
352+
$data['visualizer-json-root'] = $json_root;
353+
354+
if ( Visualizer_Module::is_pro() && ! empty( $json_paging ) ) {
355+
$data['visualizer-json-paging'] = $json_paging;
356+
}
357+
}
358+
240359
if ( Visualizer_Module::is_pro() ) {
241360
$permissions = get_post_meta( $post_id, Visualizer_PRO::CF_PERMISSIONS, true );
242361

@@ -248,6 +367,102 @@ public function get_visualizer_data( $post ) {
248367
return $data;
249368
}
250369

370+
/**
371+
* Returns the data for the query.
372+
*
373+
* @access public
374+
*/
375+
public function get_query_data( $data ) {
376+
if ( ! current_user_can( 'edit_posts' ) ) {
377+
return false;
378+
}
379+
380+
$source = new Visualizer_Source_Query( stripslashes( $data['query'] ) );
381+
$html = $source->fetch( true );
382+
$source->fetch( false );
383+
$name = $source->getSourceName();
384+
$series = $source->getSeries();
385+
$data = $source->getRawData();
386+
$error = '';
387+
if ( empty( $html ) ) {
388+
$error = $source->get_error();
389+
wp_send_json_error( array( 'msg' => $error ) );
390+
}
391+
wp_send_json_success( array( 'table' => $html, 'name' => $name, 'series' => $series, 'data' => $data ) );
392+
}
393+
394+
/**
395+
* Returns the JSON root.
396+
*
397+
* @access public
398+
*/
399+
public function get_json_root_data( $data ) {
400+
if ( ! current_user_can( 'edit_posts' ) ) {
401+
return false;
402+
}
403+
404+
$source = new Visualizer_Source_Json( $data );
405+
406+
$roots = $source->fetchRoots();
407+
if ( empty( $roots ) ) {
408+
wp_send_json_error( array( 'msg' => $source->get_error() ) );
409+
}
410+
411+
wp_send_json_success( array( 'url' => $data['url'], 'roots' => $roots ) );
412+
}
413+
414+
/**
415+
* Returns the JSON data.
416+
*
417+
* @access public
418+
*/
419+
public function get_json_data( $data ) {
420+
if ( ! current_user_can( 'edit_posts' ) ) {
421+
return false;
422+
}
423+
424+
$chart_id = $data['chart'];
425+
426+
if ( empty( $chart_id ) ) {
427+
wp_die();
428+
}
429+
430+
$source = new Visualizer_Source_Json( $data );
431+
$source->fetch();
432+
$table = $source->getRawData();
433+
434+
if ( empty( $table ) ) {
435+
wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) );
436+
}
437+
438+
$table = Visualizer_Render_Layout::show( 'editor-table', $table, $chart_id, 'viz-json-table', false, false );
439+
wp_send_json_success( array( 'table' => $table, 'root' => $data['root'], 'url' => $data['url'], 'paging' => $source->getPaginationElements() ) );
440+
}
441+
442+
/**
443+
* Set the JSON data.
444+
*
445+
* @access public
446+
*/
447+
public function set_json_data( $data ) {
448+
if ( ! current_user_can( 'edit_posts' ) ) {
449+
return false;
450+
}
451+
452+
$source = new Visualizer_Source_Json( $data );
453+
454+
$table = $source->fetch();
455+
if ( empty( $table ) ) {
456+
wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) );
457+
}
458+
459+
$source->fetchFromEditableTable();
460+
$name = $source->getSourceName();
461+
$series = json_encode( $source->getSeries() );
462+
$data = json_encode( $source->getRawData() );
463+
wp_send_json_success( array( 'name' => $name, 'series' => $series, 'data' => $data ) );
464+
}
465+
251466
/**
252467
* Rest Callback Method
253468
*/
@@ -277,6 +492,41 @@ public function update_chart_data( $data ) {
277492
apply_filters( 'visualizer_pro_remove_schedule', $data['id'] );
278493
}
279494

495+
if ( $source_type === 'Visualizer_Source_Query' ) {
496+
$db_schedule = intval( $data['visualizer-db-schedule'] );
497+
$db_query = $data['visualizer-db-query'];
498+
update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE, $db_schedule );
499+
update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY, stripslashes( $db_query ) );
500+
} else {
501+
delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE );
502+
delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY );
503+
}
504+
505+
if ( $source_type === 'Visualizer_Source_Json' ) {
506+
$json_schedule = intval( $data['visualizer-json-schedule'] );
507+
$json_url = esc_url_raw( $data['visualizer-json-url'] );
508+
$json_headers = esc_url_raw( $data['visualizer-json-headers'] );
509+
$json_root = $data['visualizer-json-root'];
510+
$json_paging = $data['visualizer-json-paging'];
511+
512+
update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE, $json_schedule );
513+
update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL, $json_url );
514+
update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS, $json_headers );
515+
update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT, $json_root );
516+
517+
if ( ! empty( $json_paging ) ) {
518+
update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING, $json_paging );
519+
} else {
520+
delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING );
521+
}
522+
} else {
523+
delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE );
524+
delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL );
525+
delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS );
526+
delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT );
527+
delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING );
528+
}
529+
280530
if ( Visualizer_Module::is_pro() ) {
281531
update_post_meta( $data['id'], Visualizer_PRO::CF_PERMISSIONS, $data['visualizer-permissions'] );
282532
}
@@ -450,9 +700,19 @@ public function get_permission_data( $data ) {
450700
*/
451701
public function add_rest_query_vars( $args, \WP_REST_Request $request ) {
452702
if ( isset( $request['meta_key'] ) && isset( $request['meta_value'] ) ) {
453-
$args['meta_key'] = $request->get_param( 'meta_key' );
454-
$args['meta_value'] = $request->get_param( 'meta_value' );
455-
$args['meta_compare'] = '!=';
703+
$args['meta_query'] = array(
704+
'relation' => 'OR',
705+
array(
706+
'key' => $request->get_param( 'meta_key' ),
707+
'value' => $request->get_param( 'meta_value' ),
708+
'compare' => '!=',
709+
),
710+
array(
711+
'key' => $request->get_param( 'meta_key' ),
712+
'value' => $request->get_param( 'meta_value' ),
713+
'compare' => 'NOT EXISTS',
714+
),
715+
);
456716
}
457717
return $args;
458718
}

0 commit comments

Comments
 (0)