PluginProbe
Groups – Memberships and Access Control / 1.10.3
Groups – Memberships and Access Control v1.10.3
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 / auto / class-groups-registered.php

class-groups-registered.php in Groups – Memberships and Access Control 1.10.3, at lib/auto/class-groups-registered.php

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