class-code-manager-admin.php
444 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Code Manager admin page |
| 4 | * |
| 5 | * @package . |
| 6 | */ |
| 7 | |
| 8 | use Code_Manager\Code_Manager_Dashboard; |
| 9 | |
| 10 | /** |
| 11 | * Class Code_Manager_Admin |
| 12 | * |
| 13 | * Defines admin specific functionality for the Code Manager. |
| 14 | * |
| 15 | * @author Peter Schulz |
| 16 | * @since 1.0.0 |
| 17 | */ |
| 18 | class Code_Manager_Admin { |
| 19 | |
| 20 | /** |
| 21 | * Current page (menu slug) |
| 22 | * |
| 23 | * @var null|string |
| 24 | */ |
| 25 | protected $page = null; |
| 26 | |
| 27 | /** |
| 28 | * Tab mode: |
| 29 | * on > Code Manager works in tab mode (edit multiple codes simultaneously) |
| 30 | * off > Code Manager works in list mode (edit single codes + enable/disable code) |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | protected $tabmode = 'off'; |
| 35 | |
| 36 | /** |
| 37 | * Handle to list view |
| 38 | * |
| 39 | * @var Code_Manager\Code_Manager_List_View |
| 40 | */ |
| 41 | protected $cm_list_view = null; |
| 42 | |
| 43 | /** |
| 44 | * Code_Manager_Admin constructor. |
| 45 | * |
| 46 | * Checks if WP Data Access is available to add additional features. |
| 47 | * |
| 48 | * @since 1.0.0 |
| 49 | */ |
| 50 | public function __construct() { |
| 51 | if ( isset( $_REQUEST['page'] ) ) { |
| 52 | $this->page = sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ); // input var okay. |
| 53 | } |
| 54 | |
| 55 | if ( isset( $_REQUEST['tabmode'] ) ) { |
| 56 | $this->tabmode = sanitize_text_field( wp_unslash( $_REQUEST['tabmode'] ) ); // input var okay. |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Add stylesheets to back-end |
| 62 | * |
| 63 | * Only added when Code Manager is visible. Adds minimal CSS depending on actual page (menu slug). |
| 64 | * |
| 65 | * @since 1.0.0 |
| 66 | */ |
| 67 | public function enqueue_styles() { |
| 68 | if ( CODE_MANAGER_MENU_SLUG === $this->page ) { |
| 69 | wp_enqueue_style( 'wp-codemirror' ); |
| 70 | |
| 71 | if ( isset( $_REQUEST['action'] ) && |
| 72 | ( 'new' === $_REQUEST['action'] || 'edit' === $_REQUEST['action'] ) |
| 73 | ) { |
| 74 | // Code Manager data entry form. |
| 75 | wp_enqueue_style( |
| 76 | 'code_manager', |
| 77 | plugins_url( '../assets/css/code_manager.css', __FILE__ ), |
| 78 | array(), |
| 79 | CODE_MANAGER_VERSION |
| 80 | ); |
| 81 | } elseif ( 'on' === $this->tabmode ) { |
| 82 | // Code Manager tab mode. |
| 83 | wp_enqueue_style( |
| 84 | 'code_manager_tabmode', |
| 85 | plugins_url( '../assets/css/code_manager_tabmode.css', __FILE__ ), |
| 86 | array(), |
| 87 | CODE_MANAGER_VERSION |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | $this->load_global_css(); |
| 92 | } elseif ( CODE_MANAGER_SETTINGS_MENU_SLUG === $this->page ) { |
| 93 | // Code Manager list mode. |
| 94 | wp_enqueue_style( |
| 95 | 'code_manager_settings', |
| 96 | plugins_url( '../assets/css/code_manager_settings.css', __FILE__ ), |
| 97 | array(), |
| 98 | CODE_MANAGER_VERSION |
| 99 | ); |
| 100 | |
| 101 | $this->load_global_css(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Adds CSS needed by all Code Manager pages |
| 107 | * |
| 108 | * @since 1.0.0 |
| 109 | */ |
| 110 | protected function load_global_css() { |
| 111 | // Dashboard CSS. |
| 112 | wp_enqueue_style( |
| 113 | 'code_manager_dashboard', |
| 114 | plugins_url( '../assets/css/code_manager_dashboard.css', __FILE__ ), |
| 115 | array(), |
| 116 | CODE_MANAGER_VERSION |
| 117 | ); |
| 118 | |
| 119 | // Global Code Manager styling. |
| 120 | wp_enqueue_style( |
| 121 | 'code_manager_global', |
| 122 | plugins_url( '../assets/css/code_manager_global.css', __FILE__ ), |
| 123 | array(), |
| 124 | CODE_MANAGER_VERSION |
| 125 | ); |
| 126 | |
| 127 | // Add tooltips to add Code Manager pages. |
| 128 | wp_enqueue_style( |
| 129 | 'code_manager_tooltip_css', |
| 130 | plugins_url( '../assets/css/jquery-ui.min.css', __FILE__ ), |
| 131 | array(), |
| 132 | CODE_MANAGER_VERSION |
| 133 | ); |
| 134 | |
| 135 | // SAVING SPACE - According to the plugin guideliness it is allowed to include external fonts: |
| 136 | // https://developer.wordpress.org/plugins/wordpress-org/detailed-plugin-guidelines/#8-plugins-may-not-send-executable-code-via-third-party-systems . |
| 137 | |
| 138 | // Material icons are used in page headers. |
| 139 | wp_enqueue_style( // phpcs:ignore WordPress.WP.EnqueuedResourceParameters |
| 140 | 'code_manager_material_icons', |
| 141 | 'https://fonts.googleapis.com/icon?family=Material+Icons', |
| 142 | array(), |
| 143 | null |
| 144 | ); |
| 145 | |
| 146 | // Load fontawesome icons. |
| 147 | wp_enqueue_style( |
| 148 | 'cm_fontawesome_icons', |
| 149 | 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/fontawesome.min.css', |
| 150 | array(), |
| 151 | null |
| 152 | ); |
| 153 | wp_enqueue_style( |
| 154 | 'cm_fontawesome_icons_solid', |
| 155 | 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/solid.min.css', |
| 156 | array(), |
| 157 | null |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Add scripts to back-end |
| 163 | * |
| 164 | * Only added when Code Manager is visible. Adds relevant JS only depending on action and tabmode. |
| 165 | * |
| 166 | * @since 1.0.0 |
| 167 | */ |
| 168 | public function enqueue_scripts() { |
| 169 | if ( CODE_MANAGER_MENU_SLUG === $this->page ) { |
| 170 | $this->load_global_js(); |
| 171 | |
| 172 | // Register codeEditor (PHP = default editor, can be overridden on user request). |
| 173 | $cm_settings['codeEditor'] = |
| 174 | wp_enqueue_code_editor( |
| 175 | array( |
| 176 | 'type' => 'application/x-httpd-php', |
| 177 | 'codemirror' => array( |
| 178 | 'lineNumbers' => true, |
| 179 | 'autoRefresh' => true, |
| 180 | 'mode' => 'php', |
| 181 | 'lineWrapping' => true, |
| 182 | ), |
| 183 | ) |
| 184 | ); |
| 185 | wp_enqueue_script( 'wp-theme-plugin-editor' ); |
| 186 | wp_localize_script( 'wp-theme-plugin-editor', 'cm_settings', $cm_settings ); |
| 187 | |
| 188 | if ( |
| 189 | isset( $_REQUEST['action'] ) && |
| 190 | ( 'new' === $_REQUEST['action'] || 'edit' === $_REQUEST['action'] ) |
| 191 | ) { |
| 192 | // Code Manager data entry form. |
| 193 | wp_enqueue_script( |
| 194 | 'code_manager', |
| 195 | plugins_url( '../assets/js/code_manager.js', __FILE__ ), |
| 196 | array(), |
| 197 | CODE_MANAGER_VERSION, |
| 198 | true |
| 199 | ); |
| 200 | } elseif ( 'on' === $this->tabmode ) { |
| 201 | // Code Manager tab mode. |
| 202 | wp_enqueue_script( |
| 203 | 'code_manager_tabmode', |
| 204 | plugins_url( '../assets/js/code_manager_tabmode.js', __FILE__ ), |
| 205 | array(), |
| 206 | CODE_MANAGER_VERSION, |
| 207 | true |
| 208 | ); |
| 209 | } else { |
| 210 | // Code Manager list mode. |
| 211 | wp_enqueue_script( |
| 212 | 'code_manager_listmode', |
| 213 | plugins_url( '../assets/js/code_manager_listmode.js', __FILE__ ), |
| 214 | array(), |
| 215 | CODE_MANAGER_VERSION, |
| 216 | true |
| 217 | ); |
| 218 | } |
| 219 | |
| 220 | // Message. |
| 221 | wp_enqueue_script( |
| 222 | 'code_manager_message', |
| 223 | plugins_url( '../assets/js/code_manager_message.js', __FILE__ ), |
| 224 | array(), |
| 225 | CODE_MANAGER_VERSION, |
| 226 | true |
| 227 | ); |
| 228 | |
| 229 | |
| 230 | // Enqueue clipboard. |
| 231 | wp_enqueue_script( 'clipboard' ); |
| 232 | |
| 233 | // Register external library notify.js. |
| 234 | wp_enqueue_script( |
| 235 | 'code_manager_notify', |
| 236 | plugins_url( '../assets/js/notify.min.js', __FILE__ ), |
| 237 | array( 'jquery' ), |
| 238 | CODE_MANAGER_VERSION, |
| 239 | true |
| 240 | ); |
| 241 | } elseif ( CODE_MANAGER_SETTINGS_MENU_SLUG === $this->page ) { |
| 242 | $this->load_global_js(); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Load global JS library files |
| 248 | * |
| 249 | * @return void |
| 250 | */ |
| 251 | protected function load_global_js() { |
| 252 | wp_enqueue_script( 'jquery' ); |
| 253 | wp_enqueue_script( 'jquery-ui-core' ); |
| 254 | wp_enqueue_script( 'jquery-ui-dialog' ); |
| 255 | wp_enqueue_script( 'jquery-ui-tooltip' ); |
| 256 | wp_enqueue_script( 'jquery-ui-sortable' ); |
| 257 | |
| 258 | // Dashboard JS. |
| 259 | wp_enqueue_script( |
| 260 | 'code_manager_dashboard', |
| 261 | plugins_url( '../assets/js/code_manager_dashboard.js', __FILE__ ), |
| 262 | array(), |
| 263 | CODE_MANAGER_VERSION, |
| 264 | true |
| 265 | ); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Add plugin menus (only accessible to admin users) |
| 270 | * |
| 271 | * @since 1.0.0 |
| 272 | */ |
| 273 | public function add_menu_items() { |
| 274 | if ( current_user_can( 'manage_options' ) ) { |
| 275 | // Check if code manager table is available. |
| 276 | $code_manager_model_class = CODE_MANAGER_MODEL_CLASS; |
| 277 | $code_manager_model = new $code_manager_model_class(); |
| 278 | $code_manager_table_found = $code_manager_model::table_exists(); |
| 279 | |
| 280 | if ( $code_manager_table_found ) { |
| 281 | // Determine view mode. |
| 282 | $code_manager_page = 'on' === $this->tabmode ? 'code_manager_tab_mode' : 'code_manager_page'; |
| 283 | } else { |
| 284 | // Show error page if plugin table is not found. |
| 285 | $code_manager_page = 'code_manager_page_not_found'; |
| 286 | } |
| 287 | |
| 288 | // Add top level menu. |
| 289 | add_menu_page( |
| 290 | CODE_MANAGER_MENU_SLUG, |
| 291 | CODE_MANAGER_MENU_TITLE, |
| 292 | 'manage_options', |
| 293 | CODE_MANAGER_MENU_SLUG, |
| 294 | null, |
| 295 | 'dashicons-editor-code', |
| 296 | 999999999 |
| 297 | ); |
| 298 | |
| 299 | // Add Code Manager submenu. |
| 300 | $cm_list_menu = add_submenu_page( |
| 301 | CODE_MANAGER_MENU_SLUG, |
| 302 | CODE_MANAGER_PAGE_TITLE, |
| 303 | CODE_MANAGER_MENU_TITLE, |
| 304 | 'manage_options', |
| 305 | CODE_MANAGER_MENU_SLUG, |
| 306 | array( $this, $code_manager_page ) |
| 307 | ); |
| 308 | |
| 309 | // Create list view to handle screen options. |
| 310 | if ( CODE_MANAGER_MENU_SLUG === $this->page && $code_manager_table_found && 'on' !== $this->tabmode ) { |
| 311 | header( 'X-XSS-Protection: 0' ); |
| 312 | $code_manager_main_class = CODE_MANAGER_MAIN_CLASS; |
| 313 | $this->cm_list_view = new $code_manager_main_class( |
| 314 | array( |
| 315 | 'page_hook_suffix' => $cm_list_menu, |
| 316 | ) |
| 317 | ); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Remove notifications from development dashboard |
| 324 | * |
| 325 | * @return void |
| 326 | */ |
| 327 | public function user_admin_notices() { |
| 328 | if ( ( 'code_manager' === $this->page || 'code_manager_settings' === $this->page ) ) { |
| 329 | $code_manager_plugin_hide_foreign_notices = get_option( 'code_manager_plugin_hide_foreign_notices' ); |
| 330 | if ( |
| 331 | false === $code_manager_plugin_hide_foreign_notices || |
| 332 | 'on' === $code_manager_plugin_hide_foreign_notices |
| 333 | ) { |
| 334 | remove_all_actions( 'admin_notices' ); |
| 335 | remove_all_actions( 'all_admin_notices' ); |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Filter menu if in dashboard mode |
| 342 | * |
| 343 | * @param array $submenu_file Sub menus. |
| 344 | * @return mixed |
| 345 | */ |
| 346 | public function submenu_filter( $submenu_file ) { |
| 347 | if ( ! Code_Manager_Dashboard::menu_enabled() ) { |
| 348 | $hidden_submenus = array( |
| 349 | 'code_manager-account', |
| 350 | 'code_manager-wp-support-forum', |
| 351 | 'code_manager-pricing', |
| 352 | 'code_manager-contact', |
| 353 | 'code_manager', |
| 354 | ); |
| 355 | } else { |
| 356 | $hidden_submenus = array(); |
| 357 | } |
| 358 | |
| 359 | foreach ( $hidden_submenus as $submenu ) { |
| 360 | remove_submenu_page( 'code_manager', $submenu ); |
| 361 | } |
| 362 | |
| 363 | return $submenu_file; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Register settings page |
| 368 | * |
| 369 | * @since 1.0.0 |
| 370 | */ |
| 371 | public function code_manager_register_settings_page() { |
| 372 | add_options_page( |
| 373 | CODE_MANAGER_SETTINGS_PAGE_TITLE, |
| 374 | CODE_MANAGER_SETTINGS_MENU_TITLE, |
| 375 | 'manage_options', |
| 376 | CODE_MANAGER_SETTINGS_MENU_SLUG, |
| 377 | array( |
| 378 | $this, |
| 379 | 'code_manager_settings_page', |
| 380 | ) |
| 381 | ); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Show settings page |
| 386 | * |
| 387 | * @since 1.0.0 |
| 388 | */ |
| 389 | public function code_manager_settings_page() { |
| 390 | Code_Manager_Dashboard::add_dashboard(); |
| 391 | |
| 392 | $code_manager_settings_class = CODE_MANAGER_SETTINGS_CLASS; |
| 393 | $cm_settings = new $code_manager_settings_class(); |
| 394 | $cm_settings->show(); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Show Code Manager in list mode |
| 399 | * |
| 400 | * @since 1.0.0 |
| 401 | */ |
| 402 | public function code_manager_page() { |
| 403 | Code_Manager_Dashboard::add_dashboard(); |
| 404 | |
| 405 | $this->cm_list_view->show(); |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Show Code Manager in tab mode |
| 410 | * |
| 411 | * @since 1.0.0 |
| 412 | */ |
| 413 | public function code_manager_tab_mode() { |
| 414 | Code_Manager_Dashboard::add_dashboard(); |
| 415 | |
| 416 | $code_manager_tab_class = CODE_MANAGER_TAB_CLASS; |
| 417 | $tabmode = new $code_manager_tab_class(); |
| 418 | $tabmode->show(); |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Plugin table not found > show error page |
| 423 | * |
| 424 | * @since 1.0.0 |
| 425 | */ |
| 426 | public function code_manager_page_not_found() { |
| 427 | Code_Manager_Dashboard::add_dashboard(); |
| 428 | ?> |
| 429 | <div class="wrap"> |
| 430 | <h1 class="wp-heading-inline"> |
| 431 | <span><?php echo CODE_MANAGER_H1_TITLE; ?></span> |
| 432 | <a href="<?php echo CODE_MANAGER_HELP_URL; ?>" target="_blank" title="Plugin Help - open a new tab or window"> |
| 433 | <span class="dashicons dashicons-editor-help" |
| 434 | style="text-decoration:none;vertical-align:top;font-size:36px;"> |
| 435 | </span></a> |
| 436 | </h1> |
| 437 | <p> |
| 438 | <?php echo __( 'ERROR: Repository table not found!', 'code-manager' ); ?> |
| 439 | </p> |
| 440 | </div> |
| 441 | <?php |
| 442 | } |
| 443 | } |
| 444 |