| 1 |
<?php |
| 2 |
/** |
| 3 |
* Edit Capabilities tab section on the edit/new role screen. |
| 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 |
* Handles building the edit caps tabs. |
| 17 |
* |
| 18 |
* @since 2.0.0 |
| 19 |
* @access public |
| 20 |
*/ |
| 21 |
final class Cap_Tabs { |
| 22 |
|
| 23 |
/** |
| 24 |
* The role object that we're creating tabs for. |
| 25 |
* |
| 26 |
* @since 2.0.0 |
| 27 |
* @access public |
| 28 |
* @var object |
| 29 |
*/ |
| 30 |
public $role; |
| 31 |
|
| 32 |
/** |
| 33 |
* Array of caps shown by the cap tabs. |
| 34 |
* |
| 35 |
* @since 2.0.0 |
| 36 |
* @access public |
| 37 |
* @var array |
| 38 |
*/ |
| 39 |
public $added_caps = array(); |
| 40 |
|
| 41 |
/** |
| 42 |
* The caps the role has. Note that if this is a new role (new role screen), the default |
| 43 |
* new role caps will be passed in. |
| 44 |
* |
| 45 |
* @since 2.0.0 |
| 46 |
* @access public |
| 47 |
* @var array |
| 48 |
*/ |
| 49 |
public $has_caps = array(); |
| 50 |
|
| 51 |
/** |
| 52 |
* Array of tab sections. |
| 53 |
* |
| 54 |
* @since 2.0.0 |
| 55 |
* @access public |
| 56 |
* @var array |
| 57 |
*/ |
| 58 |
public $sections = array(); |
| 59 |
|
| 60 |
/** |
| 61 |
* Array of single cap controls. |
| 62 |
* |
| 63 |
* @since 2.0.0 |
| 64 |
* @access public |
| 65 |
* @var array |
| 66 |
*/ |
| 67 |
public $controls = array(); |
| 68 |
|
| 69 |
/** |
| 70 |
* Array of section json data. |
| 71 |
* |
| 72 |
* @since 2.0.0 |
| 73 |
* @access public |
| 74 |
* @var array |
| 75 |
*/ |
| 76 |
public $sections_json = array(); |
| 77 |
|
| 78 |
/** |
| 79 |
* Array of control json data. |
| 80 |
* |
| 81 |
* @since 2.0.0 |
| 82 |
* @access public |
| 83 |
* @var array |
| 84 |
*/ |
| 85 |
public $controls_json = array(); |
| 86 |
|
| 87 |
/** |
| 88 |
* Sets up the cap tabs. |
| 89 |
* |
| 90 |
* @since 2.0.0 |
| 91 |
* @access public |
| 92 |
* @param string $role |
| 93 |
* @param array $has_caps |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
public function __construct( $role = '', $has_caps = array() ) { |
| 97 |
|
| 98 |
// Check if there were explicit caps passed in. |
| 99 |
if ( $has_caps ) |
| 100 |
$this->has_caps = $has_caps; |
| 101 |
|
| 102 |
// Check if we have a role. |
| 103 |
if ( $role ) { |
| 104 |
$this->role = members_get_role( $role ); |
| 105 |
|
| 106 |
// If no explicit caps were passed in, use the role's caps. |
| 107 |
if ( ! $has_caps ) |
| 108 |
$this->has_caps = $this->role->caps; |
| 109 |
} |
| 110 |
|
| 111 |
// Add sections and controls. |
| 112 |
$this->register(); |
| 113 |
|
| 114 |
// Print custom JS in the footer. |
| 115 |
add_action( 'admin_footer', array( $this, 'localize_scripts' ), 0 ); |
| 116 |
add_action( 'admin_footer', array( $this, 'print_templates' ) ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Registers the sections (and each section's controls) that will be used for |
| 121 |
* the tab content. |
| 122 |
* |
| 123 |
* @since 2.0.0 |
| 124 |
* @access public |
| 125 |
* @return void |
| 126 |
*/ |
| 127 |
public function register() { |
| 128 |
|
| 129 |
// Hook before registering. |
| 130 |
do_action( 'members_pre_edit_caps_manager_register' ); |
| 131 |
|
| 132 |
$groups = members_get_cap_groups(); |
| 133 |
|
| 134 |
uasort( $groups, 'members_priority_sort' ); |
| 135 |
|
| 136 |
// Get and loop through the available capability groups. |
| 137 |
foreach ( $groups as $group ) { |
| 138 |
|
| 139 |
$caps = $group->caps; |
| 140 |
|
| 141 |
// Remove added caps. |
| 142 |
if ( $group->diff_added ) |
| 143 |
$caps = array_diff( $group->caps, $this->added_caps ); |
| 144 |
|
| 145 |
// Add group's caps to the added caps array. |
| 146 |
$this->added_caps = array_unique( array_merge( $this->added_caps, $caps ) ); |
| 147 |
|
| 148 |
// Create a new section. |
| 149 |
$this->sections[] = $section = new Cap_Section( $this, $group->name, array( 'icon' => $group->icon, 'label' => $group->label ) ); |
| 150 |
|
| 151 |
// Get the section json data. |
| 152 |
$this->sections_json[] = $section->json(); |
| 153 |
|
| 154 |
// Create new controls for each cap. |
| 155 |
foreach ( $caps as $cap ) { |
| 156 |
|
| 157 |
$this->controls[] = $control = new Cap_Control( $this, $cap, array( 'section' => $group->name ) ); |
| 158 |
|
| 159 |
// Get the control json data. |
| 160 |
$this->controls_json[] = $control->json(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
// Create a new "All" section. |
| 165 |
$this->sections[] = $section = new Cap_Section( $this, 'all', array( 'icon' => 'dashicons-plus', 'label' => esc_html__( 'All', 'members' ) ) ); |
| 166 |
|
| 167 |
// Get the section json data. |
| 168 |
$this->sections_json[] = $section->json(); |
| 169 |
|
| 170 |
// Create new controls for each cap. |
| 171 |
foreach ( $this->added_caps as $cap ) { |
| 172 |
|
| 173 |
$this->controls[] = $control = new Cap_Control( $this, $cap, array( 'section' => 'all' ) ); |
| 174 |
|
| 175 |
// Get the control json data. |
| 176 |
$this->controls_json[] = $control->json(); |
| 177 |
} |
| 178 |
|
| 179 |
// Hook after registering. |
| 180 |
do_action( 'members_edit_caps_manager_register' ); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Displays the cap tabs. |
| 185 |
* |
| 186 |
* @since 2.0.0 |
| 187 |
* @access public |
| 188 |
* @return void |
| 189 |
*/ |
| 190 |
public function display() { ?> |
| 191 |
|
| 192 |
<div id="tabcapsdiv" class="postbox"> |
| 193 |
|
| 194 |
<h2 class="hndle"><?php printf( esc_html__( 'Edit Capabilities: %s', 'members' ), '<span class="members-which-tab"></span>' ); ?></h2> |
| 195 |
|
| 196 |
<div class="inside"> |
| 197 |
|
| 198 |
<div class="members-cap-filter"> |
| 199 |
<label for="members-cap-filter-input" class="screen-reader-text"><?php esc_html_e( 'Filter capabilities', 'members' ); ?></label> |
| 200 |
<input |
| 201 |
type="search" |
| 202 |
id="members-cap-filter-input" |
| 203 |
class="members-cap-filter-input" |
| 204 |
placeholder="<?php esc_attr_e( 'Filter capabilities...', 'members' ); ?>" |
| 205 |
autocomplete="off" |
| 206 |
/> |
| 207 |
<span class="members-cap-filter-count" aria-live="polite"></span> |
| 208 |
</div><!-- .members-cap-filter --> |
| 209 |
|
| 210 |
<div class="members-cap-tabs"> |
| 211 |
<?php $this->tab_nav(); ?> |
| 212 |
<div class="members-tab-wrap"></div> |
| 213 |
</div><!-- .members-cap-tabs --> |
| 214 |
|
| 215 |
</div><!-- .inside --> |
| 216 |
|
| 217 |
</div><!-- .postbox --> |
| 218 |
<?php } |
| 219 |
|
| 220 |
/** |
| 221 |
* Outputs the tab nav. |
| 222 |
* |
| 223 |
* @since 2.0.0 |
| 224 |
* @access public |
| 225 |
* @return void |
| 226 |
*/ |
| 227 |
public function tab_nav() { ?> |
| 228 |
|
| 229 |
<ul class="members-tab-nav"> |
| 230 |
|
| 231 |
<?php foreach ( $this->sections as $section ) : ?> |
| 232 |
|
| 233 |
<?php $icon = preg_match( '/dashicons-/', $section->icon ) ? sprintf( 'dashicons %s', sanitize_html_class( $section->icon ) ) : esc_attr( $section->icon ); ?> |
| 234 |
|
| 235 |
<li class="members-tab-title"> |
| 236 |
<a href="<?php echo esc_attr( "#members-tab-{$section->section}" ); ?>"><i class="<?php echo $icon; ?>"></i> <span class="label"><?php echo esc_html( $section->label ); ?></span></a> |
| 237 |
</li> |
| 238 |
|
| 239 |
<?php endforeach; ?> |
| 240 |
|
| 241 |
</ul><!-- .members-tab-nav --> |
| 242 |
<?php } |
| 243 |
|
| 244 |
/** |
| 245 |
* Passes our sections and controls data as json to the `edit-role.js` file. |
| 246 |
* |
| 247 |
* @since 2.0.0 |
| 248 |
* @access public |
| 249 |
* @return void |
| 250 |
*/ |
| 251 |
public function localize_scripts() { |
| 252 |
|
| 253 |
wp_localize_script( 'members-edit-role', 'members_sections', $this->sections_json ); |
| 254 |
wp_localize_script( 'members-edit-role', 'members_controls', $this->controls_json ); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Outputs the Underscore JS templates. |
| 259 |
* |
| 260 |
* @since 2.0.0 |
| 261 |
* @access public |
| 262 |
* @return void |
| 263 |
*/ |
| 264 |
public function print_templates() { ?> |
| 265 |
|
| 266 |
<script type="text/html" id="tmpl-members-cap-section"> |
| 267 |
<?php members_get_underscore_template( 'cap-section' ); ?> |
| 268 |
</script> |
| 269 |
|
| 270 |
<script type="text/html" id="tmpl-members-cap-control"> |
| 271 |
<?php members_get_underscore_template( 'cap-control' ); ?> |
| 272 |
</script> |
| 273 |
<?php } |
| 274 |
} |
| 275 |
|