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 / core / class-groups-user-capability.php

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

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