@@ -61,9 +61,156 @@ public function __construct( Visualizer_Plugin $plugin ) {
61
61
62
62
$ this ->_addAjaxAction ( Visualizer_Plugin::ACTION_FETCH_DB_DATA , 'getQueryData ' );
63
63
$ this ->_addAjaxAction ( Visualizer_Plugin::ACTION_SAVE_DB_QUERY , 'saveQuery ' );
64
+
65
+ $ this ->_addAjaxAction ( Visualizer_Plugin::ACTION_JSON_GET_ROOTS , 'getJsonRoots ' );
66
+ $ this ->_addAjaxAction ( Visualizer_Plugin::ACTION_JSON_GET_DATA , 'getJsonData ' );
67
+ $ this ->_addAjaxAction ( Visualizer_Plugin::ACTION_JSON_SET_DATA , 'setJsonData ' );
68
+ $ this ->_addAjaxAction ( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE , 'setJsonSchedule ' );
69
+
64
70
$ this ->_addAjaxAction ( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY , 'saveFilter ' );
71
+
72
+ }
73
+
74
+ /**
75
+ * Sets the schedule for how JSON-endpoint charts should be updated.
76
+ *
77
+ * @since ?
78
+ *
79
+ * @access public
80
+ */
81
+ public function setJsonSchedule () {
82
+ check_ajax_referer ( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE . Visualizer_Plugin::VERSION , 'security ' );
83
+
84
+ $ chart_id = filter_input (
85
+ INPUT_POST ,
86
+ 'chart ' ,
87
+ FILTER_VALIDATE_INT ,
88
+ array (
89
+ 'options ' => array (
90
+ 'min_range ' => 1 ,
91
+ ),
92
+ )
93
+ );
94
+
95
+ if ( ! $ chart_id ) {
96
+ wp_send_json_error ();
97
+ }
98
+
99
+ $ time = filter_input (
100
+ INPUT_POST ,
101
+ 'time ' ,
102
+ FILTER_VALIDATE_INT ,
103
+ array (
104
+ 'options ' => array (
105
+ 'min_range ' => -1 ,
106
+ ),
107
+ )
108
+ );
109
+
110
+ delete_post_meta ( $ chart_id , Visualizer_Plugin::CF_JSON_SCHEDULE );
111
+
112
+ if ( -1 < $ time ) {
113
+ add_post_meta ( $ chart_id , Visualizer_Plugin::CF_JSON_SCHEDULE , $ time );
114
+ }
115
+ wp_send_json_success ();
116
+ }
117
+
118
+ /**
119
+ * Get the root elements for JSON-endpoint.
120
+ *
121
+ * @since ?
122
+ *
123
+ * @access public
124
+ */
125
+ public function getJsonRoots () {
126
+ check_ajax_referer ( Visualizer_Plugin::ACTION_JSON_GET_ROOTS . Visualizer_Plugin::VERSION , 'security ' );
127
+
128
+ $ params = wp_parse_args ( $ _POST ['params ' ] );
129
+
130
+ $ source = new Visualizer_Source_Json ( $ params );
131
+
132
+ $ roots = $ source ->fetchRoots ();
133
+ if ( empty ( $ roots ) ) {
134
+ wp_send_json_error ();
135
+ }
136
+
137
+ wp_send_json_success ( array ( 'url ' => $ params ['url ' ], 'roots ' => $ roots ) );
65
138
}
66
139
140
+ /**
141
+ * Get the data for the JSON-endpoint corresponding to the chosen root.
142
+ *
143
+ * @since ?
144
+ *
145
+ * @access public
146
+ */
147
+ public function getJsonData () {
148
+ check_ajax_referer ( Visualizer_Plugin::ACTION_JSON_GET_DATA . Visualizer_Plugin::VERSION , 'security ' );
149
+
150
+ $ params = wp_parse_args ( $ _POST ['params ' ] );
151
+
152
+ $ chart_id = $ params ['chart ' ];
153
+
154
+ if ( empty ( $ chart_id ) ) {
155
+ wp_die ();
156
+ }
157
+
158
+ $ source = new Visualizer_Source_Json ( $ params );
159
+
160
+ $ data = $ source ->parse ();
161
+ if ( empty ( $ data ) ) {
162
+ wp_send_json_error ();
163
+ }
164
+
165
+ $ data = Visualizer_Render_Layout::show ( 'editor-table ' , $ data , $ chart_id , 'viz-json-table ' );
166
+ wp_send_json_success ( array ( 'table ' => $ data , 'root ' => $ params ['root ' ], 'url ' => $ params ['url ' ], 'paging ' => $ source ->getPaginationElements () ) );
167
+ }
168
+
169
+ /**
170
+ * Updates the database with the correct post parameters for JSON-endpoint charts.
171
+ *
172
+ * @since ?
173
+ *
174
+ * @access public
175
+ */
176
+ public function setJsonData () {
177
+ check_ajax_referer ( Visualizer_Plugin::ACTION_JSON_SET_DATA . Visualizer_Plugin::VERSION , 'security ' );
178
+
179
+ $ params = $ _POST ;
180
+ $ chart_id = $ _GET ['chart ' ];
181
+
182
+ if ( empty ( $ chart_id ) ) {
183
+ wp_die ();
184
+ }
185
+
186
+ $ chart = get_post ( $ chart_id );
187
+
188
+ $ source = new Visualizer_Source_Json ( $ params );
189
+ $ source ->fetch ();
190
+
191
+ $ content = $ source ->getData ();
192
+ $ chart ->post_content = $ content ;
193
+ wp_update_post ( $ chart ->to_array () );
194
+ update_post_meta ( $ chart ->ID , Visualizer_Plugin::CF_SERIES , $ source ->getSeries () );
195
+ update_post_meta ( $ chart ->ID , Visualizer_Plugin::CF_SOURCE , $ source ->getSourceName () );
196
+ update_post_meta ( $ chart ->ID , Visualizer_Plugin::CF_DEFAULT_DATA , 0 );
197
+ update_post_meta ( $ chart ->ID , Visualizer_Plugin::CF_JSON_URL , $ params ['url ' ] );
198
+ update_post_meta ( $ chart ->ID , Visualizer_Plugin::CF_JSON_ROOT , $ params ['root ' ] );
199
+ delete_post_meta ( $ chart ->ID , Visualizer_Plugin::CF_JSON_PAGING );
200
+ if ( ! empty ( $ params ['paging ' ] ) ) {
201
+ add_post_meta ( $ chart ->ID , Visualizer_Plugin::CF_JSON_PAGING , $ params ['paging ' ] );
202
+ }
203
+
204
+ $ render = new Visualizer_Render_Page_Update ();
205
+ $ render ->id = $ chart ->ID ;
206
+ $ render ->data = json_encode ( $ source ->getRawData () );
207
+ $ render ->series = json_encode ( $ source ->getSeries () );
208
+ $ render ->render ();
209
+
210
+ defined ( 'WP_TESTS_DOMAIN ' ) ? wp_die () : exit ();
211
+ }
212
+
213
+
67
214
/**
68
215
* Fetches charts from database.
69
216
*
@@ -316,7 +463,7 @@ public function renderChartPages() {
316
463
wp_register_style ( 'visualizer-chosen ' , VISUALIZER_ABSURL . 'css/lib/chosen.min.css ' , array (), Visualizer_Plugin::VERSION );
317
464
318
465
wp_register_style ( 'visualizer-frame ' , VISUALIZER_ABSURL . 'css/frame.css ' , array ( 'visualizer-chosen ' ), Visualizer_Plugin::VERSION );
319
- wp_register_script ( 'visualizer-frame ' , VISUALIZER_ABSURL . 'js/frame.js ' , array ( 'visualizer-chosen ' ), Visualizer_Plugin::VERSION , true );
466
+ wp_register_script ( 'visualizer-frame ' , VISUALIZER_ABSURL . 'js/frame.js ' , array ( 'visualizer-chosen ' , ' jquery-ui-accordion ' ), Visualizer_Plugin::VERSION , true );
320
467
wp_register_script ( 'visualizer-customization ' , $ this ->get_user_customization_js (), array (), null , true );
321
468
wp_register_script (
322
469
'visualizer-render ' ,
@@ -381,20 +528,21 @@ public function renderChartPages() {
381
528
private function loadCodeEditorAssets () {
382
529
global $ wp_version ;
383
530
531
+ $ wp_scripts = wp_scripts ();
532
+
533
+ // data tables assets.
534
+ wp_register_script ( 'visualizer-datatables ' , '//cdn.datatables.net/v/dt/dt-1.10.18/b-1.5.6/b-colvis-1.5.6/cr-1.5.0/fc-3.2.5/fh-3.1.4/r-2.2.2/sc-2.0.0/sl-1.3.0/datatables.min.js ' , array ( 'jquery-ui-core ' ), Visualizer_Plugin::VERSION );
535
+ wp_register_style ( 'visualizer-datatables ' , '//cdn.datatables.net/v/dt/dt-1.10.18/b-1.5.6/b-colvis-1.5.6/cr-1.5.0/fc-3.2.5/fh-3.1.4/r-2.2.2/sc-2.0.0/sl-1.3.0/datatables.min.css ' , array (), Visualizer_Plugin::VERSION );
536
+ wp_register_style ( 'visualizer-jquery-ui ' , sprintf ( '//ajax.googleapis.com/ajax/libs/jqueryui/%s/themes/smoothness/jquery-ui.css ' , $ wp_scripts ->registered ['jquery-ui-core ' ]->ver ), array ( 'visualizer-datatables ' ), Visualizer_Plugin::VERSION );
537
+ wp_enqueue_script ( 'visualizer-datatables ' );
538
+ wp_enqueue_style ( 'visualizer-jquery-ui ' );
539
+
384
540
if ( ! VISUALIZER_PRO ) {
385
541
return ;
386
542
}
387
543
388
544
$ table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping ();
389
545
390
- // data tables assets.
391
- wp_register_script ( 'visualizer-datatables ' , '//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js ' , array ( 'jquery-ui-core ' ), Visualizer_Plugin::VERSION );
392
- wp_register_style ( 'visualizer-datatables ' , '//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css ' , array (), Visualizer_Plugin::VERSION );
393
- wp_register_style ( 'visualizer-datatables-ui ' , '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css ' , array ( 'visualizer-datatables ' ), Visualizer_Plugin::VERSION );
394
-
395
- wp_enqueue_script ( 'visualizer-datatables ' );
396
- wp_enqueue_style ( 'visualizer-datatables-ui ' );
397
-
398
546
if ( version_compare ( $ wp_version , '4.9.0 ' , '< ' ) ) {
399
547
// code mirror assets.
400
548
wp_register_script ( 'visualizer-codemirror-core ' , '//codemirror.net/lib/codemirror.js ' , array ( 'jquery ' ), Visualizer_Plugin::VERSION );
@@ -485,6 +633,7 @@ private function _handleDataAndSettingsPage() {
485
633
'l10n ' => array (
486
634
'invalid_source ' => esc_html__ ( 'You have entered invalid URL. Please, insert proper URL. ' , 'visualizer ' ),
487
635
'loading ' => esc_html__ ( 'Loading... ' , 'visualizer ' ),
636
+ 'json_error ' => esc_html__ ( 'An error occured in fetching data. ' , 'visualizer ' ),
488
637
),
489
638
'charts ' => array (
490
639
'canvas ' => $ data ,
@@ -497,17 +646,25 @@ private function _handleDataAndSettingsPage() {
497
646
'nonces ' => array (
498
647
'permissions ' => wp_create_nonce ( Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA ),
499
648
'db_get_data ' => wp_create_nonce ( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION ),
649
+ 'json_get_roots ' => wp_create_nonce ( Visualizer_Plugin::ACTION_JSON_GET_ROOTS . Visualizer_Plugin::VERSION ),
650
+ 'json_get_data ' => wp_create_nonce ( Visualizer_Plugin::ACTION_JSON_GET_DATA . Visualizer_Plugin::VERSION ),
651
+ 'json_set_schedule ' => wp_create_nonce ( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE . Visualizer_Plugin::VERSION ),
500
652
),
501
653
'actions ' => array (
502
654
'permissions ' => Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA ,
503
655
'db_get_data ' => Visualizer_Plugin::ACTION_FETCH_DB_DATA ,
656
+ 'json_get_roots ' => Visualizer_Plugin::ACTION_JSON_GET_ROOTS ,
657
+ 'json_get_data ' => Visualizer_Plugin::ACTION_JSON_GET_DATA ,
658
+ 'json_set_schedule ' => Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE ,
504
659
),
505
660
),
506
661
'db_query ' => array (
507
662
'tables ' => $ table_col_mapping ,
508
663
),
509
664
'is_pro ' => VISUALIZER_PRO ,
510
665
'page_type ' => 'chart ' ,
666
+ 'json_tag_separator ' => Visualizer_Source_Json::TAG_SEPARATOR ,
667
+ 'json_tag_separator_view ' => Visualizer_Source_Json::TAG_SEPARATOR_VIEW ,
511
668
'is_front ' => false ,
512
669
)
513
670
);
@@ -632,6 +789,11 @@ public function uploadData() {
632
789
delete_post_meta ( $ chart_id , Visualizer_Plugin::CF_DB_SCHEDULE );
633
790
}
634
791
792
+ // delete json related data.
793
+ delete_post_meta ( $ chart_id , Visualizer_Plugin::CF_JSON_URL );
794
+ delete_post_meta ( $ chart_id , Visualizer_Plugin::CF_JSON_ROOT );
795
+ delete_post_meta ( $ chart_id , Visualizer_Plugin::CF_JSON_PAGING );
796
+
635
797
$ source = null ;
636
798
$ render = new Visualizer_Render_Page_Update ();
637
799
if ( isset ( $ _POST ['remote_data ' ] ) && filter_var ( $ _POST ['remote_data ' ], FILTER_VALIDATE_URL ) ) {
0 commit comments