class-code-manager-admin.php
330 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class Code_Manager_Admin |
| 5 | * |
| 6 | * Defines admin specific functionality for the Code Manager. |
| 7 | * |
| 8 | * @author Peter Schulz |
| 9 | * @since 1.0.0 |
| 10 | */ |
| 11 | class Code_Manager_Admin { |
| 12 | |
| 13 | /** |
| 14 | * Current page (menu slug) |
| 15 | * |
| 16 | * @var null|string |
| 17 | */ |
| 18 | protected $page = null; |
| 19 | |
| 20 | /** |
| 21 | * Tab mode: |
| 22 | * on > Code Manager works in tab mode (edit multiple codes simultaneously) |
| 23 | * off > Code Manager works in list mode (edit single codes + enable/disable code) |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $tabmode = 'off'; |
| 28 | |
| 29 | /** |
| 30 | * Handle to list view |
| 31 | * |
| 32 | * @var Code_Manager\Code_Manager_List_View |
| 33 | */ |
| 34 | protected $cm_list_view = null; |
| 35 | |
| 36 | /** |
| 37 | * Code_Manager_Admin constructor. |
| 38 | * |
| 39 | * Checks if WP Data Access is available to add additional features. |
| 40 | * |
| 41 | * @since 1.0.0 |
| 42 | */ |
| 43 | public function __construct() { |
| 44 | if ( isset( $_REQUEST['page'] ) ) { |
| 45 | $this->page = sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ); // input var okay. |
| 46 | } |
| 47 | |
| 48 | if ( isset( $_REQUEST['tabmode'] ) ) { |
| 49 | $this->tabmode = sanitize_text_field( wp_unslash( $_REQUEST['tabmode'] ) ); // input var okay. |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Add stylesheets to back-end |
| 55 | * |
| 56 | * Only added when Code Manager is visible. Adds minimal CSS depending on actual page (menu slug). |
| 57 | * |
| 58 | * @since 1.0.0 |
| 59 | */ |
| 60 | public function enqueue_styles() { |
| 61 | if ( CODE_MANAGER_MENU_SLUG === $this->page ) { |
| 62 | wp_enqueue_style( 'wp-codemirror' ); |
| 63 | |
| 64 | if ( isset( $_REQUEST['action'] ) && |
| 65 | ( 'new' === $_REQUEST['action'] || 'edit' === $_REQUEST['action'] ) |
| 66 | ) { |
| 67 | // Code Manager data entry form |
| 68 | wp_enqueue_style( |
| 69 | 'code_manager', |
| 70 | plugins_url( '../assets/css/code_manager.css', __FILE__ ), |
| 71 | [], |
| 72 | CODE_MANAGER_VERSION |
| 73 | ); |
| 74 | } elseif ( 'on' === $this->tabmode ) { |
| 75 | // Code Manager tab mode |
| 76 | wp_enqueue_style( |
| 77 | 'code_manager_tabmode', |
| 78 | plugins_url( '../assets/css/code_manager_tabmode.css', __FILE__ ), |
| 79 | [], |
| 80 | CODE_MANAGER_VERSION |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | $this->load_global_css(); |
| 85 | } elseif ( CODE_MANAGER_SETTINGS_MENU_SLUG === $this->page ) { |
| 86 | // Code Manager list mode |
| 87 | wp_enqueue_style( |
| 88 | 'code_manager_settings', |
| 89 | plugins_url( '../assets/css/code_manager_settings.css', __FILE__ ), |
| 90 | [], |
| 91 | CODE_MANAGER_VERSION |
| 92 | ); |
| 93 | |
| 94 | $this->load_global_css(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Adds CSS needed by all Code Manager pages |
| 100 | * |
| 101 | * @since 1.0.0 |
| 102 | */ |
| 103 | protected function load_global_css() { |
| 104 | // Material icons are used in page headers |
| 105 | wp_enqueue_style( |
| 106 | 'code_manager_material_icons', |
| 107 | plugins_url( '../assets/icons/material-icons.css', __FILE__ ), |
| 108 | [], |
| 109 | CODE_MANAGER_VERSION |
| 110 | ); |
| 111 | |
| 112 | // Global Code Manager styling |
| 113 | wp_enqueue_style( |
| 114 | 'code_manager_global', |
| 115 | plugins_url( '../assets/css/code_manager_global.css', __FILE__ ), |
| 116 | [], |
| 117 | CODE_MANAGER_VERSION |
| 118 | ); |
| 119 | |
| 120 | // Add tooltips to add Code Manager pages |
| 121 | wp_enqueue_style( |
| 122 | 'code_manager_tooltip_css', |
| 123 | // plugins_url( '../assets/css/jquery-ui-darkness.css', __FILE__ ), |
| 124 | plugins_url( '../assets/css/jquery-ui.min.css', __FILE__ ), |
| 125 | [], |
| 126 | CODE_MANAGER_VERSION |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Add scripts to back-end |
| 132 | * |
| 133 | * Only added when Code Manager is visible. Adds relevant JS only depending on action and tabmode. |
| 134 | * |
| 135 | * @since 1.0.0 |
| 136 | */ |
| 137 | public function enqueue_scripts() { |
| 138 | if ( CODE_MANAGER_MENU_SLUG === $this->page ) { |
| 139 | wp_enqueue_script( 'jquery' ); |
| 140 | wp_enqueue_script( 'jquery-ui-tooltip' ); |
| 141 | wp_enqueue_script( 'jquery-ui-dialog' ); |
| 142 | |
| 143 | // Register codeEditor (PHP = default editor, can be overridden on user request) |
| 144 | $cm_settings['codeEditor'] = |
| 145 | wp_enqueue_code_editor( |
| 146 | [ |
| 147 | 'type' => 'application/x-httpd-php', |
| 148 | 'codemirror' => [ |
| 149 | 'lineNumbers' => true, |
| 150 | 'autoRefresh' => true, |
| 151 | 'mode' => 'php', |
| 152 | 'lineWrapping' => true, |
| 153 | ], |
| 154 | ] |
| 155 | ); |
| 156 | wp_enqueue_script( 'wp-theme-plugin-editor' ); |
| 157 | wp_localize_script( 'wp-theme-plugin-editor', 'cm_settings', $cm_settings ); |
| 158 | |
| 159 | if ( |
| 160 | isset( $_REQUEST['action'] ) && |
| 161 | ( 'new' === $_REQUEST['action'] || 'edit' === $_REQUEST['action'] ) |
| 162 | ) { |
| 163 | // Code Manager data entry form |
| 164 | wp_enqueue_script( |
| 165 | 'code_manager', |
| 166 | plugins_url( '../assets/js/code_manager.js', __FILE__ ), |
| 167 | [], |
| 168 | CODE_MANAGER_VERSION |
| 169 | ); |
| 170 | } elseif ( 'on' === $this->tabmode ) { |
| 171 | // Code Manager tab mode |
| 172 | wp_enqueue_script( |
| 173 | 'code_manager_tabmode', |
| 174 | plugins_url( '../assets/js/code_manager_tabmode.js', __FILE__ ), |
| 175 | [], |
| 176 | CODE_MANAGER_VERSION |
| 177 | ); |
| 178 | } else { |
| 179 | // Code Manager list mode |
| 180 | wp_enqueue_script( |
| 181 | 'code_manager_listmode', |
| 182 | plugins_url( '../assets/js/code_manager_listmode.js', __FILE__ ), |
| 183 | [], |
| 184 | CODE_MANAGER_VERSION |
| 185 | ); |
| 186 | |
| 187 | // Enqueue clipboard |
| 188 | wp_enqueue_script( 'clipboard' ); |
| 189 | } |
| 190 | |
| 191 | // Register external library notify.js |
| 192 | wp_enqueue_script( |
| 193 | 'code_manager_notify', |
| 194 | plugins_url( '../assets/js/notify.min.js', __FILE__ ), |
| 195 | [ 'jquery' ], |
| 196 | CODE_MANAGER_VERSION |
| 197 | ); |
| 198 | } elseif ( CODE_MANAGER_SETTINGS_MENU_SLUG === $this->page ) { |
| 199 | wp_enqueue_script( 'jquery' ); |
| 200 | wp_enqueue_script( 'jquery-ui-tooltip' ); |
| 201 | wp_enqueue_script( 'jquery-ui-dialog' ); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Add plugin menus (only accessible to admin users) |
| 207 | * |
| 208 | * @since 1.0.0 |
| 209 | */ |
| 210 | public function add_menu_items() { |
| 211 | if ( current_user_can( 'manage_options' ) ) { |
| 212 | // Check if code manager table is available |
| 213 | $code_manager_model_class = CODE_MANAGER_MODEL_CLASS; |
| 214 | $code_manager_model = new $code_manager_model_class(); |
| 215 | $code_manager_table_found = $code_manager_model::table_exists(); |
| 216 | |
| 217 | if ( $code_manager_table_found ) { |
| 218 | // Determine view mode |
| 219 | $code_manager_page = 'on' === $this->tabmode ? 'code_manager_tab_mode' : 'code_manager_page'; |
| 220 | } else { |
| 221 | // Show error page if plugin table is not found |
| 222 | $code_manager_page = 'code_manager_page_not_found'; |
| 223 | } |
| 224 | |
| 225 | // Add top level menu |
| 226 | add_menu_page( |
| 227 | CODE_MANAGER_MENU_SLUG, |
| 228 | CODE_MANAGER_MENU_TITLE, |
| 229 | 'manage_options', |
| 230 | CODE_MANAGER_MENU_SLUG, |
| 231 | null, |
| 232 | 'dashicons-editor-code', |
| 233 | 999999999 |
| 234 | ); |
| 235 | |
| 236 | // Add Code Manager submenu |
| 237 | $cm_list_menu = add_submenu_page( |
| 238 | CODE_MANAGER_MENU_SLUG, |
| 239 | CODE_MANAGER_PAGE_TITLE, |
| 240 | CODE_MANAGER_MENU_TITLE, |
| 241 | 'manage_options', |
| 242 | CODE_MANAGER_MENU_SLUG, |
| 243 | [ $this, $code_manager_page ] |
| 244 | ); |
| 245 | |
| 246 | // Create list view to handle screen options |
| 247 | if ( $this->page === CODE_MANAGER_MENU_SLUG && $code_manager_table_found && 'on' !== $this->tabmode ) { |
| 248 | header( "X-XSS-Protection: 0" ); |
| 249 | $code_manager_main_class = CODE_MANAGER_MAIN_CLASS; |
| 250 | $this->cm_list_view = new $code_manager_main_class( |
| 251 | [ |
| 252 | 'page_hook_suffix' => $cm_list_menu, |
| 253 | ] |
| 254 | ); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Register settings page |
| 261 | * |
| 262 | * @since 1.0.0 |
| 263 | */ |
| 264 | public function code_manager_register_settings_page() { |
| 265 | add_options_page( |
| 266 | CODE_MANAGER_SETTINGS_PAGE_TITLE, |
| 267 | CODE_MANAGER_SETTINGS_MENU_TITLE, |
| 268 | 'manage_options', |
| 269 | CODE_MANAGER_SETTINGS_MENU_SLUG, |
| 270 | [ |
| 271 | $this, |
| 272 | 'code_manager_settings_page' |
| 273 | ] |
| 274 | ); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Show settings page |
| 279 | * |
| 280 | * @since 1.0.0 |
| 281 | */ |
| 282 | public function code_manager_settings_page() { |
| 283 | $code_manager_settings_class = CODE_MANAGER_SETTINGS_CLASS; |
| 284 | $cm_settings = new $code_manager_settings_class(); |
| 285 | $cm_settings->show(); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Show Code Manager in list mode |
| 290 | * |
| 291 | * @since 1.0.0 |
| 292 | */ |
| 293 | public function code_manager_page() { |
| 294 | $this->cm_list_view->show(); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Show Code Manager in tab mode |
| 299 | * |
| 300 | * @since 1.0.0 |
| 301 | */ |
| 302 | public function code_manager_tab_mode() { |
| 303 | $code_manager_tab_class = CODE_MANAGER_TAB_CLASS; |
| 304 | $tabmode = new $code_manager_tab_class(); |
| 305 | $tabmode->show(); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Plugin table not found > show error page |
| 310 | * |
| 311 | * @since 1.0.0 |
| 312 | */ |
| 313 | public function code_manager_page_not_found() { |
| 314 | ?> |
| 315 | <div class="wrap"> |
| 316 | <h1 class="wp-heading-inline"> |
| 317 | <span><?php echo CODE_MANAGER_H1_TITLE; ?></span> |
| 318 | <a href="<?php echo CODE_MANAGER_HELP_URL; ?>" target="_blank" title="Plugin Help - open a new tab or window"> |
| 319 | <span class="dashicons dashicons-editor-help" |
| 320 | style="text-decoration:none;vertical-align:top;font-size:36px;"> |
| 321 | </span></a> |
| 322 | </h1> |
| 323 | <p> |
| 324 | <?php echo __( 'ERROR: Repository table not found!', 'code-manager' ); ?> |
| 325 | </p> |
| 326 | </div> |
| 327 | <?php |
| 328 | } |
| 329 | } |
| 330 |