PluginProbe
Groups – Memberships and Access Control / 1.1.5
Groups – Memberships and Access Control v1.1.5
4.7.1 4.7.0 4.6.0 4.5.0 4.4.0 4.3.0 trunk 1.0.0-beta-1 1.0.0-beta-2 1.0.0-beta-3 1.0.0-beta-3b 1.0.0-beta-3c 1.0.0-beta-3d 1.1.4 1.1.5 1.10.0 1.10.1 1.10.2 1.10.3 1.11.0 1.11.1 1.11.2 1.11.3 1.12.0 1.13.0 All 131 releases
groups / lib / core / class-groups-controller.php

class-groups-controller.php in Groups – Memberships and Access Control 1.1.5, at lib/core/class-groups-controller.php

369 lines 12.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * class-groups-controller.php
4 *
5 * Copyright (c) "kento" Karim Rahimpur www.itthinx.com
6 *
7 * This code is released under the GNU General Public License.
8 * See COPYRIGHT.txt and LICENSE.txt.
9 *
10 * This code is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * This header and all notices must be kept intact.
16 *
17 * @author Karim Rahimpur
18 * @package groups
19 * @since groups 1.0.0
20 */
21
22 /**
23 * Plugin controller
24 */
25 class Groups_Controller {
26
27 /**
28 * Cache-safe switching in case any multi-site hiccups might occur.
29 * Clears the cache after switching to the given blog to avoid using
30 * another blog's cached values.
31 * See wp_cache_reset() in wp-includes/cache.php
32 * @see wp_cache_reset()
33 * @link http://core.trac.wordpress.org/ticket/14941
34 *
35 * @param int $blog_id
36 */
37 public static function switch_to_blog( $blog_id ) {
38 switch_to_blog( $blog_id );
39 wp_cache_reset();
40 }
41
42 /**
43 * Switch back to previous blog.
44 */
45 public static function restore_current_blog() {
46 restore_current_blog();
47 }
48
49 /**
50 * Boot the plugin.
51 * @see Groups_Registered::wpmu_new_blog()
52 */
53 public static function boot() {
54 register_activation_hook( GROUPS_FILE, array( __CLASS__, 'activate' ) );
55 register_deactivation_hook( GROUPS_FILE, array( __CLASS__, 'deactivate' ) );
56 add_action( 'init', array( __CLASS__, 'init' ) );
57
58 // priority 9 because it needs to be called before Groups_Registered's
59 // wpmu_new_blog kicks in
60 add_action( 'wpmu_new_blog', array( __CLASS__, 'wpmu_new_blog' ), 9, 2 );
61 add_action( 'delete_blog', array( __CLASS__, 'delete_blog' ), 10, 2 );
62 }
63
64 /**
65 * Run activation for a newly created blog in a multisite environment.
66 *
67 * @param int $blog_id
68 */
69 public static function wpmu_new_blog( $blog_id, $user_id ) {
70 if ( is_multisite() ) {
71 $active_sitewide_plugins = get_site_option( 'active_sitewide_plugins', array() );
72 if ( key_exists( 'groups/groups.php', $active_sitewide_plugins ) ) {
73 self::switch_to_blog( $blog_id );
74 self::setup();
75 self::restore_current_blog();
76 }
77 }
78 }
79
80 /**
81 * Run deactivation for a blog that is about to be deleted in a multisite
82 * environment.
83 *
84 * @param int $blog_id
85 */
86 public static function delete_blog( $blog_id, $drop = false ) {
87 if ( is_multisite() ) {
88 $active_sitewide_plugins = get_site_option( 'active_sitewide_plugins', array() );
89 if ( key_exists( 'groups/groups.php', $active_sitewide_plugins ) ) {
90 self::switch_to_blog( $blog_id );
91 self::cleanup( $drop );
92 self::restore_current_blog();
93 }
94 }
95 }
96
97 /**
98 * Initialize.
99 * Loads the plugin's translations.
100 */
101 public static function init() {
102 load_plugin_textdomain( GROUPS_PLUGIN_DOMAIN, null, GROUPS_PLUGIN_DIR );
103 self::version_check();
104 }
105
106 /**
107 * Plugin activation.
108 * @param boolean $network_wide
109 */
110 public static function activate( $network_wide = false ) {
111 if ( is_multisite() && $network_wide ) {
112 $blog_ids = Groups_Utility::get_blogs();
113 foreach ( $blog_ids as $blog_id ) {
114 self::switch_to_blog( $blog_id );
115 self::setup();
116 self::restore_current_blog();
117 }
118 } else {
119 self::setup();
120 }
121 }
122
123 /**
124 * Plugin activation work.
125 */
126 private static function setup() {
127 global $wpdb, $wp_roles;
128
129 // create WP capabilities
130 Groups_Controller::set_default_capabilities();
131
132 $charset_collate = '';
133 if ( ! empty( $wpdb->charset ) ) {
134 $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
135 }
136 if ( ! empty( $wpdb->collate ) ) {
137 $charset_collate .= " COLLATE $wpdb->collate";
138 }
139
140 // create tables
141 $group_table = _groups_get_tablename( 'group' );
142 if ( $wpdb->get_var( "SHOW TABLES LIKE '$group_table'" ) != $group_table ) {
143 $queries[] = "CREATE TABLE $group_table (
144 group_id BIGINT(20) UNSIGNED NOT NULL auto_increment,
145 parent_id BIGINT(20) DEFAULT NULL,
146 creator_id BIGINT(20) DEFAULT NULL,
147 datetime DATETIME DEFAULT NULL,
148 name VARCHAR(100) NOT NULL,
149 description LONGTEXT DEFAULT NULL,
150 PRIMARY KEY (group_id),
151 UNIQUE INDEX group_n (name)
152 ) $charset_collate;";
153 }
154 $capability_table = _groups_get_tablename( 'capability' );
155 if ( $wpdb->get_var( "SHOW TABLES LIKE '$capability_table'" ) != $capability_table ) {
156 $queries[] = "CREATE TABLE $capability_table (
157 capability_id BIGINT(20) UNSIGNED NOT NULL auto_increment,
158 capability VARCHAR(255) UNIQUE NOT NULL,
159 class VARCHAR(255) DEFAULT NULL,
160 object VARCHAR(255) DEFAULT NULL,
161 name VARCHAR(100) DEFAULT NULL,
162 description LONGTEXT DEFAULT NULL,
163 PRIMARY KEY (capability_id),
164 INDEX capability_kco (capability(20),class(20),object(20))
165 ) $charset_collate;";
166 }
167 $user_group_table = _groups_get_tablename( 'user_group' );
168 if ( $wpdb->get_var( "SHOW TABLES LIKE '$user_group_table'" ) != $user_group_table ) {
169 $queries[] = "CREATE TABLE $user_group_table (
170 user_id bigint(20) unsigned NOT NULL,
171 group_id bigint(20) unsigned NOT NULL,
172 PRIMARY KEY (user_id, group_id),
173 INDEX user_group_gu (group_id,user_id)
174 ) $charset_collate;";
175 }
176 $user_capability_table = _groups_get_tablename( 'user_capability' );
177 if ( $wpdb->get_var( "SHOW TABLES LIKE '$user_capability_table'" ) != $user_capability_table ) {
178 $queries[] = "CREATE TABLE $user_capability_table (
179 user_id bigint(20) unsigned NOT NULL,
180 capability_id bigint(20) unsigned NOT NULL,
181 PRIMARY KEY (user_id, capability_id),
182 INDEX user_capability_cu (capability_id,user_id)
183 ) $charset_collate;";
184 }
185 $group_capability_table = _groups_get_tablename( 'group_capability' );
186 if ( $wpdb->get_var( "SHOW TABLES LIKE '$group_capability_table'" ) != $group_capability_table ) {
187 $queries[] = "CREATE TABLE $group_capability_table (
188 group_id bigint(20) unsigned NOT NULL,
189 capability_id bigint(20) unsigned NOT NULL,
190 PRIMARY KEY (group_id, capability_id),
191 INDEX group_capability_cg (capability_id,group_id)
192 ) $charset_collate;";
193 }
194 if ( !empty( $queries ) ) {
195 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
196 dbDelta( $queries );
197 }
198 // needs to be called to create its capabilities
199 Groups_Post_Access::activate();
200 // same thing to created groups for registered users
201 Groups_Registered::activate();
202 // add WordPress capabilities
203 Groups_WordPress::activate();
204 // ... end of plugin activation work.
205 }
206
207 /**
208 * Checks current version and triggers update if needed.
209 */
210 public static function version_check() {
211 global $groups_version, $groups_admin_messages;
212 $previous_version = get_option( 'groups_plugin_version', null );
213 $groups_version = GROUPS_CORE_VERSION;
214 if ( strcmp( $previous_version, $groups_version ) < 0 ) {
215 if ( self::update( $previous_version ) ) {
216 update_option( 'groups_plugin_version', $groups_version );
217 } else {
218 $groups_admin_messages[] = '<div class="error">Updating Groups plugin core <em>failed</em>.</div>';
219 }
220 }
221 }
222
223 /**
224 * Update maintenance.
225 */
226 public static function update( $previous_version ) {
227 global $wpdb, $groups_admin_messages;
228 $result = true;
229 $queries = array();
230 switch ( $previous_version ) {
231 case '1.0.0' :
232 $capability_table = _groups_get_tablename( 'capability' );
233 if ( $wpdb->get_var( "SHOW TABLES LIKE '$capability_table'" ) == $capability_table ) {
234 // increase column sizes
235 $queries[] = "ALTER TABLE $capability_table MODIFY capability VARCHAR(255) UNIQUE NOT NULL;";
236 $queries[] = "ALTER TABLE $capability_table MODIFY class VARCHAR(255) DEFAULT NULL;";
237 $queries[] = "ALTER TABLE $capability_table MODIFY object VARCHAR(255) DEFAULT NULL;";
238 // correct capabilities
239 $queries[] = "UPDATE $capability_table SET capability='delete_published_pages' WHERE capability='delete_published_pag';";
240 $queries[] = "UPDATE $capability_table SET capability='delete_published_posts' WHERE capability='delete_published_pos';";
241 // fix hideously big index
242 $queries[] = "ALTER TABLE $capability_table DROP INDEX capability_kco;";
243 $queries[] = "ALTER TABLE $capability_table ADD INDEX capability_kco (capability(20),class(20),object(20));";
244 }
245 break;
246 case '1.0.0-beta-3d' :
247 $capability_table = _groups_get_tablename( 'capability' );
248 if ( $wpdb->get_var( "SHOW TABLES LIKE '$capability_table'" ) == $capability_table ) {
249 // increase column sizes
250 $queries[] = "ALTER TABLE $capability_table MODIFY capability VARCHAR(255) UNIQUE NOT NULL;";
251 $queries[] = "ALTER TABLE $capability_table MODIFY class VARCHAR(255) DEFAULT NULL;";
252 $queries[] = "ALTER TABLE $capability_table MODIFY object VARCHAR(255) DEFAULT NULL;";
253 // correct capabilities
254 $queries[] = "UPDATE $capability_table SET capability='delete_published_pages' WHERE capability='delete_published_pag';";
255 $queries[] = "UPDATE $capability_table SET capability='delete_published_posts' WHERE capability='delete_published_pos';";
256 }
257 break;
258 default :
259 } // switch
260 foreach ( $queries as $query ) {
261 if ( $wpdb->query( $query ) === false ) {
262 $result = false;
263 }
264 }
265 return $result;
266 }
267
268 /**
269 * Drop tables and clear data if the plugin is deactivated.
270 * This will happen only if the user chooses to delete data upon deactivation.
271 * @param boolean $network_wide
272 */
273 public static function deactivate( $network_wide = false ) {
274 if ( is_multisite() && $network_wide ) {
275 if ( Groups_Options::get_option( 'groups_network_delete_data', false ) ) {
276 $blog_ids = Groups_Utility::get_blogs();
277 foreach ( $blog_ids as $blog_id ) {
278 self::switch_to_blog( $blog_id );
279 self::cleanup( true );
280 self::restore_current_blog();
281 }
282 }
283 } else {
284 self::cleanup();
285 }
286 }
287
288 /**
289 * Plugin deactivation cleanup.
290 * @param $drop overrides the groups_delete_data option, default is false
291 */
292 private static function cleanup( $drop = false ) {
293
294 global $wpdb, $wp_roles;
295
296 $delete_data = Groups_Options::get_option( 'groups_delete_data', false );
297 if ( $delete_data || $drop ) {
298 foreach ( $wp_roles->role_objects as $role ) {
299 $role->remove_cap( GROUPS_ACCESS_GROUPS );
300 $role->remove_cap( GROUPS_ADMINISTER_GROUPS );
301 $role->remove_cap( GROUPS_ADMINISTER_OPTIONS );
302 }
303 $wpdb->query('DROP TABLE IF EXISTS ' . _groups_get_tablename( 'group' ) );
304 $wpdb->query('DROP TABLE IF EXISTS ' . _groups_get_tablename( 'capability' ) );
305 $wpdb->query('DROP TABLE IF EXISTS ' . _groups_get_tablename( 'user_group' ) );
306 $wpdb->query('DROP TABLE IF EXISTS ' . _groups_get_tablename( 'user_capability' ) );
307 $wpdb->query('DROP TABLE IF EXISTS ' . _groups_get_tablename( 'group_capability' ) );
308 Groups_Options::flush_options();
309 delete_option( GROUPS_ADMINISTRATOR_ACCESS_OVERRIDE );
310 delete_option( 'groups_plugin_version' );
311 delete_option( 'groups_delete_data' );
312 }
313 }
314
315 /**
316 * Determines the default capabilities for the administrator role.
317 * In lack of an administrator role, these capabilities are assigned
318 * to any role that can manage_options.
319 * This is also used to assure a minimum set of capabilities is
320 * assigned to an appropriate role, so that it's not possible
321 * to lock yourself out (although deactivating and then activating
322 * the plugin would have the same effect but with the danger of
323 * deleting all plugin data).
324 * @param boolean $activate defaults to true, when this function is called upon plugin activation
325 * @access private
326 */
327 public static function set_default_capabilities() {
328 global $wp_roles;
329 // The administrator role should be there, if it's not, assign privileges to
330 // any role that can manage_options:
331 if ( $administrator_role = $wp_roles->get_role( 'administrator' ) ) {
332 $administrator_role->add_cap( GROUPS_ACCESS_GROUPS );
333 $administrator_role->add_cap( GROUPS_ADMINISTER_GROUPS );
334 $administrator_role->add_cap( GROUPS_ADMINISTER_OPTIONS );
335 } else {
336 foreach ( $wp_roles->role_objects as $role ) {
337 if ($role->has_cap( 'manage_options' ) ) {
338 $role->add_cap( GROUPS_ACCESS_GROUPS );
339 $role->add_cap( GROUPS_ADMINISTER_GROUPS );
340 $role->add_cap( GROUPS_ADMINISTER_OPTIONS );
341 }
342 }
343 }
344 }
345
346 /**
347 * There must be at least one role with the minimum set of capabilities
348 * to access and manage the Groups plugin's options.
349 * If this condition is not met, the minimum set of capabilities is
350 * reestablished.
351 */
352 public static function assure_capabilities() {
353 global $wp_roles;
354 $complies = false;
355 $roles = $wp_roles->role_objects;
356 foreach( $roles as $role ) {
357 if ( $role->has_cap( GROUPS_ACCESS_GROUPS ) && ( $role->has_cap( GROUPS_ADMINISTER_OPTIONS ) ) ) {
358 $complies = true;
359 break;
360 }
361 }
362 if ( !$complies ) {
363 self::set_default_capabilities();
364 }
365 }
366
367 }
368 Groups_Controller::boot();
369