| 1 |
<?php |
| 2 |
/** |
| 3 |
* class-groups-group-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_Group_Capability { |
| 22 |
|
| 23 |
/** |
| 24 |
* Hook into appropriate actions when needed. |
| 25 |
* For now, this does nothing. |
| 26 |
* |
| 27 |
* @see Groups_Group::delete() |
| 28 |
*/ |
| 29 |
public static function init() { |
| 30 |
// Note that group-capabilities are deleted when a group is deleted. |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Persist a group-capability relation. |
| 35 |
* |
| 36 |
* @param array $map attributes - must provide group_id and capability_id |
| 37 |
* @return true on success, otherwise false |
| 38 |
*/ |
| 39 |
public static function create( $map ) { |
| 40 |
|
| 41 |
global $wpdb; |
| 42 |
extract( $map ); |
| 43 |
$result = false; |
| 44 |
|
| 45 |
// avoid nonsense requests |
| 46 |
if ( !empty( $group_id ) && !empty( $capability_id) ) { |
| 47 |
// make sure group and capability exist |
| 48 |
if ( Groups_Group::read( $group_id ) && Groups_Capability::read( $capability_id ) ) { |
| 49 |
$group_capability_table = _groups_get_tablename( 'group_capability' ); |
| 50 |
// don't try to create duplicate entries |
| 51 |
// also it would raise an error for duplicate PK |
| 52 |
if ( 0 === intval( $wpdb->get_var( $wpdb->prepare( |
| 53 |
"SELECT COUNT(*) FROM $group_capability_table WHERE group_id = %d AND capability_id = %d", |
| 54 |
Groups_Utility::id( $group_id ), |
| 55 |
Groups_Utility::id( $capability_id ) |
| 56 |
) ) ) ) { |
| 57 |
$data = array( |
| 58 |
'group_id' => Groups_Utility::id( $group_id ), |
| 59 |
'capability_id' => Groups_Utility::id( $capability_id ) |
| 60 |
); |
| 61 |
$formats = array( '%d', '%d' ); |
| 62 |
if ( $wpdb->insert( $group_capability_table, $data, $formats ) ) { |
| 63 |
$result = true; |
| 64 |
do_action( "groups_created_group_capability", $group_id, $capability_id ); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
return $result; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Retrieve a group-capability relation. |
| 74 |
* |
| 75 |
* @param int $group_id group's id |
| 76 |
* @param int $capability_id capability's id |
| 77 |
* @return object upon success, otherwise false |
| 78 |
*/ |
| 79 |
public static function read( $group_id, $capability_id ) { |
| 80 |
global $wpdb; |
| 81 |
$result = false; |
| 82 |
|
| 83 |
$group_capability_table = _groups_get_tablename( 'group_capability' ); |
| 84 |
$group_capability = $wpdb->get_row( $wpdb->prepare( |
| 85 |
"SELECT * FROM $group_capability_table WHERE group_id = %d AND capability_id = %d", |
| 86 |
Groups_Utility::id( $group_id ), |
| 87 |
Groups_Utility::id( $capability_id ) |
| 88 |
) ); |
| 89 |
if ( $group_capability !== null ) { |
| 90 |
$result = $group_capability; |
| 91 |
} |
| 92 |
return $result; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Update group-capability relation. |
| 97 |
* |
| 98 |
* This changes nothing so as of now it's pointless to even call this. |
| 99 |
* |
| 100 |
* @param array $map |
| 101 |
* @return true if successful, false otherwise |
| 102 |
*/ |
| 103 |
public static function update( $map ) { |
| 104 |
$result = false; |
| 105 |
if ( !empty( $group_id ) && !empty( $capability_id) ) { |
| 106 |
// make sure group and capability exist |
| 107 |
if ( Groups_Group::read( $group_id ) && Groups_Capability::read( $capability_id ) ) { |
| 108 |
$result = true; |
| 109 |
do_action( "groups_updated_group_capability", $group_id, $capability_id ); |
| 110 |
} |
| 111 |
} |
| 112 |
return $result; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Remove group-capability relation. |
| 117 |
* |
| 118 |
* @param int $group_id |
| 119 |
* @param int $capability_id |
| 120 |
* @return true if successful, false otherwise |
| 121 |
*/ |
| 122 |
public static function delete( $group_id, $capability_id ) { |
| 123 |
|
| 124 |
global $wpdb; |
| 125 |
$result = false; |
| 126 |
|
| 127 |
// avoid nonsense requests |
| 128 |
if ( !empty( $group_id ) && !empty( $capability_id) ) { |
| 129 |
// we can omit checking if the group and capability exist, to |
| 130 |
// allow resolving the relationship after they have been deleted |
| 131 |
$group_capability_table = _groups_get_tablename( 'group_capability' ); |
| 132 |
// get rid of it |
| 133 |
$rows = $wpdb->query( $wpdb->prepare( |
| 134 |
"DELETE FROM $group_capability_table WHERE group_id = %d AND capability_id = %d", |
| 135 |
Groups_Utility::id( $group_id ), |
| 136 |
Groups_Utility::id( $capability_id ) |
| 137 |
) ); |
| 138 |
// must have affected a row, otherwise no great success |
| 139 |
$result = ( $rows !== false ) && ( $rows > 0 ); |
| 140 |
if ( $result ) { |
| 141 |
do_action( "groups_deleted_group_capability", $group_id, $capability_id ); |
| 142 |
} |
| 143 |
} |
| 144 |
return $result; |
| 145 |
} |
| 146 |
} |
| 147 |
Groups_Group_Capability::init(); |
| 148 |
|