|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Pages: Abstract Admin Page. |
|
4
|
|
|
* |
|
5
|
|
|
* An abstract class meant to be extended by admin pages. |
|
6
|
|
|
* |
|
7
|
|
|
* @since 3.11.0 |
|
8
|
|
|
* @package Wordlift |
|
9
|
|
|
* @subpackage Wordlift/admin |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Define the {@link Wordlift_Admin_Page} class. |
|
14
|
|
|
* |
|
15
|
|
|
* @since 3.11.0 |
|
16
|
|
|
* @package Wordlift |
|
17
|
|
|
* @subpackage Wordlift/admin |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class Wordlift_Admin_Page { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Get the parent slug. |
|
23
|
|
|
* |
|
24
|
|
|
* @since 3.11.0 |
|
25
|
|
|
* |
|
26
|
|
|
* @return string The parent slug (default 'wl_admin_menu'). |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function get_parent_slug() { |
|
29
|
|
|
|
|
30
|
|
|
return 'wl_admin_menu'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get the required capability. |
|
35
|
|
|
* |
|
36
|
|
|
* @since 3.11.0 |
|
37
|
|
|
* |
|
38
|
|
|
* @return string The capability (default 'manage_options'). |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function get_capability() { |
|
41
|
|
|
|
|
42
|
|
|
return 'manage_options'; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get the page title. Will be translated. |
|
47
|
|
|
* |
|
48
|
|
|
* @since 3.11.0 |
|
49
|
|
|
* |
|
50
|
|
|
* @return string The page title. |
|
51
|
|
|
*/ |
|
52
|
|
|
abstract function get_page_title(); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get the menu title. Will be translated. |
|
56
|
|
|
* |
|
57
|
|
|
* @since 3.11.0 |
|
58
|
|
|
* |
|
59
|
|
|
* @return string The menu title. |
|
60
|
|
|
*/ |
|
61
|
|
|
abstract function get_menu_title(); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the menu slug. |
|
65
|
|
|
* |
|
66
|
|
|
* @since 3.11.0 |
|
67
|
|
|
* |
|
68
|
|
|
* @return string The menu slug. |
|
69
|
|
|
*/ |
|
70
|
|
|
abstract function get_menu_slug(); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get the page url. |
|
74
|
|
|
* |
|
75
|
|
|
* @since 3.14.0 |
|
76
|
|
|
* |
|
77
|
|
|
* @return string The escaped url of the admin page |
|
78
|
|
|
*/ |
|
79
|
|
|
function get_url() { |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
// ideally should have used menu_page_url, but it is loaded later than some usages. |
|
82
|
|
|
$url = admin_url( 'admin.php?page=' . $this->get_menu_slug() ); |
|
83
|
|
|
|
|
84
|
|
|
return esc_url( $url ); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get the partial file name, used in the {@link render} function. |
|
89
|
|
|
* |
|
90
|
|
|
* @since 3.11.0 |
|
91
|
|
|
* |
|
92
|
|
|
* @return string The partial file name. |
|
93
|
|
|
*/ |
|
94
|
|
|
abstract function get_partial_name(); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* The `admin_menu` callback. Will call {@link add_submenu_page} to add the |
|
98
|
|
|
* page to the admin menu. |
|
99
|
|
|
* |
|
100
|
|
|
* @since 3.11.0 |
|
101
|
|
|
* |
|
102
|
|
|
* @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
103
|
|
|
*/ |
|
104
|
|
|
public function admin_menu() { |
|
105
|
|
|
|
|
106
|
|
|
// Add the sub-menu page. |
|
107
|
|
|
// |
|
108
|
|
|
// See http://codex.wordpress.org/Function_Reference/add_submenu_page |
|
109
|
|
|
$page = add_submenu_page( |
|
110
|
|
|
$this->get_parent_slug(), |
|
111
|
|
|
$this->get_page_title(), |
|
112
|
|
|
$this->get_menu_title(), |
|
113
|
|
|
$this->get_capability(), // The required capability, provided by the calling hook. |
|
114
|
|
|
$this->get_menu_slug(), |
|
115
|
|
|
array( $this, 'render' ) |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
// Set a hook to enqueue scripts only when the settings page is displayed. |
|
119
|
|
|
add_action( 'admin_print_scripts-' . $page, array( |
|
120
|
|
|
$this, |
|
121
|
|
|
'enqueue_scripts', |
|
122
|
|
|
) ); |
|
123
|
|
|
|
|
124
|
|
|
// Finally return the page hook_suffix. |
|
125
|
|
|
return $page; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Enqueue scripts for the specific page. Subclasses can override this function |
|
130
|
|
|
* to provide their own styles/scripts. |
|
131
|
|
|
* |
|
132
|
|
|
* @since 3.11.0 |
|
133
|
|
|
*/ |
|
134
|
|
|
public function enqueue_scripts() { |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Render the page. |
|
139
|
|
|
*/ |
|
140
|
|
|
public function render() { |
|
141
|
|
|
|
|
142
|
|
|
// Include the partial. |
|
143
|
|
|
include( plugin_dir_path( __FILE__ ) . 'partials/' . $this->get_partial_name() ); |
|
144
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.