PluginProbe
Code Snippets / 2.13.0
Code Snippets v2.13.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.13.0, at php/admin-menus/class-admin-menu.php

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