PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.83
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.83
5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 5.5.43 All 159 releases
wp-data-access / admin / class-wp-data-access-admin.php

class-wp-data-access-admin.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.83, at admin/class-wp-data-access-admin.php

1,105 lines 37.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Suppress "error - 0 - No summary was found for this file" on phpdoc generation
5 *
6 * @package plugin\admin
7 */
8 use WPDataAccess\API\WPDA_API;
9 use WPDataAccess\API\WPDA_API_Core;
10 use WPDataAccess\Backup\WPDA_Data_Export;
11 use WPDataAccess\CSV_Files\WPDA_CSV_Import;
12 use WPDataAccess\Dashboard\WPDA_Dashboard;
13 use WPDataAccess\Data_Apps\WPDA_App_Builder;
14 use WPDataAccess\Data_Apps\WPDA_Data_Explorer;
15 use WPDataAccess\Data_Apps\WPDA_Table_Container;
16 use WPDataAccess\Global_Search\WPDA_Global_Search;
17 use WPDataAccess\List_Table\WPDA_List_View;
18 use WPDataAccess\Plugin_Table_Models\WPDA_App_Model;
19 use WPDataAccess\Plugin_Table_Models\WPDA_Design_Table_Model;
20 use WPDataAccess\Plugin_Table_Models\WPDA_Publisher_Model;
21 use WPDataAccess\Plugin_Table_Models\WPDA_User_Menus_Model;
22 use WPDataAccess\Premium\WPDAPRO_Data_Publisher\WPDAPRO_Data_Publisher_Manage_Styles;
23 use WPDataAccess\Settings\WPDA_Settings;
24 use WPDataAccess\Utilities\WPDA_Add_App_To_Menu;
25 use WPDataAccess\Utilities\WPDA_Repository;
26 use WPDataAccess\WPDA;
27 use WPDataAccess\WPDA_Navi\WPDA_Navi;
28 use WPDataProjects\WPDP;
29 /**
30 * Class WP_Data_Access_Admin
31 *
32 * Defines admin specific functionality for plugin WP Data Access.
33 *
34 * @author Peter Schulz
35 * @since 1.0.0
36 */
37 // phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing -- verified on page
38 class WP_Data_Access_Admin {
39 /**
40 * Menu slug for main page
41 */
42 const PAGE_MAIN = 'wpda';
43
44 /**
45 * Menu slug for navigator page
46 */
47 const PAGE_NAVI = 'wpda_navi';
48
49 /**
50 * Menu slug for apps page
51 */
52 const PAGE_APPS = 'wpda_apps';
53
54 /**
55 * Menu slug for dashboard page
56 */
57 const PAGE_DASHBOARD = 'wpda_dashboard';
58
59 /**
60 * Menu slug for setting page
61 */
62 const PAGE_SETTINGS = 'wpdataaccess';
63
64 /**
65 * Menu slug for explorer page
66 */
67 const PAGE_EXPLORER = 'wpda_explorer';
68
69 /**
70 * Menu slug for query builder page
71 */
72 const PAGE_QUERY_BUILDER = 'wpda_query_builder';
73
74 /**
75 * Menu slug for Data Tables page
76 */
77 const PAGE_PUBLISHER = 'wpda_publisher';
78
79 /**
80 * Menu slug for charts page
81 */
82 const PAGE_CHARTS = 'wpda_charts';
83
84 /**
85 * Menu slug for designer page
86 */
87 const PAGE_DESIGNER = 'wpda_designer';
88
89 /**
90 * Plugin main page slug
91 */
92 const MAIN_PAGE_SLUG = self::PAGE_NAVI;
93
94 const PAGE_TABLE = 'wpda_table';
95
96 /**
97 * Menu slug for my tables page
98 */
99 const PAGE_MY_TABLES = 'wpda_my_tables';
100
101 /**
102 * Page hook suffix to Data Explorer page or false
103 *
104 * @var string|false
105 */
106 protected $wpda_data_explorer_menu;
107
108 protected $wpda_table;
109
110 /**
111 * Page hook suffix to Data Designer page or false
112 *
113 * @var string|false
114 */
115 protected $wpda_data_designer_menu;
116
117 /**
118 * Page hook suffix to Data Tables page or false
119 *
120 * @var string|false
121 */
122 protected $wpda_data_publisher_menu;
123
124 /**
125 * Page hook suffix to Charts page or false
126 *
127 * @var string|false
128 */
129 protected $wpda_charts_menu;
130
131 /**
132 * Reference to list view for Data Explorer page
133 *
134 * @var WPDA_List_View
135 */
136 protected $wpda_data_explorer_view;
137
138 /**
139 * Reference to list view for Data Designer page
140 *
141 * @var WPDA_List_View
142 */
143 protected $wpda_data_designer_view;
144
145 /**
146 * Reference to list view for Data Tables page
147 *
148 * @var WPDA_List_View
149 */
150 protected $wpda_data_publisher_view;
151
152 /**
153 * Reference to list view for Charts page
154 *
155 * @var WPDA_List_View
156 */
157 protected $wpda_charts_view;
158
159 /**
160 * Array of page hook suffixes to user defined sub menus
161 *
162 * @var array
163 */
164 protected $wpda_my_table_list_menu = array();
165
166 /**
167 * Array of list view for user defined sub menus
168 *
169 * @var array
170 */
171 protected $wpda_my_table_list_view = array();
172
173 /**
174 * Page hook suffix help page or false
175 *
176 * @var string|false
177 */
178 protected $wpda_help;
179
180 /**
181 * Menu slug or null
182 *
183 * @var null
184 */
185 protected $page = null;
186
187 /**
188 * Status loading indicator
189 *
190 * @var bool
191 */
192 protected $loaded_user_main_menu = false;
193
194 /**
195 * Main menu page
196 *
197 * @var null
198 */
199 protected $first_page = null;
200
201 /**
202 * WP_Data_Access_Admin constructor
203 *
204 * @since 1.0.0
205 */
206 public function __construct() {
207 if ( isset( $_REQUEST['page'] ) ) {
208 $this->page = sanitize_text_field( wp_unslash( $_REQUEST['page'] ) );
209 }
210 }
211
212 private function load_app_css() {
213 // WPDataAccess apps CSS.
214 wp_register_style(
215 'wpda_apps',
216 plugins_url( '../assets/css/wpda_apps.css', __FILE__ ),
217 array(),
218 WPDA::get_option( WPDA::OPTION_WPDA_VERSION )
219 );
220 }
221
222 /**
223 * Add stylesheets to back-end
224 *
225 * The following stylesheets are added:
226 * + Plugin stylesheet
227 * + Visual editor stylesheet
228 *
229 * The plugin stylesheet is used to style the setting forms {@see WPDA_Settings}, simple forms
230 * {@see \WPDataAccess\Simple_Form\WPDA_Simple_Form}.
231 *
232 * @since 1.0.0
233 *
234 * @see WPDA_Settings
235 * @see \WPDataAccess\Simple_Form\WPDA_Simple_Form
236 * @see WP_Data_Access_Public
237 */
238 public function enqueue_styles() {
239 if ( !WPDA::is_plugin_page( $this->page ) ) {
240 // Admin styles are only added to plugin admin pages.
241 // App pages still require the apps.css file.
242 $apps = WPDA_App_Model::add_to_dashboard_menu();
243 $css_added = false;
244 for ($i = 0; $i < count( $apps ) && !$css_added; $i++) {
245 $settings = json_decode( $apps[$i]['app_settings'], true );
246 if ( isset( $settings['settings']['app_menu_title'] ) && $this->page === $settings['settings']['app_menu_title'] ) {
247 $this->load_app_css();
248 $css_added = true;
249 }
250 }
251 return;
252 }
253 if ( self::PAGE_NAVI === $this->page ) {
254 // WPDataAccess CSS.
255 wp_enqueue_style(
256 'wpdanavi',
257 plugins_url( '../assets/css/wpda_navi.css', __FILE__ ),
258 array(),
259 WPDA::get_option( WPDA::OPTION_WPDA_VERSION )
260 );
261 }
262 wp_enqueue_style( 'wp-jquery-ui-core' );
263 wp_enqueue_style( 'wp-jquery-ui-dialog' );
264 wp_enqueue_style( 'wp-jquery-ui-sortable' );
265 wp_enqueue_style( 'wp-jquery-ui-tabs' );
266 $this->load_app_css();
267 // WPDataAccess CSS.
268 wp_enqueue_style(
269 'wpdataaccess',
270 plugins_url( '../assets/css/wpda_style.css', __FILE__ ),
271 array(),
272 WPDA::get_option( WPDA::OPTION_WPDA_VERSION )
273 );
274 // WPDataAccess dashboard.
275 wp_register_style(
276 'wpdataaccess_dashboard',
277 plugins_url( '../assets/css/wpda_dashboard.css', __FILE__ ),
278 array(),
279 WPDA::get_option( WPDA::OPTION_WPDA_VERSION )
280 );
281 // Add WP Data Projects stylesheet.
282 wp_enqueue_style(
283 'wpdataprojects',
284 plugins_url( '../WPDataProjects/assets/css/wpdp_style.css', __FILE__ ),
285 array(),
286 WPDA::get_option( WPDA::OPTION_WPDA_VERSION )
287 );
288 // Register datetimepicker external library.
289 wp_register_style(
290 'datetimepicker',
291 plugins_url( '../assets/css/jquery.datetimepicker.min.css', __FILE__ ),
292 array(),
293 WPDA::get_option( WPDA::OPTION_WPDA_VERSION )
294 );
295 // Register JQuery DataTables to use data tables in the dashboard.
296 wp_register_style(
297 'jquery_datatables',
298 plugins_url( '../assets/css/jquery.dataTables.min.css', __FILE__ ),
299 array(),
300 WPDA::get_option( WPDA::OPTION_WPDA_VERSION )
301 );
302 // Register JQuery DataTables Responsive to use data tables in the dashboard.
303 wp_register_style(
304 'jquery_datatables_responsive',
305 plugins_url( '../assets/css/responsive.dataTables.min.css', __FILE__ ),
306 array(),
307 WPDA::get_option( WPDA::OPTION_WPDA_VERSION )
308 );
309 if ( self::PAGE_MAIN === $this->page || self::PAGE_APPS === $this->page || self::PAGE_DASHBOARD === $this->page || self::PAGE_PUBLISHER === $this->page || self::PAGE_CHARTS === $this->page ) {
310 // Load UI smoothness theme.
311 wp_enqueue_style(
312 'wpda_ui_smoothness',
313 plugins_url( '../assets/css/jquery-ui.smoothness.min.css', __FILE__ ),
314 array(),
315 WPDA::get_option( WPDA::OPTION_WPDA_VERSION )
316 );
317 } else {
318 // Load UI darkness theme.
319 wp_enqueue_style(
320 'wpda_ui_darkness',
321 plugins_url( '../assets/css/jquery-ui.min.css', __FILE__ ),
322 array(),
323 WPDA::get_option( WPDA::OPTION_WPDA_VERSION )
324 );
325 }
326 $hide_button_icons = WPDA::get_option( WPDA::OPTION_BE_HIDE_BUTTON_ICONS );
327 if ( false !== $hide_button_icons && 'on' !== $hide_button_icons ) {
328 // SAVING SPACE - According to the plugin guidelines it is allowed to include external fonts:
329 // https://developer.wordpress.org/plugins/wordpress-org/detailed-plugin-guidelines/#8-plugins-may-not-send-executable-code-via-third-party-systems .
330 // Load fontawesome icons.
331 // phpcs:disable WordPress.WP.EnqueuedResourceParameters.MissingVersion
332 wp_enqueue_style(
333 'wpda_fontawesome_icons',
334 WPDA::CDN_FONTAWESOME . 'all.min.css',
335 array(),
336 null,
337 false
338 );
339 // phpcs:enable WordPress.WP.EnqueuedResourceParameters.MissingVersion
340 }
341 if ( self::PAGE_PUBLISHER === $this->page ) {
342 wp_register_style(
343 'wpda_datatables_default',
344 plugins_url( '../assets/css/wpda_datatables_default.css', __FILE__ ),
345 array(),
346 WPDA::get_option( WPDA::OPTION_WPDA_VERSION )
347 );
348 // Add jQuery multiselect resources.
349 wp_enqueue_style(
350 'wpda_jquery_multiselect',
351 plugins_url( '../assets/css/jquery.multiselect.sortable.js.css', __FILE__ ),
352 array(),
353 WPDA::get_option( WPDA::OPTION_WPDA_VERSION ),
354 false
355 );
356 }
357 if ( !WPDA::current_user_is_admin() ) {
358 wp_enqueue_style(
359 'wpda_non_admin',
360 plugins_url( '../assets/css/wpda_non_admin.css', __FILE__ ),
361 array(),
362 WPDA::get_option( WPDA::OPTION_WPDA_VERSION )
363 );
364 }
365 }
366
367 /**
368 * Remove icons from buttons (if configured)
369 *
370 * @return void
371 */
372 public function remove_icons() {
373 if ( !WPDA::is_plugin_page( $this->page ) ) {
374 // Only executed on plugin pages.
375 return;
376 }
377 if ( 'on' === WPDA::get_option( WPDA::OPTION_BE_HIDE_BUTTON_ICONS ) ) {
378 echo '
379 <style>
380 i.fas.wpda_icon_on_button {
381 display: none;
382 }
383 </style>
384 ';
385 }
386 }
387
388 /**
389 * Add scripts to back-end
390 *
391 * @since 1.0.0
392 *
393 * @see WP_Data_Access_Public
394 */
395 public function enqueue_scripts() {
396 if ( !WPDA::is_plugin_page( $this->page ) ) {
397 // Admin styles are only added to plugin admin pages.
398 return;
399 }
400 wp_enqueue_script( 'jquery-ui-core' );
401 wp_enqueue_script( 'jquery-ui-dialog' );
402 wp_enqueue_script( 'jquery-ui-tabs' );
403 wp_enqueue_script( 'jquery-ui-sortable' );
404 wp_enqueue_script( 'jquery-ui-tooltip' );
405 wp_enqueue_script( 'jquery-ui-autocomplete' );
406 // Register wpda rest api.
407 wp_enqueue_script(
408 'wpda_rest_api',
409 plugins_url( '../assets/js/wpda_rest_api.js', __FILE__ ),
410 array('wp-api'),
411 WPDA::get_option( WPDA::OPTION_WPDA_VERSION ),
412 false
413 );
414 wp_localize_script( 'wpda_rest_api', 'wpdaApiSettings', array(
415 'path' => WPDA_API::WPDA_NAMESPACE,
416 ) );
417 // Register wpda admin functions.
418 wp_enqueue_script(
419 'wpda_admin_scripts',
420 plugins_url( '../assets/js/wpda_admin.js', __FILE__ ),
421 array(),
422 WPDA::get_option( WPDA::OPTION_WPDA_VERSION ),
423 false
424 );
425 wp_localize_script( 'wpda_admin_scripts', 'wpda_admin_vars', array(
426 'wpda_ajaxurl' => admin_url( 'admin-ajax.php' ),
427 ) );
428 // Register dashboard.
429 wp_register_script(
430 'wpdataaccess_dashboard',
431 plugins_url( '../assets/js/wpda_dashboard.js', __FILE__ ),
432 array(),
433 WPDA::get_option( WPDA::OPTION_WPDA_VERSION ),
434 false
435 );
436 wp_localize_script( 'wpdataaccess_dashboard', 'wpda_dashboard_vars', array(
437 'wpda_ajaxurl' => admin_url( 'admin-ajax.php' ),
438 ) );
439 // Add WP Data Projects JS functions.RESEARCH.
440 wp_enqueue_script(
441 'wpdataprojects',
442 plugins_url( '../WPDataProjects/assets/js/wpdp_admin.js', __FILE__ ),
443 array(),
444 WPDA::get_option( WPDA::OPTION_WPDA_VERSION ),
445 false
446 );
447 // Register jQuery DataTables to use data tables in the dashboard.
448 wp_register_script(
449 'jquery_datatables',
450 plugins_url( '../assets/js/jquery.dataTables.min.js', __FILE__ ),
451 array(),
452 WPDA::get_option( WPDA::OPTION_WPDA_VERSION ),
453 false
454 );
455 // Register jQuery DataTables Responsive to use data tables in the dashboard.
456 wp_register_script(
457 'jquery_datatables_responsive',
458 plugins_url( '../assets/js/dataTables.responsive.min.js', __FILE__ ),
459 array(),
460 WPDA::get_option( WPDA::OPTION_WPDA_VERSION ),
461 false
462 );
463 // Ajax call to WPDA datatables implementation to use data tables in the dashboard.
464 wp_register_script(
465 'wpda_datatables',
466 plugins_url( '../assets/js/wpda_datatables.js', __FILE__ ),
467 array(),
468 WPDA::get_option( WPDA::OPTION_WPDA_VERSION ),
469 false
470 );
471 wp_localize_script( 'wpda_datatables', 'wpda_publication_vars', array(
472 'wpda_ajaxurl' => admin_url( 'admin-ajax.php' ),
473 ) );
474 // Register notify external library.
475 wp_enqueue_script(
476 'wpda_notify',
477 plugins_url( '../assets/js/notify.min.js', __FILE__ ),
478 array(),
479 WPDA::get_option( WPDA::OPTION_WPDA_VERSION ),
480 false
481 );
482 // Register datetimepicker external library.
483 wp_register_script(
484 'datetimepicker',
485 plugins_url( '../assets/js/jquery.datetimepicker.full.min.js', __FILE__ ),
486 array(),
487 WPDA::get_option( WPDA::OPTION_WPDA_VERSION ),
488 false
489 );
490 // Register clipboard.js.
491 wp_enqueue_script( 'clipboard' );
492 if ( self::PAGE_PUBLISHER === $this->page ) {
493 if ( isset( $_REQUEST['action'] ) && in_array( $_REQUEST['action'], array('view', 'new', 'edit'), true ) ) {
494 $json_editing = WPDA::get_option( WPDA::OPTION_DP_JSON_EDITING );
495 if ( WPDA::OPTION_DP_JSON_EDITING[1] === $json_editing ) {
496 // Register codeEditor to support JSON editing in Data Tables (table options advanced).
497 $cm_settings['codeEditor'] = wp_enqueue_code_editor( array(
498 'type' => 'application/json',
499 'codemirror' => array(
500 'autoRefresh' => true,
501 ),
502 ) );
503 wp_localize_script( 'wp-theme-plugin-editor', 'cm_settings', $cm_settings );
504 wp_enqueue_script( 'wp-theme-plugin-editor' );
505 }
506 // Add jQuery multiselect resources.
507 wp_enqueue_script(
508 'wpda_jquery_multiselect',
509 plugins_url( '../assets/js/jquery.multiselect.sortable.js', __FILE__ ),
510 array(),
511 WPDA::get_option( WPDA::OPTION_WPDA_VERSION ),
512 false
513 );
514 }
515 }
516 if ( self::PAGE_CHARTS === $this->page ) {
517 $this->load_google_charts();
518 }
519 if ( self::PAGE_DASHBOARD === $this->page ) {
520 $this->load_google_charts();
521 // Load DBMS panels.
522 wp_enqueue_script(
523 'wpda_dbms',
524 plugins_url( '../assets/js/wpda_dbms.js', __FILE__ ),
525 array(),
526 WPDA::get_option( WPDA::OPTION_WPDA_VERSION ),
527 false
528 );
529 wp_localize_script( 'wpda_dbms', 'wpda_dbms_vars', array(
530 'wpda_ajaxurl' => admin_url( 'admin-ajax.php' ),
531 ) );
532 }
533 wp_enqueue_media();
534 }
535
536 /**
537 * Load Google Charts resources
538 *
539 * @return void
540 */
541 private function load_google_charts() {
542 // Load Google Charts.
543 // phpcs:disable WordPress.WP.EnqueuedResourceParameters
544 wp_enqueue_script(
545 'wpda_google_charts',
546 WPDA::GOOGLE_CHARTS,
547 array(),
548 null,
549 false
550 );
551 // phpcs:enable WordPress.WP.EnqueuedResourceParameters
552 wp_enqueue_script(
553 'wpda_google_charts_fnc',
554 plugins_url( '../assets/js/wpda_google_charts.js', __FILE__ ),
555 array(),
556 WPDA::get_option( WPDA::OPTION_WPDA_VERSION ),
557 false
558 );
559 wp_localize_script( 'wpda_google_charts_fnc', 'wpda_chart_vars', array(
560 'wpda_ajaxurl' => admin_url( 'admin-ajax.php' ),
561 'wpda_chartdir' => plugin_dir_url( __FILE__ ) . '../assets/images/google_chart_types/',
562 'wpda_premium' => ( wpda_freemius()->can_use_premium_code__premium_only() ? 'true' : 'false' ),
563 ) );
564 }
565
566 /**
567 * Hide admin notices
568 *
569 * @return void
570 */
571 public function user_admin_notices() {
572 if ( WPDA::is_plugin_page( $this->page ) && 'on' === WPDA::get_option( WPDA::OPTION_PLUGIN_HIDE_NOTICES ) ) {
573 remove_all_actions( 'admin_notices' );
574 remove_all_actions( 'all_admin_notices' );
575 }
576 }
577
578 /**
579 * Add plugin menu and sub menus
580 *
581 * Adds the following menu and sub menus to the back-end menu:
582 * + WP Data Access
583 * + Data Explorer
584 * + Data Designer
585 * + Data Projects
586 * + Manage Plugin
587 *
588 * @since 1.0.0
589 */
590 public function add_menu_items() {
591 if ( WPDA::current_user_is_admin() ) {
592 if ( 'on' === WPDA::get_option( WPDA::OPTION_PLUGIN_HIDE_ADMIN_MENU ) ) {
593 // Show Data Projects.
594 $this->add_data_projects();
595 // Hide admin menu.
596 return;
597 }
598 // Main menu and items are only available to admin users (set capability to 'manage_options').
599 add_menu_page(
600 'WP Data Access',
601 'WP Data Access',
602 'manage_options',
603 self::MAIN_PAGE_SLUG,
604 null,
605 'dashicons-database',
606 999999999
607 );
608 // Add navigator page to WPDA menu.
609 add_submenu_page(
610 self::MAIN_PAGE_SLUG,
611 'WP Data Access',
612 'Tool Guide',
613 'manage_options',
614 self::PAGE_NAVI,
615 array($this, 'data_navi')
616 );
617 // Add apps to menu.
618 add_submenu_page(
619 self::MAIN_PAGE_SLUG,
620 'WP Data Access',
621 'App Builder',
622 'manage_options',
623 self::PAGE_APPS,
624 array($this, 'data_apps')
625 );
626 // Add data explorer to WPDA menu.
627 $this->wpda_data_explorer_menu = add_submenu_page(
628 self::MAIN_PAGE_SLUG,
629 'WP Data Access',
630 'Data Explorer',
631 'manage_options',
632 self::PAGE_MAIN,
633 array($this, 'data_explorer_page')
634 );
635 // Add data explorer to WPDA menu.
636 $this->wpda_table = add_submenu_page(
637 self::PAGE_TABLE,
638 'WP Data Access',
639 'Data Explorer',
640 'manage_options',
641 self::PAGE_TABLE,
642 array($this, 'data_explorer_table_page')
643 );
644 // Add submenu for Query Builder.
645 $this->wpda_data_publisher_menu = add_submenu_page(
646 self::MAIN_PAGE_SLUG,
647 'WP Data Access',
648 'SQL Query Builder',
649 'manage_options',
650 self::PAGE_QUERY_BUILDER,
651 array($this, 'query_builder')
652 );
653 // Add submenu for Data Tables.
654 $this->wpda_data_publisher_menu = add_submenu_page(
655 self::MAIN_PAGE_SLUG,
656 'WP Data Access',
657 'Table Builder',
658 'manage_options',
659 self::PAGE_PUBLISHER,
660 array($this, 'data_publisher_page')
661 );
662 if ( self::PAGE_PUBLISHER === $this->page ) {
663 $this->wpda_data_publisher_view = new WPDA_List_View(array(
664 'page_hook_suffix' => $this->wpda_data_publisher_menu,
665 'table_name' => WPDA_Publisher_Model::get_base_table_name(),
666 'list_table_class' => 'WPDataAccess\\Data_Publisher\\WPDA_Publisher_List_Table',
667 'edit_form_class' => 'WPDataAccess\\Data_Publisher\\WPDA_Publisher_Form',
668 ));
669 }
670 // Add Data Projects menu.
671 $wpdp = new WPDP(self::MAIN_PAGE_SLUG);
672 $wpdp->add_menu_items();
673 // Add data designer to WPDA menu.
674 $this->wpda_data_designer_menu = add_submenu_page(
675 self::MAIN_PAGE_SLUG,
676 'WP Data Access',
677 'Data Designer',
678 'manage_options',
679 self::PAGE_DESIGNER,
680 array($this, 'data_designer_page')
681 );
682 if ( self::PAGE_DESIGNER === $this->page ) {
683 $this->wpda_data_designer_view = new WPDA_List_View(array(
684 'page_hook_suffix' => $this->wpda_data_designer_menu,
685 'table_name' => WPDA_Design_Table_Model::get_base_table_name(),
686 'list_table_class' => 'WPDataAccess\\Design_Table\\WPDA_Design_Table_List_Table',
687 'edit_form_class' => 'WPDataAccess\\Design_Table\\WPDA_Design_Table_Form',
688 'subtitle' => '',
689 ));
690 }
691 } else {
692 $this->grant_access_to_dashboard();
693 $this->grant_access_to_data_publications();
694 }
695 $this->add_data_projects();
696 $this->add_apps();
697 }
698
699 /**
700 * Remove plugin sub menu items (when in dashboard mode)
701 *
702 * @param mixed $submenu_file Dashboard menu items.
703 * @return mixed
704 */
705 public function wpda_submenu_filter( $submenu_file ) {
706 if ( WPDA::current_user_is_admin() ) {
707 $hidden_submenus = array(
708 self::PAGE_DASHBOARD,
709 self::PAGE_CHARTS,
710 self::PAGE_PUBLISHER,
711 WPDP::PAGE_MAIN,
712 self::PAGE_DESIGNER,
713 WPDP::PAGE_TEMPLATES,
714 self::MAIN_PAGE_SLUG . '-account',
715 self::MAIN_PAGE_SLUG . '-wp-support-forum'
716 );
717 foreach ( $hidden_submenus as $submenu ) {
718 remove_submenu_page( self::MAIN_PAGE_SLUG, $submenu );
719 }
720 } else {
721 global $submenu;
722 $submenu[self::PAGE_MAIN][0][2] = self::PAGE_DASHBOARD;
723 // phpcs:ignore WordPress.WP.GlobalVariablesOverride
724 }
725 return $submenu_file;
726 }
727
728 /**
729 * Add apps to dashboard menu.
730 *
731 * @return void
732 */
733 protected function add_apps() {
734 // Add apps to dashbaord menu
735 $apps = new WPDA_Add_App_To_Menu();
736 $apps->add_apps_to_menu();
737 }
738
739 /**
740 * Add Data Projects and Project Templates to plugin navigation
741 *
742 * @return void
743 */
744 protected function add_data_projects() {
745 // Add Data Projects.
746 $wpdp = new WPDP();
747 $wpdp->add_projects();
748 }
749
750 /**
751 * Allow authorized users to access WP Data Access dashboard
752 *
753 * @return void
754 */
755 protected function grant_access_to_dashboard() {
756 // Check user role.
757 $user_roles = WPDA::get_current_user_roles();
758 if ( false === $user_roles || !is_array( $user_roles ) ) {
759 // Cannot determine the user roles (not able to show menus).
760 return;
761 }
762 // Check dashboard role access.
763 $dashboard_roles = get_option( \WPDataAccess\Settings\WPDA_Settings_Legacy_Dashboard::DASHBOARD_ROLES );
764 $user_has_role = false;
765 foreach ( $user_roles as $user_role ) {
766 if ( false !== strpos( $dashboard_roles, $user_role ) ) {
767 $user_has_role = true;
768 break;
769 }
770 }
771 // Check dashboard user access.
772 $dashboard_users = get_option( \WPDataAccess\Settings\WPDA_Settings_Legacy_Dashboard::DASHBOARD_USERS );
773 $user_has_access = false !== strpos( $dashboard_users, WPDA::get_current_user_login() );
774 if ( !$user_has_role && !$user_has_access ) {
775 return;
776 }
777 // User has dashboard access: add menu item.
778 $this->create_non_admin_menu( self::PAGE_DASHBOARD );
779 add_submenu_page(
780 $this->first_page,
781 'Dashboard',
782 'Dashboard',
783 WPDA::get_current_user_capability(),
784 self::PAGE_DASHBOARD,
785 array($this, 'data_dashboard_page')
786 );
787 }
788
789 /**
790 * Allow authorized users to access Data Tables
791 *
792 * @return void
793 */
794 protected function grant_access_to_data_publications() {
795 // Check user role.
796 $user_roles = WPDA::get_current_user_roles();
797 if ( false === $user_roles || !is_array( $user_roles ) ) {
798 // Cannot determine the user roles (not able to show menus).
799 return;
800 }
801 $publication_roles = WPDA::get_option( WPDA::OPTION_DP_PUBLICATION_ROLES );
802 if ( '' === $publication_roles || 'administrator' === $publication_roles ) {
803 // No access.
804 return;
805 }
806 $user_has_role = false;
807 foreach ( $user_roles as $user_role ) {
808 if ( false !== stripos( $publication_roles, $user_role ) ) {
809 $user_has_role = true;
810 }
811 }
812 if ( !$user_has_role ) {
813 // No access.
814 return;
815 }
816 // Grant access to main menu.
817 $this->create_non_admin_menu( self::PAGE_PUBLISHER );
818 // Add submenu for Data Tables.
819 $this->wpda_data_publisher_menu = add_submenu_page(
820 $this->first_page,
821 'WP Data Access',
822 'Data Tables',
823 WPDA::get_current_user_capability(),
824 self::PAGE_PUBLISHER,
825 array($this, 'data_publisher_page')
826 );
827 if ( self::PAGE_PUBLISHER === $this->page ) {
828 global $wpdb;
829 $this->wpda_data_publisher_view = new WPDA_List_View(array(
830 'page_hook_suffix' => $this->wpda_data_publisher_menu,
831 'table_name' => $wpdb->prefix . 'wpda_publisher',
832 'list_table_class' => 'WPDataAccess\\Data_Publisher\\WPDA_Publisher_List_Table',
833 'edit_form_class' => 'WPDataAccess\\Data_Publisher\\WPDA_Publisher_Form',
834 ));
835 }
836 }
837
838 /**
839 * Add dashboard page
840 *
841 * @return void
842 */
843 public function data_dashboard_page() {
844 WPDA_Dashboard::add_dashboard( true );
845 }
846
847 public function data_explorer_page() {
848 WPDA_Dashboard::add_dashboard();
849 if ( isset( $_REQUEST['page_action'] ) && 'wpda_backup' === $_REQUEST['page_action'] ) {
850 $this->backup_page();
851 } elseif ( isset( $_REQUEST['page_action'] ) && 'wpda_import_csv' === $_REQUEST['page_action'] ) {
852 $this->import_csv();
853 } elseif ( isset( $_REQUEST['page_action'] ) && 'wpda_global_search' === $_REQUEST['page_action'] ) {
854 $this->advanced_search();
855 } else {
856 $explorer = new WPDA_Data_Explorer();
857 $explorer->show();
858 }
859 }
860
861 public function data_explorer_table_page() {
862 WPDA_Dashboard::add_dashboard();
863 if ( !isset(
864 $_POST['dbs'],
865 $_POST['tbl'],
866 $_POST['s'],
867 $_POST['c']
868 ) ) {
869 wp_die( esc_attr__( 'ERROR: Invalid usage', 'wp-data-access' ) );
870 }
871 $dbs = WPDA_API_Core::sanitize_db_identifier( sanitize_text_field( wp_unslash( $_POST['dbs'] ) ) );
872 $tbl = WPDA_API_Core::sanitize_db_identifier( sanitize_text_field( wp_unslash( $_POST['tbl'] ) ) );
873 $s = WPDA_API_Core::sanitize_db_identifier( sanitize_text_field( wp_unslash( $_POST['s'] ) ) );
874 $c = WPDA_API_Core::sanitize_db_identifier( sanitize_text_field( wp_unslash( $_POST['c'] ) ) );
875 $explorer = new WPDA_Table_Container(array(
876 'dbs' => $dbs,
877 'tbl' => $tbl,
878 's' => $s,
879 'c' => $c,
880 ));
881 $explorer->show();
882 }
883
884 public function data_navi() {
885 if ( isset( $_POST['wpda-legacy-tool-status'] ) ) {
886 // Legacy tools update
887 $update_status = json_decode( sanitize_text_field( wp_unslash( $_POST['wpda-legacy-tool-status'] ) ), true );
888 if ( null !== $update_status ) {
889 if ( isset(
890 $update_status['tables'],
891 $update_status['forms'],
892 $update_status['templates'],
893 $update_status['designer'],
894 $update_status['dashboards'],
895 $update_status['charts']
896 ) ) {
897 // Get current legacy tool settings
898 $option_legacy_tools = \WPDataAccess\Utilities\WPDA_Legacy_Tool_Visibility::get();
899 // Update legacy tool settings
900 $option_legacy_tools['tables'][0] = $update_status['tables'];
901 $option_legacy_tools['forms'][0] = $update_status['forms'];
902 $option_legacy_tools['templates'][0] = $update_status['templates'];
903 $option_legacy_tools['designer'][0] = $update_status['designer'];
904 $option_legacy_tools['dashboards'][0] = $update_status['dashboards'];
905 $option_legacy_tools['charts'][0] = $update_status['charts'];
906 // Save legacy tool settings
907 WPDA::set_option( WPDA::OPTION_PLUGIN_LEGACY_TOOLS, $option_legacy_tools );
908 }
909 }
910 }
911 WPDA_Dashboard::add_dashboard();
912 $navi = new WPDA_Navi();
913 $navi->show();
914 }
915
916 public function data_apps() {
917 WPDA_Dashboard::add_dashboard();
918 $wpda_app_builder = new WPDA_App_Builder();
919 $wpda_app_builder->show();
920 }
921
922 public function advanced_search() {
923 $advanced_search = new WPDA_Global_Search();
924 $advanced_search->show();
925 }
926
927 /**
928 * Add Query Builder
929 *
930 * @return void
931 */
932 public function query_builder() {
933 WPDA_Dashboard::add_dashboard();
934 $query_builder = new \WPDataAccess\Data_Apps\WPDA_Query_Builder();
935 $query_builder->show();
936 }
937
938 /**
939 * Add CSV page
940 *
941 * @return void
942 */
943 public function import_csv() {
944 $csv_import = new WPDA_CSV_Import();
945 $csv_import->show();
946 }
947
948 /**
949 * Show data designer main page
950 *
951 * Initialization of $this->wpda_data_designer_view is done earlier in
952 * {@see WP_Data_Access_Admin::add_menu_items()} to support screen options. This method just shows the page
953 * containing the list table.
954 *
955 * @since 1.0.0
956 *
957 * @see WP_Data_Access_Admin::add_menu_items()
958 */
959 public function data_designer_page() {
960 WPDA_Dashboard::add_dashboard();
961 $data_designer_table_found = WPDA_Design_Table_Model::table_exists();
962 if ( $data_designer_table_found ) {
963 $this->wpda_data_designer_view->show();
964 } else {
965 $this->data_designer_page_not_found();
966 }
967 }
968
969 /**
970 * Data Designer repository table not found
971 */
972 public function data_designer_page_not_found() {
973 WPDA_Dashboard::add_dashboard();
974 $wpda_repository = new WPDA_Repository();
975 $wpda_repository->inform_user();
976 ?>
977 <div class="wrap">
978 <h1 class="wp-heading-inline">
979 <span>Data Designer</span>
980 <a href="https://docs.legacy.wpdataaccess.com/docs/data-designer-getting-started/" target="_blank" class="wpda_tooltip" title="Plugin Help - opens in a new tab or window">
981 <span class="dashicons dashicons-editor-help" style="text-decoration:none;vertical-align:top;font-size:30px;">
982 </span>
983 </a>
984 </h1>
985 <p>
986 <?php
987 esc_html_e( 'ERROR: Repository table not found!', 'wp-data-access' );
988 ?>
989 </p>
990 </div>
991 <?php
992 }
993
994 /**
995 * Show Data Tables main page
996 */
997 public function data_publisher_page() {
998 if ( WPDA::current_user_is_admin() ) {
999 WPDA_Dashboard::add_dashboard();
1000 }
1001 $data_publisher_table_found = WPDA_Publisher_Model::table_exists();
1002 if ( $data_publisher_table_found ) {
1003 $this->wpda_data_publisher_view->show();
1004 } else {
1005 $this->data_publisher_page_not_found();
1006 }
1007 }
1008
1009 public function data_publisher_mngstl() {
1010 $mngstl = new WPDAPRO_Data_Publisher_Manage_Styles();
1011 $mngstl->show();
1012 }
1013
1014 /**
1015 * Add Charts page
1016 *
1017 * @return void
1018 */
1019 public function data_charts_page() {
1020 }
1021
1022 /**
1023 * Data Tables repository table not found
1024 */
1025 public function data_publisher_page_not_found() {
1026 if ( WPDA::current_user_is_admin() ) {
1027 WPDA_Dashboard::add_dashboard();
1028 }
1029 $wpda_repository = new WPDA_Repository();
1030 $wpda_repository->inform_user();
1031 ?>
1032 <div class="wrap">
1033 <h1 class="wp-heading-inline">
1034 <span>Data Tables</span>
1035 <a href="https://docs.legacy.wpdataaccess.com/docs/data-tables-getting-started/" target="_blank" class="wpda_tooltip" title="Plugin Help - opens in a new tab or window">
1036 <span class="dashicons dashicons-editor-help" style="text-decoration:none;vertical-align:top;font-size:30px;">
1037 </span>
1038 </a>
1039 </h1>
1040 <p>
1041 <?php
1042 esc_html_e( 'ERROR: Repository table not found!', 'wp-data-access' );
1043 ?>
1044 </p>
1045 </div>
1046 <?php
1047 }
1048
1049 /**
1050 * Show data backup main page
1051 *
1052 * Calls a page to create automatic backups (in fact data exports) and offers possibilities to restore (in fact
1053 * data imports).
1054 *
1055 * @since 2.0.6
1056 *
1057 * @see WPDA_Data_Export::show_wp_cron()
1058 */
1059 public function backup_page() {
1060 $wpda_backup = new WPDA_Data_Export();
1061 $wp_nonce = ( isset( $_REQUEST['wp_nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wp_nonce'] ) ) : '?' );
1062 if ( isset( $_REQUEST['action'] ) && wp_verify_nonce( $wp_nonce, 'wpda-backup-' . WPDA::get_current_user_login() ) ) {
1063 $action = sanitize_text_field( wp_unslash( $_REQUEST['action'] ) );
1064 if ( 'new' === $action ) {
1065 $wpda_backup->create_export( 'add' );
1066 } elseif ( 'add' === $action ) {
1067 $wpda_backup->wpda_add_cron_job();
1068 } elseif ( 'remove' === $action ) {
1069 $wpda_backup->wpda_remove_cron_job();
1070 } elseif ( 'edit' === $action ) {
1071 $wpda_backup->create_export( 'update' );
1072 } elseif ( 'update' === $action ) {
1073 $wpda_backup->wpda_update_cron_job();
1074 }
1075 } else {
1076 $wpda_backup->show_wp_cron();
1077 }
1078 }
1079
1080 /**
1081 * Add main menu for non admin user
1082 *
1083 * @param string $first_page First plugin page.
1084 * @return void
1085 */
1086 protected function create_non_admin_menu( $first_page ) {
1087 if ( !$this->loaded_user_main_menu ) {
1088 add_menu_page(
1089 'WP Data Access',
1090 'WP Data Access',
1091 WPDA::get_current_user_capability(),
1092 $first_page,
1093 function () {
1094 },
1095 'dashicons-database-view',
1096 999999999
1097 );
1098 $this->loaded_user_main_menu = true;
1099 $this->first_page = $first_page;
1100 }
1101 }
1102
1103 }
1104
1105 // phpcs:enable WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing