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