| @@ -103,9 +103,9 @@ | ||
| 103 | 103 | $hook = add_submenu_page( |
| 104 | 104 | $this->base_slug, |
| 105 | 105 | $title, |
| 106 | 106 | $label, |
| 107 | - code_snippets()->get_cap(), | |
| 107 | + $this->menu_cap(), | |
| 108 | 108 | $slug, |
| 109 | 109 | array( $this, 'render' ) |
| 110 | 110 | ); |
| 111 | 111 | |
| @@ -112,8 +112,21 @@ | ||
| 112 | 112 | add_action( 'load-' . $hook, array( $this, 'load' ) ); |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | /** |
| 116 | + * Capability required to access this menu. | |
| 117 | + * | |
| 118 | + * Defaults to the snippet access capability. Subclasses such as the | |
| 119 | + * settings menu override this to require the core admin capability so they | |
| 120 | + * remain accessible only to full administrators. | |
| 121 | + * | |
| 122 | + * @return string | |
| 123 | + */ | |
| 124 | + protected function menu_cap(): string { | |
| 125 | + return code_snippets()->get_cap(); | |
| 126 | + } | |
| 127 | + | |
| 128 | + /** | |
| 116 | 129 | * Register the admin menu |
| 117 | 130 | */ |
| 118 | 131 | public function register() { |
| 119 | 132 | $this->add_menu( $this->slug, $this->label, $this->title ); |
| @@ -119,12 +132,31 @@ | ||
| 119 | 132 | $this->add_menu( $this->slug, $this->label, $this->title ); |
| 120 | 133 | } |
| 121 | 134 | |
| 122 | 135 | /** |
| 136 | + * Retrieve the WordPress hookname computed for this menu's admin page. | |
| 137 | + * | |
| 138 | + * @return string | |
| 139 | + */ | |
| 140 | + public function get_hookname(): string { | |
| 141 | + return get_plugin_page_hookname( $this->slug, $this->base_slug ); | |
| 142 | + } | |
| 143 | + | |
| 144 | + /** | |
| 145 | + * Retrieve every WordPress hookname registered by this menu, including any | |
| 146 | + * additional pages it registers beyond its primary slug. | |
| 147 | + * | |
| 148 | + * @return string[] | |
| 149 | + */ | |
| 150 | + public function get_hooknames(): array { | |
| 151 | + return [ $this->get_hookname() ]; | |
| 152 | + } | |
| 153 | + | |
| 154 | + /** | |
| 123 | 155 | * Render the navigation bar at the top of the admin page. |
| 124 | 156 | */ |
| 125 | 157 | protected function render_navigation() { |
| 126 | - echo '<div id="code-snippets-toolbar-container" class="wrap"></div>'; | |
| 158 | + echo '<div id="code-snippets-toolbar-container"></div>'; | |
| 127 | 159 | } |
| 128 | 160 | |
| 129 | 161 | /** |
| 130 | 162 | * Render the menu |
| @@ -135,9 +167,9 @@ | ||
| 135 | 167 | * Executed when the admin page is loaded |
| 136 | 168 | */ |
| 137 | 169 | public function load() { |
| 138 | 170 | // Make sure the user has permission to be here. |
| 139 | - if ( ! current_user_can( code_snippets()->get_cap() ) ) { | |
| 171 | + if ( ! current_user_can( $this->menu_cap() ) ) { | |
| 140 | 172 | wp_die( esc_html__( 'You are not authorized to access this page.', 'code-snippets' ) ); |
| 141 | 173 | } |
| 142 | 174 | |
| 143 | 175 | // Create the snippet tables if they are missing. |