Skip to content

Commit

Permalink
refactor: move UserGroup parts of MiniOrange integration into separat…
Browse files Browse the repository at this point in the history
…e UserGroup feature (#1250)
  • Loading branch information
thorbrink authored Jan 15, 2025
1 parent d754fc8 commit 7beb6a0
Show file tree
Hide file tree
Showing 18 changed files with 519 additions and 150 deletions.
17 changes: 13 additions & 4 deletions library/Admin/Login/RedirectUserToGroupUrlIfIsPreferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
use Municipio\HooksRegistrar\Hookable;
use WpService\WpService;
use Municipio\Helper\User\User;
use WpService\Contracts\AddQueryArg;

/**
* Redirect user to group url if user prefers group url
*/
class RedirectUserToGroupUrlIfIsPreferred implements Hookable
{
public function __construct(private WpService $wpService, private User $userHelper)
/**
* Constructor.
*/
public function __construct(private WpService&AddQueryArg $wpService, private User $userHelper)
{
}

Expand Down Expand Up @@ -39,12 +46,14 @@ public function redirectToGroupUrl($redirectTo, $request, $userInHook)
if ($user != null) {
$perfersGroupUrl = $this->userHelper->getUserPrefersGroupUrl();
$groupUrl = $this->userHelper->getUserGroupUrl();

if ($perfersGroupUrl && $groupUrl) {
return add_query_arg(['offerPersistantHomeUrl' => 'true'], $groupUrl);
return $this->wpService->addQueryArg([
'loggedin' => 'true',
'prefersgroup' => 'true'
], $groupUrl);
}
}
return $redirectTo;
}

}
68 changes: 46 additions & 22 deletions library/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,11 @@ public function __construct(
*/
$this->setupLoginLogout();

/**
* UserGroup feature
*/
$this->setupUserGroupFeature();

/**
* MiniOrange integration
*/
Expand Down Expand Up @@ -426,6 +431,38 @@ private function setupLoginLogout(): void
$redirectUserToGroupUrlIfIsPrefered->addHooks();
}

/**
* Set up the user group feature.
*/
private function setupUserGroupFeature(): void
{
$config = new \Municipio\UserGroup\Config\UserGroupConfig();

if ($config->isEnabled() === false) {
return;
}

// Create user group taxonomy
$createUserGroupTaxonomy = new \Municipio\UserGroup\CreateUserGroupTaxonomy($this->wpService, $config);
$createUserGroupTaxonomy->addHooks();

// Add user group to users list
$displayUserGroupTaxonomyInUsersList = new \Municipio\UserGroup\DisplayUserGroupTaxonomyInUsersList($this->wpService, $config);
$displayUserGroupTaxonomyInUsersList->addHooks();

// Add user group to admin menu
$displayUserGroupTaxonomyLinkInAdminUi = new \Municipio\UserGroup\DisplayUserGroupTaxonomyLinkInAdminUi($this->wpService, $config);
$displayUserGroupTaxonomyLinkInAdminUi->addHooks();

// Add user group to user profile & populate
$displayUserGroupTaxonomyInUserProfile = new \Municipio\UserGroup\DisplayUserGroupTaxonomyInUserProfile($this->wpService, $this->acfService, $config);
$displayUserGroupTaxonomyInUserProfile->addHooks();

// User group url
$populateUserGroupUrlBlogIdField = new \Municipio\UserGroup\PopulateUserGroupUrlBlogIdField($this->wpService);
$populateUserGroupUrlBlogIdField->addHooks();
}

/**
* Set up the MiniOrange integration.
*
Expand All @@ -435,7 +472,10 @@ private function setupLoginLogout(): void
*/
private function setUpMiniOrangeIntegration(): void
{
$config = new \Municipio\Integrations\MiniOrange\Config\MiniOrangeConfig($this->wpService);
$termHelper = new \Municipio\Helper\Term\Term($this->wpService, $this->acfService);
$userGroupConfig = new \Municipio\UserGroup\Config\UserGroupConfig();
$config = new \Municipio\Integrations\MiniOrange\Config\MiniOrangeConfig($this->wpService);

if ($config->isEnabled() === false) {
return;
}
Expand All @@ -451,29 +491,13 @@ private function setUpMiniOrangeIntegration(): void
$attributeMapper = new \Municipio\Integrations\MiniOrange\AttributeMapper($this->wpService, $config, ...$mappingProviders);
$attributeMapper->addHooks();

//Create user group taxonomy
$createUserGroupTaxonomy = new \Municipio\Integrations\MiniOrange\CreateUserGroupTaxonomy($this->wpService, $config);
$createUserGroupTaxonomy->addHooks();
if ($userGroupConfig->isEnabled() === false) {
return;
}

//Set group as taxonomy
$setGroupAsTaxonomy = new \Municipio\Integrations\MiniOrange\SetGroupAsTaxonomy($this->wpService, $config);
// Set group as taxonomy
$setGroupAsTaxonomy = new \Municipio\Integrations\MiniOrange\SetUserGroupFromSsoLoginGroup($this->wpService, $termHelper, $userGroupConfig);
$setGroupAsTaxonomy->addHooks();

//Add user group to users list
$displayUserGroupTaxonomyInUsersList = new \Municipio\Integrations\MiniOrange\DisplayUserGroupTaxonomyInUsersList($this->wpService, $config);
$displayUserGroupTaxonomyInUsersList->addHooks();

//Add user group to admin menu
$displayUserGroupTaxonomyLinkInAdminUi = new \Municipio\Integrations\MiniOrange\DisplayUserGroupTaxonomyLinkInAdminUi($this->wpService, $config);
$displayUserGroupTaxonomyLinkInAdminUi->addHooks();

//Add user group to user profile & populate
$displayUserGroupTaxonomyInUserProfile = new \Municipio\Integrations\MiniOrange\DisplayUserGroupTaxonomyInUserProfile($this->wpService, $this->acfService, $config);
$displayUserGroupTaxonomyInUserProfile->addHooks();

//User group url
$populateUserGroupUrlBlogIdField = new \Municipio\Integrations\MiniOrange\PopulateUserGroupUrlBlogIdField($this->wpService);
$populateUserGroupUrlBlogIdField->addHooks();
}

/**
Expand Down
15 changes: 15 additions & 0 deletions library/Helper/Term/Contracts/CreateOrGetTermIdFromString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Municipio\Helper\Term\Contracts;

interface CreateOrGetTermIdFromString
{
/**
* Create or get a term from a string.
*
* @param string $termString The term string.
* @param string $taxonomy The taxonomy.
* @return int|null The term ID or null if the term could not be created or found.
*/
public function createOrGetTermIdFromString(string $termString, string $taxonomy): ?int;
}
31 changes: 25 additions & 6 deletions library/Helper/Term/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
namespace Municipio\Helper\Term;

use AcfService\Contracts\GetField;
use Municipio\Helper\Term\Contracts\CreateOrGetTermIdFromString;
use Municipio\Helper\Term\Contracts\GetTermColor;
use Municipio\Helper\Term\Contracts\GetTermIcon;
use WpService\Contracts\ApplyFilters;
use WpService\Contracts\GetAncestors;
use WpService\Contracts\GetTermBy;
use WpService\Contracts\WpGetAttachmentImageUrl;
use WP_Term;
use WpService\Contracts\{WpInsertTerm, WpGetAttachmentImageUrl, IsWpError, GetTermBy, GetAncestors, ApplyFilters};

/**
* Class Term
*/
class Term implements GetTermColor, GetTermIcon
class Term implements GetTermColor, GetTermIcon, CreateOrGetTermIdFromString
{
/**
* Constructor.
*/
public function __construct(
private GetTermBy&ApplyFilters&GetAncestors&WpGetAttachmentImageUrl $wpService,
private GetTermBy&ApplyFilters&GetAncestors&WpGetAttachmentImageUrl&WpInsertTerm&IsWpError $wpService,
private GetField $acfService
) {
}
Expand Down Expand Up @@ -138,4 +137,24 @@ public function getTermIcon(int|string|\WP_Term $term, string $taxonomy = ''): a

return $result;
}

/**
* @inheritDoc
*/
public function createOrGetTermIdFromString(string $termString, string $taxonomy): ?int
{
$term = $this->wpService->getTermBy('name', $termString, $taxonomy, 'OBJECT');

if (!$term) {
$result = $this->wpService->wpInsertTerm($termString, $taxonomy);
if ($this->wpService->isWpError($result)) {
return null;
}
$termId = $result['term_id'];
} else {
$termId = $term->term_id;
}

return $termId ?? null;
}
}
39 changes: 39 additions & 0 deletions library/Helper/Term/Term.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,43 @@ public function testGetTermIconReturnsTheSvgIconOfTheTermIfItHasOne()
$this->assertEquals('description', $icon['description']);
$this->assertEquals('description', $icon['alt']);
}

/**
* @testdox createOrGetTermIdFromString() returns termId if term already exists
*/
public function testCreateOrGetTermIdFromStringReturnsTermIdIfTermAlreadyExists()
{
$wpService = new FakeWpService(['getTermBy' => WpMockFactory::createWpTerm(['term_id' => 123])]);
$termHelper = new Term($wpService, new FakeAcfService());

$this->assertEquals(123, $termHelper->createOrGetTermIdFromString('term', 'category'));
}

/**
* @testdox createOrGetTermIdFromString() returns null if term could no be created
*/
public function testCreateOrGetTermIdFromStringReturnsNullIfTermCouldNotBeCreated()
{
$wpService = new FakeWpService([
'getTermBy' => false,
'wpInsertTerm' => WpMockFactory::createWpError(),
'isWpError' => true]);
$termHelper = new Term($wpService, new FakeAcfService());

$this->assertNull($termHelper->createOrGetTermIdFromString('term', 'category'));
}

/**
* @testdox createOrGetTermIdFromString() returns termId if term was created
*/
public function testCreateOrGetTermIdFromStringReturnsTermIdIfTermWasCreated()
{
$wpService = new FakeWpService([
'getTermBy' => false,
'wpInsertTerm' => ['term_id' => 123],
'isWpError' => false]);
$termHelper = new Term($wpService, new FakeAcfService());

$this->assertEquals(123, $termHelper->createOrGetTermIdFromString('term', 'category'));
}
}
92 changes: 0 additions & 92 deletions library/Integrations/MiniOrange/SetGroupAsTaxonomy.php

This file was deleted.

Loading

0 comments on commit 7beb6a0

Please sign in to comment.