| 1 |
<?php |
| 2 |
/** |
| 3 |
* Capability groups API. Offers a standardized method for creating capability groups. |
| 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 |
if (!defined('ABSPATH')) { |
| 13 |
die('You are not allowed to call this page directly.'); |
| 14 |
} |
| 15 |
|
| 16 |
# Registers default groups. |
| 17 |
add_action( 'init', 'members_register_cap_groups', 95 ); |
| 18 |
add_action( 'members_register_cap_groups', 'members_register_default_cap_groups', 5 ); |
| 19 |
|
| 20 |
/** |
| 21 |
* Fires the cap group registration action hook. |
| 22 |
* |
| 23 |
* @since 1.0.0 |
| 24 |
* @access public |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
function members_register_cap_groups() { |
| 28 |
|
| 29 |
// Hook for registering cap groups. Plugins should always register on this hook. |
| 30 |
do_action( 'members_register_cap_groups' ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Registers the default cap groups. |
| 35 |
* |
| 36 |
* @since 2.0.0 |
| 37 |
* @access public |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
function members_register_default_cap_groups() { |
| 41 |
|
| 42 |
// Registers the general group. |
| 43 |
members_register_cap_group( 'general', |
| 44 |
array( |
| 45 |
'label' => esc_html__( 'General', 'members' ), |
| 46 |
'icon' => 'dashicons-wordpress', |
| 47 |
'priority' => 5 |
| 48 |
) |
| 49 |
); |
| 50 |
|
| 51 |
// Loop through every custom post type. |
| 52 |
foreach ( get_post_types( array(), 'objects' ) as $type ) { |
| 53 |
|
| 54 |
// Skip revisions and nave menu items. |
| 55 |
if ( in_array( $type->name, array( 'revision', 'nav_menu_item', 'custom_css', 'customize_changeset' ) ) ) |
| 56 |
continue; |
| 57 |
|
| 58 |
// Get the caps for the post type. |
| 59 |
$has_caps = members_get_post_type_group_caps( $type->name ); |
| 60 |
|
| 61 |
// Skip if the post type doesn't have caps. |
| 62 |
if ( empty( $has_caps ) ) |
| 63 |
continue; |
| 64 |
|
| 65 |
// Set the default post type icon. |
| 66 |
$icon = $type->hierarchical ? 'dashicons-admin-page' : 'dashicons-admin-post'; |
| 67 |
|
| 68 |
// Get the post type icon. |
| 69 |
if ( is_string( $type->menu_icon ) && preg_match( '/dashicons-/i', $type->menu_icon ) ) |
| 70 |
$icon = $type->menu_icon; |
| 71 |
|
| 72 |
else if ( 'attachment' === $type->name ) |
| 73 |
$icon = 'dashicons-admin-media'; |
| 74 |
|
| 75 |
else if ( 'download' === $type->name ) |
| 76 |
$icon = 'dashicons-download'; // EDD |
| 77 |
|
| 78 |
else if ( 'product' === $type->name ) |
| 79 |
$icon = 'dashicons-cart'; |
| 80 |
|
| 81 |
// Register the post type cap group. |
| 82 |
members_register_cap_group( "type-{$type->name}", |
| 83 |
array( |
| 84 |
'label' => $type->labels->name, |
| 85 |
'caps' => $has_caps, |
| 86 |
'icon' => $icon, |
| 87 |
'priority' => 10 |
| 88 |
) |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
// Register the taxonomy group. |
| 93 |
members_register_cap_group( 'taxonomy', |
| 94 |
array( |
| 95 |
'label' => esc_html__( 'Taxonomies', 'members' ), |
| 96 |
'caps' => members_get_taxonomy_group_caps(), |
| 97 |
'icon' => 'dashicons-tag', |
| 98 |
'diff_added' => true, |
| 99 |
'priority' => 15 |
| 100 |
) |
| 101 |
); |
| 102 |
|
| 103 |
// Register the theme group. |
| 104 |
members_register_cap_group( 'theme', |
| 105 |
array( |
| 106 |
'label' => esc_html__( 'Appearance', 'members' ), |
| 107 |
'icon' => 'dashicons-admin-appearance', |
| 108 |
'priority' => 20 |
| 109 |
) |
| 110 |
); |
| 111 |
|
| 112 |
// Register the plugin group. |
| 113 |
members_register_cap_group( 'plugin', |
| 114 |
array( |
| 115 |
'label' => esc_html__( 'Plugins', 'members' ), |
| 116 |
'icon' => 'dashicons-admin-plugins', |
| 117 |
'priority' => 25 |
| 118 |
) |
| 119 |
); |
| 120 |
|
| 121 |
// Register the user group. |
| 122 |
members_register_cap_group( 'user', |
| 123 |
array( |
| 124 |
'label' => esc_html__( 'Users', 'members' ), |
| 125 |
'icon' => 'dashicons-admin-users', |
| 126 |
'priority' => 30 |
| 127 |
) |
| 128 |
); |
| 129 |
|
| 130 |
// Register the custom group. |
| 131 |
members_register_cap_group( 'custom', |
| 132 |
array( |
| 133 |
'label' => esc_html__( 'Custom', 'members' ), |
| 134 |
'caps' => members_get_capabilities(), |
| 135 |
'icon' => 'dashicons-admin-generic', |
| 136 |
'diff_added' => true, |
| 137 |
'priority' => 995 |
| 138 |
) |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Returns the instance of cap group registry. |
| 144 |
* |
| 145 |
* @since 2.0.0 |
| 146 |
* @access public |
| 147 |
* @return object |
| 148 |
*/ |
| 149 |
function members_cap_group_registry() { |
| 150 |
|
| 151 |
return \Members\Registry::get_instance( 'cap_group' ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Function for registering a cap group. |
| 156 |
* |
| 157 |
* @since 1.0.0 |
| 158 |
* @access public |
| 159 |
* @param string $name |
| 160 |
* @param array $args |
| 161 |
* @return void |
| 162 |
*/ |
| 163 |
function members_register_cap_group( $name, $args = array() ) { |
| 164 |
|
| 165 |
members_cap_group_registry()->register( $name, new \Members\Cap_Group( $name, $args ) ); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Unregisters a group. |
| 170 |
* |
| 171 |
* @since 1.0.0 |
| 172 |
* @access public |
| 173 |
* @param string $name |
| 174 |
* @return void |
| 175 |
*/ |
| 176 |
function members_unregister_cap_group( $name ) { |
| 177 |
|
| 178 |
members_cap_group_registry()->unregister( $name ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Checks if a group exists. |
| 183 |
* |
| 184 |
* @since 1.0.0 |
| 185 |
* @access public |
| 186 |
* @param string $name |
| 187 |
* @return bool |
| 188 |
*/ |
| 189 |
function members_cap_group_exists( $name ) { |
| 190 |
|
| 191 |
return members_cap_group_registry()->exists( $name ); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Returns an array of registered group objects. |
| 196 |
* |
| 197 |
* @since 1.0.0 |
| 198 |
* @access public |
| 199 |
* @return array |
| 200 |
*/ |
| 201 |
function members_get_cap_groups() { |
| 202 |
|
| 203 |
return members_cap_group_registry()->get_collection(); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Returns a group object if it exists. Otherwise, `FALSE`. |
| 208 |
* |
| 209 |
* @since 1.0.0 |
| 210 |
* @access public |
| 211 |
* @param string $name |
| 212 |
* @return object|bool |
| 213 |
*/ |
| 214 |
function members_get_cap_group( $name ) { |
| 215 |
|
| 216 |
return members_cap_group_registry()->get( $name ); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Returns the caps for a specific post type capability group. |
| 221 |
* |
| 222 |
* @since 1.0.0 |
| 223 |
* @access public |
| 224 |
* @return array |
| 225 |
*/ |
| 226 |
function members_get_post_type_group_caps( $post_type = 'post' ) { |
| 227 |
|
| 228 |
// Get the post type caps. |
| 229 |
$caps = (array) get_post_type_object( $post_type )->cap; |
| 230 |
|
| 231 |
// remove meta caps. |
| 232 |
unset( $caps['edit_post'] ); |
| 233 |
unset( $caps['read_post'] ); |
| 234 |
unset( $caps['delete_post'] ); |
| 235 |
|
| 236 |
// Get the cap names only. |
| 237 |
$caps = array_values( $caps ); |
| 238 |
|
| 239 |
// If this is not a core post/page post type. |
| 240 |
if ( ! in_array( $post_type, array( 'post', 'page' ) ) ) { |
| 241 |
|
| 242 |
// Get the post and page caps. |
| 243 |
$post_caps = array_values( (array) get_post_type_object( 'post' )->cap ); |
| 244 |
$page_caps = array_values( (array) get_post_type_object( 'page' )->cap ); |
| 245 |
|
| 246 |
// Remove post/page caps from the current post type caps. |
| 247 |
$caps = array_diff( $caps, $post_caps, $page_caps ); |
| 248 |
} |
| 249 |
|
| 250 |
// If attachment post type, add the `unfiltered_upload` cap. |
| 251 |
if ( 'attachment' === $post_type ) |
| 252 |
$caps[] = 'unfiltered_upload'; |
| 253 |
|
| 254 |
$registered_caps = array_keys( wp_list_filter( members_get_caps(), array( 'group' => "type-{$post_type}" ) ) ); |
| 255 |
|
| 256 |
if ( $registered_caps ) |
| 257 |
array_merge( $caps, $registered_caps ); |
| 258 |
|
| 259 |
// Make sure there are no duplicates and return. |
| 260 |
return array_unique( $caps ); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Returns the caps for the taxonomy capability group. |
| 265 |
* |
| 266 |
* @since 1.0.0 |
| 267 |
* @access public |
| 268 |
* @return array |
| 269 |
*/ |
| 270 |
function members_get_taxonomy_group_caps() { |
| 271 |
|
| 272 |
$do_not_add = array( |
| 273 |
'assign_categories', |
| 274 |
'edit_categories', |
| 275 |
'delete_categories', |
| 276 |
'assign_post_tags', |
| 277 |
'edit_post_tags', |
| 278 |
'delete_post_tags', |
| 279 |
'manage_post_tags' |
| 280 |
); |
| 281 |
|
| 282 |
$taxi = get_taxonomies( array(), 'objects' ); |
| 283 |
|
| 284 |
$caps = array(); |
| 285 |
|
| 286 |
foreach ( $taxi as $tax ) |
| 287 |
$caps = array_merge( $caps, array_values( (array) $tax->cap ) ); |
| 288 |
|
| 289 |
$registered_caps = array_keys( wp_list_filter( members_get_caps(), array( 'group' => 'taxonomy' ) ) ); |
| 290 |
|
| 291 |
if ( $registered_caps ) |
| 292 |
array_merge( $caps, $registered_caps ); |
| 293 |
|
| 294 |
return array_diff( array_unique( $caps ), $do_not_add ); |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Returns the caps for the custom capability group. |
| 299 |
* |
| 300 |
* @since 1.0.0 |
| 301 |
* @access public |
| 302 |
* @return array |
| 303 |
*/ |
| 304 |
function members_get_custom_group_caps() { |
| 305 |
|
| 306 |
$caps = members_get_capabilities(); |
| 307 |
|
| 308 |
$registered_caps = array_keys( wp_list_filter( members_get_caps(), array( 'group' => 'custom' ) ) ); |
| 309 |
|
| 310 |
if ( $registered_caps ) |
| 311 |
array_merge( $caps, $registered_caps ); |
| 312 |
|
| 313 |
return array_unique( $caps ); |
| 314 |
} |
| 315 |
|