| 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 $name; |
| 16 |
|
| 17 |
/** |
| 18 |
* The label shown in the admin menu |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public $label; |
| 23 |
|
| 24 |
/** |
| 25 |
* The text used for the page title |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
public $title; |
| 30 |
|
| 31 |
/** |
| 32 |
* The base slug for the top-level admin menu. |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
protected $base_slug; |
| 37 |
|
| 38 |
/** |
| 39 |
* The slug for this admin menu. |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
protected $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( $name, $label, $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 |
public function run() { |
| 65 |
if ( ! code_snippets()->is_compact_menu() ) { |
| 66 |
add_action( 'admin_menu', array( $this, 'register' ) ); |
| 67 |
add_action( 'network_admin_menu', array( $this, 'register' ) ); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Add a sub-menu to the Snippets menu |
| 73 |
* |
| 74 |
* @param string $slug Menu slug. |
| 75 |
* @param string $label Label shown in admin menu. |
| 76 |
* @param string $title Page title. |
| 77 |
* |
| 78 |
* @uses add_submenu_page() to register a submenu |
| 79 |
*/ |
| 80 |
public function add_menu( $slug, $label, $title ) { |
| 81 |
$hook = add_submenu_page( |
| 82 |
$this->base_slug, |
| 83 |
$title, |
| 84 |
$label, |
| 85 |
code_snippets()->get_cap(), |
| 86 |
$slug, |
| 87 |
array( $this, 'render' ) |
| 88 |
); |
| 89 |
|
| 90 |
add_action( 'load-' . $hook, array( $this, 'load' ) ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Register the admin menu |
| 95 |
*/ |
| 96 |
public function register() { |
| 97 |
$this->add_menu( $this->slug, $this->label, $this->title ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Render the content of a vew template |
| 102 |
* |
| 103 |
* @param string $name Name of view template to render. |
| 104 |
*/ |
| 105 |
protected function render_view( $name ) { |
| 106 |
include dirname( PLUGIN_FILE ) . '/php/views/' . $name . '.php'; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Render the menu |
| 111 |
*/ |
| 112 |
public function render() { |
| 113 |
$this->render_view( $this->name ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Print the status and error messages |
| 118 |
*/ |
| 119 |
abstract protected function print_messages(); |
| 120 |
|
| 121 |
/** |
| 122 |
* Retrieve a result message based on a posted status |
| 123 |
* |
| 124 |
* @param array $messages List of possible messages to display. |
| 125 |
* @param string $request_var Name of $_REQUEST variable to check. |
| 126 |
* @param string $class Class to use on buttons. Default 'updated'. |
| 127 |
* |
| 128 |
* @return bool Whether a result message was printed. |
| 129 |
*/ |
| 130 |
protected function print_result_message( $messages, $request_var = 'result', $class = 'updated' ) { |
| 131 |
|
| 132 |
if ( empty( $_REQUEST[ $request_var ] ) ) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
$result = sanitize_key( $_REQUEST[ $request_var ] ); |
| 137 |
|
| 138 |
if ( isset( $messages[ $result ] ) ) { |
| 139 |
printf( |
| 140 |
'<div id="message" class="%2$s fade"><p>%1$s</p></div>', |
| 141 |
wp_kses_post( $messages[ $result ] ), |
| 142 |
esc_attr( $class ) |
| 143 |
); |
| 144 |
|
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Print a result message based on a posted status |
| 153 |
* |
| 154 |
* @param array $messages List of possible messages to display. |
| 155 |
* @param string $request_var Name of $_REQUEST variable to check. |
| 156 |
* @param string $class Class to use on buttons. Default 'updated'. |
| 157 |
*/ |
| 158 |
protected function show_result_message( $messages, $request_var = 'result', $class = 'updated' ) { |
| 159 |
$message = $this->get_result_message( $messages, $request_var, $class ); |
| 160 |
|
| 161 |
if ( $message ) { |
| 162 |
echo wp_kses_post( $message ); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Executed when the admin page is loaded |
| 168 |
*/ |
| 169 |
public function load() { |
| 170 |
/* Make sure the user has permission to be here */ |
| 171 |
if ( ! current_user_can( code_snippets()->get_cap() ) ) { |
| 172 |
wp_die( esc_html__( 'You are not authorized to access this page.', 'code-snippets' ) ); |
| 173 |
} |
| 174 |
|
| 175 |
/* Create the snippet tables if they don't exist */ |
| 176 |
$db = code_snippets()->db; |
| 177 |
|
| 178 |
if ( is_multisite() ) { |
| 179 |
$db->create_missing_table( $db->ms_table ); |
| 180 |
} |
| 181 |
$db->create_missing_table( $db->table ); |
| 182 |
|
| 183 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Enqueue scripts and stylesheets for the admin page, if necessary |
| 188 |
*/ |
| 189 |
abstract public function enqueue_assets(); |
| 190 |
|
| 191 |
/** |
| 192 |
* Render a list of links to other pages in the page title |
| 193 |
* |
| 194 |
* @param array $actions List of actions to render as links, as array values. |
| 195 |
*/ |
| 196 |
protected function page_title_actions( $actions ) { |
| 197 |
|
| 198 |
foreach ( $actions as $action ) { |
| 199 |
if ( 'settings' === $action && ! isset( code_snippets()->admin->menus['settings'] ) ) { |
| 200 |
continue; |
| 201 |
} |
| 202 |
|
| 203 |
$url = code_snippets()->get_menu_url( $action ); |
| 204 |
|
| 205 |
if ( isset( $_GET['type'] ) && in_array( $_GET['type'], Snippet::get_types(), true ) ) { |
| 206 |
$url = add_query_arg( 'type', sanitize_key( wp_unslash( $_GET['type'] ) ), $url ); |
| 207 |
} |
| 208 |
|
| 209 |
printf( '<a href="%s" class="page-title-action">', esc_url( $url ) ); |
| 210 |
|
| 211 |
switch ( $action ) { |
| 212 |
case 'manage': |
| 213 |
echo esc_html_x( 'Manage', 'snippets', 'code-snippets' ); |
| 214 |
break; |
| 215 |
case 'add': |
| 216 |
echo esc_html_x( 'Add New', 'snippet', 'code-snippets' ); |
| 217 |
break; |
| 218 |
case 'import': |
| 219 |
echo esc_html_x( 'Import', 'snippets', 'code-snippets' ); |
| 220 |
break; |
| 221 |
case 'settings': |
| 222 |
echo esc_html_x( 'Settings', 'snippets', 'code-snippets' ); |
| 223 |
break; |
| 224 |
} |
| 225 |
|
| 226 |
echo '</a>'; |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|