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