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

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