| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets; |
| 4 |
|
| 5 |
/** |
| 6 |
* Base class for a plugin admin menu. |
| 7 |
*/ |
| 8 |
abstract class Admin_Menu { |
| 9 |
|
| 10 |
/** |
| 11 |
* The snippet page short name. |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
public string $name; |
| 16 |
|
| 17 |
/** |
| 18 |
* The label shown in the admin menu. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public string $label; |
| 23 |
|
| 24 |
/** |
| 25 |
* The text used for the page title. |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
public string $title; |
| 30 |
|
| 31 |
/** |
| 32 |
* The base slug for the top-level admin menu. |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
protected string $base_slug; |
| 37 |
|
| 38 |
/** |
| 39 |
* The slug for this admin menu. |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
protected string $slug; |
| 44 |
|
| 45 |
/** |
| 46 |
* Constructor. |
| 47 |
* |
| 48 |
* @param string $name The snippet page short name. |
| 49 |
* @param string $label The label shown in the admin menu. |
| 50 |
* @param string $title The text used for the page title. |
| 51 |
*/ |
| 52 |
public function __construct( string $name, string $label, string $title ) { |
| 53 |
$this->name = $name; |
| 54 |
$this->label = $label; |
| 55 |
$this->title = $title; |
| 56 |
|
| 57 |
$this->base_slug = code_snippets()->get_menu_slug(); |
| 58 |
$this->slug = code_snippets()->get_menu_slug( $name ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Register action and filter hooks. |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function run() { |
| 67 |
if ( ! code_snippets()->is_compact_menu() ) { |
| 68 |
add_action( 'admin_menu', array( $this, 'register' ) ); |
| 69 |
add_action( 'network_admin_menu', array( $this, 'register' ) ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Add a sub-menu to the Snippets menu. |
| 75 |
* |
| 76 |
* @param string $slug Menu slug. |
| 77 |
* @param string $label Label shown in admin menu. |
| 78 |
* @param string $title Page title. |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function add_menu( string $slug, string $label, string $title ) { |
| 83 |
$hook = add_submenu_page( |
| 84 |
$this->base_slug, |
| 85 |
$title, |
| 86 |
$label, |
| 87 |
code_snippets()->get_cap(), |
| 88 |
$slug, |
| 89 |
array( $this, 'render' ) |
| 90 |
); |
| 91 |
|
| 92 |
add_action( 'load-' . $hook, array( $this, 'load' ) ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Register the admin menu |
| 97 |
*/ |
| 98 |
public function register() { |
| 99 |
$this->add_menu( $this->slug, $this->label, $this->title ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Render the content of a vew template |
| 104 |
* |
| 105 |
* @param string $name Name of view template to render. |
| 106 |
*/ |
| 107 |
protected function render_view( string $name ) { |
| 108 |
include dirname( PLUGIN_FILE ) . '/php/views/' . $name . '.php'; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Render the menu |
| 113 |
*/ |
| 114 |
public function render() { |
| 115 |
$this->render_view( $this->name ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Print the status and error messages |
| 120 |
*/ |
| 121 |
protected function print_messages() { |
| 122 |
// None required by default. |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Executed when the admin page is loaded |
| 127 |
*/ |
| 128 |
public function load() { |
| 129 |
// Make sure the user has permission to be here. |
| 130 |
if ( ! current_user_can( code_snippets()->get_cap() ) ) { |
| 131 |
wp_die( esc_html__( 'You are not authorized to access this page.', 'code-snippets' ) ); |
| 132 |
} |
| 133 |
|
| 134 |
// Create the snippet tables if they are missing. |
| 135 |
$db = code_snippets()->db; |
| 136 |
|
| 137 |
if ( is_multisite() ) { |
| 138 |
$db->create_missing_table( $db->ms_table ); |
| 139 |
} |
| 140 |
$db->create_missing_table( $db->table ); |
| 141 |
|
| 142 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Enqueue scripts and stylesheets for the admin page, if necessary |
| 147 |
*/ |
| 148 |
abstract public function enqueue_assets(); |
| 149 |
|
| 150 |
/** |
| 151 |
* Generate a list of page title links for passing to React. |
| 152 |
* |
| 153 |
* @param array<string> $actions List of actions to convert into links, as array values. |
| 154 |
* |
| 155 |
* @return array<string, string> Link labels keyed to link URLs. |
| 156 |
*/ |
| 157 |
public function page_title_action_links( array $actions ): array { |
| 158 |
$plugin = code_snippets(); |
| 159 |
$links = []; |
| 160 |
|
| 161 |
foreach ( $actions as $action ) { |
| 162 |
if ( 'settings' === $action && ! isset( $plugin->admin->menus['settings'] ) ) { |
| 163 |
continue; |
| 164 |
} |
| 165 |
|
| 166 |
$url = $plugin->get_menu_url( $action ); |
| 167 |
|
| 168 |
if ( isset( $_GET['type'] ) && in_array( $_GET['type'], Snippet::get_types(), true ) ) { |
| 169 |
$url = add_query_arg( 'type', sanitize_key( wp_unslash( $_GET['type'] ) ), $url ); |
| 170 |
} |
| 171 |
|
| 172 |
switch ( $action ) { |
| 173 |
case 'manage': |
| 174 |
$label = _x( 'Manage', 'snippets', 'code-snippets' ); |
| 175 |
break; |
| 176 |
case 'add': |
| 177 |
$label = _x( 'Add New', 'snippet', 'code-snippets' ); |
| 178 |
break; |
| 179 |
case 'import': |
| 180 |
$label = _x( 'Import', 'snippets', 'code-snippets' ); |
| 181 |
break; |
| 182 |
case 'settings': |
| 183 |
$label = _x( 'Settings', 'snippets', 'code-snippets' ); |
| 184 |
break; |
| 185 |
default: |
| 186 |
$label = ''; |
| 187 |
} |
| 188 |
|
| 189 |
if ( $label && $url ) { |
| 190 |
$links[ $label ] = $url; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
return $links; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Render a list of links to other pages in the page title |
| 199 |
* |
| 200 |
* @param array<string> $actions List of actions to render as links, as array values. |
| 201 |
*/ |
| 202 |
public function render_page_title_actions( array $actions ) { |
| 203 |
foreach ( $this->page_title_action_links( $actions ) as $label => $url ) { |
| 204 |
printf( '<a href="%s" class="page-title-action">%s</a>', esc_url( $url ), esc_html( $label ) ); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|