forked from wpaam/AAM-Feature-Example
/
extension.php
243 lines (222 loc) · 8.71 KB
/
extension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/**
Copyright (C) Vasyl Martyniuk <support@wpaam.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Feature Example Controller
*
* Each feature should have atleast one controller. This is the main class that
* handle communication between AAM and feature. All filters and actions can be
* defined in this controller.
*
* Each controller must inherit AAM_Core_Extension class.
*
* @package AAM
* @author Vasyl Martyniuk <support@wpaam.com>
* @copyright Copyright C Vasyl Martyniuk
* @license GNU General Public License {@link http://www.gnu.org/licenses/}
*/
class AAM_Feature_Example extends AAM_Core_Extension {
/**
* Unique Feature ID
*
* Each Extension should have Unique ID or name that is used to mark the
* Extension. It is a good practise to have this ID defined in one place as
* constant.
*/
const FEATURE_ID = 'feature_example';
/**
* Constructor
*
* @param aam $parent Main AAM object
*
* @return void
*
* @access public
* @see aam It is class in advanced-access-manager/aam.php file
*/
public function __construct(aam $parent) {
parent::__construct($parent);
//include login counter object
require_once(dirname(__FILE__) . '/counter.php');
if (is_admin()) {
$this->registerFeature();
}
//define new AAM Object. See aam_Control_Subject::getObject function for more
//information about aam_object filter
add_filter('aam_object', array($this, 'counterObject'), 10, 4);
//login hooks
add_action('wp_login', array($this, 'login'), 10, 2);
}
/**
* Register feature
*
* The Feature Example Extension has UI interface. That is why it is very
* important to make sure that it is backend (there is no need to load this
* Extension if user is browsing frontend).
*
* From release 2.5, each Extension has its own capability assigned to it. By
* default all Extensions have capability "Administrator" but this can be changed
* with ConfigPress setting aam.feature.[you feature ID].capability.
* For more information about this part of functionality chech official AAM
* tutorial "AAM Super Admin" http://wpaam.com/tutorials/aam-super-admin/
*
* @return void
*
* @access protected
*/
protected function registerFeature() {
//add feature
$capability = aam_Core_ConfigPress::getParam(
'aam.feature.' . self::FEATURE_ID . '.capability',
'administrator'
);
//makre sure that current user has access to current Extension. This is
//mandatory check and should be obeyed by all developers
if (current_user_can($capability)) {
//register the Extension's javascript
add_action('admin_print_scripts', array($this, 'printScripts'));
//register the Extension's stylesheet
add_action('admin_print_styles', array($this, 'printStyles'));
//register the Feature
aam_View_Collection::registerFeature((object) array(
//uid is mandatory and this should be the unique ID
'uid' => self::FEATURE_ID,
//Extension Position is the list of AAM features. This works
//the same way as WordPress Admin Menu
'position' => 150,
//Extension's Title
'title' => __('Feature Example', 'aam'),
//Define what subjects can see the Extenion's UI. AAM is based on
//idea of Subjects (Blog, Role, User & Visitor) and Objects (Post,
//Term, Event, Menu etc). This property defines what Subjects are
//allowed to see this feature. So as Example if you click on Visitor
//(on the Control Manager) you will not find Menu or Metabox Features.
//This is because any visitor of your website does not have access to
//backend so there is no need to show these features.
'subjects' => array(
aam_Control_Subject_Role::UID,
aam_Control_Subject_User::UID,
aam_Control_Subject_Visitor::UID
),
//Reference to Extension's Controller. Make sure that if your
//Extension display any UI, the controller property contains the
//instance of the Extenion's Controller and Controller has function
//content. See aam_View_Manager::retrieveFeatures function for more
//information
'controller' => $this
));
}
}
/**
* Render UI Content
*
* If Extension shows UI, this function is mandatory and should return the HTML
* string.
*
* @param aam_Control_Subject $subject Current Subject
*
* @return string HTML Template
*
* @access public
* @see aam_View_Manager::retrieveFeatures
*/
public function content(aam_Control_Subject $subject) {
ob_start();
require dirname(__FILE__) . '/ui.phtml';
$content = ob_get_contents();
ob_end_clean();
return $content;
}
/**
* Register Extension's javascript
*
* This function should check if user is on AAM Page. Otherwise there is no need
* to load any javascript to the header of HTML page.
*
* @return void
*
* @access public
* @see aam::isAAMScreen
*/
public function printScripts() {
if ($this->getParent()->isAAMScreen()) {
wp_enqueue_script(
'aam-feature-example-admin',
AAM_FEATURE_EXAMPLE_BASE_URL . '/javascript.js',
array('aam-admin') //make sure that main aam javascript is loaded
);
}
}
/**
* Register Extenion's stylesheet
*
* This function should check if user is on AAM Page. Otherwise there is no need
* to load any stylesheet to the header of HTML page.
*
* @return void
*
* @access public
* @see aam::isAAMScreen
*/
public function printStyles() {
if ($this->getParent()->isAAMScreen()) {
wp_enqueue_style(
'aam-feature-example-admin',
AAM_FEATURE_EXAMPLE_BASE_URL . '/stylesheet.css',
array('aam-style') //Extension can overwrite the main AAM style
);
}
}
/**
* Retrieve the Login Counter Object
*
* The filter aam_object is shared by all AAM Extensions that is why the check
* if $object_id equals the Extension's Object. See the first if statement in
* this function.
*
* @param null $object Default Object
* @param int $object_uid Current User ID
* @param string $object_id Request Object ID
* @param aam_Control_Subject $subject Current Subject
*
* @return aam_Control_Object_LoginCounter
*
* @access public
* @see aam_Control_Subject::getObject
*/
public function counterObject($object, $object_uid, $object_id, $subject) {
if ($object_uid === aam_Control_Object_LoginCounter::UID) {
$object = new aam_Control_Object_LoginCounter($subject, $object_id);
}
return $object;
}
/**
* User Login Hook
*
* This hook track the user's successfull login and increase the login counter.
*
* @param string $username User Login name
* @param Wp_User $user Current user object
*
* @return void
*
* @access public
*/
public function login($username, $user) {
//get current logged in Subject and increment the Login Counter
$this->getParent()->getUser()
->getObject(aam_Control_Object_LoginCounter::UID)
->increment();
}
}