| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Admin\Menus; |
| 4 |
|
| 5 |
use function Code_Snippets\code_snippets; |
| 6 |
|
| 7 |
/** |
| 8 |
* Base class for a plugin admin menu. |
| 9 |
*/ |
| 10 |
abstract class Admin_Menu { |
| 11 |
|
| 12 |
/** |
| 13 |
* The snippet page short name. |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
public string $name; |
| 18 |
|
| 19 |
/** |
| 20 |
* The label shown in the admin menu. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public string $label; |
| 25 |
|
| 26 |
/** |
| 27 |
* The text used for the page title. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
public string $title; |
| 32 |
|
| 33 |
/** |
| 34 |
* The base slug for the top-level admin menu. |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
protected string $base_slug; |
| 39 |
|
| 40 |
/** |
| 41 |
* The slug for this admin menu. |
| 42 |
* |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
protected string $slug; |
| 46 |
|
| 47 |
/** |
| 48 |
* Common JavaScript dependencies required for React components. |
| 49 |
* |
| 50 |
* @var string[] |
| 51 |
*/ |
| 52 |
public static array $script_deps = [ |
| 53 |
'react', |
| 54 |
'react-dom', |
| 55 |
'react-jsx-runtime', |
| 56 |
'wp-url', |
| 57 |
'wp-i18n', |
| 58 |
'wp-date', |
| 59 |
'wp-element', |
| 60 |
'wp-components', |
| 61 |
]; |
| 62 |
|
| 63 |
/** |
| 64 |
* Common CSS dependencies required for React components. |
| 65 |
* |
| 66 |
* @var string[] |
| 67 |
*/ |
| 68 |
public static array $style_deps = [ |
| 69 |
'wp-components', |
| 70 |
]; |
| 71 |
|
| 72 |
/** |
| 73 |
* Constructor. |
| 74 |
* |
| 75 |
* @param string $name The snippet page short name. |
| 76 |
* @param string $label The label shown in the admin menu. |
| 77 |
* @param string $title The text used for the page title. |
| 78 |
*/ |
| 79 |
public function __construct( string $name, string $label, string $title ) { |
| 80 |
$this->name = $name; |
| 81 |
$this->label = $label; |
| 82 |
$this->title = $title; |
| 83 |
|
| 84 |
$this->base_slug = code_snippets()->get_menu_slug(); |
| 85 |
$this->slug = code_snippets()->get_menu_slug( $name ); |
| 86 |
|
| 87 |
if ( ! code_snippets()->is_compact_menu() ) { |
| 88 |
add_action( 'admin_menu', array( $this, 'register' ) ); |
| 89 |
add_action( 'network_admin_menu', array( $this, 'register' ) ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Add a sub-menu to the Snippets menu. |
| 95 |
* |
| 96 |
* @param string $slug Menu slug. |
| 97 |
* @param string $label Label shown in admin menu. |
| 98 |
* @param string $title Page title. |
| 99 |
* |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
public function add_menu( string $slug, string $label, string $title ) { |
| 103 |
$hook = add_submenu_page( |
| 104 |
$this->base_slug, |
| 105 |
$title, |
| 106 |
$label, |
| 107 |
code_snippets()->get_cap(), |
| 108 |
$slug, |
| 109 |
array( $this, 'render' ) |
| 110 |
); |
| 111 |
|
| 112 |
add_action( 'load-' . $hook, array( $this, 'load' ) ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Register the admin menu |
| 117 |
*/ |
| 118 |
public function register() { |
| 119 |
$this->add_menu( $this->slug, $this->label, $this->title ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Retrieve the WordPress hookname computed for this menu's admin page. |
| 124 |
* |
| 125 |
* @return string |
| 126 |
*/ |
| 127 |
public function get_hookname(): string { |
| 128 |
return get_plugin_page_hookname( $this->slug, $this->base_slug ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Retrieve every WordPress hookname registered by this menu, including any |
| 133 |
* additional pages it registers beyond its primary slug. |
| 134 |
* |
| 135 |
* @return string[] |
| 136 |
*/ |
| 137 |
public function get_hooknames(): array { |
| 138 |
return [ $this->get_hookname() ]; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Render the navigation bar at the top of the admin page. |
| 143 |
*/ |
| 144 |
protected function render_navigation() { |
| 145 |
echo '<div id="code-snippets-toolbar-container"></div>'; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Render the menu |
| 150 |
*/ |
| 151 |
abstract public function render(); |
| 152 |
|
| 153 |
/** |
| 154 |
* Executed when the admin page is loaded |
| 155 |
*/ |
| 156 |
public function load() { |
| 157 |
// Make sure the user has permission to be here. |
| 158 |
if ( ! current_user_can( code_snippets()->get_cap() ) ) { |
| 159 |
wp_die( esc_html__( 'You are not authorized to access this page.', 'code-snippets' ) ); |
| 160 |
} |
| 161 |
|
| 162 |
// Create the snippet tables if they are missing. |
| 163 |
$db = code_snippets()->db; |
| 164 |
|
| 165 |
if ( is_multisite() ) { |
| 166 |
$db->create_missing_table( $db->ms_table ); |
| 167 |
} |
| 168 |
$db->create_missing_table( $db->table ); |
| 169 |
|
| 170 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Enqueue scripts and stylesheets for the admin page, if necessary |
| 175 |
*/ |
| 176 |
abstract public function enqueue_assets(); |
| 177 |
} |
| 178 |
|