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