| 1 |
<?php |
| 2 |
/** |
| 3 |
* class-groups-registered.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 |
| 27 |
|
| 28 |
/** |
| 29 |
* "Registered" group automation. |
| 30 |
*/ |
| 31 |
class Groups_Registered { |
| 32 |
|
| 33 |
const REGISTERED_GROUP_NAME = 'Registered'; |
| 34 |
|
| 35 |
const BATCH_LIMIT = 100; |
| 36 |
|
| 37 |
/** |
| 38 |
* Creates groups for registered users. |
| 39 |
* Must be called explicitly or hooked into activation. |
| 40 |
* |
| 41 |
* As of Groups 2.2.0 this does not trigger the 'groups_created_user_group' action for each entry. |
| 42 |
*/ |
| 43 |
public static function activate() { |
| 44 |
|
| 45 |
global $wpdb; |
| 46 |
|
| 47 |
// create a group for the blog if it doesn't exist |
| 48 |
if ( !( $group = Groups_Group::read_by_name( self::REGISTERED_GROUP_NAME ) ) ) { |
| 49 |
$group_id = Groups_Group::create( array( 'name' => self::REGISTERED_GROUP_NAME ) ); |
| 50 |
} else { |
| 51 |
$group_id = $group->group_id; |
| 52 |
} |
| 53 |
if ( $group_id ) { |
| 54 |
$user_group_table = _groups_get_tablename( 'user_group' ); |
| 55 |
$query = $wpdb->prepare( |
| 56 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 57 |
"INSERT IGNORE INTO $user_group_table " . |
| 58 |
"SELECT ID, %d FROM $wpdb->users", |
| 59 |
Groups_Utility::id( $group_id ) |
| 60 |
); |
| 61 |
$rows = $wpdb->query( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Initialize hooks that handle addition and removal of users and blogs. |
| 67 |
*/ |
| 68 |
public static function init() { |
| 69 |
|
| 70 |
// @since 3.3.1 |
| 71 |
add_action( 'init', array( __CLASS__, 'wp_init' ) ); |
| 72 |
|
| 73 |
// When a blog is added, create a new "Registered" group for that blog. |
| 74 |
add_action( 'wpmu_new_blog', array( __CLASS__, 'wpmu_new_blog' ), 10, 2 ); |
| 75 |
|
| 76 |
// Remove group when a blog is deleted? When a blog is deleted, |
| 77 |
// Groups_Controller::delete_blog() takes appropriate action. |
| 78 |
|
| 79 |
// When a user is added, add it to the "Registered" group. |
| 80 |
add_action( 'user_register', array( __CLASS__, 'user_register' ) ); |
| 81 |
|
| 82 |
// Note : When a user is deleted this is handled from core. |
| 83 |
|
| 84 |
// When a user is added to a blog, add it to the blog's "Registered" group. |
| 85 |
add_action( 'add_user_to_blog', array( __CLASS__, 'add_user_to_blog' ), 10, 3 ); |
| 86 |
|
| 87 |
// Note : When a user is removed from a blog it's handled from core. |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Hooked on the init action. |
| 92 |
* |
| 93 |
* @since 3.3.1 |
| 94 |
*/ |
| 95 |
public static function wp_init() { |
| 96 |
// For translation of the "Registered" group(s) |
| 97 |
// @since 3.3.1 postponed to after the init action fired |
| 98 |
__( 'Registered', 'groups' ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Create "Registered" group for new blog and add its admin user. |
| 103 |
* |
| 104 |
* @see Groups_Controller::wpmu_new_blog() |
| 105 |
* |
| 106 |
* @param int $blog_id |
| 107 |
* @param int $user_id blog's admin user's id |
| 108 |
* @param string $domain (optional) |
| 109 |
* @param string $path (optional) |
| 110 |
* @param int $site_id (optional) |
| 111 |
* @param array $meta (optional) |
| 112 |
*/ |
| 113 |
public static function wpmu_new_blog( $blog_id, $user_id, $domain = null, $path = null, $site_id = null, $meta = null ) { |
| 114 |
if ( is_multisite() ) { |
| 115 |
Groups_Controller::switch_to_blog( $blog_id ); |
| 116 |
} |
| 117 |
if ( !( $group = Groups_Group::read_by_name( self::REGISTERED_GROUP_NAME ) ) ) { |
| 118 |
$group_id = Groups_Group::create( array( 'name' => self::REGISTERED_GROUP_NAME ) ); |
| 119 |
} else { |
| 120 |
$group_id = $group->group_id; |
| 121 |
} |
| 122 |
// add the blog's admin user to the group |
| 123 |
if ( $group_id ) { |
| 124 |
// Do NOT use Groups_User::user_is_member( ... ) here as we do not allow the result of this to be filtered: |
| 125 |
if ( !Groups_User_Group::read( $user_id, $group_id ) ) { |
| 126 |
Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $group_id ) ); |
| 127 |
} |
| 128 |
} |
| 129 |
if ( is_multisite() ) { |
| 130 |
Groups_Controller::restore_current_blog(); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Assign a newly created user to its "Registered" group. |
| 136 |
* |
| 137 |
* @param int $user_id |
| 138 |
*/ |
| 139 |
public static function user_register( $user_id ) { |
| 140 |
|
| 141 |
$registered_group = Groups_Group::read_by_name( self::REGISTERED_GROUP_NAME ); |
| 142 |
if ( !$registered_group ) { |
| 143 |
$registered_group_id = Groups_Group::create( array( 'name' => self::REGISTERED_GROUP_NAME ) ); |
| 144 |
} else { |
| 145 |
$registered_group_id = $registered_group->group_id; |
| 146 |
} |
| 147 |
if ( $registered_group_id ) { |
| 148 |
// Multisite: If a new user registers with the main blog, |
| 149 |
// the user is added, but it doesn't appear on the Users admin |
| 150 |
// screen of the main blog. It doesn't have the Subscriber role |
| 151 |
// (or any other) for that blog, unless it is explicitly added by the |
| 152 |
// blog's admin to the site. In other words, a user that has just |
| 153 |
// registered with the site's main blog can access the profile page |
| 154 |
// on the back end, but doesn't appear as a user to the site's admin. |
| 155 |
// Currently, on WP 3.3.2, like it or not, it's like that. |
| 156 |
// Unless the user actually has a capability (role) |
| 157 |
// for a blog, it won't appear in the blog's users list. After |
| 158 |
// registering with the blog, the user does not have a capability. |
| 159 |
// Thus, we need to check that is_user_member_of_blog( $user_id ) here. |
| 160 |
if ( !is_multisite() || is_user_member_of_blog( $user_id ) ) { |
| 161 |
Groups_User_Group::create( |
| 162 |
array( |
| 163 |
'user_id' => $user_id, |
| 164 |
'group_id' => $registered_group_id |
| 165 |
) |
| 166 |
); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Assign a user to its "Registered" group for the given blog. |
| 173 |
* |
| 174 |
* @param int $user_id User ID. |
| 175 |
* @param string $role User role. |
| 176 |
* @param int $blog_id Blog ID. |
| 177 |
*/ |
| 178 |
public static function add_user_to_blog( $user_id, $role, $blog_id ) { |
| 179 |
|
| 180 |
if ( is_multisite() ) { |
| 181 |
Groups_Controller::switch_to_blog( $blog_id ); |
| 182 |
} |
| 183 |
|
| 184 |
global $wpdb; |
| 185 |
|
| 186 |
// Check if the group table exists, if it does not exist, we are |
| 187 |
// probably here because the action has been triggered in the middle |
| 188 |
// of wpmu_create_blog() before the wpmu_new_blog action has been |
| 189 |
// triggered. In that case, just skip this as the user will be added |
| 190 |
// later when wpmu_new_blog is triggered, the activation sequence has |
| 191 |
// created the tables and all users of the new blog are added to |
| 192 |
// that blog's "Registered" group. |
| 193 |
$group_table = _groups_get_tablename( 'group' ); |
| 194 |
if ( $wpdb->get_var( "SHOW TABLES LIKE '" . $group_table . "'" ) == $group_table ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 195 |
$registered_group = Groups_Group::read_by_name( self::REGISTERED_GROUP_NAME ); |
| 196 |
if ( !$registered_group ) { |
| 197 |
$registered_group_id = Groups_Group::create( array( 'name' => self::REGISTERED_GROUP_NAME ) ); |
| 198 |
} else { |
| 199 |
$registered_group_id = $registered_group->group_id; |
| 200 |
} |
| 201 |
if ( $registered_group_id ) { |
| 202 |
Groups_User_Group::create( |
| 203 |
array( |
| 204 |
'user_id' => $user_id, |
| 205 |
'group_id' => $registered_group_id |
| 206 |
) |
| 207 |
); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
if ( is_multisite() ) { |
| 212 |
Groups_Controller::restore_current_blog(); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
} |
| 217 |
Groups_Registered::init(); |
| 218 |
|