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