PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.35
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.35
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.35, at admin/class-wp-data-access-admin.php

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