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-capability.php

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

244 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-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 class Groups_Capability {
23
24 /**
25 * @var persisted capability object
26 */
27 var $capability = null;
28
29 /**
30 * Create by capability id.
31 * Must have been persisted.
32 * @param int $capability_id
33 */
34 public function __construct( $capability_id ) {
35 $this->capability = self::read( $capability_id );
36 }
37
38 /**
39 * Retrieve a property by name.
40 *
41 * Possible properties:
42 * - capability_id
43 * - capability
44 * - class
45 * - object
46 * - name
47 * - description
48 *
49 * @param string $name property's name
50 * @return property value, will return null if property does not exist
51 */
52 public function __get( $name ) {
53 $result = null;
54 if ( $this->capability !== null ) {
55 switch( $name ) {
56 case "capability_id" :
57 case "capability" :
58 case "class" :
59 case "object" :
60 case "name" :
61 case "description" :
62 $result = $this->capability->$name;
63 break;
64 }
65 }
66 return $result;
67 }
68
69 /**
70 * Persist a capability.
71 *
72 * Possible keys in $map:
73 *
74 * - "capability" (required) - unique capability label, max 20 characters
75 * - "class" (optional) - class the capability applies to, max 100 chars
76 * - "object" (optional) - identifies object of that class, max 100 chars
77 * - "name" (optional) - name it if you have to
78 * - "description" (optional) - dito
79 *
80 * @param array $map attributes, requires at least: "capability"
81 * @return capability_id on success, otherwise false
82 */
83 public static function create( $map ) {
84
85 global $wpdb;
86 extract( $map );
87 $result = false;
88
89 if ( !empty( $capability ) ) {
90
91 if ( self::read_by_capability( $capability ) === false ) {
92
93 $data = array(
94 'capability' => $capability
95 );
96 $formats = array( '%s' );
97
98 if ( !empty( $class ) ) {
99 $data['class'] = $class;
100 $formats[] = '%s';
101 }
102 if ( !empty( $object ) ) {
103 $data['object'] = $object;
104 $formats[] = '%s';
105 }
106 if ( !empty( $name ) ) {
107 $data['name'] = $name;
108 $formats[] = '%s';
109 }
110 if ( !empty( $description ) ) {
111 $data['description'] = $description;
112 $formats[] = '%s';
113 }
114 $capability_table = _groups_get_tablename( 'capability' );
115 if ( $wpdb->insert( $capability_table, $data, $formats ) ) {
116 if ( $result = $wpdb->get_var( $wpdb->prepare( "SELECT LAST_INSERT_ID()" ) ) ) {
117 do_action( "groups_created_capability", $result );
118 }
119 }
120 }
121 }
122 return $result;
123 }
124
125 /**
126 * Retrieve a capability.
127 *
128 * Use Groups_Capability::read_capability() if you are trying to retrieve a capability by its unique label.
129 *
130 * @see Groups_Capability::read_by_capability()
131 * @param int $capability_id capability's id
132 * @return object upon success, otherwise false
133 */
134 public static function read( $capability_id ) {
135 global $wpdb;
136 $result = false;
137 $capability_table = _groups_get_tablename( 'capability' );
138 $capability = $wpdb->get_row( $wpdb->prepare(
139 "SELECT * FROM $capability_table WHERE capability_id = %d",
140 Groups_Utility::id( $capability_id )
141 ) );
142 if ( isset( $capability->capability_id ) ) {
143 $result = $capability;
144 }
145 return $result;
146 }
147
148 /**
149 * Retrieve a capability by its unique label.
150 *
151 * @param string $capability capability's unique label
152 * @return object upon success, otherwise false
153 */
154 public static function read_by_capability( $capability ) {
155 global $wpdb;
156 $result = false;
157
158 $capability_table = _groups_get_tablename( 'capability' );
159 $capability = $wpdb->get_row( $wpdb->prepare(
160 "SELECT * FROM $capability_table WHERE capability = %s",
161 $capability
162 ) );
163 if ( isset( $capability->capability_id ) ) {
164 $result = $capability;
165 }
166 return $result;
167 }
168
169
170 /**
171 * Update capability.
172 *
173 * @param array $map capability attribute, must contain capability_id
174 * @return capability_id on success, otherwise false
175 */
176 public static function update( $map ) {
177
178 global $wpdb;
179 extract( $map );
180 $result = false;
181
182 if ( isset( $capability_id ) && !empty( $capability ) ) {
183 $capability_table = _groups_get_tablename( 'capability' );
184 $old_capability = Groups_Capability::read( $capability_id );
185 if ( $old_capability ) {
186 if ( isset( $capability ) ) {
187 $old_capability->capability = $capability;
188 }
189 if ( isset( $class ) ) {
190 $old_capability->class = $class;
191 }
192 if ( isset( $object ) ) {
193 $old_capability->object = $object;
194 }
195 if ( isset( $name ) ) {
196 $old_capability->name = $name;
197 }
198 if ( isset( $description ) ) {
199 $old_capability->description = $description;
200 }
201 $rows = $wpdb->query( $wpdb->prepare(
202 "UPDATE $capability_table SET capability = %s, class = %s, object = %s, name = %s, description = %s WHERE capability_id = %d",
203 $old_capability->capability,
204 $old_capability->class,
205 $old_capability->object,
206 $old_capability->name,
207 $old_capability->description,
208 Groups_Utility::id( $capability_id )
209 ) );
210 if ( ( $rows !== false ) && ( $rows > 0 ) ) {
211 $result = $capability_id;
212 do_action( "groups_updated_capability", $result );
213 }
214 }
215 }
216 return $result;
217 }
218
219 /**
220 * Remove capability and its relations.
221 *
222 * @param int $capability_id
223 * @return capability_id if successful, false otherwise
224 */
225 public static function delete( $capability_id ) {
226
227 global $wpdb;
228 $result = false;
229
230 // avoid nonsense requests
231 if ( $capability = Groups_Capability::read( $capability_id ) ) {
232 $capability_table = _groups_get_tablename( 'capability' );
233 // get rid of it
234 if ( $rows = $wpdb->query( $wpdb->prepare(
235 "DELETE FROM $capability_table WHERE capability_id = %d",
236 Groups_Utility::id( $capability_id )
237 ) ) ) {
238 $result = $capability_id;
239 do_action( "groups_deleted_capability", $result );
240 }
241 }
242 return $result;
243 }
244 }