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