| 1 |
<?php |
| 2 |
/** |
| 3 |
* Capability control class for use in the edit capabilities tabs. |
| 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\Admin; |
| 13 |
|
| 14 |
defined('ABSPATH') || exit; |
| 15 |
/** |
| 16 |
* Cap control class. |
| 17 |
* |
| 18 |
* @since 2.0.0 |
| 19 |
* @access public |
| 20 |
*/ |
| 21 |
final class Cap_Control { |
| 22 |
|
| 23 |
/** |
| 24 |
* Stores the cap tabs object. |
| 25 |
* |
| 26 |
* @see Members_Cap_Tabs |
| 27 |
* @since 2.0.0 |
| 28 |
* @access public |
| 29 |
* @var object |
| 30 |
*/ |
| 31 |
public $manager; |
| 32 |
|
| 33 |
/** |
| 34 |
* Name of the capability the control is for. |
| 35 |
* |
| 36 |
* @since 2.0.0 |
| 37 |
* @access public |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
public $cap = ''; |
| 41 |
|
| 42 |
/** |
| 43 |
* ID of the section the control is for. |
| 44 |
* |
| 45 |
* @since 2.0.0 |
| 46 |
* @access public |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
public $section = ''; |
| 50 |
|
| 51 |
/** |
| 52 |
* Array of data to pass as a json object to the Underscore template. |
| 53 |
* |
| 54 |
* @since 2.0.0 |
| 55 |
* @access public |
| 56 |
* @var array |
| 57 |
*/ |
| 58 |
public $json = array(); |
| 59 |
|
| 60 |
/** |
| 61 |
* Creates a new control object. |
| 62 |
* |
| 63 |
* @since 2.0.0 |
| 64 |
* @access public |
| 65 |
* @param object $manager |
| 66 |
* @param string $cap |
| 67 |
* @param array $args |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function __construct( $manager, $cap, $args = array() ) { |
| 71 |
|
| 72 |
foreach ( array_keys( get_object_vars( $this ) ) as $key ) { |
| 73 |
|
| 74 |
if ( isset( $args[ $key ] ) ) |
| 75 |
$this->$key = $args[ $key ]; |
| 76 |
} |
| 77 |
|
| 78 |
$this->manager = $manager; |
| 79 |
$this->cap = $cap; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns the json array. |
| 84 |
* |
| 85 |
* @since 2.0.0 |
| 86 |
* @access public |
| 87 |
* @return array |
| 88 |
*/ |
| 89 |
public function json() { |
| 90 |
$this->to_json(); |
| 91 |
return $this->json; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Adds custom data to the json array. This data is passed to the Underscore template. |
| 96 |
* |
| 97 |
* @since 2.0.0 |
| 98 |
* @access public |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
public function to_json() { |
| 102 |
|
| 103 |
// Is the role editable? |
| 104 |
$is_editable = $this->manager->role ? members_is_role_editable( $this->manager->role->name ) : true; |
| 105 |
|
| 106 |
// Get the current capability. |
| 107 |
$this->json['cap'] = $this->cap; |
| 108 |
|
| 109 |
// Add the section ID. |
| 110 |
$this->json['section'] = $this->section; |
| 111 |
|
| 112 |
// If the cap is not editable, the inputs should be read-only. |
| 113 |
$this->json['readonly'] = $is_editable ? '' : ' disabled="disabled" readonly="readonly"'; |
| 114 |
|
| 115 |
// Set up the input labels. |
| 116 |
$this->json['label'] = array( |
| 117 |
'cap' => members_show_human_caps() && members_cap_exists( $this->cap ) ? members_get_cap( $this->cap )->label : $this->cap, |
| 118 |
'grant' => sprintf( esc_html__( 'Grant %s capability', 'members' ), "<code>{$this->cap}</code>" ), |
| 119 |
'deny' => sprintf( esc_html__( 'Deny %s capability', 'members' ), "<code>{$this->cap}</code>" ) |
| 120 |
); |
| 121 |
|
| 122 |
// Set up the input `name` attributes. |
| 123 |
$this->json['name'] = array( |
| 124 |
'grant' => 'grant-caps[]', |
| 125 |
'deny' => 'deny-caps[]' |
| 126 |
); |
| 127 |
|
| 128 |
// Is this a granted or denied cap? |
| 129 |
$this->json['is_granted_cap'] = isset( $this->manager->has_caps[ $this->cap ] ) && $this->manager->has_caps[ $this->cap ]; |
| 130 |
$this->json['is_denied_cap'] = isset( $this->manager->has_caps[ $this->cap ] ) && false === $this->manager->has_caps[ $this->cap ]; |
| 131 |
} |
| 132 |
} |
| 133 |
|