-
Notifications
You must be signed in to change notification settings - Fork 2
/
bu-section-editing.php
278 lines (200 loc) · 8.43 KB
/
bu-section-editing.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
<?php
/*
Plugin Name: BU Section Editing
Plugin URI: http://developer.bu.edu/bu-section-editing/
Author: Boston University (IS&T)
Author URI: http://sites.bu.edu/web/
Description: Enhances WordPress content editing workflow by providing section editing groups and permissions
Version: 0.10.0
Text Domain: bu-section-editing
Domain Path: /languages
*/
/**
Copyright 2012 by Boston University
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 2 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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**/
/*
@author Gregory Cornelius <[email protected]>
@author Mike Burns <[email protected]>
@author Andrew Bauer <[email protected]>
*/
require_once( dirname( __FILE__ ) . '/classes.capabilities.php' );
require_once( dirname( __FILE__ ) . '/classes.groups.php' );
require_once( dirname( __FILE__ ) . '/classes.permissions.php' );
define( 'BUSE_PLUGIN_PATH', basename( dirname( __FILE__ ) ) );
define( 'BUSE_TEXTDOMAIN', 'bu-section-editing' );
define( 'BUSE_NAV_INSTALL_LINK', 'http://wordpress.org/extend/plugins/bu-navigation/' );
define( 'BUSE_NAV_UPGRADE_LINK', 'http://wordpress.org/extend/plugins/bu-navigation/' );
/**
* Plugin entry point
*/
class BU_Section_Editing_Plugin {
public static $caps;
public static $upgrader;
const BUSE_VERSION = '0.9.9';
const BUSE_VERSION_OPTION = '_buse_version';
public static function register_hooks() {
register_activation_hook( __FILE__, array( __CLASS__, 'on_activate' ) );
add_action( 'init', array( __CLASS__, 'l10n' ), 5 );
add_action( 'init', array( __CLASS__, 'init' ) );
add_action( 'init', array( __CLASS__, 'add_post_type_support' ), 20 );
add_action( 'admin_init', array( __CLASS__, 'version_check' ) );
add_action( 'load-plugins.php', array( __CLASS__, 'repopulate_roles' ) );
add_action( 'load-themes.php', array( __CLASS__, 'repopulate_roles' ) );
BU_Edit_Groups::register_hooks();
}
public static function l10n() {
load_plugin_textdomain( BUSE_TEXTDOMAIN, false, plugin_basename( dirname( __FILE__ ) ) . '/languages/' );
}
public static function init() {
self::$caps = new BU_Section_Capabilities();
// Roles and capabilities
add_filter( 'map_meta_cap', array( self::$caps, 'map_meta_cap' ), 10, 4 );
// Admin requests
if ( is_admin() ) {
require_once( dirname( __FILE__ ) . '/admin.groups.php' );
require_once( dirname( __FILE__ ) . '/admin-ajax.groups.php' );
BU_Groups_Admin::register_hooks();
BU_Groups_Admin_Ajax::register_hooks();
add_action( 'load-plugins.php', array( __CLASS__, 'load_plugins_screen' ) );
add_filter( 'plugin_action_links', array( __CLASS__, 'plugin_settings_link' ), 10, 2 );
// Load support code for the BU Navigation plugin if it's active
if ( class_exists( 'BU_Navigation_Plugin' ) ) {
require_once( dirname( __FILE__ ) . '/plugin-support/bu-navigation/section-editor-nav.php' );
}
}
}
/**
* Look for the BU Navigation plugin when this plugin activates
*/
public static function on_activate() {
$msg = '';
if ( ! class_exists( 'BU_Navigation_Plugin' ) ) {
$install_link = sprintf( '<a href="%s">%s</a>', BUSE_NAV_INSTALL_LINK, __( 'BU Navigation plugin', BUSE_TEXTDOMAIN ) );
$msg = '<p>' . __( 'The BU Section Editing plugin relies on the BU Navigation plugin for displaying hierarchical permission editors.', BUSE_TEXTDOMAIN ) . '</p>';
$msg .= '<p>' . sprintf(
__( 'Please install and activate the %s in order to set permissions for hierarchical post types.', BUSE_TEXTDOMAIN ),
$install_link ) . '</p>';
} else if ( version_compare( BU_Navigation_Plugin::VERSION, '1.1', '<' ) ) {
$upgrade_link = sprintf( '<a href="%s">%s</a>', BUSE_NAV_UPGRADE_LINK, __( 'upgrade your copy of BU Navigation', BUSE_TEXTDOMAIN ) );
$msg = '<p>' . __( 'The BU Section Editing plugin relies on the BU Navigation plugin for displaying hierarchical permission editors.', BUSE_TEXTDOMAIN ) . '</p>';
$msg .= '<p>' . __( 'This version of BU Section Editing requires at least version 1.1 of BU Navigation.', BUSE_TEXTDOMAIN ) . '</p>';
$msg .= '<p>' . sprintf(
__( 'Please %s to enable permissions for hierarchical post types.', BUSE_TEXTDOMAIN ),
$upgrade_link ) . '</p>';
}
if ( $msg ) {
set_transient( 'buse_nav_dep_nag', $msg );
}
}
/**
* Check for the BU Navigation plugin when the user vistis the "Plugins" page
*/
public static function load_plugins_screen() {
add_action( 'admin_notices', array( __CLASS__, 'plugin_dependency_nag' ) );
}
/**
* Display a notice on the "Plugins" page if a sufficient version of the BU Navigation plugin is not activated
*/
public static function plugin_dependency_nag() {
$notice = get_transient( 'buse_nav_dep_nag' );
if ( $notice ) {
echo "<div class=\"error\">$notice</div>\n";
delete_transient( 'buse_nav_dep_nag' );
}
}
public static function add_post_type_support() {
// Support posts and pages + all custom post types with show_ui by default
$post_types = get_post_types( array( 'show_ui' => true, '_builtin' => false ) );
$post_types = array_merge( $post_types, array( 'post', 'page' ) );
foreach ( $post_types as $post_type ) {
add_post_type_support( $post_type, 'section-editing' );
}
}
public static function plugin_settings_link( $links, $file ) {
if ( $file != plugin_basename( __FILE__ ) ) {
return $links;
}
$groups_url = admin_url( BU_Groups_Admin::MANAGE_GROUPS_PAGE );
array_unshift( $links, "<a href=\"$groups_url\" title=\"Manage Section Editing Groups\" class=\"edit\">" . __( 'Manage Groups', BUSE_TEXTDOMAIN ) . '</a>' );
return $links;
}
/**
* Checks currently installed plugin version against last version stored in DB,
* performing upgrades as needed.
*/
public static function version_check() {
$version = get_option( self::BUSE_VERSION_OPTION );
if ( empty( $version ) ) {
$version = '0';
}
// Check if plugin has been updated (or just installed) and store current version
if ( version_compare( $version, self::BUSE_VERSION, '<' ) ) {
require_once( dirname( __FILE__ ) . '/classes.upgrade.php' );
self::$upgrader = new BU_Section_Editing_Upgrader();
self::$upgrader->upgrade( $version );
// Store new version
update_option( self::BUSE_VERSION_OPTION, self::BUSE_VERSION );
}
}
/**
* Regenerate roles & capabilities when a plugin is activated or theme as switched
*
* Both actions potentially introduce new post types, which require a repopulation of the
* per-post type section editing caps -- (edit|publish|delete)_in_section
*/
public static function repopulate_roles() {
// Look for any query params that signify updates
if ( array_key_exists( 'activated', $_GET ) || array_key_exists( 'activate', $_GET ) || array_key_exists( 'activate-multi', $_GET ) ) {
require_once( dirname( __FILE__ ) . '/classes.upgrade.php' );
self::$upgrader = new BU_Section_Editing_Upgrader();
self::$upgrader->populate_roles();
}
}
/**
* Query for all users with the cability to be added to section groups
*/
public static function get_allowed_users( $query_args = array() ) {
$defaults = array(
'search_columns' => array( 'user_login', 'user_nicename', 'user_email' ),
);
$query_args = wp_parse_args( $query_args, $defaults );
$wp_user_query = new WP_User_Query( $query_args );
$allowed_users = array();
// Filter blog users by section editing status
foreach ( $wp_user_query->get_results() as $user ) {
if ( self::is_allowed_user( $user->ID ) ) {
$allowed_users[] = $user;
}
}
return $allowed_users;
}
/**
* Check if a user has the capability to be added to section groups
*/
public static function is_allowed_user( $user = null ) {
if ( is_null( $user ) ) {
$user = wp_get_current_user();
} else if ( is_numeric( $user ) ) {
$user = new WP_User( intval( $user ) );
}
if ( is_super_admin( $user->ID ) ) {
return false;
}
if ( $user->has_cap( 'edit_in_section' ) ) {
return true;
}
return false;
}
}
BU_Section_Editing_Plugin::register_hooks();