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

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