PluginProbe
Groups – Memberships and Access Control / 1.1.4
Groups – Memberships and Access Control v1.1.4
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.4, at lib/wp/class-groups-wordpress.php

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