| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class for handling a capability group object. |
| 4 |
* |
| 5 |
* @package Members |
| 6 |
* @subpackage Admin |
| 7 |
* @author The MemberPress Team |
| 8 |
* @copyright Copyright (c) 2009 - 2018, The MemberPress Team |
| 9 |
* @link https://members-plugin.com/ |
| 10 |
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 11 |
*/ |
| 12 |
namespace Members; |
| 13 |
|
| 14 |
defined('ABSPATH') || exit; |
| 15 |
/** |
| 16 |
* Capability group object class. |
| 17 |
* |
| 18 |
* @since 2.0.0 |
| 19 |
* @access public |
| 20 |
*/ |
| 21 |
final class Cap_Group { |
| 22 |
|
| 23 |
/** |
| 24 |
* Name/ID for the group. |
| 25 |
* |
| 26 |
* @since 2.0.0 |
| 27 |
* @access public |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
public $name = ''; |
| 31 |
|
| 32 |
/** |
| 33 |
* Internationalized text label for the group. |
| 34 |
* |
| 35 |
* @since 2.0.0 |
| 36 |
* @access public |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
public $label = ''; |
| 40 |
|
| 41 |
/** |
| 42 |
* Icon for the group. This can be a dashicons class or a custom class. |
| 43 |
* |
| 44 |
* @since 2.0.0 |
| 45 |
* @access public |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
public $icon = 'dashicons-admin-generic'; |
| 49 |
|
| 50 |
/** |
| 51 |
* Capabilities for the group. |
| 52 |
* |
| 53 |
* @since 2.0.0 |
| 54 |
* @access public |
| 55 |
* @var array |
| 56 |
*/ |
| 57 |
public $caps = array(); |
| 58 |
|
| 59 |
/** |
| 60 |
* Sort order priority. |
| 61 |
* |
| 62 |
* @since 2.0.0 |
| 63 |
* @access public |
| 64 |
* @var int |
| 65 |
*/ |
| 66 |
public $priority = 10; |
| 67 |
|
| 68 |
/** |
| 69 |
* Whether to remove previously-added caps from this group's caps. |
| 70 |
* |
| 71 |
* @since 2.0.0 |
| 72 |
* @access public |
| 73 |
* @var bool |
| 74 |
*/ |
| 75 |
public $diff_added = false; |
| 76 |
|
| 77 |
/** |
| 78 |
* Magic method to use in case someone tries to output the object as a string. |
| 79 |
* We'll just return the name. |
| 80 |
* |
| 81 |
* @since 2.0.0 |
| 82 |
* @access public |
| 83 |
* @return string |
| 84 |
*/ |
| 85 |
public function __toString() { |
| 86 |
return $this->name; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Register a new object. |
| 91 |
* |
| 92 |
* @since 2.0.0 |
| 93 |
* @access public |
| 94 |
* @param string $name |
| 95 |
* @param array $args { |
| 96 |
* @type string $label Internationalized text label. |
| 97 |
* @type string $icon Dashicon icon in the form of `dashicons-icon-name`. |
| 98 |
* @type array $caps Array of capabilities in the group. |
| 99 |
* @type bool $merge_added Whether to merge this caps into the added caps array. |
| 100 |
* @type bool $diff_added Whether to remove previously-added caps from this group. |
| 101 |
* } |
| 102 |
* @return void |
| 103 |
*/ |
| 104 |
public function __construct( $name, $args = array() ) { |
| 105 |
|
| 106 |
foreach ( array_keys( get_object_vars( $this ) ) as $key ) { |
| 107 |
|
| 108 |
if ( isset( $args[ $key ] ) ) |
| 109 |
$this->$key = $args[ $key ]; |
| 110 |
} |
| 111 |
|
| 112 |
$this->name = sanitize_key( $name ); |
| 113 |
|
| 114 |
$registered_caps = array_keys( wp_list_filter( members_get_caps(), array( 'group' => $this->name ) ) ); |
| 115 |
|
| 116 |
$this->caps = array_unique( array_merge( $this->caps, $registered_caps ) ); |
| 117 |
|
| 118 |
$this->caps = members_remove_hidden_caps( $this->caps ); |
| 119 |
} |
| 120 |
} |
| 121 |
|