| 1 |
<?php |
| 2 |
/** |
| 3 |
* Creates a new role object. This is an extension of the core `get_role()` functionality. It's |
| 4 |
* just been beefed up a bit to provide more useful info for our plugin. |
| 5 |
* |
| 6 |
* @package Members |
| 7 |
* @subpackage Includes |
| 8 |
* @author The MemberPress Team |
| 9 |
* @copyright Copyright (c) 2009 - 2018, The MemberPress Team |
| 10 |
* @link https://members-plugin.com/ |
| 11 |
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 12 |
*/ |
| 13 |
namespace Members; |
| 14 |
|
| 15 |
defined('ABSPATH') || exit; |
| 16 |
/** |
| 17 |
* Role class. |
| 18 |
* |
| 19 |
* @since 2.0.0 |
| 20 |
* @access public |
| 21 |
*/ |
| 22 |
class Role { |
| 23 |
|
| 24 |
/** |
| 25 |
* The role name. |
| 26 |
* |
| 27 |
* @since 2.0.0 |
| 28 |
* @access public |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
public $name = ''; |
| 32 |
|
| 33 |
/** |
| 34 |
* The role label. |
| 35 |
* |
| 36 |
* @since 2.0.0 |
| 37 |
* @access public |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
public $label = ''; |
| 41 |
|
| 42 |
/** |
| 43 |
* The group the role belongs to. |
| 44 |
* |
| 45 |
* @see Members\Role_Group |
| 46 |
* @since 2.0.0 |
| 47 |
* @access public |
| 48 |
* @var string |
| 49 |
*/ |
| 50 |
public $group = ''; |
| 51 |
|
| 52 |
/** |
| 53 |
* Whether the role has caps (granted). |
| 54 |
* |
| 55 |
* @since 2.0.0 |
| 56 |
* @access public |
| 57 |
* @var bool |
| 58 |
*/ |
| 59 |
public $has_caps = false; |
| 60 |
|
| 61 |
/** |
| 62 |
* Capability count for the role. |
| 63 |
* |
| 64 |
* @since 2.0.0 |
| 65 |
* @access public |
| 66 |
* @var int |
| 67 |
*/ |
| 68 |
public $granted_cap_count = 0; |
| 69 |
|
| 70 |
/** |
| 71 |
* Capability count for the role. |
| 72 |
* |
| 73 |
* @since 2.0.0 |
| 74 |
* @access public |
| 75 |
* @var int |
| 76 |
*/ |
| 77 |
public $denied_cap_count = 0; |
| 78 |
|
| 79 |
/** |
| 80 |
* Array of capabilities that the role has in the form of `array( $cap => $bool )`. |
| 81 |
* |
| 82 |
* @since 2.0.0 |
| 83 |
* @access public |
| 84 |
* @var array |
| 85 |
*/ |
| 86 |
public $caps = array(); |
| 87 |
|
| 88 |
/** |
| 89 |
* Array of granted capabilities that the role has. |
| 90 |
* |
| 91 |
* @since 2.0.0 |
| 92 |
* @access public |
| 93 |
* @var array |
| 94 |
*/ |
| 95 |
public $granted_caps = array(); |
| 96 |
|
| 97 |
/** |
| 98 |
* Array of denied capabilities that the role has. |
| 99 |
* |
| 100 |
* @since 2.0.0 |
| 101 |
* @access public |
| 102 |
* @var array |
| 103 |
*/ |
| 104 |
public $denied_caps = array(); |
| 105 |
|
| 106 |
/** |
| 107 |
* Return the role string in attempts to use the object as a string. |
| 108 |
* |
| 109 |
* @since 2.0.0 |
| 110 |
* @access public |
| 111 |
* @return string |
| 112 |
*/ |
| 113 |
public function __toString() { |
| 114 |
return $this->name; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Creates a new role object. |
| 119 |
* |
| 120 |
* @since 2.0.0 |
| 121 |
* @access public |
| 122 |
* @param string $role |
| 123 |
* @param array $args |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
public function __construct( $name, $args = array() ) { |
| 127 |
|
| 128 |
foreach ( array_keys( get_object_vars( $this ) ) as $key ) { |
| 129 |
|
| 130 |
if ( isset( $args[ $key ] ) ) |
| 131 |
$this->$key = $args[ $key ]; |
| 132 |
} |
| 133 |
|
| 134 |
$this->name = members_sanitize_role( $name ); |
| 135 |
|
| 136 |
if ( $this->caps ) { |
| 137 |
|
| 138 |
// Validate cap values as booleans in case they are stored as strings. |
| 139 |
$this->caps = array_map( 'members_validate_boolean', $this->caps ); |
| 140 |
|
| 141 |
// Get granted and denied caps. |
| 142 |
$this->granted_caps = array_keys( $this->caps, true ); |
| 143 |
$this->denied_caps = array_keys( $this->caps, false ); |
| 144 |
|
| 145 |
// Remove user levels from granted/denied caps. |
| 146 |
$this->granted_caps = members_remove_old_levels( $this->granted_caps ); |
| 147 |
$this->denied_caps = members_remove_old_levels( $this->denied_caps ); |
| 148 |
|
| 149 |
// Remove hidden caps from granted/denied caps. |
| 150 |
$this->granted_caps = members_remove_hidden_caps( $this->granted_caps ); |
| 151 |
$this->denied_caps = members_remove_hidden_caps( $this->denied_caps ); |
| 152 |
|
| 153 |
// Set the cap count. |
| 154 |
$this->granted_cap_count = count( $this->granted_caps ); |
| 155 |
$this->denied_cap_count = count( $this->denied_caps ); |
| 156 |
|
| 157 |
// Check if we have caps. |
| 158 |
$this->has_caps = 0 < $this->granted_cap_count; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Magic method for getting media object properties. Let's keep from failing if a theme |
| 164 |
* author attempts to access a property that doesn't exist. |
| 165 |
* |
| 166 |
* @since 2.0.2 |
| 167 |
* @access public |
| 168 |
* @param string $property |
| 169 |
* @return mixed |
| 170 |
*/ |
| 171 |
public function get( $property ) { |
| 172 |
|
| 173 |
if ( 'label' === $property ) |
| 174 |
return members_translate_role( $this->name ); |
| 175 |
|
| 176 |
return isset( $this->$property ) ? $this->$property : false; |
| 177 |
} |
| 178 |
} |
| 179 |
|