PluginProbe
Code Snippets / 2.14.3
Code Snippets v2.14.3
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.3, at php/admin-menus/class-admin-menu.php

147 lines 3.2 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 *
53 * @param string $slug The slug of the menu
54 * @param string $label The label shown in the admin menu
55 * @param string $title The page title
56 *
57 * @uses add_submenu_page() to register a submenu
58 *
59 */
60 public function add_menu( $slug, $label, $title ) {
61 $hook = add_submenu_page(
62 $this->base_slug,
63 $title,
64 $label,
65 code_snippets()->get_cap(),
66 $slug,
67 array( $this, 'render' )
68 );
69
70 add_action( 'load-' . $hook, array( $this, 'load' ) );
71 }
72
73 /**
74 * Register the admin menu
75 */
76 public function register() {
77 $this->add_menu( $this->slug, $this->label, $this->title );
78 }
79
80 /**
81 * Render the menu
82 */
83 public function render() {
84 $this->print_messages();
85 include dirname( dirname( __FILE__ ) ) . "/views/{$this->name}.php";
86 }
87
88 /**
89 * Print the status and error messages
90 */
91 protected function print_messages() {
92 }
93
94 /**
95 * Retrieve a result message based on a posted status
96 *
97 * @param array $messages
98 * @param string $request_var
99 * @param string $class
100 *
101 * @return string|bool The result message if a valid status was received, otherwise false
102 */
103 protected function get_result_message( $messages, $request_var = 'result', $class = 'updated' ) {
104
105 if ( empty( $_REQUEST[ $request_var ] ) ) {
106 return false;
107 }
108
109 $result = $_REQUEST[ $request_var ];
110
111 if ( isset( $messages[ $result ] ) ) {
112 return sprintf(
113 '<div id="message" class="%2$s fade"><p>%1$s</p></div>',
114 $messages[ $result ], $class
115 );
116 }
117
118 return false;
119 }
120
121 /**
122 * Executed when the admin page is loaded
123 */
124 public function load() {
125 /* Make sure the user has permission to be here */
126 if ( ! current_user_can( code_snippets()->get_cap() ) ) {
127 wp_die( __( 'You are not authorized to access this page.', 'code-snippets' ) );
128 }
129
130 /* Create the snippet tables if they don't exist */
131 $db = code_snippets()->db;
132
133 if ( is_multisite() ) {
134 $db->create_missing_table( $db->ms_table );
135 }
136 $db->create_missing_table( $db->table );
137
138 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
139 }
140
141 /**
142 * Enqueue scripts and stylesheets for the admin page, if necessary
143 */
144 public function enqueue_assets() {
145 }
146 }
147