1 | <?php |
---|
2 | /* |
---|
3 | Plugin Name: Trac 16808 |
---|
4 | Plugin URI: http://wordpress.org |
---|
5 | Description: Trac 16808 |
---|
6 | Author: Alex Koti |
---|
7 | Version: 1 |
---|
8 | Author URI: http://alexkoti.com |
---|
9 | */ |
---|
10 | |
---|
11 | /** |
---|
12 | * Activation |
---|
13 | * Create subscriber test user |
---|
14 | * Assign 'argus_visitors' cap to user |
---|
15 | * |
---|
16 | */ |
---|
17 | register_activation_hook( __FILE__, 'trac16808_activation' ); |
---|
18 | function trac16808_activation(){ |
---|
19 | if ( ! current_user_can( 'activate_plugins' ) ) |
---|
20 | return; |
---|
21 | $plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : ''; |
---|
22 | check_admin_referer( "activate-plugin_{$plugin}" ); |
---|
23 | |
---|
24 | // new subscriber user |
---|
25 | $new_user_id = wp_create_user( 'trac16808', 'trac16808', 'trac16808@trac16808.com' ); |
---|
26 | |
---|
27 | // add 'argus_visitors' cap |
---|
28 | $role_subscriber = get_role( 'subscriber' ); |
---|
29 | $role_subscriber->add_cap( 'argus_visitors' ); |
---|
30 | } |
---|
31 | |
---|
32 | /** |
---|
33 | * Deactivation |
---|
34 | * Delete test user and remove 'argus_visitors' cap |
---|
35 | * |
---|
36 | */ |
---|
37 | register_deactivation_hook( __FILE__, 'trac16808_deactivation' ); |
---|
38 | function trac16808_deactivation(){ |
---|
39 | if ( ! current_user_can( 'activate_plugins' ) ) |
---|
40 | return; |
---|
41 | $plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : ''; |
---|
42 | check_admin_referer( "deactivate-plugin_{$plugin}" ); |
---|
43 | |
---|
44 | // remove 'argus_visitors' cap |
---|
45 | $role_subscriber = get_role( 'subscriber' ); |
---|
46 | $role_subscriber->remove_cap( 'argus_visitors' ); |
---|
47 | |
---|
48 | $user = get_user_by( 'login', 'trac16808' ); |
---|
49 | wp_delete_user( $user->ID, 1 ); |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Register post types |
---|
54 | * |
---|
55 | * 1) Argus > Visitor : cap > admin, subscriber |
---|
56 | * 2) Argus > Comics : cap > admin, subscriber |
---|
57 | * 3) Argus > Candies : cap > admin |
---|
58 | * 4) Top Level > Toys : cap > admin, subscriber |
---|
59 | * 5) Top Level > Books : cap > admin |
---|
60 | * |
---|
61 | */ |
---|
62 | add_action( 'init', 'trac16808_register_post_types' ); |
---|
63 | function trac16808_register_post_types(){ |
---|
64 | |
---|
65 | /** |
---|
66 | * CPT Visitors inside Argus admin page |
---|
67 | * Subscriber have permission to add new posts |
---|
68 | * |
---|
69 | */ |
---|
70 | $args = array( |
---|
71 | 'labels' => array ( |
---|
72 | 'name' => 'Visitors', |
---|
73 | 'singular_name' => 'Visitor', |
---|
74 | 'add_new' => 'Register New', |
---|
75 | 'add_new_item' => 'Register New Visitor', |
---|
76 | ), |
---|
77 | 'public' => true, |
---|
78 | 'publicly_queryable' => false, |
---|
79 | 'exclude_from_search' => true, |
---|
80 | 'show_ui' => true, |
---|
81 | 'show_in_menu' => 'argus', // remove this line and the subscriber will can add new post |
---|
82 | 'hiearchical' => false, |
---|
83 | 'supports' => array( |
---|
84 | 'title', |
---|
85 | 'editor', |
---|
86 | ), |
---|
87 | 'capabilities' => array( |
---|
88 | 'edit_post' => 'argus_visitors', |
---|
89 | 'read_post' => 'argus_visitors', |
---|
90 | 'delete_post' => 'argus_visitors', |
---|
91 | 'edit_posts' => 'argus_visitors', |
---|
92 | 'edit_others_posts' => 'argus_visitors', |
---|
93 | 'publish_posts' => 'argus_visitors', |
---|
94 | 'read_private_posts' => 'argus_visitors', |
---|
95 | 'read' => 'argus_visitors', |
---|
96 | 'delete_posts' => 'argus_visitors', |
---|
97 | 'delete_private_posts' => 'argus_visitors', |
---|
98 | 'delete_published_posts' => 'argus_visitors', |
---|
99 | 'delete_others_posts' => 'argus_visitors', |
---|
100 | 'edit_private_posts' => 'argus_visitors', |
---|
101 | 'edit_published_posts' => 'argus_visitors', |
---|
102 | ), |
---|
103 | ); |
---|
104 | register_post_type( 'visitor', $args ); |
---|
105 | |
---|
106 | /** |
---|
107 | * CPT Comics inside Argus admin page |
---|
108 | * Subscriber have permission to add new posts |
---|
109 | * |
---|
110 | */ |
---|
111 | $args = array( |
---|
112 | 'labels' => array ( |
---|
113 | 'name' => 'Comics', |
---|
114 | 'singular_name' => 'Comic', |
---|
115 | 'add_new' => 'Add New', |
---|
116 | 'add_new_item' => 'Add New Comic', |
---|
117 | ), |
---|
118 | 'public' => true, |
---|
119 | 'publicly_queryable' => false, |
---|
120 | 'exclude_from_search' => true, |
---|
121 | 'show_ui' => true, |
---|
122 | 'show_in_menu' => 'argus', // remove this line and the subscriber will can add new post |
---|
123 | 'hiearchical' => false, |
---|
124 | 'supports' => array( |
---|
125 | 'title', |
---|
126 | 'editor', |
---|
127 | ), |
---|
128 | 'capabilities' => array( |
---|
129 | 'edit_post' => 'argus_visitors', |
---|
130 | 'read_post' => 'argus_visitors', |
---|
131 | 'delete_post' => 'argus_visitors', |
---|
132 | 'edit_posts' => 'argus_visitors', |
---|
133 | 'edit_others_posts' => 'argus_visitors', |
---|
134 | 'publish_posts' => 'argus_visitors', |
---|
135 | 'read_private_posts' => 'argus_visitors', |
---|
136 | 'read' => 'argus_visitors', |
---|
137 | 'delete_posts' => 'argus_visitors', |
---|
138 | 'delete_private_posts' => 'argus_visitors', |
---|
139 | 'delete_published_posts' => 'argus_visitors', |
---|
140 | 'delete_others_posts' => 'argus_visitors', |
---|
141 | 'edit_private_posts' => 'argus_visitors', |
---|
142 | 'edit_published_posts' => 'argus_visitors', |
---|
143 | ), |
---|
144 | ); |
---|
145 | register_post_type( 'comic', $args ); |
---|
146 | |
---|
147 | /** |
---|
148 | * CPT Candies inside Argus admin page |
---|
149 | * Subscriber don't have permission to add new posts |
---|
150 | * |
---|
151 | */ |
---|
152 | $args = array( |
---|
153 | 'labels' => array ( |
---|
154 | 'name' => 'Candies', |
---|
155 | 'singular_name' => 'Candy', |
---|
156 | 'add_new' => 'Add New', |
---|
157 | 'add_new_item' => 'Add New Candy', |
---|
158 | ), |
---|
159 | 'public' => true, |
---|
160 | 'publicly_queryable' => false, |
---|
161 | 'exclude_from_search' => true, |
---|
162 | 'show_ui' => true, |
---|
163 | 'show_in_menu' => 'argus', |
---|
164 | 'hiearchical' => false, |
---|
165 | 'supports' => array( |
---|
166 | 'title', |
---|
167 | 'editor', |
---|
168 | ), |
---|
169 | 'capabilities' => array( |
---|
170 | 'edit_post' => 'manage_options', |
---|
171 | 'read_post' => 'manage_options', |
---|
172 | 'delete_post' => 'manage_options', |
---|
173 | 'edit_posts' => 'manage_options', |
---|
174 | 'edit_others_posts' => 'manage_options', |
---|
175 | 'publish_posts' => 'manage_options', |
---|
176 | 'read_private_posts' => 'manage_options', |
---|
177 | 'read' => 'manage_options', |
---|
178 | 'delete_posts' => 'manage_options', |
---|
179 | 'delete_private_posts' => 'manage_options', |
---|
180 | 'delete_published_posts' => 'manage_options', |
---|
181 | 'delete_others_posts' => 'manage_options', |
---|
182 | 'edit_private_posts' => 'manage_options', |
---|
183 | 'edit_published_posts' => 'manage_options', |
---|
184 | ), |
---|
185 | ); |
---|
186 | register_post_type( 'candy', $args ); |
---|
187 | |
---|
188 | /** |
---|
189 | * Regular CPT Books |
---|
190 | * Subscriber have permission to add new posts |
---|
191 | * |
---|
192 | */ |
---|
193 | $args = array( |
---|
194 | 'labels' => array ( |
---|
195 | 'name' => 'Toys', |
---|
196 | 'singular_name' => 'Toy', |
---|
197 | 'add_new' => 'Add New', |
---|
198 | 'add_new_item' => 'Add New Toy', |
---|
199 | ), |
---|
200 | 'public' => true, |
---|
201 | 'publicly_queryable' => true, |
---|
202 | 'exclude_from_search' => false, |
---|
203 | 'show_ui' => true, |
---|
204 | 'show_in_menu' => true, |
---|
205 | 'hiearchical' => false, |
---|
206 | 'supports' => array( |
---|
207 | 'title', |
---|
208 | 'editor', |
---|
209 | ), |
---|
210 | 'capabilities' => array( |
---|
211 | 'edit_post' => 'argus_visitors', |
---|
212 | 'read_post' => 'argus_visitors', |
---|
213 | 'delete_post' => 'argus_visitors', |
---|
214 | 'edit_posts' => 'argus_visitors', |
---|
215 | 'edit_others_posts' => 'argus_visitors', |
---|
216 | 'publish_posts' => 'argus_visitors', |
---|
217 | 'read_private_posts' => 'argus_visitors', |
---|
218 | 'read' => 'argus_visitors', |
---|
219 | 'delete_posts' => 'argus_visitors', |
---|
220 | 'delete_private_posts' => 'argus_visitors', |
---|
221 | 'delete_published_posts' => 'argus_visitors', |
---|
222 | 'delete_others_posts' => 'argus_visitors', |
---|
223 | 'edit_private_posts' => 'argus_visitors', |
---|
224 | 'edit_published_posts' => 'argus_visitors', |
---|
225 | ), |
---|
226 | ); |
---|
227 | register_post_type( 'toy', $args ); |
---|
228 | |
---|
229 | /** |
---|
230 | * Regular CPT Book |
---|
231 | * Subscriber don't have permission to add new posts |
---|
232 | * |
---|
233 | */ |
---|
234 | $args = array( |
---|
235 | 'labels' => array ( |
---|
236 | 'name' => 'Books', |
---|
237 | 'singular_name' => 'Book', |
---|
238 | 'add_new' => 'Add New', |
---|
239 | 'add_new_item' => 'Add New Book', |
---|
240 | ), |
---|
241 | 'public' => true, |
---|
242 | 'publicly_queryable' => true, |
---|
243 | 'exclude_from_search' => false, |
---|
244 | 'show_ui' => true, |
---|
245 | 'show_in_menu' => true, |
---|
246 | 'hiearchical' => false, |
---|
247 | 'supports' => array( |
---|
248 | 'title', |
---|
249 | 'editor', |
---|
250 | ), |
---|
251 | 'capabilities' => array( |
---|
252 | 'edit_post' => 'manage_options', |
---|
253 | 'read_post' => 'manage_options', |
---|
254 | 'delete_post' => 'manage_options', |
---|
255 | 'edit_posts' => 'manage_options', |
---|
256 | 'edit_others_posts' => 'manage_options', |
---|
257 | 'publish_posts' => 'manage_options', |
---|
258 | 'read_private_posts' => 'manage_options', |
---|
259 | 'read' => 'manage_options', |
---|
260 | 'delete_posts' => 'manage_options', |
---|
261 | 'delete_private_posts' => 'manage_options', |
---|
262 | 'delete_published_posts' => 'manage_options', |
---|
263 | 'delete_others_posts' => 'manage_options', |
---|
264 | 'edit_private_posts' => 'manage_options', |
---|
265 | 'edit_published_posts' => 'manage_options', |
---|
266 | ), |
---|
267 | ); |
---|
268 | register_post_type( 'book', $args ); |
---|
269 | } |
---|
270 | |
---|
271 | /** |
---|
272 | * Add menus |
---|
273 | * Set priority to 0 just to add submenu on top |
---|
274 | * |
---|
275 | */ |
---|
276 | add_action( 'admin_menu', 'trac16808_add_menus', 9 ); |
---|
277 | function trac16808_add_menus(){ |
---|
278 | add_menu_page( 'Argus', 'Argus Admin', 'argus_visitors', 'argus', 'trac16808_admin_page_render', false, 40 ); |
---|
279 | add_submenu_page( 'argus', 'Argus Administration', 'Control Panel', 'argus_visitors', 'argus', 'trac16808_admin_page_render' ); |
---|
280 | } |
---|
281 | |
---|
282 | /** |
---|
283 | * Render admin page |
---|
284 | * |
---|
285 | */ |
---|
286 | function trac16808_admin_page_render(){ |
---|
287 | echo '<div class="wrap"><h2>Argus Admin Page</h2></div>'; |
---|
288 | } |
---|
289 | |
---|
290 | /** |
---|
291 | * Workaround |
---|
292 | * Add submenu pages with 'post-new.php?post_type=visitor' |
---|
293 | * Based on wp-includes/post.php function _add_post_type_submenus() |
---|
294 | * |
---|
295 | */ |
---|
296 | //add_action( 'admin_menu', 'trac16808_add_post_type_submenus', 99 ); |
---|
297 | function trac16808_add_post_type_submenus(){ |
---|
298 | foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) { |
---|
299 | $ptype_obj = get_post_type_object( $ptype ); |
---|
300 | // Sub-menus only. |
---|
301 | if ( ! $ptype_obj->show_in_menu || $ptype_obj->show_in_menu === true ) |
---|
302 | continue; |
---|
303 | add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->add_new, $ptype_obj->labels->add_new_item, $ptype_obj->cap->edit_posts, "post-new.php?post_type=$ptype" ); |
---|
304 | } |
---|
305 | } |
---|