| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Base class for a snippets admin menu |
| 5 |
*/ |
| 6 |
class Code_Snippets_Admin_Menu { |
| 7 |
|
| 8 |
public $name, $label, $title; |
| 9 |
|
| 10 |
/** |
| 11 |
* Constructor |
| 12 |
* @param string $name The snippet page shortname |
| 13 |
* @param string $label The label shown in the admin menu |
| 14 |
* @param string $title The text used for the page title |
| 15 |
*/ |
| 16 |
function __construct( $name, $label, $title ) { |
| 17 |
$this->name = $name; |
| 18 |
$this->label = $label; |
| 19 |
$this->title = $title; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Register action and filter hooks |
| 24 |
*/ |
| 25 |
public function run() { |
| 26 |
add_action( 'admin_menu', array( $this, 'register' ) ); |
| 27 |
add_action( 'network_admin_menu', array( $this, 'register' ) ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Add a sub-menu to the Snippets menu |
| 32 |
* @uses add_submenu_page() to register a submenu |
| 33 |
* @param string $slug The slug of the menu |
| 34 |
* @param string $label The label shown in the admin menu |
| 35 |
* @param string $title The page title |
| 36 |
*/ |
| 37 |
public function add_menu( $slug, $label, $title ) { |
| 38 |
$hook = add_submenu_page( |
| 39 |
code_snippets()->get_menu_slug(), |
| 40 |
$title, |
| 41 |
$label, |
| 42 |
code_snippets()->get_cap(), |
| 43 |
$slug, |
| 44 |
array( $this, 'render' ) |
| 45 |
); |
| 46 |
|
| 47 |
add_action( 'load-' . $hook, array( $this, 'load' ) ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Register the admin menu |
| 52 |
*/ |
| 53 |
public function register() { |
| 54 |
$this->add_menu( code_snippets()->get_menu_slug( $this->name ), $this->label, $this->title ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Render the menu |
| 59 |
*/ |
| 60 |
public function render() { |
| 61 |
$this->print_messages(); |
| 62 |
include dirname( dirname( __FILE__ ) ) . "/views/{$this->name}.php"; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Print the status and error messages |
| 67 |
*/ |
| 68 |
protected function print_messages() {} |
| 69 |
|
| 70 |
/** |
| 71 |
* Retrieve a result message based on a posted status |
| 72 |
* |
| 73 |
* @param array $messages |
| 74 |
* @param string $request_var |
| 75 |
* @param string $class |
| 76 |
* |
| 77 |
* @return string|bool The result message if a valid status was received, otherwise false |
| 78 |
*/ |
| 79 |
protected function get_result_message( $messages, $request_var = 'result', $class = 'updated' ) { |
| 80 |
|
| 81 |
if ( empty( $_REQUEST[ $request_var ] ) ) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
$result = $_REQUEST[ $request_var ]; |
| 86 |
|
| 87 |
if ( isset( $messages[ $result ] ) ) { |
| 88 |
return sprintf( |
| 89 |
'<div id="message" class="%2$s fade"><p>%1$s</p></div>', |
| 90 |
$messages[ $result ], $class |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Executed when the admin page is loaded |
| 99 |
*/ |
| 100 |
public function load() { |
| 101 |
/* Make sure the user has permission to be here */ |
| 102 |
if ( ! current_user_can( code_snippets()->get_cap() ) ) { |
| 103 |
wp_die( __( 'You are not authorized to access this page.', 'code-snippets' ) ); |
| 104 |
} |
| 105 |
|
| 106 |
/* Create the snippet tables if they don't exist */ |
| 107 |
code_snippets()->db->create_tables(); |
| 108 |
} |
| 109 |
} |
| 110 |
|