| 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 |
if ( !defined( 'ABSPATH' ) ) { |
| 23 |
exit; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Capability OPM |
| 28 |
*/ |
| 29 |
class Groups_Capability { |
| 30 |
|
| 31 |
const CACHE_GROUP = 'groups'; |
| 32 |
const READ_BY_CAPABILITY = 'read_by_capability'; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var persisted capability object |
| 36 |
*/ |
| 37 |
var $capability = null; |
| 38 |
|
| 39 |
/** |
| 40 |
* Create by capability id. |
| 41 |
* Must have been persisted. |
| 42 |
* @param int $capability_id |
| 43 |
*/ |
| 44 |
public function __construct( $capability_id ) { |
| 45 |
$this->capability = self::read( $capability_id ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Retrieve a property by name. |
| 50 |
* |
| 51 |
* Possible properties: |
| 52 |
* - capability_id |
| 53 |
* - capability |
| 54 |
* - class |
| 55 |
* - object |
| 56 |
* - name |
| 57 |
* - description |
| 58 |
* |
| 59 |
* - group_ids groups that have the capability |
| 60 |
* |
| 61 |
* @param string $name property's name |
| 62 |
* @return property value, will return null if property does not exist |
| 63 |
*/ |
| 64 |
public function __get( $name ) { |
| 65 |
|
| 66 |
global $wpdb; |
| 67 |
|
| 68 |
$result = null; |
| 69 |
if ( $this->capability !== null ) { |
| 70 |
switch( $name ) { |
| 71 |
case "capability_id" : |
| 72 |
case "capability" : |
| 73 |
case "class" : |
| 74 |
case "object" : |
| 75 |
case "name" : |
| 76 |
case "description" : |
| 77 |
$result = $this->capability->$name; |
| 78 |
break; |
| 79 |
case 'group_ids' : |
| 80 |
$group_capability_table = _groups_get_tablename( "group_capability" ); |
| 81 |
$rows = $wpdb->get_results( $wpdb->prepare( |
| 82 |
"SELECT group_id FROM $group_capability_table WHERE capability_id = %d", |
| 83 |
Groups_Utility::id( $this->capability->capability_id ) |
| 84 |
) ); |
| 85 |
if ( $rows ) { |
| 86 |
$result = array(); |
| 87 |
foreach( $rows as $row ) { |
| 88 |
$result[] = $row->group_id; |
| 89 |
} |
| 90 |
} |
| 91 |
break; |
| 92 |
case 'groups' : |
| 93 |
$group_capability_table = _groups_get_tablename( "group_capability" ); |
| 94 |
$rows = $wpdb->get_results( $wpdb->prepare( |
| 95 |
"SELECT group_id FROM $group_capability_table WHERE capability_id = %d", |
| 96 |
Groups_Utility::id( $this->capability->capability_id ) |
| 97 |
) ); |
| 98 |
if ( $rows ) { |
| 99 |
$result = array(); |
| 100 |
foreach( $rows as $row ) { |
| 101 |
$result[] = new Groups_Group( $row->group_id ); |
| 102 |
} |
| 103 |
} |
| 104 |
break; |
| 105 |
} |
| 106 |
} |
| 107 |
return $result; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Persist a capability. |
| 112 |
* |
| 113 |
* Possible keys in $map: |
| 114 |
* |
| 115 |
* - "capability" (required) - unique capability label, max 20 characters |
| 116 |
* - "class" (optional) - class the capability applies to, max 100 chars |
| 117 |
* - "object" (optional) - identifies object of that class, max 100 chars |
| 118 |
* - "name" (optional) - name it if you have to |
| 119 |
* - "description" (optional) - dito |
| 120 |
* |
| 121 |
* @param array $map attributes, requires at least: "capability" |
| 122 |
* @return capability_id on success, otherwise false |
| 123 |
*/ |
| 124 |
public static function create( $map ) { |
| 125 |
|
| 126 |
global $wpdb; |
| 127 |
extract( $map ); |
| 128 |
$result = false; |
| 129 |
|
| 130 |
if ( !empty( $capability ) ) { |
| 131 |
|
| 132 |
if ( self::read_by_capability( $capability ) === false ) { |
| 133 |
|
| 134 |
$data = array( |
| 135 |
'capability' => $capability |
| 136 |
); |
| 137 |
$formats = array( '%s' ); |
| 138 |
|
| 139 |
if ( !empty( $class ) ) { |
| 140 |
$data['class'] = $class; |
| 141 |
$formats[] = '%s'; |
| 142 |
} |
| 143 |
if ( !empty( $object ) ) { |
| 144 |
$data['object'] = $object; |
| 145 |
$formats[] = '%s'; |
| 146 |
} |
| 147 |
if ( !empty( $name ) ) { |
| 148 |
$data['name'] = $name; |
| 149 |
$formats[] = '%s'; |
| 150 |
} |
| 151 |
if ( !empty( $description ) ) { |
| 152 |
$data['description'] = $description; |
| 153 |
$formats[] = '%s'; |
| 154 |
} |
| 155 |
$capability_table = _groups_get_tablename( 'capability' ); |
| 156 |
if ( $wpdb->insert( $capability_table, $data, $formats ) ) { |
| 157 |
if ( $result = $wpdb->get_var( "SELECT LAST_INSERT_ID()" ) ) { |
| 158 |
// read_by_capability above created a cache entry which needs to be reset |
| 159 |
Groups_Cache::delete( self::READ_BY_CAPABILITY . '_' . $capability, self::CACHE_GROUP ); |
| 160 |
do_action( "groups_created_capability", $result ); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
return $result; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Retrieve a capability. |
| 170 |
* |
| 171 |
* Use Groups_Capability::read_capability() if you are trying to retrieve a capability by its unique label. |
| 172 |
* |
| 173 |
* @see Groups_Capability::read_by_capability() |
| 174 |
* @param int $capability_id capability's id |
| 175 |
* @return object upon success, otherwise false |
| 176 |
*/ |
| 177 |
public static function read( $capability_id ) { |
| 178 |
global $wpdb; |
| 179 |
$result = false; |
| 180 |
$capability_table = _groups_get_tablename( 'capability' ); |
| 181 |
$capability = $wpdb->get_row( $wpdb->prepare( |
| 182 |
"SELECT * FROM $capability_table WHERE capability_id = %d", |
| 183 |
Groups_Utility::id( $capability_id ) |
| 184 |
) ); |
| 185 |
if ( isset( $capability->capability_id ) ) { |
| 186 |
$result = $capability; |
| 187 |
} |
| 188 |
return $result; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Retrieve a capability by its unique label. |
| 193 |
* |
| 194 |
* @param string $capability capability's unique label |
| 195 |
* @return object upon success, otherwise false |
| 196 |
*/ |
| 197 |
public static function read_by_capability( $capability ) { |
| 198 |
global $wpdb; |
| 199 |
$_capability = $capability; |
| 200 |
$cached = Groups_Cache::get( self::READ_BY_CAPABILITY . '_' . $_capability, self::CACHE_GROUP ); |
| 201 |
if ( $cached !== null ) { |
| 202 |
$result = $cached->value; |
| 203 |
unset( $cached ); |
| 204 |
} else { |
| 205 |
$result = false; |
| 206 |
$capability_table = _groups_get_tablename( 'capability' ); |
| 207 |
$capability = $wpdb->get_row( $wpdb->prepare( |
| 208 |
"SELECT * FROM $capability_table WHERE capability = %s", |
| 209 |
$capability |
| 210 |
) ); |
| 211 |
if ( isset( $capability->capability_id ) ) { |
| 212 |
$result = $capability; |
| 213 |
} |
| 214 |
Groups_Cache::set( self::READ_BY_CAPABILITY . '_' . $_capability, $result, self::CACHE_GROUP ); |
| 215 |
} |
| 216 |
return $result; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Update capability. |
| 221 |
* |
| 222 |
* @param array $map capability attribute, must contain capability_id |
| 223 |
* @return capability_id on success, otherwise false |
| 224 |
*/ |
| 225 |
public static function update( $map ) { |
| 226 |
|
| 227 |
global $wpdb; |
| 228 |
extract( $map ); |
| 229 |
$result = false; |
| 230 |
|
| 231 |
if ( isset( $capability_id ) && !empty( $capability ) ) { |
| 232 |
$capability_table = _groups_get_tablename( 'capability' ); |
| 233 |
$old_capability = Groups_Capability::read( $capability_id ); |
| 234 |
if ( $old_capability ) { |
| 235 |
if ( isset( $capability ) ) { |
| 236 |
$old_capability_capability = $old_capability->capability; |
| 237 |
$old_capability->capability = $capability; |
| 238 |
} |
| 239 |
if ( isset( $class ) ) { |
| 240 |
$old_capability->class = $class; |
| 241 |
} |
| 242 |
if ( isset( $object ) ) { |
| 243 |
$old_capability->object = $object; |
| 244 |
} |
| 245 |
if ( isset( $name ) ) { |
| 246 |
$old_name = $old_capability->name; |
| 247 |
$old_capability->name = $name; |
| 248 |
} |
| 249 |
if ( isset( $description ) ) { |
| 250 |
$old_capability->description = $description; |
| 251 |
} |
| 252 |
$rows = $wpdb->query( $wpdb->prepare( |
| 253 |
"UPDATE $capability_table SET capability = %s, class = %s, object = %s, name = %s, description = %s WHERE capability_id = %d", |
| 254 |
$old_capability->capability, |
| 255 |
$old_capability->class, |
| 256 |
$old_capability->object, |
| 257 |
$old_capability->name, |
| 258 |
$old_capability->description, |
| 259 |
Groups_Utility::id( $capability_id ) |
| 260 |
) ); |
| 261 |
if ( ( $rows !== false ) ) { |
| 262 |
$result = $capability_id; |
| 263 |
if ( !empty( $old_capability ) && !empty( $old_capability->capability ) ) { |
| 264 |
Groups_Cache::delete( self::READ_BY_CAPABILITY . '_' . $old_capability->capability, self::CACHE_GROUP ); |
| 265 |
} |
| 266 |
if ( !empty( $old_capability_capability ) ) { |
| 267 |
Groups_Cache::delete( self::READ_BY_CAPABILITY . '_' . $old_capability_capability, self::CACHE_GROUP ); |
| 268 |
} |
| 269 |
do_action( "groups_updated_capability", $result ); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
return $result; |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Remove capability and its relations. |
| 278 |
* |
| 279 |
* @param int $capability_id |
| 280 |
* @return capability_id if successful, false otherwise |
| 281 |
*/ |
| 282 |
public static function delete( $capability_id ) { |
| 283 |
|
| 284 |
global $wpdb; |
| 285 |
$result = false; |
| 286 |
|
| 287 |
// avoid nonsense requests |
| 288 |
if ( $capability = Groups_Capability::read( $capability_id ) ) { |
| 289 |
$capability_table = _groups_get_tablename( 'capability' ); |
| 290 |
// get rid of it |
| 291 |
if ( $rows = $wpdb->query( $wpdb->prepare( |
| 292 |
"DELETE FROM $capability_table WHERE capability_id = %d", |
| 293 |
Groups_Utility::id( $capability_id ) |
| 294 |
) ) ) { |
| 295 |
$result = $capability_id; |
| 296 |
if ( !empty( $capability->capability ) ) { |
| 297 |
Groups_Cache::delete( self::READ_BY_CAPABILITY . '_' . $capability->capability, self::CACHE_GROUP ); |
| 298 |
do_action( 'groups_deleted_capability_capability', $capability->capability ); |
| 299 |
} |
| 300 |
do_action( "groups_deleted_capability", $result ); |
| 301 |
} |
| 302 |
} |
| 303 |
return $result; |
| 304 |
} |
| 305 |
} |