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

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