Skip to content

Commit

Permalink
fixed broken behaviour when plugin was activated through cli
Browse files Browse the repository at this point in the history
  • Loading branch information
hansmorb committed Sep 11, 2023
1 parent 5d98e90 commit 19ebc3c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 25 deletions.
75 changes: 54 additions & 21 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,65 @@ public static function deactivation() {
do_action( Scheduler::UNSCHEDULER_HOOK );
}

protected static function addCPTRoleCaps() {
$customPostTypes = commonsbooking_isCurrentUserAdmin() ? self::getCustomPostTypes() : self::getCBManagerCustomPostTypes();

/**
* Adds capabilities for custom post types.
* This function runs after plugin activation and upon plugin update.
*
* NOTE: Before the change, this function did not work because it was dependent
* on the role of the user upon activation. This did not make sense, as this is always
* the admin. The function was changed to add the correct capabilities for all roles.
*
* This usually only happens, when the plugin is activated through wp-cli (because no user is logged in there)
* The way this function has worked before is instead to assign the CB-Manager the capabilites of the admin.
*
* When we change the behaviour to what was probably intended, we break the functionality for the CB Manager to edit posts (like bookings or items).
* This is why we now assign the CB-Manager the capabilities of the admin, the supposedly correct behaviour is commented out below.
*
* Therefore, this function does not work differently, it just has the same behaviour when plugin is activated through wp-cli or through the admin interface.
* @return void
*/
public static function addCPTRoleCaps() {
//admins are allowed to see all custom post types
$adminAllowedCPT = self::getCustomPostTypes();
$CBManagerAllowedCPT = self::getCBManagerCustomPostTypes();
// Add capabilities for user roles
foreach ( $customPostTypes as $customPostType ) {
self::addRoleCaps( $customPostType::$postType );
foreach ( $adminAllowedCPT as $customPostType ) {
self::addRoleCaps( $customPostType::$postType, 'administrator' );
//assign all capabilities of admin to CB-Manager (see comment above)
self::addRoleCaps( $customPostType::$postType, self::$CB_MANAGER_ID );
}
/*
foreach ( $CBManagerAllowedCPT as $customPostType ) {
self::addRoleCaps( $customPostType::$postType, self::$CB_MANAGER_ID );
}
*/
}

/**
* Returns needed roles and caps.
* Returns needed roles and caps for specific roles
*
* @return \bool[][]
*/
public static function getRoleCapMapping() {
return [
self::$CB_MANAGER_ID => [
'read' => true,
'manage_' . COMMONSBOOKING_PLUGIN_SLUG => true,
],
'administrator' => [
'read' => true,
'edit_posts' => true,
'manage_' . COMMONSBOOKING_PLUGIN_SLUG => true,
],
];
public static function getRoleCapMapping( $roleName = null) {
if ( $roleName === null ) {
return [
self::$CB_MANAGER_ID => [
'read' => true,
'manage_' . COMMONSBOOKING_PLUGIN_SLUG => true,
],
'administrator' => [
'read' => true,
'edit_posts' => true,
'manage_' . COMMONSBOOKING_PLUGIN_SLUG => true,
],
];
}
else {
$roleCapMapping = self::getRoleCapMapping();
return [
$roleName => $roleCapMapping[$roleName]
];
}
}

/**
Expand Down Expand Up @@ -161,13 +194,13 @@ public static function getCBManagerCustomPostTypes(): array {
}

/**
* Adds permissions for cb users.
* Adds permissions to edit custom post types for specified role.
*
* @param $postType
*/
public static function addRoleCaps( $postType ) {
protected static function addRoleCaps( $postType, $roleName ) {
// Add the roles you'd like to administer the custom post types
$roles = array_keys( self::getRoleCapMapping() );
$roles = array_keys( self::getRoleCapMapping( $roleName ) );

// Loop through each role and assign capabilities
foreach ( $roles as $the_role ) {
Expand Down
5 changes: 1 addition & 4 deletions tests/Wordpress/CustomPostTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,7 @@ public function createCBManager(){
//we need to run the functions that add the custom user role and assign it to the user
Plugin::addCustomUserRoles();
//and add the caps for each of our custom post types
$postTypes = Plugin::getCustomPostTypes();
foreach ($postTypes as $customPostType) {
Plugin::addRoleCaps($customPostType::$postType);
}
Plugin::addCPTRoleCaps();
$wp_user = get_user_by('email',"cbmanager@cbmanager.de");
if (! $wp_user) {
$this->cbManagerUserID = wp_create_user( "cbmanager", "cbmanager", "cbmanager@cbmanager.de" );
Expand Down

0 comments on commit 19ebc3c

Please sign in to comment.