PluginProbe
Code Snippets / 2.14.0
Code Snippets v2.14.0
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / php / admin-menus / class-admin-menu.php

class-admin-menu.php in Code Snippets 2.14.0, at php/admin-menus/class-admin-menu.php

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