From a6dde15b52b45a0387aaf0e3c912d77486845689 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 5 Nov 2024 17:21:42 +0100 Subject: [PATCH] (refactor): addCustomRoles --- src/UserRoles.php | 118 +++++++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 43 deletions(-) diff --git a/src/UserRoles.php b/src/UserRoles.php index 211e7e0..1eb1e98 100644 --- a/src/UserRoles.php +++ b/src/UserRoles.php @@ -8,6 +8,7 @@ use Webmozart\Assert\Assert; use WP_CLI; use WP_CLI\ExitException; +use WP_Role; class UserRoles { @@ -54,7 +55,7 @@ private function deleteCustomRoles(): void { $this->wpCli::log($this->wpCli::colorize('%MDelete custom roles:%n')); - $customRoles = $this->getCurrentCustomRoles(); + $customRoles = $this->currentCustomRoles(); if (0 === count($customRoles)) { $this->wpCli::warning("No custom roles with prefix '" . $this->prefix . "' found in database. Skipping custom role deletion."); @@ -70,7 +71,7 @@ private function deleteCustomRoles(): void /** * @return array */ - private function getCurrentCustomRoles(): array + private function currentCustomRoles(): array { return array_filter( array_keys(wp_roles()->roles), @@ -90,64 +91,95 @@ private function addCustomRoles(): void $roles = $this->config['roles']; - foreach ($roles as $role => $properties) { - if (! isset($properties['display_name']) || ! is_string($properties['display_name'])) { + foreach ($roles as $role => $props) { + if (! isset($props['display_name']) || ! is_string($props['display_name'])) { $this->wpCli::warning("No display name configured for role $role. Skipping role creation."); continue; } - $capabilities = []; - $removeCapabilities = []; + if ($this->cloneValid($props)) { + $this->addClone($role, $props); + } else { + $this->addRole($role, $props); + } + } + } - if ($this->cloneValid($properties)) { - $this->createRole($role, $properties, ['clone' => $properties['clone']['from']]); + /** + * @param array $props + */ + private function addRole(string $role, array $props): void + { + $role = $this->createRole($role, $props); + $capabilities = $this->capsFromRoleProps($props); - if (isset($properties['clone']['add']) && is_array($properties['clone']['add'])) { - $capabilities = $this->capabilitiesFromRoleProperties($properties['clone']['add']); - } + $this->addCaps($role, $capabilities); + } - if (isset($properties['clone']['remove']) && is_array($properties['clone']['remove'])) { - $removeCapabilities = $this->capabilitiesFromRoleProperties($properties['clone']['remove']); - } - } else { - $this->createRole($role, $properties); - $capabilities = $this->capabilitiesFromRoleProperties($properties); - } + /** + * @param array $props + */ + private function addClone(string $role, array $props): void + { + $role = $this->createRole($role, $props, ['clone' => $props['clone']['from']]); - $role = get_role($this->prefix . $role); + if (isset($props['clone']['add']) && is_array($props['clone']['add'])) { + $caps = $this->capsFromRoleProps($props['clone']['add']); + $this->addCaps($role, $caps); + } - Assert::notNull($role); + if (isset($props['clone']['remove']) && is_array($props['clone']['remove'])) { + $removeCaps = $this->capsFromRoleProps($props['clone']['remove']); + $this->removeCaps($role, $removeCaps); + } + } - foreach ($capabilities as $cap => $grant) { - $role->add_cap((string)$cap, $grant); - } + /** + * @param array $caps + */ + private function addCaps(WP_Role $role, array $caps): void + { + foreach ($caps as $cap => $grant) { + $role->add_cap((string)$cap, $grant); + } + } - foreach ($removeCapabilities as $cap => $grant) { - $role->remove_cap((string)$cap); - } + /** + * @param array $caps + */ + private function removeCaps(WP_Role $role, array $caps): void + { + foreach ($caps as $cap => $grant) { + $role->remove_cap((string)$cap); } } /** - * @param array $properties + * @param array $props */ - private function cloneValid(array $properties): bool + private function cloneValid(array $props): bool { - return isset($properties['clone']['from']) - && is_string($properties['clone']['from']); + return isset($props['clone']['from']) + && is_string($props['clone']['from']); } /** - * @param array $properties + * @param array $props * @param array $assocArgs */ - private function createRole(string $role, array $properties, array $assocArgs = []): void + private function createRole(string $role, array $props, array $assocArgs = []): WP_Role { $this->roleCommand->create([ $this->prefix . $role, - $properties['display_name'], + $props['display_name'], ], $assocArgs); + + $wpRole = get_role($this->prefix . $role); + + Assert::notNull($wpRole); + + return $wpRole; } private function rolesValid(): bool @@ -191,18 +223,18 @@ private function coreRolesValid(): bool } /** - * @param array $properties + * @param array $props * * @return array */ - public function capabilitiesFromRoleProperties(array $properties): array + public function capsFromRoleProps(array $props): array { $capabilities = []; - if (! empty($properties['cap_groups'])) { + if (! empty($props['cap_groups'])) { $capGroups = $this->config['cap_groups']; Assert::isArray($capGroups); - Assert::isArray($properties['cap_groups']); - foreach ($properties['cap_groups'] as $group) { + Assert::isArray($props['cap_groups']); + foreach ($props['cap_groups'] as $group) { $groupCaps = $capGroups[$group]; Assert::isArray($groupCaps); @@ -212,9 +244,9 @@ public function capabilitiesFromRoleProperties(array $properties): array } } - if (! empty($properties['post_type_caps'])) { - Assert::isArray($properties['post_type_caps']); - foreach ($properties['post_type_caps'] as $postType) { + if (! empty($props['post_type_caps'])) { + Assert::isArray($props['post_type_caps']); + foreach ($props['post_type_caps'] as $postType) { $postTypeCaps = get_post_type_object($postType)?->cap; if (null === $postTypeCaps) { $this->wpCli::warning("Post type '$postType' does not exist. Skipping post type caps."); @@ -228,8 +260,8 @@ public function capabilitiesFromRoleProperties(array $properties): array } } - if (! empty($properties['caps'])) { - foreach ($properties['caps'] as $cap) { + if (! empty($props['caps'])) { + foreach ($props['caps'] as $cap) { $capabilities[$cap] = true; } }