| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Base class for a snippets admin menu |
| 5 |
*/ |
| 6 |
class Code_Snippets_Admin_Menu { |
| 7 |
|
| 8 |
public $name; |
| 9 |
|
| 10 |
public $label; |
| 11 |
|
| 12 |
public $title; |
| 13 |
|
| 14 |
/** |
| 15 |
* The base slug for the top-level admin menu. |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
protected $base_slug; |
| 20 |
|
| 21 |
/** |
| 22 |
* The slug for this admin menu. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
protected $slug; |
| 27 |
|
| 28 |
/** |
| 29 |
* Constructor |
| 30 |
* |
| 31 |
* @param string $name The snippet page short name |
| 32 |
* @param string $label The label shown in the admin menu |
| 33 |
* @param string $title The text used for the page title |
| 34 |
*/ |
| 35 |
public function __construct( $name, $label, $title ) { |
| 36 |
$this->name = $name; |
| 37 |
$this->label = $label; |
| 38 |
$this->title = $title; |
| 39 |
|
| 40 |
$this->base_slug = code_snippets()->get_menu_slug(); |
| 41 |
$this->slug = code_snippets()->get_menu_slug( $name ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Register action and filter hooks |
| 46 |
*/ |
| 47 |
public function run() { |
| 48 |
if ( ! code_snippets()->admin->is_compact_menu() ) { |
| 49 |
add_action( 'admin_menu', array( $this, 'register' ) ); |
| 50 |
add_action( 'network_admin_menu', array( $this, 'register' ) ); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Add a sub-menu to the Snippets menu |
| 56 |
* |
| 57 |
* @param string $slug The slug of the menu |
| 58 |
* @param string $label The label shown in the admin menu |
| 59 |
* @param string $title The page title |
| 60 |
* |
| 61 |
* @uses add_submenu_page() to register a submenu |
| 62 |
* |
| 63 |
*/ |
| 64 |
public function add_menu( $slug, $label, $title ) { |
| 65 |
$hook = add_submenu_page( |
| 66 |
$this->base_slug, |
| 67 |
$title, |
| 68 |
$label, |
| 69 |
code_snippets()->get_cap(), |
| 70 |
$slug, |
| 71 |
array( $this, 'render' ) |
| 72 |
); |
| 73 |
|
| 74 |
add_action( 'load-' . $hook, array( $this, 'load' ) ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Register the admin menu |
| 79 |
*/ |
| 80 |
public function register() { |
| 81 |
$this->add_menu( $this->slug, $this->label, $this->title ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Render the menu |
| 86 |
*/ |
| 87 |
public function render() { |
| 88 |
$this->print_messages(); |
| 89 |
include dirname( dirname( __FILE__ ) ) . "/views/{$this->name}.php"; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Print the status and error messages |
| 94 |
*/ |
| 95 |
protected function print_messages() { |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Retrieve a result message based on a posted status |
| 100 |
* |
| 101 |
* @param array $messages |
| 102 |
* @param string $request_var |
| 103 |
* @param string $class |
| 104 |
* |
| 105 |
* @return bool Whether a result message was printed. |
| 106 |
*/ |
| 107 |
protected function print_result_message( $messages, $request_var = 'result', $class = 'updated' ) { |
| 108 |
|
| 109 |
if ( empty( $_REQUEST[ $request_var ] ) ) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
$result = sanitize_key( $_REQUEST[ $request_var ] ); |
| 114 |
|
| 115 |
if ( isset( $messages[ $result ] ) ) { |
| 116 |
printf( |
| 117 |
'<div id="message" class="%2$s fade"><p>%1$s</p></div>', |
| 118 |
wp_kses_post( $messages[ $result ] ), |
| 119 |
esc_attr( $class ) |
| 120 |
); |
| 121 |
|
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Executed when the admin page is loaded |
| 130 |
*/ |
| 131 |
public function load() { |
| 132 |
/* Make sure the user has permission to be here */ |
| 133 |
if ( ! current_user_can( code_snippets()->get_cap() ) ) { |
| 134 |
wp_die( esc_html__( 'You are not authorized to access this page.', 'code-snippets' ) ); |
| 135 |
} |
| 136 |
|
| 137 |
/* Create the snippet tables if they don't exist */ |
| 138 |
$db = code_snippets()->db; |
| 139 |
|
| 140 |
if ( is_multisite() ) { |
| 141 |
$db->create_missing_table( $db->ms_table ); |
| 142 |
} |
| 143 |
$db->create_missing_table( $db->table ); |
| 144 |
|
| 145 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Enqueue scripts and stylesheets for the admin page, if necessary |
| 150 |
*/ |
| 151 |
public function enqueue_assets() { |
| 152 |
} |
| 153 |
} |
| 154 |
|