| 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 |
// phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 27 |
|
| 28 |
/** |
| 29 |
* Plugin controller |
| 30 |
*/ |
| 31 |
class Groups_Controller { |
| 32 |
|
| 33 |
/** |
| 34 |
* Version 2.0.0 number |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
const GROUPS_200 = '2.0.0'; |
| 39 |
|
| 40 |
/** |
| 41 |
* Holds mofile when determined. |
| 42 |
* |
| 43 |
* @since 3.10.0 |
| 44 |
* |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
private static $mofile = null; |
| 48 |
|
| 49 |
/** |
| 50 |
* Cache-safe switching in case any multi-site hiccups might occur. |
| 51 |
* |
| 52 |
* Clears the cache after switching to the given blog to avoid using |
| 53 |
* another blog's cached values. |
| 54 |
* |
| 55 |
* Some implementations don't have wp_cache_switch_to_blog() nor the deprecated |
| 56 |
* wp_cache_reset(), e.g. WP Engine's object-cache.php which has wp_cache_flush(). |
| 57 |
* Cache plugins are responsible for acting accordingly when switch_to_blog() or |
| 58 |
* restore_current_blog() is invoked. In 4.0.0 we dropped any additional calls |
| 59 |
* other than those to switch_to_blog() and restore_current_blog(). |
| 60 |
* |
| 61 |
* @since 2.10.0 giving preference to call wp_cache_init() since it will reset the blog_id |
| 62 |
* @since 2.10.0 removed alternative call to wp_cache_reset() |
| 63 |
* @since 4.0.0 multisite guard added; not calling functions already called within switch_to_blog; cache plugins should act appropriately |
| 64 |
* |
| 65 |
* @see wp_cache_switch_to_blog() |
| 66 |
* @see wp_cache_init() |
| 67 |
* @see wp_cache_flush() |
| 68 |
* @see wp_cache_reset() |
| 69 |
* @see self::restore_current_blog() |
| 70 |
* |
| 71 |
* @link http://core.trac.wordpress.org/ticket/14941 |
| 72 |
* |
| 73 |
* @param int $blog_id |
| 74 |
*/ |
| 75 |
public static function switch_to_blog( $blog_id ) { |
| 76 |
if ( is_multisite() ) { |
| 77 |
switch_to_blog( $blog_id ); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Switch back to previous blog. |
| 83 |
* |
| 84 |
* @see self::switch_to_blog() |
| 85 |
*/ |
| 86 |
public static function restore_current_blog() { |
| 87 |
if ( is_multisite() ) { |
| 88 |
restore_current_blog(); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Boot the plugin. |
| 94 |
* |
| 95 |
* @see Groups_Registered::wpmu_new_blog() |
| 96 |
*/ |
| 97 |
public static function boot() { |
| 98 |
register_activation_hook( GROUPS_FILE, array( __CLASS__, 'activate' ) ); |
| 99 |
register_deactivation_hook( GROUPS_FILE, array( __CLASS__, 'deactivate' ) ); |
| 100 |
add_action( 'init', array( __CLASS__, 'init' ) ); |
| 101 |
add_filter( 'load_textdomain_mofile', array( __CLASS__, 'load_textdomain_mofile' ), 10, 2 ); // as of 3.10.0 this filter is removed within its function while processing, any changes made here should be reflected there, too |
| 102 |
add_filter( 'load_script_translation_file', array( __CLASS__, 'load_script_translation_file' ), 10, 3 ); // @since 3.10.0 |
| 103 |
// priority 9 because it needs to be called before Groups_Registered's |
| 104 |
// wpmu_new_blog kicks in |
| 105 |
add_action( 'wpmu_new_blog', array( __CLASS__, 'wpmu_new_blog' ), 9, 2 ); |
| 106 |
add_action( 'delete_blog', array( __CLASS__, 'delete_blog' ), 10, 2 ); |
| 107 |
add_action( 'wp_loaded', array( __CLASS__, 'wp_loaded' ) ); // @since 2.16.0 |
| 108 |
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) ); // @since 2.16.0 |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Run activation for a newly created blog in a multisite environment. |
| 113 |
* |
| 114 |
* @param int $blog_id |
| 115 |
*/ |
| 116 |
public static function wpmu_new_blog( $blog_id, $user_id ) { |
| 117 |
if ( is_multisite() ) { |
| 118 |
$active_sitewide_plugins = get_site_option( 'active_sitewide_plugins', array() ); |
| 119 |
if ( key_exists( 'groups/groups.php', $active_sitewide_plugins ) ) { |
| 120 |
self::switch_to_blog( $blog_id ); |
| 121 |
self::setup(); |
| 122 |
self::restore_current_blog(); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Run deactivation for a blog that is about to be deleted in a multisite |
| 129 |
* environment. |
| 130 |
* |
| 131 |
* @param int $blog_id |
| 132 |
*/ |
| 133 |
public static function delete_blog( $blog_id, $drop = false ) { |
| 134 |
if ( is_multisite() ) { |
| 135 |
$active_sitewide_plugins = get_site_option( 'active_sitewide_plugins', array() ); |
| 136 |
if ( key_exists( 'groups/groups.php', $active_sitewide_plugins ) ) { |
| 137 |
self::switch_to_blog( $blog_id ); |
| 138 |
self::cleanup( $drop ); |
| 139 |
self::restore_current_blog(); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Initialize. |
| 146 |
* Loads the plugin's translations. |
| 147 |
* Invokes version check. |
| 148 |
*/ |
| 149 |
public static function init() { |
| 150 |
|
| 151 |
unload_textdomain( 'groups' ); |
| 152 |
|
| 153 |
// Load our current translations first ... |
| 154 |
$mofile = self::get_mofile(); |
| 155 |
load_textdomain( 'groups', $mofile ); |
| 156 |
|
| 157 |
// ... otherwise load_plugin_textdomain will simply get those in WP's languages |
| 158 |
// and we won't have our up-to-date translations. |
| 159 |
//load_plugin_textdomain( 'groups', null, 'groups/languages' ); |
| 160 |
self::version_check(); |
| 161 |
|
| 162 |
// load the notice class |
| 163 |
if ( is_admin() ) { |
| 164 |
if ( Groups_User::current_user_can( 'activate_plugins' ) ) { // important: after init hook |
| 165 |
require_once GROUPS_ADMIN_LIB . '/class-groups-admin-notice.php'; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Register the menu style. |
| 172 |
* |
| 173 |
* @since 2.16.0 |
| 174 |
*/ |
| 175 |
public static function wp_loaded() { |
| 176 |
global $groups_version; |
| 177 |
if ( !wp_style_is( 'groups-menu', 'registered' ) ) { |
| 178 |
wp_register_style( 'groups-menu', GROUPS_PLUGIN_URL . 'css/groups-menu.css', array(), $groups_version ); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Enqueue the menu style. |
| 184 |
* |
| 185 |
* @since 2.16.0 |
| 186 |
*/ |
| 187 |
public static function admin_enqueue_scripts() { |
| 188 |
wp_enqueue_style( 'groups-menu' ); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Builds the mofile string for our own translations. |
| 193 |
* |
| 194 |
* @return string mofile |
| 195 |
*/ |
| 196 |
private static function get_mofile() { |
| 197 |
|
| 198 |
// @since 3.10.0 determine the file only once and avoid redundant work |
| 199 |
if ( self::$mofile !== null ) { |
| 200 |
return self::$mofile; |
| 201 |
} |
| 202 |
|
| 203 |
$locale = get_locale(); |
| 204 |
if ( function_exists( 'get_user_locale' ) ) { |
| 205 |
$locale = get_user_locale(); |
| 206 |
} |
| 207 |
$locale = apply_filters( 'plugin_locale', $locale, 'groups' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 208 |
$mofile = GROUPS_CORE_DIR . '/languages/groups-' . $locale . '.mo'; |
| 209 |
// @since 3.3.0 load language-generic translation if available |
| 210 |
if ( !file_exists( $mofile ) ) { |
| 211 |
$parts = explode( '_', $locale ); |
| 212 |
$language = $parts[0]; |
| 213 |
$country = isset( $parts[1] ) ? $parts[1] : ''; |
| 214 |
$form = isset( $parts[2] ) ? $parts[2] : ''; |
| 215 |
switch ( $country ) { |
| 216 |
case 'CH': |
| 217 |
switch ( $form ) { |
| 218 |
case 'informal': |
| 219 |
$form = ''; |
| 220 |
break; |
| 221 |
case '': |
| 222 |
$form = 'formal'; |
| 223 |
break; |
| 224 |
} |
| 225 |
break; |
| 226 |
} |
| 227 |
$the_mofile = null; |
| 228 |
if ( $form !== '' ) { |
| 229 |
$the_mofile = GROUPS_CORE_DIR . '/languages/groups' . '-' . $language . '_' . $form . '.mo'; |
| 230 |
if ( !file_exists( $the_mofile ) ) { |
| 231 |
$the_mofile = null; |
| 232 |
} |
| 233 |
} |
| 234 |
if ( $the_mofile === null ) { |
| 235 |
$the_mofile = GROUPS_CORE_DIR . '/languages/groups' . '-' . $language . '.mo'; |
| 236 |
if ( !file_exists( $the_mofile ) ) { |
| 237 |
$the_mofile = null; |
| 238 |
} |
| 239 |
} |
| 240 |
if ( $the_mofile !== null ) { |
| 241 |
$mofile = $the_mofile; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
self::$mofile = $mofile; |
| 246 |
|
| 247 |
return $mofile; |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Makes sure that our own translation file is loaded first. |
| 252 |
* |
| 253 |
* @param string $mofile |
| 254 |
* @param string $domain |
| 255 |
* |
| 256 |
* @return string mofile |
| 257 |
*/ |
| 258 |
public static function load_textdomain_mofile( $mofile, $domain ) { |
| 259 |
if ( $domain === 'groups' ) { |
| 260 |
// @since 3.10.0 remove the filter while processing and avoid potential infinite recursion during processing |
| 261 |
remove_filter( 'load_textdomain_mofile', array( __CLASS__, 'load_textdomain_mofile' ), 10 ); |
| 262 |
$own_mofile = self::get_mofile(); |
| 263 |
if ( $own_mofile != $mofile ) { |
| 264 |
if ( !is_textdomain_loaded( $domain ) ) { |
| 265 |
if ( is_readable( $own_mofile ) ) { |
| 266 |
$mofile = $own_mofile; |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
add_filter( 'load_textdomain_mofile', array( __CLASS__, 'load_textdomain_mofile' ), 10, 2 ); // see self::boot() where this filter is originally added |
| 271 |
} |
| 272 |
return $mofile; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Load our script translations. |
| 277 |
* |
| 278 |
* @since 3.10.0 |
| 279 |
* |
| 280 |
* @param string $file |
| 281 |
* @param string $handle |
| 282 |
* @param string $domain |
| 283 |
* |
| 284 |
* @return string |
| 285 |
*/ |
| 286 |
public static function load_script_translation_file( $file, $handle, $domain ) { |
| 287 |
if ( $domain === 'groups' ) { |
| 288 |
$mofile = self::get_mofile(); |
| 289 |
if ( $mofile !== null ) { |
| 290 |
$base = basename( $mofile, '.mo' ); |
| 291 |
$path = GROUPS_CORE_DIR . '/languages/js/' . $base . '.json'; |
| 292 |
if ( file_exists( $path ) ) { |
| 293 |
$file = $path; |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
return $file; |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Plugin activation. |
| 302 |
* |
| 303 |
* @param boolean $network_wide |
| 304 |
*/ |
| 305 |
public static function activate( $network_wide = false ) { |
| 306 |
$sem_id = self::sem_get( self::get_sem_key() ); |
| 307 |
if ( ( $sem_id === false ) || self::sem_acquire( $sem_id ) ) { |
| 308 |
if ( is_multisite() && $network_wide ) { |
| 309 |
$blog_ids = Groups_Utility::get_blogs(); |
| 310 |
foreach ( $blog_ids as $blog_id ) { |
| 311 |
self::switch_to_blog( $blog_id ); |
| 312 |
self::setup(); |
| 313 |
self::restore_current_blog(); |
| 314 |
} |
| 315 |
} else { |
| 316 |
self::setup(); |
| 317 |
// @since 3.3.1 Check here already if it is a single activation of Groups and only then set the transient |
| 318 |
if ( self::is_single_activate() ) { |
| 319 |
set_transient( 'groups_plugin_activated', true, 60 ); |
| 320 |
} |
| 321 |
} |
| 322 |
if ( $sem_id !== false ) { |
| 323 |
self::sem_release( $sem_id ); |
| 324 |
self::sem_remove( $sem_id ); |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Plugin activation work. |
| 331 |
*/ |
| 332 |
private static function setup() { |
| 333 |
global $wpdb, $wp_roles; |
| 334 |
|
| 335 |
// create WP capabilities |
| 336 |
Groups_Controller::set_default_capabilities(); |
| 337 |
|
| 338 |
$charset_collate = ''; |
| 339 |
if ( ! empty( $wpdb->charset ) ) { |
| 340 |
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; |
| 341 |
} |
| 342 |
if ( ! empty( $wpdb->collate ) ) { |
| 343 |
$charset_collate .= " COLLATE $wpdb->collate"; |
| 344 |
} |
| 345 |
|
| 346 |
// create tables |
| 347 |
$group_table = _groups_get_tablename( 'group' ); |
| 348 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 349 |
if ( $wpdb->get_var( "SHOW TABLES LIKE '$group_table'" ) != $group_table ) { |
| 350 |
$queries[] = "CREATE TABLE IF NOT EXISTS $group_table ( |
| 351 |
group_id BIGINT(20) UNSIGNED NOT NULL auto_increment, |
| 352 |
parent_id BIGINT(20) DEFAULT NULL, |
| 353 |
creator_id BIGINT(20) DEFAULT NULL, |
| 354 |
datetime DATETIME DEFAULT NULL, |
| 355 |
name VARCHAR(100) NOT NULL, |
| 356 |
description LONGTEXT DEFAULT NULL, |
| 357 |
PRIMARY KEY (group_id), |
| 358 |
UNIQUE INDEX group_n (name) |
| 359 |
) $charset_collate;"; |
| 360 |
} |
| 361 |
$capability_table = _groups_get_tablename( 'capability' ); |
| 362 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 363 |
if ( $wpdb->get_var( "SHOW TABLES LIKE '$capability_table'" ) != $capability_table ) { |
| 364 |
$queries[] = "CREATE TABLE IF NOT EXISTS $capability_table ( |
| 365 |
capability_id BIGINT(20) UNSIGNED NOT NULL auto_increment, |
| 366 |
capability VARCHAR(255) NOT NULL, |
| 367 |
class VARCHAR(255) DEFAULT NULL, |
| 368 |
object VARCHAR(255) DEFAULT NULL, |
| 369 |
name VARCHAR(100) DEFAULT NULL, |
| 370 |
description LONGTEXT DEFAULT NULL, |
| 371 |
PRIMARY KEY (capability_id), |
| 372 |
UNIQUE INDEX capability (capability(100)), |
| 373 |
INDEX capability_kco (capability(20),class(20),object(20)) |
| 374 |
) $charset_collate;"; |
| 375 |
} |
| 376 |
$user_group_table = _groups_get_tablename( 'user_group' ); |
| 377 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 378 |
if ( $wpdb->get_var( "SHOW TABLES LIKE '$user_group_table'" ) != $user_group_table ) { |
| 379 |
$queries[] = "CREATE TABLE IF NOT EXISTS $user_group_table ( |
| 380 |
user_id bigint(20) unsigned NOT NULL, |
| 381 |
group_id bigint(20) unsigned NOT NULL, |
| 382 |
PRIMARY KEY (user_id, group_id), |
| 383 |
INDEX user_group_gu (group_id,user_id) |
| 384 |
) $charset_collate;"; |
| 385 |
} |
| 386 |
$user_capability_table = _groups_get_tablename( 'user_capability' ); |
| 387 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 388 |
if ( $wpdb->get_var( "SHOW TABLES LIKE '$user_capability_table'" ) != $user_capability_table ) { |
| 389 |
$queries[] = "CREATE TABLE IF NOT EXISTS $user_capability_table ( |
| 390 |
user_id bigint(20) unsigned NOT NULL, |
| 391 |
capability_id bigint(20) unsigned NOT NULL, |
| 392 |
PRIMARY KEY (user_id, capability_id), |
| 393 |
INDEX user_capability_cu (capability_id,user_id) |
| 394 |
) $charset_collate;"; |
| 395 |
} |
| 396 |
$group_capability_table = _groups_get_tablename( 'group_capability' ); |
| 397 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 398 |
if ( $wpdb->get_var( "SHOW TABLES LIKE '$group_capability_table'" ) != $group_capability_table ) { |
| 399 |
$queries[] = "CREATE TABLE IF NOT EXISTS $group_capability_table ( |
| 400 |
group_id bigint(20) unsigned NOT NULL, |
| 401 |
capability_id bigint(20) unsigned NOT NULL, |
| 402 |
PRIMARY KEY (group_id, capability_id), |
| 403 |
INDEX group_capability_cg (capability_id,group_id) |
| 404 |
) $charset_collate;"; |
| 405 |
} |
| 406 |
if ( !empty( $queries ) ) { |
| 407 |
// For the record ... (and https://core.trac.wordpress.org/ticket/12773 should not be closed) |
| 408 |
// dbDelta() fails to handle queries "CREATE TABLE IF NOT EXISTS ..." |
| 409 |
// (a regex results in "IF" used as array index holding only last query to create table). |
| 410 |
//require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 411 |
//dbDelta( $queries ); |
| 412 |
foreach ( $queries as $query ) { |
| 413 |
$wpdb->query( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 414 |
} |
| 415 |
} |
| 416 |
// needs to be called to create its capabilities |
| 417 |
Groups_Post_Access::activate(); |
| 418 |
// same thing to created groups for registered users |
| 419 |
Groups_Registered::activate(); |
| 420 |
// add WordPress capabilities |
| 421 |
Groups_WordPress::activate(); |
| 422 |
// ... end of plugin activation work. |
| 423 |
} |
| 424 |
|
| 425 |
/** |
| 426 |
* Checks current version and triggers update if needed. |
| 427 |
*/ |
| 428 |
public static function version_check() { |
| 429 |
global $groups_version, $groups_admin_messages; |
| 430 |
$previous_version = get_option( 'groups_plugin_version', '' ); |
| 431 |
$groups_version = GROUPS_CORE_VERSION; |
| 432 |
// auto-enable legacy support on upgrade from Groups previous to 2.0.0 |
| 433 |
if ( $previous_version ) { |
| 434 |
if ( version_compare( $previous_version, self::GROUPS_200 ) < 0 ) { |
| 435 |
if ( Groups_Options::get_option( GROUPS_LEGACY_ENABLE ) === null ) { |
| 436 |
Groups_Options::update_option( GROUPS_LEGACY_ENABLE, true ); |
| 437 |
} |
| 438 |
set_transient( 'groups_plugin_updated_legacy', true, 60 ); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
// disable legacy support on new installations |
| 443 |
if ( Groups_Options::get_option( GROUPS_LEGACY_ENABLE ) === null ) { |
| 444 |
Groups_Options::update_option( GROUPS_LEGACY_ENABLE, false ); |
| 445 |
} |
| 446 |
|
| 447 |
// run update procedure if newer version is installed |
| 448 |
if ( $previous_version ) { |
| 449 |
if ( version_compare( $previous_version, $groups_version ) < 0 ) { |
| 450 |
if ( self::update( $previous_version ) ) { |
| 451 |
if ( update_option( 'groups_plugin_version', $groups_version ) ) { |
| 452 |
set_transient( 'groups_plugin_updated', true, 60 ); |
| 453 |
} |
| 454 |
} else { |
| 455 |
$groups_admin_messages[] = '<div class="error">Updating Groups plugin core <em>failed</em>.</div>'; |
| 456 |
} |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* Update maintenance. |
| 463 |
*/ |
| 464 |
public static function update( $previous_version ) { |
| 465 |
|
| 466 |
global $wpdb, $groups_admin_messages; |
| 467 |
|
| 468 |
$result = true; |
| 469 |
|
| 470 |
$sem_id = self::sem_get( self::get_sem_key() ); |
| 471 |
if ( ( $sem_id === false ) || self::sem_acquire( $sem_id ) ) { |
| 472 |
$queries = array(); |
| 473 |
switch ( $previous_version ) { |
| 474 |
case '1.0.0' : |
| 475 |
$capability_table = _groups_get_tablename( 'capability' ); |
| 476 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 477 |
if ( $wpdb->get_var( "SHOW TABLES LIKE '$capability_table'" ) == $capability_table ) { |
| 478 |
// increase column sizes |
| 479 |
$queries[] = "ALTER TABLE $capability_table MODIFY capability VARCHAR(255) UNIQUE NOT NULL;"; |
| 480 |
$queries[] = "ALTER TABLE $capability_table MODIFY class VARCHAR(255) DEFAULT NULL;"; |
| 481 |
$queries[] = "ALTER TABLE $capability_table MODIFY object VARCHAR(255) DEFAULT NULL;"; |
| 482 |
// correct capabilities |
| 483 |
$queries[] = "UPDATE $capability_table SET capability='delete_published_pages' WHERE capability='delete_published_pag';"; |
| 484 |
$queries[] = "UPDATE $capability_table SET capability='delete_published_posts' WHERE capability='delete_published_pos';"; |
| 485 |
// fix hideously big index |
| 486 |
$queries[] = "ALTER TABLE $capability_table DROP INDEX capability_kco;"; |
| 487 |
$queries[] = "ALTER TABLE $capability_table ADD INDEX capability_kco (capability(20),class(20),object(20));"; |
| 488 |
} |
| 489 |
break; |
| 490 |
case '1.0.0-beta-3d' : |
| 491 |
$capability_table = _groups_get_tablename( 'capability' ); |
| 492 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 493 |
if ( $wpdb->get_var( "SHOW TABLES LIKE '$capability_table'" ) == $capability_table ) { |
| 494 |
// increase column sizes |
| 495 |
$queries[] = "ALTER TABLE $capability_table MODIFY capability VARCHAR(255) UNIQUE NOT NULL;"; |
| 496 |
$queries[] = "ALTER TABLE $capability_table MODIFY class VARCHAR(255) DEFAULT NULL;"; |
| 497 |
$queries[] = "ALTER TABLE $capability_table MODIFY object VARCHAR(255) DEFAULT NULL;"; |
| 498 |
// correct capabilities |
| 499 |
$queries[] = "UPDATE $capability_table SET capability='delete_published_pages' WHERE capability='delete_published_pag';"; |
| 500 |
$queries[] = "UPDATE $capability_table SET capability='delete_published_posts' WHERE capability='delete_published_pos';"; |
| 501 |
} |
| 502 |
break; |
| 503 |
default : |
| 504 |
if ( !empty( $previous_version ) ) { |
| 505 |
if ( version_compare( $previous_version, '1.1.6' ) < 0 ) { |
| 506 |
Groups_Options::update_option( Groups_Post_Access::READ_POST_CAPABILITIES, array( Groups_Post_Access::READ_POST_CAPABILITY ) ); |
| 507 |
$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 ) ); |
| 508 |
} |
| 509 |
if ( version_compare( $previous_version, '1.5.1' ) < 0 ) { |
| 510 |
$capability_table = _groups_get_tablename( 'capability' ); |
| 511 |
$queries[] = "ALTER TABLE $capability_table DROP INDEX capability, ADD UNIQUE INDEX capability(capability(100));"; |
| 512 |
} |
| 513 |
} |
| 514 |
} // switch |
| 515 |
if ( !empty( $previous_version ) && version_compare( $previous_version, '2.0.0' ) < 0 ) { |
| 516 |
self::set_default_capabilities(); |
| 517 |
Groups_WordPress::refresh_capabilities(); |
| 518 |
} |
| 519 |
foreach ( $queries as $query ) { |
| 520 |
if ( $wpdb->query( $query ) === false ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 521 |
$result = false; |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
return $result; |
| 526 |
} |
| 527 |
|
| 528 |
/** |
| 529 |
* Drop tables and clear data if the plugin is deactivated. |
| 530 |
* This will happen only if the user chooses to delete data upon deactivation. |
| 531 |
* |
| 532 |
* @param boolean $network_wide |
| 533 |
*/ |
| 534 |
public static function deactivate( $network_wide = false ) { |
| 535 |
$sem_id = self::sem_get( self::get_sem_key() ); |
| 536 |
if ( ( $sem_id === false ) || self::sem_acquire( $sem_id ) ) { |
| 537 |
if ( is_multisite() && $network_wide ) { |
| 538 |
if ( Groups_Options::get_option( 'groups_network_delete_data', false ) ) { |
| 539 |
$blog_ids = Groups_Utility::get_blogs(); |
| 540 |
foreach ( $blog_ids as $blog_id ) { |
| 541 |
self::switch_to_blog( $blog_id ); |
| 542 |
self::cleanup( true ); |
| 543 |
self::restore_current_blog(); |
| 544 |
} |
| 545 |
} |
| 546 |
} else { |
| 547 |
self::cleanup(); |
| 548 |
} |
| 549 |
if ( $sem_id !== false ) { |
| 550 |
self::sem_release( $sem_id ); |
| 551 |
self::sem_remove( $sem_id ); |
| 552 |
} |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
/** |
| 557 |
* Plugin deactivation cleanup. |
| 558 |
* |
| 559 |
* @param $drop boolean overrides the groups_delete_data option, default is false |
| 560 |
*/ |
| 561 |
private static function cleanup( $drop = false ) { |
| 562 |
|
| 563 |
global $wpdb, $wp_roles; |
| 564 |
|
| 565 |
$delete_data = Groups_Options::get_option( 'groups_delete_data', false ); |
| 566 |
if ( $delete_data || $drop ) { |
| 567 |
foreach ( $wp_roles->role_objects as $role ) { |
| 568 |
$role->remove_cap( GROUPS_ACCESS_GROUPS ); |
| 569 |
$role->remove_cap( GROUPS_ADMINISTER_GROUPS ); |
| 570 |
$role->remove_cap( GROUPS_ADMINISTER_OPTIONS ); |
| 571 |
$role->remove_cap( GROUPS_RESTRICT_ACCESS ); |
| 572 |
} |
| 573 |
$wpdb->query( 'DROP TABLE IF EXISTS ' . _groups_get_tablename( 'group' ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 574 |
$wpdb->query( 'DROP TABLE IF EXISTS ' . _groups_get_tablename( 'capability' ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 575 |
$wpdb->query( 'DROP TABLE IF EXISTS ' . _groups_get_tablename( 'user_group' ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 576 |
$wpdb->query( 'DROP TABLE IF EXISTS ' . _groups_get_tablename( 'user_capability' ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 577 |
$wpdb->query( 'DROP TABLE IF EXISTS ' . _groups_get_tablename( 'group_capability' ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 578 |
Groups_Options::flush_options(); |
| 579 |
if ( class_exists( 'Groups_Admin_Notice' ) ) { |
| 580 |
delete_metadata( 'user', null, Groups_Admin_Notice::HIDE_REVIEW_NOTICE, null, true ); |
| 581 |
delete_site_option( Groups_Admin_Notice::INIT_TIME ); |
| 582 |
} |
| 583 |
delete_option( GROUPS_ADMINISTRATOR_ACCESS_OVERRIDE ); // keep this to delete the deprecated option @since 2.1.1 |
| 584 |
delete_option( 'groups_plugin_version' ); |
| 585 |
delete_option( 'groups_delete_data' ); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
/** |
| 590 |
* Whether this is an individual activation of the plugin. |
| 591 |
* |
| 592 |
* @since 3.3.1 |
| 593 |
* |
| 594 |
* @return boolean |
| 595 |
*/ |
| 596 |
private static function is_single_activate() { |
| 597 |
$is = false; |
| 598 |
$groups_basename = plugin_basename( GROUPS_FILE ); |
| 599 |
$action = groups_sanitize_request( 'action' ); |
| 600 |
if ( is_string( $action ) ) { |
| 601 |
switch ( $action ) { |
| 602 |
case 'activate': |
| 603 |
// Single plugin activation of Groups: |
| 604 |
$slug = groups_sanitize_request( 'plugin' ); |
| 605 |
if ( $slug === $groups_basename ) { |
| 606 |
$is = true; |
| 607 |
} |
| 608 |
break; |
| 609 |
case 'activate-selected': |
| 610 |
// Bulk plugin activation of Groups but it is the only plugin being activated: |
| 611 |
$slugs = groups_sanitize_request( 'checked' ); |
| 612 |
if ( is_array( $slugs ) && count( $slugs ) === 1 ) { |
| 613 |
$slug = array_pop( $slugs ); |
| 614 |
if ( $slug === $groups_basename ) { |
| 615 |
$is = true; |
| 616 |
break; |
| 617 |
} |
| 618 |
} |
| 619 |
break; |
| 620 |
} |
| 621 |
} |
| 622 |
return $is; |
| 623 |
} |
| 624 |
|
| 625 |
/** |
| 626 |
* Determines the default capabilities for the administrator role. |
| 627 |
* In lack of an administrator role, these capabilities are assigned |
| 628 |
* to any role that can manage_options. |
| 629 |
* This is also used to assure a minimum set of capabilities is |
| 630 |
* assigned to an appropriate role, so that it's not possible |
| 631 |
* to lock yourself out (although deactivating and then activating |
| 632 |
* the plugin would have the same effect but with the danger of |
| 633 |
* deleting all plugin data). |
| 634 |
* |
| 635 |
* @access private |
| 636 |
*/ |
| 637 |
public static function set_default_capabilities() { |
| 638 |
global $wp_roles; |
| 639 |
// The administrator role should be there, if it's not, assign privileges to |
| 640 |
// any role that can manage_options: |
| 641 |
if ( $administrator_role = $wp_roles->get_role( 'administrator' ) ) { |
| 642 |
$administrator_role->add_cap( GROUPS_ACCESS_GROUPS ); |
| 643 |
$administrator_role->add_cap( GROUPS_ADMINISTER_GROUPS ); |
| 644 |
$administrator_role->add_cap( GROUPS_ADMINISTER_OPTIONS ); |
| 645 |
$administrator_role->add_cap( GROUPS_RESTRICT_ACCESS ); |
| 646 |
} else { |
| 647 |
foreach ( $wp_roles->role_objects as $role ) { |
| 648 |
if ($role->has_cap( 'manage_options' ) ) { |
| 649 |
$role->add_cap( GROUPS_ACCESS_GROUPS ); |
| 650 |
$role->add_cap( GROUPS_ADMINISTER_GROUPS ); |
| 651 |
$role->add_cap( GROUPS_ADMINISTER_OPTIONS ); |
| 652 |
$role->add_cap( GROUPS_RESTRICT_ACCESS ); |
| 653 |
} |
| 654 |
} |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
/** |
| 659 |
* There must be at least one role with the minimum set of capabilities |
| 660 |
* to access and manage the Groups plugin's options. |
| 661 |
* If this condition is not met, the minimum set of capabilities is |
| 662 |
* reestablished. |
| 663 |
*/ |
| 664 |
public static function assure_capabilities() { |
| 665 |
global $wp_roles; |
| 666 |
$complies = false; |
| 667 |
$roles = $wp_roles->role_objects; |
| 668 |
foreach ( $roles as $role ) { |
| 669 |
if ( $role->has_cap( GROUPS_ACCESS_GROUPS ) && ( $role->has_cap( GROUPS_ADMINISTER_OPTIONS ) ) ) { |
| 670 |
$complies = true; |
| 671 |
break; |
| 672 |
} |
| 673 |
} |
| 674 |
if ( !$complies ) { |
| 675 |
self::set_default_capabilities(); |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
/** |
| 680 |
* Guarded sem_get() wrapper. |
| 681 |
* |
| 682 |
* @see sem_get() |
| 683 |
* |
| 684 |
* @param int $key |
| 685 |
* @param number $max_acquire |
| 686 |
* @param number $perm |
| 687 |
* @param number $auto_release |
| 688 |
* |
| 689 |
* @return boolean|resource |
| 690 |
*/ |
| 691 |
private static function sem_get( $key, $max_acquire = 1, $perm = 0666, $auto_release = 1 ) { |
| 692 |
$result = false; |
| 693 |
if ( function_exists( 'sem_get' ) ) { |
| 694 |
$result = sem_get( $key, $max_acquire, $perm, $auto_release ); |
| 695 |
} |
| 696 |
return $result; |
| 697 |
} |
| 698 |
|
| 699 |
/** |
| 700 |
* Guarded sem_acquire() wrapper. |
| 701 |
* |
| 702 |
* To maintain backwards-compatibility with servers running PHP < 5.6 where |
| 703 |
* the second parameter to sem_acquire() is not supported, we use sem_remove() |
| 704 |
* and have any calls waiting on sem_acquire() fail silently (achieving that |
| 705 |
* the activation, update or deactivation routines are not run for those |
| 706 |
* processes that have been waiting and which would have duplicated execution |
| 707 |
* unnecessarily). |
| 708 |
* |
| 709 |
* @see sem_acquire() |
| 710 |
* |
| 711 |
* @param resource $sem_identifier |
| 712 |
* @param boolean $nowait (only taken into account and effective on PHP >= 5.6.1) |
| 713 |
* |
| 714 |
* @return boolean |
| 715 |
*/ |
| 716 |
private static function sem_acquire( $sem_identifier, $nowait = false ) { |
| 717 |
$result = false; |
| 718 |
if ( function_exists( 'sem_acquire' ) ) { |
| 719 |
if ( version_compare( phpversion(), '5.6.1' ) >= 0 ) { |
| 720 |
$result = @sem_acquire( $sem_identifier, $nowait ); |
| 721 |
} else { |
| 722 |
$result = @sem_acquire( $sem_identifier ); |
| 723 |
} |
| 724 |
} |
| 725 |
return $result; |
| 726 |
} |
| 727 |
|
| 728 |
/** |
| 729 |
* Guarded sem_release() wrapper. |
| 730 |
* |
| 731 |
* @see sem_release() |
| 732 |
* |
| 733 |
* @param resource $sem_identifier |
| 734 |
* |
| 735 |
* @return boolean |
| 736 |
*/ |
| 737 |
private static function sem_release( $sem_identifier ) { |
| 738 |
$result = false; |
| 739 |
if ( function_exists( 'sem_release' ) ) { |
| 740 |
$result = @sem_release( $sem_identifier ); |
| 741 |
} |
| 742 |
return $result; |
| 743 |
} |
| 744 |
|
| 745 |
/** |
| 746 |
* Guarded sem_remove() wrapper. |
| 747 |
* |
| 748 |
* @see sem_remove() |
| 749 |
* |
| 750 |
* @param resource $sem_identifier |
| 751 |
* |
| 752 |
* @return boolean |
| 753 |
*/ |
| 754 |
private static function sem_remove( $sem_identifier ) { |
| 755 |
$result = false; |
| 756 |
if ( function_exists( 'sem_remove' ) ) { |
| 757 |
$result = @sem_remove( $sem_identifier ); |
| 758 |
} |
| 759 |
return $result; |
| 760 |
} |
| 761 |
|
| 762 |
/** |
| 763 |
* Produces a file-based key for use with sem_get(). |
| 764 |
* |
| 765 |
* @return number |
| 766 |
*/ |
| 767 |
private static function get_sem_key() { |
| 768 |
$key = -1; |
| 769 |
if ( function_exists( 'ftok' ) ) { |
| 770 |
$key = ftok( __FILE__, 'g' ); |
| 771 |
} |
| 772 |
if ( $key == -1 ) { |
| 773 |
$key = fileinode( __FILE__ ); |
| 774 |
} |
| 775 |
return $key; |
| 776 |
} |
| 777 |
} |
| 778 |
Groups_Controller::boot(); |
| 779 |
|