| 1 |
<?php |
| 2 |
/** |
| 3 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 4 |
* |
| 5 |
* @package plugin\admin |
| 6 |
*/ |
| 7 |
|
| 8 |
use WPDataAccess\Design_Table\WPDA_Design_Table_Model; |
| 9 |
use WPDataAccess\List_Table\WPDA_List_View; |
| 10 |
use WPDataAccess\Settings\WPDA_Settings; |
| 11 |
use WPDataAccess\User_Menu\WPDA_User_Menu_Model; |
| 12 |
use WPDataAccess\WPDA; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class WP_Data_Access_Admin |
| 16 |
* |
| 17 |
* Defines admin specific functionality for plugin WP Data Access. |
| 18 |
* |
| 19 |
* @package plugin\admin |
| 20 |
* @author Peter Schulz |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
class WP_Data_Access_Admin { |
| 24 |
|
| 25 |
/** |
| 26 |
* Menu slug for main page |
| 27 |
*/ |
| 28 |
const PAGE_MAIN = 'wpda'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Menu slug for setting page |
| 32 |
*/ |
| 33 |
const PAGE_SETTINGS = 'wpda_settings'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Menu slug for manage menus page |
| 37 |
*/ |
| 38 |
const PAGE_MENUS = 'wpda_menus'; |
| 39 |
|
| 40 |
/** |
| 41 |
* Menu slug for explorer page |
| 42 |
*/ |
| 43 |
const PAGE_EXPLORER = 'wpda_explorer'; |
| 44 |
|
| 45 |
/** |
| 46 |
* Menu slug for designer page |
| 47 |
*/ |
| 48 |
const PAGE_DESIGNER = 'wpda_designer'; |
| 49 |
|
| 50 |
/** |
| 51 |
* Menu slug for my tables page |
| 52 |
*/ |
| 53 |
const PAGE_MY_TABLES = 'wpda_my_tables'; |
| 54 |
|
| 55 |
/** |
| 56 |
* Page hook suffix to Manage Menus page or false |
| 57 |
* |
| 58 |
* @var string|false |
| 59 |
*/ |
| 60 |
protected $wpda_manage_menus_menu; |
| 61 |
|
| 62 |
/** |
| 63 |
* Reference to list view for Manage Menus page |
| 64 |
* |
| 65 |
* @var WPDA_List_View |
| 66 |
*/ |
| 67 |
protected $wpda_manage_menus_view; |
| 68 |
|
| 69 |
/** |
| 70 |
* Page hook suffix to Data Explorer page or false |
| 71 |
* |
| 72 |
* @var string|false |
| 73 |
*/ |
| 74 |
protected $wpda_data_explorer_menu; |
| 75 |
|
| 76 |
/** |
| 77 |
* Page hook suffix to Data Designer page or false |
| 78 |
* |
| 79 |
* @var string|false |
| 80 |
*/ |
| 81 |
protected $wpda_data_designer_menu; |
| 82 |
|
| 83 |
/** |
| 84 |
* Reference to list view for Data Explorer page |
| 85 |
* |
| 86 |
* @var WPDA_List_View |
| 87 |
*/ |
| 88 |
protected $wpda_data_explorer_view; |
| 89 |
|
| 90 |
/** |
| 91 |
* Reference to list view for Data Designer page |
| 92 |
* |
| 93 |
* @var WPDA_List_View |
| 94 |
*/ |
| 95 |
protected $wpda_data_designer_view; |
| 96 |
|
| 97 |
/** |
| 98 |
* Array of page hook suffixes to user defined sub menus |
| 99 |
* |
| 100 |
* @var array |
| 101 |
*/ |
| 102 |
protected $wpda_my_table_list_menu = []; |
| 103 |
|
| 104 |
/** |
| 105 |
* Array of list view for user defined sub menus |
| 106 |
* |
| 107 |
* @var array |
| 108 |
*/ |
| 109 |
protected $wpda_my_table_list_view = []; |
| 110 |
|
| 111 |
/** |
| 112 |
* Main menu title (dynamically set to support translations) |
| 113 |
* |
| 114 |
* @var string |
| 115 |
*/ |
| 116 |
protected $title_menu_menu; |
| 117 |
|
| 118 |
/** |
| 119 |
* Data explorer sub menu title (dynamically set to support translations) |
| 120 |
* |
| 121 |
* @var string |
| 122 |
*/ |
| 123 |
protected $title_submenu_explorer; |
| 124 |
|
| 125 |
/** |
| 126 |
* Data designer sub menu title (dynamically set to support translations) |
| 127 |
* |
| 128 |
* @var string |
| 129 |
*/ |
| 130 |
protected $title_submenu_designer; |
| 131 |
|
| 132 |
/** |
| 133 |
* Manage menus sub menu title (dynamically set to support translations) |
| 134 |
* |
| 135 |
* @var string |
| 136 |
*/ |
| 137 |
protected $title_submenu_menus; |
| 138 |
|
| 139 |
/** |
| 140 |
* Settings page sub menu title (dynamically set to support translations) |
| 141 |
* |
| 142 |
* @var string |
| 143 |
*/ |
| 144 |
protected $title_submenu_settings; |
| 145 |
|
| 146 |
/** |
| 147 |
* Menu slug or null |
| 148 |
* |
| 149 |
* @var null |
| 150 |
*/ |
| 151 |
protected $page = null; |
| 152 |
|
| 153 |
/** |
| 154 |
* My tables sub menu title (dynamically set to support translations) |
| 155 |
* |
| 156 |
* @var string |
| 157 |
*/ |
| 158 |
protected $title_menu_my_tables; |
| 159 |
|
| 160 |
/** |
| 161 |
* WP_Data_Access_Admin constructor |
| 162 |
* |
| 163 |
* @since 1.0.0 |
| 164 |
*/ |
| 165 |
public function __construct() { |
| 166 |
|
| 167 |
if ( isset( $_REQUEST['page'] ) ) { |
| 168 |
$this->page = sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ); // input var okay. |
| 169 |
} |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Handle plugin cookies |
| 175 |
* |
| 176 |
* Cookies are use to remember values during navigation: (max 1 hour) |
| 177 |
* 1) SCHEMA NAME |
| 178 |
* The schema name is saved as a cookie when changed in the Data Explorer. As long as the user stays within the |
| 179 |
* page the saved schema is used. When the user moves to another page the value is destroyed. The user gets the |
| 180 |
* default value on the next visit. |
| 181 |
* 2) FAVOURITE SELECTION |
| 182 |
* The favourite selection is saved as cookie when changed in the Data Explorer. As long as the user stays within |
| 183 |
* the page the saved selection is used. When the user moves to another page the value is destroyed. The user gets |
| 184 |
* the default value on the next visit. |
| 185 |
* 3) SEARCH ARGUMENT |
| 186 |
* Search arguments are saved as cookies per table. As long as the user stays within the same page the saved |
| 187 |
* search value is used. When the user moves to another page the value is destroyed. This allows users to navigate |
| 188 |
* between pages without losing the search value. |
| 189 |
* |
| 190 |
* @since 1.6.0 |
| 191 |
*/ |
| 192 |
public function handle_plugin_cookies() { |
| 193 |
if ( $this->page === self::PAGE_MAIN ) { |
| 194 |
// Handle Data Explorer cookies (search cookie is handled in next section). |
| 195 |
// Handle cookie to remember active schema. |
| 196 |
$cookie_name = self::PAGE_MAIN . '_schema_name'; |
| 197 |
if ( isset( $_REQUEST['wpda_main_db_schema'] ) && '' !== $_REQUEST['wpda_main_db_schema'] ) { |
| 198 |
$requested_db_schema = sanitize_text_field( wp_unslash( $_REQUEST['wpda_main_db_schema'] ) ); // input var okay. |
| 199 |
setcookie($cookie_name, $requested_db_schema, time() + 3600 ); |
| 200 |
} else { |
| 201 |
// Check referer: clear cookie on new page request. |
| 202 |
$url = parse_url( wp_get_referer() ); |
| 203 |
if ( isset( $url['query'] ) ) { |
| 204 |
parse_str( $url['query'], $path ); |
| 205 |
if ( isset( $path['page'] ) ) { |
| 206 |
$page = $path['page']; |
| 207 |
if ( $this->page !== $page ) { |
| 208 |
// New page request: reset cookie. |
| 209 |
setcookie($cookie_name, '', time() - 3600 ); |
| 210 |
unset( $_COOKIE[ $cookie_name ] ); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
// Handle cookie to remember favourite selection. |
| 217 |
$cookie_name = $this->page . '_favourites'; |
| 218 |
if ( isset( $_REQUEST['wpda_main_favourites'] ) ) { |
| 219 |
$favourites = sanitize_text_field( wp_unslash( $_REQUEST['wpda_main_favourites'] ) ); // input var okay. |
| 220 |
setcookie($cookie_name, $favourites, time() + 3600 ); |
| 221 |
} else { |
| 222 |
// Check referer: clear cookie on new page request. |
| 223 |
$url = parse_url( wp_get_referer() ); |
| 224 |
if ( isset( $url['query'] ) ) { |
| 225 |
parse_str( $url['query'], $path ); |
| 226 |
if ( isset( $path['page'] ) ) { |
| 227 |
$page = $path['page']; |
| 228 |
if ( $this->page !== $page ) { |
| 229 |
// New page request: reset cookie. |
| 230 |
setcookie($cookie_name, '', time() - 3600 ); |
| 231 |
unset( $_COOKIE[ $cookie_name ] ); |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
// Handle cookie for search value. |
| 239 |
$table_name = |
| 240 |
isset( $_REQUEST['table_name'] ) ? |
| 241 |
sanitize_text_field( wp_unslash( $_REQUEST['table_name'] ) ) : |
| 242 |
\WPDataAccess\List_Table\WPDA_List_Table::LIST_BASE_TABLE; // input var okay. |
| 243 |
$cookie_name = $this->page . '_search_' . str_replace( '.', '_', $table_name ); |
| 244 |
if ( isset( $_REQUEST['s'] ) ) { // input var okay. |
| 245 |
$search_argument = sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ); // input var okay. |
| 246 |
if ( '' !== $search_argument ) { |
| 247 |
setcookie( $cookie_name, $search_argument, time() + 3600 ); |
| 248 |
} else { |
| 249 |
setcookie( $cookie_name, '', time() - 3600 ); |
| 250 |
unset( $_COOKIE[ $cookie_name ] ); |
| 251 |
} |
| 252 |
} else { |
| 253 |
// Check referer: clear cookie on new page request. |
| 254 |
$url = parse_url( wp_get_referer() ); |
| 255 |
if ( isset( $url['query'] ) ) { |
| 256 |
parse_str( $url['query'], $path ); |
| 257 |
if ( isset( $path['page'] ) ) { |
| 258 |
$page = $path['page']; |
| 259 |
if ( $this->page !== $page ) { |
| 260 |
// New page request: reset cookie and all cookies for subpages. |
| 261 |
foreach ( $_COOKIE as $key => $value ) { |
| 262 |
if ( $this->page . '_search_' === substr( $key, 0, strlen( $this->page . '_search_' ) ) ) { |
| 263 |
setcookie( $key, '', time() - 3600 ); |
| 264 |
unset( $_COOKIE[ $key ] ); |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Add stylesheets to back-end |
| 276 |
* |
| 277 |
* The following stylesheets are added: |
| 278 |
* + Plugin stylesheet |
| 279 |
* + Visual editor stylesheet |
| 280 |
* |
| 281 |
* The plugin stylesheet is used to style the setting forms {@see WPDA_Settings}, simple forms |
| 282 |
* {@see \WPDataAccess\Simple_Form\WPDA_Simple_Form} and the user menu edit form |
| 283 |
* {@see \WPDataAccess\User_Menu\WPDA_User_Menu_Form}. The visual editor stysheets is used to style |
| 284 |
* the plugin button in the visual editor {@see WP_Data_Access_Public}. |
| 285 |
* |
| 286 |
* @since 1.0.0 |
| 287 |
* |
| 288 |
* @see WPDA_Settings |
| 289 |
* @see \WPDataAccess\Simple_Form\WPDA_Simple_Form |
| 290 |
* @see \WPDataAccess\User_Menu\WPDA_User_Menu_Form |
| 291 |
* @see WP_Data_Access_Public |
| 292 |
*/ |
| 293 |
public function enqueue_styles() { |
| 294 |
|
| 295 |
// WPDataAccess CSS. |
| 296 |
wp_register_style( |
| 297 |
'wpdataaccess', |
| 298 |
plugins_url( '../assets/css/wpda_style.css', __FILE__ ), |
| 299 |
[], |
| 300 |
WPDA::get_option( WPDA::OPTION_WPDA_VERSION ) |
| 301 |
); |
| 302 |
wp_enqueue_style( 'wpdataaccess' ); |
| 303 |
|
| 304 |
// Add style for shortcode button in editor toolbar. |
| 305 |
wp_register_style( |
| 306 |
'wpda_shortcode_button_css', |
| 307 |
plugins_url( '../assets/css/wpda_shortcode_button.css', __FILE__ ), |
| 308 |
[], |
| 309 |
WPDA::get_option( WPDA::OPTION_WPDA_VERSION ) |
| 310 |
); |
| 311 |
wp_enqueue_style( 'wpda_shortcode_button_css' ); |
| 312 |
|
| 313 |
wp_enqueue_style( 'wp-jquery-ui-core' ); |
| 314 |
wp_enqueue_style( 'wp-jquery-ui-dialog' ); |
| 315 |
wp_enqueue_style( 'wp-jquery-ui-tabs' ); |
| 316 |
global $wp_scripts; |
| 317 |
$queryui = $wp_scripts->query('jquery-ui-core'); |
| 318 |
$url = "//ajax.googleapis.com/ajax/libs/jqueryui/{$queryui->ver}/themes/smoothness/jquery-ui.css"; |
| 319 |
wp_enqueue_style('jquery-ui-smoothness', $url, false, null); |
| 320 |
|
| 321 |
// Add WP Data Projects stylesheet if add-on is installed. |
| 322 |
if ( WPDA::projects() ) { |
| 323 |
wp_register_style( |
| 324 |
'wpdataprojects', |
| 325 |
plugins_url( '../WPDataProjects/assets/css/wpdp_style.css', __FILE__ ), |
| 326 |
[], |
| 327 |
WPDA::get_option( WPDA::OPTION_WPDA_VERSION ) |
| 328 |
); |
| 329 |
wp_enqueue_style( 'wpdataprojects' ); |
| 330 |
} |
| 331 |
|
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Add scripts to back-end |
| 336 |
* |
| 337 |
* Adds jquery ui to the back-end which is used to build the shortcode button functionality |
| 338 |
* {@see WP_Data_Access_Public}. |
| 339 |
* |
| 340 |
* @since 1.0.0 |
| 341 |
* |
| 342 |
* @see WP_Data_Access_Public |
| 343 |
*/ |
| 344 |
public function enqueue_scripts() { |
| 345 |
|
| 346 |
wp_enqueue_script( 'jquery-ui-core' ); |
| 347 |
wp_enqueue_script( 'jquery-ui-dialog' ); |
| 348 |
wp_enqueue_script( 'jquery-ui-tabs' ); |
| 349 |
|
| 350 |
// Register purl external library. |
| 351 |
wp_register_script( |
| 352 |
'purl', |
| 353 |
plugins_url( '../assets/js/purl.js', __FILE__ ) , |
| 354 |
[], |
| 355 |
WPDA::get_option( WPDA::OPTION_WPDA_VERSION ) |
| 356 |
); |
| 357 |
wp_enqueue_script( 'purl' ); |
| 358 |
|
| 359 |
// Register clipboard.js external library. |
| 360 |
wp_register_script( |
| 361 |
'clipboard', |
| 362 |
plugins_url( '../assets/js/clipboard.min.js', __FILE__ ) , |
| 363 |
[], |
| 364 |
WPDA::get_option( WPDA::OPTION_WPDA_VERSION ) |
| 365 |
); |
| 366 |
wp_enqueue_script( 'clipboard' ); |
| 367 |
|
| 368 |
// Register wpda admin functions. |
| 369 |
wp_register_script( |
| 370 |
'wpda_admin_scripts', |
| 371 |
plugins_url('../assets/js/wpda_admin.js', __FILE__ ), |
| 372 |
[], |
| 373 |
WPDA::get_option( WPDA::OPTION_WPDA_VERSION ) |
| 374 |
); |
| 375 |
wp_enqueue_script( 'wpda_admin_scripts' ); |
| 376 |
|
| 377 |
// Add WP Data Projects stylesheet if add-on is installed. |
| 378 |
if ( WPDA::projects() ) { |
| 379 |
wp_register_script( |
| 380 |
'wpdataprojects', |
| 381 |
plugins_url('../WPDataProjects/assets/js/wpdp_admin.js', __FILE__ ), |
| 382 |
[], |
| 383 |
WPDA::get_option( WPDA::OPTION_WPDA_VERSION ) |
| 384 |
); |
| 385 |
wp_enqueue_script( 'wpdataprojects' ); |
| 386 |
} |
| 387 |
|
| 388 |
} |
| 389 |
|
| 390 |
/** |
| 391 |
* Available tables for front-end |
| 392 |
* |
| 393 |
* Generates javascript code to support dynamic requests for tables that can be viewed on the front-end. The list |
| 394 |
* of available tables is requested via ajax call 'wpda_tinymce_listbox_tables' in |
| 395 |
* {@see WP_Data_Access::define_public_hooks()}. |
| 396 |
* |
| 397 |
* @since 1.0.0 |
| 398 |
* |
| 399 |
* @see WP_Data_Access::define_public_hooks() |
| 400 |
*/ |
| 401 |
public function tinymce_listboxes_get_tables() { |
| 402 |
|
| 403 |
if ( get_user_option( 'rich_editing' ) === 'true' && |
| 404 |
current_user_can( 'edit_posts' ) && |
| 405 |
current_user_can( 'edit_pages' ) |
| 406 |
) { |
| 407 |
|
| 408 |
// TinyMCE add table list to listbox. |
| 409 |
?> |
| 410 |
|
| 411 |
<script type="text/javascript"> |
| 412 |
jQuery(document).ready(function (jQuery) { |
| 413 |
var wpda_data = { |
| 414 |
'action': 'wpda_tinymce_listbox_tables' |
| 415 |
}; |
| 416 |
// Load table listbox. |
| 417 |
jQuery.post(ajaxurl, wpda_data, function (response) { |
| 418 |
if (typeof(tinyMCE) != 'undefined') { |
| 419 |
if (tinyMCE.activeEditor != null) { |
| 420 |
tinyMCE.activeEditor.settings.wpda_table_list = response; |
| 421 |
} |
| 422 |
} |
| 423 |
}); |
| 424 |
}); |
| 425 |
</script> |
| 426 |
|
| 427 |
<?php |
| 428 |
|
| 429 |
} |
| 430 |
|
| 431 |
} |
| 432 |
|
| 433 |
/** |
| 434 |
* Add plugin menu and sub menus |
| 435 |
* |
| 436 |
* Adds the following menu and sub menus to the back-end menu: |
| 437 |
* + WP Data Access |
| 438 |
* + Data Explorer |
| 439 |
* + Manage Menus |
| 440 |
* + Manage Plugin |
| 441 |
* |
| 442 |
* Menu titles are dynamically set in {@see WP_Data_Access_Admin::set_menu_titles()} to support translations. |
| 443 |
* |
| 444 |
* @since 1.0.0 |
| 445 |
* |
| 446 |
* @see WP_Data_Access_Admin::set_menu_titles() |
| 447 |
*/ |
| 448 |
public function add_menu_items() { |
| 449 |
|
| 450 |
global $wpdb; |
| 451 |
|
| 452 |
if ( current_user_can( 'manage_options' ) ) { |
| 453 |
|
| 454 |
// Dynamically set menu titles. |
| 455 |
$this->set_menu_titles(); |
| 456 |
|
| 457 |
// Specific list tables (and forms) can be made available for specific capabilities: |
| 458 |
// managed in method add_menu_my_tables. |
| 459 |
// Main menu and items are only available to admin users (set capability to 'manage_options'). |
| 460 |
add_menu_page( |
| 461 |
$this->title_menu_menu, |
| 462 |
$this->title_menu_menu, |
| 463 |
'manage_options', |
| 464 |
self::PAGE_MAIN, |
| 465 |
null, |
| 466 |
'dashicons-editor-table', |
| 467 |
999999999 |
| 468 |
); |
| 469 |
|
| 470 |
// Add data explorer to WPDA menu. |
| 471 |
$this->wpda_data_explorer_menu = add_submenu_page( |
| 472 |
self::PAGE_MAIN, |
| 473 |
$this->title_menu_menu, |
| 474 |
$this->title_submenu_explorer, |
| 475 |
'manage_options', |
| 476 |
self::PAGE_MAIN, |
| 477 |
[ $this, 'data_explorer_page' ] |
| 478 |
); |
| 479 |
$this->wpda_data_explorer_view = new WPDA_List_View( |
| 480 |
[ |
| 481 |
'page_hook_suffix' => $this->wpda_data_explorer_menu, |
| 482 |
] |
| 483 |
); |
| 484 |
|
| 485 |
if ( WPDA_Design_Table_Model::table_table_design_exists() ) { |
| 486 |
// Add data designer to WPDA menu. |
| 487 |
$this->wpda_data_designer_menu = add_submenu_page( |
| 488 |
self::PAGE_MAIN, |
| 489 |
$this->title_menu_menu, |
| 490 |
$this->title_submenu_designer, |
| 491 |
'manage_options', |
| 492 |
self::PAGE_DESIGNER, |
| 493 |
[ $this, 'data_designer_page' ] |
| 494 |
); |
| 495 |
$this->wpda_data_designer_view = new WPDA_List_View( |
| 496 |
[ |
| 497 |
'page_hook_suffix' => $this->wpda_data_designer_menu, |
| 498 |
'table_name' => $wpdb->prefix . WPDA::get_option( WPDA::OPTION_WPDA_PREFIX ) . 'table_design', |
| 499 |
'list_table_class' => 'WPDataAccess\\Design_Table\\WPDA_Design_Table_List_Table', |
| 500 |
'edit_form_class' => 'WPDataAccess\\Design_Table\\WPDA_Design_Table_Form', |
| 501 |
] |
| 502 |
); |
| 503 |
} |
| 504 |
|
| 505 |
// Add Data Projects menu if add-on is installed. |
| 506 |
if ( WPDA::projects() ) { |
| 507 |
$wpdp = new \WPDataProjects\WPDP(); |
| 508 |
$wpdp->add_menu_items(); |
| 509 |
} |
| 510 |
|
| 511 |
// Add menu Manage Menus. |
| 512 |
if ( WPDA_User_Menu_Model::table_menu_items_exists() ) { |
| 513 |
// Add manage menus page to WPDA menu. |
| 514 |
$this->wpda_manage_menus_menu = add_submenu_page( |
| 515 |
self::PAGE_MAIN, |
| 516 |
$this->title_menu_menu, |
| 517 |
$this->title_submenu_menus, |
| 518 |
'manage_options', |
| 519 |
self::PAGE_MENUS, |
| 520 |
[ $this, 'manage_menus_page' ] |
| 521 |
); |
| 522 |
$this->wpda_manage_menus_view = new WPDA_List_View( |
| 523 |
[ |
| 524 |
'page_hook_suffix' => $this->wpda_manage_menus_menu, |
| 525 |
'table_name' => $wpdb->prefix . WPDA::get_option( WPDA::OPTION_WPDA_PREFIX ) . 'menu_items', |
| 526 |
'title' => $this->title_submenu_menus, |
| 527 |
'edit_form_class' => 'WPDataAccess\\User_Menu\\WPDA_User_Menu_Form', |
| 528 |
'column_headers' => [ |
| 529 |
'menu_name' => __( 'Menu name', 'wp-data-access' ), |
| 530 |
'menu_table_name' => __( 'Table name', 'wp-data-access' ), |
| 531 |
'capability' => __( 'Capability', 'wp-data-access' ), |
| 532 |
'menu_slug' => __( 'Menu slug', 'wp-data-access' ), |
| 533 |
], |
| 534 |
] |
| 535 |
); |
| 536 |
} |
| 537 |
|
| 538 |
// Add plugin settings page to WPDA menu. |
| 539 |
add_submenu_page( |
| 540 |
self::PAGE_MAIN, |
| 541 |
$this->title_menu_menu, |
| 542 |
$this->title_submenu_settings, |
| 543 |
'manage_options', |
| 544 |
self::PAGE_SETTINGS, |
| 545 |
[ $this, 'settings_page' ] |
| 546 |
); |
| 547 |
|
| 548 |
} |
| 549 |
|
| 550 |
} |
| 551 |
|
| 552 |
/** |
| 553 |
* Dynamically set menu titles |
| 554 |
* |
| 555 |
* Dynamically set menu titles to support translations. |
| 556 |
* |
| 557 |
* @since 1.0.0 |
| 558 |
*/ |
| 559 |
protected function set_menu_titles() { |
| 560 |
|
| 561 |
$this->title_menu_menu = __( 'WP Data Access', 'wp-data-access' ); |
| 562 |
$this->title_submenu_explorer = __( 'Data Explorer', 'wp-data-access' ); |
| 563 |
$this->title_submenu_designer = __( 'Data Designer', 'wp-data-access' ); |
| 564 |
$this->title_submenu_menus = __( 'Data Menus', 'wp-data-access' ); |
| 565 |
$this->title_submenu_settings = __( 'Manage Plugin', 'wp-data-access' ); |
| 566 |
$this->title_menu_my_tables = __( 'WP Data Tables', 'wp-data-access' ); |
| 567 |
|
| 568 |
} |
| 569 |
|
| 570 |
/** |
| 571 |
* Show data explorer |
| 572 |
* |
| 573 |
* Initialization of $this->wpda_data_explorer_view is done earlier in |
| 574 |
* {@see WP_Data_Access_Admin::add_menu_items()} to support screen options. This method just shows the page |
| 575 |
* containing the list table. |
| 576 |
* |
| 577 |
* @since 1.0.0 |
| 578 |
* |
| 579 |
* @see WP_Data_Access_Admin::add_menu_items() |
| 580 |
*/ |
| 581 |
public function data_explorer_page() { |
| 582 |
|
| 583 |
$this->wpda_data_explorer_view->show(); |
| 584 |
|
| 585 |
} |
| 586 |
|
| 587 |
/** |
| 588 |
* Show data designer |
| 589 |
* |
| 590 |
* Initialization of $this->wpda_data_designer_view is done earlier in |
| 591 |
* {@see WP_Data_Access_Admin::add_menu_items()} to support screen options. This method just shows the page |
| 592 |
* containing the list table. |
| 593 |
* |
| 594 |
* @since 1.0.0 |
| 595 |
* |
| 596 |
* @see WP_Data_Access_Admin::add_menu_items() |
| 597 |
*/ |
| 598 |
public function data_designer_page() { |
| 599 |
|
| 600 |
$this->wpda_data_designer_view->show(); |
| 601 |
|
| 602 |
} |
| 603 |
|
| 604 |
/** |
| 605 |
* Show setting pages |
| 606 |
* |
| 607 |
* @since 1.0.0 |
| 608 |
* |
| 609 |
* @see WPDA_Settings::show() |
| 610 |
*/ |
| 611 |
public function settings_page() { |
| 612 |
|
| 613 |
$wpda_settings = new WPDA_Settings(); |
| 614 |
$wpda_settings->show(); |
| 615 |
|
| 616 |
} |
| 617 |
|
| 618 |
/** |
| 619 |
* Show manage menus page |
| 620 |
* |
| 621 |
* Shows a list table containing all user defined menu items. From the list table the user menu form |
| 622 |
* {\WPDataAccess\User_Menu\WPDA_User_Menu_Form} can be called to edit menu items. |
| 623 |
* |
| 624 |
* Initialization of $this->wpda_manage_menus_view is done earlier in |
| 625 |
* {@see WP_Data_Access_Admin::add_menu_items()} to support screen options. This method just shows the page |
| 626 |
* containing the list table. |
| 627 |
* |
| 628 |
* @since 1.0.0 |
| 629 |
* |
| 630 |
* @see WP_Data_Access_Admin::add_menu_items() |
| 631 |
* @see \WPDataAccess\User_Menu\WPDA_User_Menu_Form |
| 632 |
*/ |
| 633 |
public function manage_menus_page() { |
| 634 |
|
| 635 |
$this->wpda_manage_menus_view->show(); |
| 636 |
|
| 637 |
} |
| 638 |
|
| 639 |
/** |
| 640 |
* Add user defined sub menu |
| 641 |
* |
| 642 |
* WPDA allows users to create sub menu for table lists and simple forms. Sub menus can be added to the WPDA |
| 643 |
* menu or any other (external) menu. A sub menu is added to an external menu via the menu slug. Sub menus are |
| 644 |
* taken from {@see WPDA_User_Menu_Model}. |
| 645 |
* |
| 646 |
* This method is called from the admin_menu action with a lower priority to make sure other menus are available. |
| 647 |
* User defined menu items are added to avalable menus in this method. These can be WPDA menus or external menus |
| 648 |
* as mentioned in the according list table and edit form. WPDA menus are added to menu WP Data Tables. External |
| 649 |
* menus are added to the menu having the menu slug defined by the user. |
| 650 |
* |
| 651 |
* This method does not actually show the list tables! It just creates the menu items. When the user clicks on such |
| 652 |
* a dynamiccally defined menu item, method {@see WP_Data_Access_Admin::my_tables_page()} is called, which takes |
| 653 |
* care of showing the list table. |
| 654 |
* |
| 655 |
* @since 1.0.0 |
| 656 |
* |
| 657 |
* @see WP_Data_Access_Admin::my_tables_page() |
| 658 |
* @see WPDA_User_Menu_Model |
| 659 |
*/ |
| 660 |
public function add_menu_my_tables() { |
| 661 |
|
| 662 |
// Dynamically set menu titles. |
| 663 |
$this->set_menu_titles(); |
| 664 |
|
| 665 |
$menus_shown_to_current_user = []; |
| 666 |
|
| 667 |
// Add list tables to external menus. |
| 668 |
foreach ( WPDA_User_Menu_Model::list_external_menus() as $menu ) { |
| 669 |
if ( current_user_can( $menu->menu_capability ) ) { |
| 670 |
if ( ! isset( $menus_shown_to_current_user[ $menu->menu_slug . '/' . $menu->menu_name . '/' . $menu->menu_table_name ] ) ) { |
| 671 |
$menu_slug = self::PAGE_EXPLORER . '_' . $menu->menu_table_name; |
| 672 |
|
| 673 |
$this->wpda_my_table_list_menu[ $menu->menu_table_name ] = |
| 674 |
add_submenu_page( |
| 675 |
$menu->menu_slug, |
| 676 |
$this->title_menu_menu . ' : ' . strtoupper( $menu->menu_table_name ), |
| 677 |
$menu->menu_name, |
| 678 |
$menu->menu_capability, |
| 679 |
$menu_slug, |
| 680 |
[ $this, 'my_tables_page' ] |
| 681 |
); |
| 682 |
|
| 683 |
$this->wpda_my_table_list_view[ $menu->menu_table_name ] = |
| 684 |
new WPDA_List_View( |
| 685 |
[ |
| 686 |
'page_hook_suffix' => $this->wpda_my_table_list_menu[ $menu->menu_table_name ], |
| 687 |
'table_name' => $menu->menu_table_name, |
| 688 |
] |
| 689 |
); |
| 690 |
|
| 691 |
$menus_shown_to_current_user[ $menu->menu_slug . '/' . $menu->menu_name . '/' . $menu->menu_table_name ] = true; |
| 692 |
} |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
} |
| 697 |
|
| 698 |
/** |
| 699 |
* Show user defined menus |
| 700 |
* |
| 701 |
* A user defined menu that are added to the plugin menu in {@see WP_Data_Access_Admin::add_menu_my_tables()} is |
| 702 |
* shown here. This method is called when the user clicks on the menu item generated in |
| 703 |
* {@see WP_Data_Access_Admin::add_menu_my_tables()}. |
| 704 |
* |
| 705 |
* @since 1.0.0 |
| 706 |
* |
| 707 |
* @see WP_Data_Access_Admin::add_menu_my_tables() |
| 708 |
*/ |
| 709 |
public function my_tables_page() { |
| 710 |
|
| 711 |
// Grab table name from menu slug. |
| 712 |
if ( null !== $this->page ) { |
| 713 |
if ( strpos( $this->page, self::PAGE_EXPLORER ) !== false ) { |
| 714 |
$table = substr( $this->page, strlen( self::PAGE_EXPLORER . '_' ) ); |
| 715 |
} else { |
| 716 |
$table = substr( $this->page, strlen( self::PAGE_MY_TABLES . '_' ) ); |
| 717 |
} |
| 718 |
// Show list table. |
| 719 |
$this->wpda_my_table_list_view[ $table ]->show(); |
| 720 |
} |
| 721 |
|
| 722 |
} |
| 723 |
|
| 724 |
} |
| 725 |
|