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

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

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