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 / wp / class-groups-wordpress.php

class-groups-wordpress.php in Groups – Memberships and Access Control 1.1.5, at lib/wp/class-groups-wordpress.php

119 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * class-groups-wordpress.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 * WordPress capabilities integration.
24 */
25 class Groups_WordPress {
26
27 /**
28 * Hook into actions to extend user capabilities.
29 *
30 * @todo We might want to keep up with new capabilities when added, so
31 * that others don't have to add these explicitly to Groups when they
32 * add them to WordPress. Currently there's no hook for when a capability
33 * is added and checking this in any other way is too costly.
34 */
35 public static function init() {
36 // args: string $result, Groups_User $groups_user, string $capability
37 add_filter( 'groups_user_can', array( __CLASS__, 'groups_user_can' ), 10, 3 );
38 add_filter( 'user_has_cap', array( __CLASS__, 'user_has_cap' ), 10, 3 );
39 }
40
41 /**
42 * Extends Groups user capability with its WP_User capability.
43 *
44 * @param string $result
45 * @param Groups_User $groups_user
46 * @param string $capability
47 */
48 public static function groups_user_can( $result, $groups_user, $capability ) {
49 if ( !$result ) {
50 // Check if the capability exists, otherwise this will
51 // produce a deprecation warning "Usage of user levels by plugins
52 // and themes is deprecated", not because we actually use a
53 // deprecated user level, but because it doesn't exist.
54 if ( Groups_Capability::read_by_capability( $capability ) ) {
55 $result = user_can( $groups_user->user->ID, $capability );
56 }
57 }
58 return $result;
59 }
60
61 /**
62 * Extend user capabilities with Groups user capabilities.
63 *
64 * @param array $allcaps the capabilities the user has
65 * @param unknown_type $caps
66 * @param unknown_type $args
67 */
68 public static function user_has_cap( $allcaps, $caps, $args ) {
69 $user_id = isset( $args[1] ) ? $args[1] : null;
70 $groups_user = new Groups_User( $user_id );
71 if ( is_array( $caps ) ) {
72 // we need to deactivate this because invoking $groups_user->can()
73 // would trigger this same function and we would end up
74 // in an infinite loop
75 remove_filter( 'user_has_cap', array( __CLASS__, 'user_has_cap' ), 10, 3 );
76 foreach( $caps as $cap ) {
77 if ( $groups_user->can( $cap ) ) {
78 $allcaps[$cap] = true;
79 }
80 }
81 add_filter( 'user_has_cap', array( __CLASS__, 'user_has_cap' ), 10, 3 );
82 }
83 return $allcaps;
84 }
85
86 /**
87 * Adds WordPress capabilities to Groups capabilities.
88 * Must be called explicitly.
89 * @see Groups_Controller::activate()
90 */
91 public static function activate() {
92 global $wp_roles;
93 $capabilities = array();
94
95 if ( !isset( $wp_roles ) ) {
96 // just trigger initialization
97 get_role( 'administrator' );
98 }
99 $roles = $wp_roles->roles;
100 if ( is_array( $roles ) ) {
101 foreach ( $roles as $rolename => $atts ) {
102 if ( isset( $atts['capabilities'] ) && is_array( $atts['capabilities'] ) ) {
103 foreach ( $atts['capabilities'] as $capability => $value ) {
104 if ( !in_array( $capability, $capabilities ) ) {
105 $capabilities[] = $capability;
106 }
107 }
108 }
109 }
110 }
111 foreach ( $capabilities as $capability ) {
112 if ( !Groups_Capability::read_by_capability( $capability ) ) {
113 Groups_Capability::create( array( 'capability' => $capability ) );
114 }
115 }
116 }
117 }
118 Groups_WordPress::init();
119