PluginProbe
Groups – Memberships and Access Control / 1.1.5
Groups – Memberships and Access Control v1.1.5
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.1.5, at lib/auto/class-groups-registered.php

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