PluginProbe
Groups – Memberships and Access Control / 1.11.1
Groups – Memberships and Access Control v1.11.1
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 / core / class-groups-user-capability.php

class-groups-user-capability.php in Groups – Memberships and Access Control 1.11.1, at lib/core/class-groups-user-capability.php

209 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * class-groups-user-capability.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 * User Capability OPM.
28 */
29 class Groups_User_Capability {
30
31 /**
32 * Hook into appropriate actions.
33 *
34 * @see wp_delete_user()
35 * @see remove_user_from_blog()
36 */
37 public static function init() {
38
39 // when a user is deleted, user-capabilities must be removed
40 // triggered by wp_delete_user()
41 add_action( "deleted_user", array( __CLASS__, "deleted_user" ) );
42 // when a capability is deleted the relationship must also be resolved
43 add_action( 'groups_deleted_capability', array( __CLASS__, 'groups_deleted_capability' ) );
44 }
45
46 /**
47 * Persist a user-capability relation.
48 *
49 * @param array $map attributes - must provide user_id and capability_id
50 * @return true on success, otherwise false
51 */
52 public static function create( $map ) {
53
54 global $wpdb;
55 extract( $map );
56 $result = false;
57
58 // avoid nonsense requests
59 // if ( !empty( $user_id ) && !empty( $capability_id) ) {
60 if ( !empty( $capability_id) ) {
61 // make sure user and capability exist
62 if ( ( false !== Groups_Utility::id( $user_id ) ) && get_user_by( "id", $user_id ) && Groups_Capability::read( $capability_id ) ) {
63 $user_capability_table = _groups_get_tablename( 'user_capability' );
64 // don't try to create duplicate entries
65 // also it would raise an error for duplicate PK
66 if ( 0 === intval( $wpdb->get_var( $wpdb->prepare(
67 "SELECT COUNT(*) FROM $user_capability_table WHERE user_id = %d AND capability_id = %d",
68 Groups_Utility::id( $user_id ),
69 Groups_Utility::id( $capability_id )
70 ) ) ) ) {
71 $data = array(
72 'user_id' => Groups_Utility::id( $user_id ),
73 'capability_id' => Groups_Utility::id( $capability_id )
74 );
75 $formats = array( '%d', '%d' );
76 if ( $wpdb->insert( $user_capability_table, $data, $formats ) ) {
77 $result = true;
78 do_action( "groups_created_user_capability", $user_id, $capability_id );
79 }
80 }
81 }
82
83 }
84 return $result;
85 }
86
87 /**
88 * Retrieve a user-capability relation.
89 *
90 * @param int $user_id user's id
91 * @param int $capability_id capability's id
92 * @return object upon success, otherwise false
93 */
94 public static function read( $user_id, $capability_id ) {
95 global $wpdb;
96 $result = false;
97
98 $user_capability_table = _groups_get_tablename( 'user_capability' );
99 $user_capability = $wpdb->get_row( $wpdb->prepare(
100 "SELECT * FROM $user_capability_table WHERE user_id = %d AND capability_id = %d",
101 Groups_Utility::id( $user_id ),
102 Groups_Utility::id( $capability_id )
103 ) );
104 if ( $user_capability !== null ) {
105 $result = $user_capability;
106 }
107 return $result;
108 }
109
110 /**
111 * Update user-capability relation.
112 *
113 * This changes nothing so as of now it's pointless to even call this.
114 *
115 * @param array $map
116 * @return true if successful, false otherwise
117 */
118 public static function update( $map ) {
119 $result = false;
120 // if ( !empty( $user_id ) && !empty( $capability_id) ) {
121 if ( !empty( $capability_id) ) {
122 // make sure user and capability exist
123 if ( ( false !== Groups_Utility::id( $user_id ) ) && get_user_by( "id", $user_id ) && Groups_Capability::read( $capability_id ) ) {
124 $result = true;
125 do_action( "groups_updated_user_capability", $user_id, $capability_id );
126 }
127 }
128 return $result;
129 }
130
131 /**
132 * Remove user-capability relation.
133 *
134 * @param int $user_id
135 * @param int $capability_id
136 * @return true if successful, false otherwise
137 */
138 public static function delete( $user_id, $capability_id ) {
139
140 global $wpdb;
141 $result = false;
142
143 // avoid nonsense requests
144 // if ( !empty( $user_id ) && !empty( $capability_id) ) {
145 if ( !empty( $capability_id) ) {
146 // to allow deletion of an entry after a user has been deleted,
147 // we don't check if the user exists
148 $user_capability_table = _groups_get_tablename( 'user_capability' );
149 // get rid of it
150 $rows = $wpdb->query( $wpdb->prepare(
151 "DELETE FROM $user_capability_table WHERE user_id = %d AND capability_id = %d",
152 Groups_Utility::id( $user_id ),
153 Groups_Utility::id( $capability_id )
154 ) );
155 // must have affected a row, otherwise no great success
156 $result = ( $rows !== false ) && ( $rows > 0 );
157 if ( $result ) {
158 do_action( "groups_deleted_user_capability", $user_id, $capability_id );
159 }
160 }
161 return $result;
162 }
163
164 /**
165 * Hooks into the deleted_user action to remove the deleted user from
166 * all capabilities it is related to.
167 *
168 * @param int $user_id
169 */
170 public static function deleted_user( $user_id ) {
171 global $wpdb;
172
173 $user_capability_table = _groups_get_tablename( "user_capability" );
174 $rows = $wpdb->get_results( $wpdb->prepare(
175 "SELECT * FROM $user_capability_table WHERE user_id = %d",
176 Groups_Utility::id( $user_id )
177 ) );
178 if ( $rows ) {
179 foreach( $rows as $row ) {
180 // don't optimize that in preference of a standard deletion
181 // process (trigger actions ...)
182 self::delete( $row->user_id, $row->capability_id );
183 }
184 }
185 }
186
187 /**
188 * Hooks into groups_deleted_capability to resolve all existing relations
189 * between users and the deleted capability.
190 * @param int $capability_id
191 */
192 public static function groups_deleted_capability( $capability_id ) {
193 global $wpdb;
194
195 $user_capability_table = _groups_get_tablename( "user_capability" );
196 $rows = $wpdb->get_results( $wpdb->prepare(
197 "SELECT * FROM $user_capability_table WHERE capability_id = %d",
198 Groups_Utility::id( $capability_id )
199 ) );
200 if ( $rows ) {
201 foreach( $rows as $row ) {
202 // do NOT 'optimize' (must trigger actions ... same as above)
203 self::delete( $row->user_id, $row->capability_id );
204 }
205 }
206 }
207 }
208 Groups_User_Capability::init();
209