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