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

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

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