PluginProbe
Groups – Memberships and Access Control / 1.10.1
Groups – Memberships and Access Control v1.10.1
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.10.1, at lib/core/class-groups-group-capability.php

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