/
taxonomy-permissions.php
326 lines (259 loc) · 9.78 KB
/
taxonomy-permissions.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/*
Plugin Name: WP Document Revisions - Taxonomy-Bassed Permissions
Plugin URI: https://github.com/benbalter/WP-Document-Revisions-Code-Cookbook
Description: Extends the Members (or other permissions plugins) to allow taxonomy (category, tag, etc.) based permissions
Version: 1.0
Author: Benjamin J. Balter
Author URI: http://ben.balter.com
License: GPL3
*/
/* WP Document Revisions - Taxonomy-bassed Permissions
*
* Extends the Members (or other permissions plugins) to allow
* taxonomy (category, tag, etc.) based permissions
*
* =================================================================
*
* USAGE:
*
* (1) (Optionally) Change the target taxonomy, listed below, and
* if creating a new taxonomy, update the taxonomy labels
*
* (2) Install and activate the Members (or another capabilities plugin)
*
* (3) Create user roles as necessary, and assign the taxonomy based
* permissions using the easy-to-use interface Members provides.
*
* =================================================================
*
* Copyright (C) 2011-2012 Benjamin J. Balter ( ben@balter.com -- http://ben.balter.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/>.
*
* @copyright 2011-2012
* @license GPL v3
* @version 1.0
* @package WP_Document_Revisions
* @subpackage Taxonomy_Permissions
* @author Benjamin J. Balter <ben@balter.com>
*/
/**
* Taxonomy upon which permissions are based
*
* This can be an existing taxonomy, such as `category` or `workflow_state`
* or can be a new taxonomy. If creating a taxonomy, be sure to update
* the "labels" immediately below to match your taxonomy name.
*
* To based permissions off of the native `workflow_state` taxonomy, for example,
* simply change the word `department` on the following line to `workflow_state`.
* No other changes are needed.
*/
$wpdr_permissions_taxonomy = 'department';
/**
* Taxonomy Labels:
*
* These should be updated if you would like to create a new taxonomy
* that does not exist and is not `department`.
*
* Replace the words "Department" and "Departments" with the appropriate
* human-readible label for your custom taxonomy.
*/
$wpdr_permissions_labels = array(
'name' => _x( 'Departments', 'wp-document-revisions' ),
'singular_name' => _x( 'Department', 'wp-document-revisions' ),
'search_items' => _x( 'Search Departments', 'wp-document-revisions' ),
'popular_items' => _x( 'Popular Departments', 'wp-document-revisions' ),
'all_items' => _x( 'All Departments', 'wp-document-revisions' ),
'parent_item' => _x( 'Parent Department', 'wp-document-revisions' ),
'parent_item_colon' => _x( 'Parent Department:', 'wp-document-revisions' ),
'edit_item' => _x( 'Edit Department', 'wp-document-revisions' ),
'update_item' => _x( 'Update Department', 'wp-document-revisions' ),
'add_new_item' => _x( 'Add New Department', 'wp-document-revisions' ),
'new_item_name' => _x( 'New Department', 'wp-document-revisions' ),
'separate_items_with_commas' => _x( 'Separate departments with commas', 'wp-document-revisions' ),
'add_or_remove_items' => _x( 'Add or remove departments', 'wp-document-revisions' ),
'choose_from_most_used' => _x( 'Choose from the most used departments', 'wp-document-revisions' ),
'menu_name' => _x( 'Departments', 'wp-document-revisions' ),
);
/**
* Taxonomy settings:
* These can be freely edited, but there should be no need to out of the box
* For more information see: http://codex.wordpress.org/Function_Reference/register_taxonomy
*/
$wpdr_permissions_taxonomy_args = array(
'labels' => $wpdr_permissions_labels,
'public' => false,
'show_in_nav_menus' => false,
'show_ui' => true,
'show_tagcloud' => false,
'hierarchical' => true,
'rewrite' => false,
'query_var' => true,
'capabilities' => array(
'manage_terms' => 'manage_departments',
'edit_terms' => 'edit_departments',
'delete_terms' => 'delete_departments',
'assign_terms' => 'assign_departments',
)
);
/**
* ======================================================
* Note: There should be no need to edit below this point
* ======================================================
*/
class WPDR_Taxonomy_Permissions {
public $taxonomy = '';
public $flag = 'wpdr_init_taxonomy_permissions';
public $base_caps = array(
'edit_documents',
'edit_others_documents',
'edit_private_documents',
'edit_published_documents',
'read_documents',
'read_document_revisions',
'read_private_documents',
'delete_documents',
'delete_others_documents',
'delete_private_documents',
'delete_published_documents',
'publish_documents',
'override_document_lock',
);
/**
* Add hooks to WP API
*/
function __construct() {
//set defaults
global $wpdr_permissions_taxonomy;
$this->taxonomy = ( $wpdr_permissions_taxonomy ) ? $wpdr_permissions_taxonomy : 'department';
//register with necessary capabilitiy APIs
add_filter( 'map_meta_cap', array( &$this, 'map_meta_cap' ), 15, 4 );
add_filter( 'document_caps', array( &$this, 'default_caps_filter' ), 10, 2 );
//init hooks
add_action( 'init', array( &$this, 'maybe_register_taxonomy' ), 15 );
add_action( 'admin_init', array( &$this, 'admin_init' ) );
register_activation_hook( __FILE__, array( &$this, 'activation' ) );
//re-init caps on taxonomy change
add_action( 'delete_' . $this->taxonomy, array( &$this, 'remove_caps' ) );
add_action( 'created_' . $this->taxonomy, array( &$this, 'add_caps' ) );
add_action( 'edited_' . $this->taxonomy, array( &$this, 'add_caps' ) );
}
/**
* Fires on admin init to register capabilities
* Can't add initial caps on activation, because taxonomy isn't yet registered
*/
function admin_init() {
if ( !get_option( $this->flag ) )
return;
$this->add_caps();
}
/**
* Add our capabilities using the global WPDR object's native capabilities function
*/
function add_caps() {
global $wpdr;
if ( !$wpdr )
$wpdr = new Document_Revisions;
$wpdr->add_caps();
delete_option( $this->flag );
}
/**
* Remove capabilities when a term is removed
* @param unknown $term
*/
function remove_caps( $term ) {
$term_obj = get_term( $term, $this->taxonomy );
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles;
foreach ( $wp_roles->role_names as $role => $label )
foreach ( $this->base_caps as $base_cap )
$wp_roles->remove_cap( $role, $base_cap . '_in_' . $term_obj->slug );
}
/**
* Checks that WP Document Revisisons is activated when this plugin is activated
* If so, toggle flag to add initial capabilities
*/
function activation() {
if ( !class_exists( 'Document_Revisions' ) )
wp_die( __( 'WP Document Revisions must be activated to use Taxonomy Permissions', 'wp-document-revisions' ) );
update_option( $this->flag, true );
}
/**
* Conditionally registers the target taxonomy
*/
function maybe_register_taxonomy() {
global $wpdr_permissions_taxonomy_args;
//taxonomy exists, no need to register
if ( taxonomy_exists( $this->taxonomy ) )
return;
//Register the taxonomy
register_taxonomy( $this->taxonomy, array('document'), $wpdr_permissions_taxonomy_args );
}
/**
* Adds capabilities to each role
* Suggest using Members or similar plugin to then manage each permission
* @uses document_caps filter
* @param unknown $caps
* @param unknown $role
* @return unknown
*/
function default_caps_filter( $caps, $role ) {
//get terms in the selected taxonomy
$terms = get_terms( $this->taxonomy, array( 'hide_empty'=> false ) );
//build out term specific caps
foreach ( $caps as $cap => $grant )
foreach ( $terms as $term )
$caps[ $cap . '_in_' . $term->slug ] = $grant;
//build out taxonomy capabilities
$taxonomy = get_taxonomy( $this->taxonomy );
foreach ( get_object_vars( $taxonomy->cap ) as $cap )
$caps[ $cap ] = $grant;
return $caps;
}
/**
* Maps caps from e.g., `edit_document` to `edit_document_in_accounting`
* @param unknown $caps
* @param unknown $cap
* @param unknown $user_id
* @param unknown $args
* @return unknown
*/
function map_meta_cap( $caps, $cap, $user_id, $args ) {
global $wpdr;
//attempt to grab the postID
//note: will default to global $post if none passde
$postID = ( !empty( $args ) ) ? $args[0] : null;
//array of primative caps that all document caps are based on
$primitive_caps = array( 'read_post', 'edit_post', 'delete_post', 'publish_post', 'override_document_lock' );
//current cap being checked is not a post-specific cap, kick to save effort
if ( !in_array( $cap, $primitive_caps ) )
return $caps;
//cap being checked is not related to a document, kick
if ( !$wpdr->verify_post_type( $postID ) )
return $caps;
//get the terms in the taxonomy
$terms = wp_get_post_terms( $postID, $this->taxonomy );
//if no terms, assume primative roles
if ( sizeof( $terms ) == 0 )
return $caps;
//add taxonomy specific caps
foreach ( $caps as $cap )
foreach ( $terms as $term )
$caps[] = $cap . '_in_' . $term->slug;
return $caps;
}
}
new WPDR_Taxonomy_Permissions;