diff --git a/library/AcfFields/json/user-group-url.json b/library/AcfFields/json/user-group-url.json new file mode 100644 index 000000000..1f8384b91 --- /dev/null +++ b/library/AcfFields/json/user-group-url.json @@ -0,0 +1,149 @@ +[{ + "key": "group_677e6a05e347c", + "title": "User Group Home Url", + "fields": [ + { + "key": "field_677e6a08b2126", + "label": "Select type of link", + "name": "user_group_type_of_link", + "aria-label": "", + "type": "select", + "instructions": "", + "required": 1, + "conditional_logic": 0, + "wrapper": { + "width": "", + "class": "", + "id": "" + }, + "choices": { + "disabled": "Disabled", + "arbitrary_url": "Arbitrary URL", + "post_type": "Post Type", + "blog_id": "Network Site (multisite only)" + }, + "default_value": "disabled", + "return_format": "value", + "multiple": 0, + "allow_custom": 0, + "search_placeholder": "", + "allow_null": 0, + "ui": 1, + "ajax": 0, + "placeholder": "" + }, + { + "key": "field_677e6ad862963", + "label": "Arbitrary URL", + "name": "arbitrary_url", + "aria-label": "", + "type": "url", + "instructions": "", + "required": 1, + "conditional_logic": [ + [ + { + "field": "field_677e6a08b2126", + "operator": "==", + "value": "arbitrary_url" + } + ] + ], + "wrapper": { + "width": "", + "class": "", + "id": "" + }, + "default_value": "", + "placeholder": "" + }, + { + "key": "field_677e6b1765fc5", + "label": "Post Type", + "name": "post_type", + "aria-label": "", + "type": "posttype_select", + "instructions": "", + "required": 0, + "conditional_logic": [ + [ + { + "field": "field_677e6a08b2126", + "operator": "==", + "value": "post_type" + } + ] + ], + "wrapper": { + "width": "", + "class": "", + "id": "" + }, + "default_value": "", + "allow_null": 0, + "multiple": 0, + "placeholder": "", + "disabled": 0, + "readonly": 0 + }, + { + "key": "field_677e6b8534123", + "label": "Blog", + "name": "blog_id", + "aria-label": "", + "type": "select", + "instructions": "", + "required": 1, + "conditional_logic": [ + [ + { + "field": "field_677e6a08b2126", + "operator": "==", + "value": "blog_id" + } + ] + ], + "wrapper": { + "width": "", + "class": "", + "id": "" + }, + "choices": {}, + "default_value": false, + "return_format": "value", + "multiple": 0, + "allow_custom": 0, + "search_placeholder": "", + "allow_null": 0, + "ui": 1, + "ajax": 1, + "placeholder": "" + } + ], + "location": [ + [ + { + "param": "taxonomy", + "operator": "==", + "value": "user_group" + } + ] + ], + "menu_order": 0, + "position": "normal", + "style": "default", + "label_placement": "left", + "instruction_placement": "label", + "hide_on_screen": "", + "active": true, + "description": "", + "show_in_rest": 0, + "acfe_display_title": "", + "acfe_autosync": [ + "json" + ], + "acfe_form": 0, + "acfe_meta": "", + "acfe_note": "" +}] + diff --git a/library/AcfFields/php/user-group-url.php b/library/AcfFields/php/user-group-url.php new file mode 100644 index 000000000..1f54bae86 --- /dev/null +++ b/library/AcfFields/php/user-group-url.php @@ -0,0 +1,155 @@ + 'group_677e6a05e347c', + 'title' => __('User Group Home Url', 'municipio'), + 'fields' => array( + 0 => array( + 'key' => 'field_677e6a08b2126', + 'label' => __('Select type of link', 'municipio'), + 'name' => 'user_group_type_of_link', + 'aria-label' => '', + 'type' => 'select', + 'instructions' => '', + 'required' => 1, + 'conditional_logic' => 0, + 'wrapper' => array( + 'width' => '', + 'class' => '', + 'id' => '', + ), + 'choices' => array( + 'disabled' => __('Disabled', 'municipio'), + 'arbitrary_url' => __('Arbitrary URL', 'municipio'), + 'post_type' => __('Post Type', 'municipio'), + 'blog_id' => __('Network Site (multisite only)', 'municipio'), + ), + 'default_value' => __('disabled', 'municipio'), + 'return_format' => 'value', + 'multiple' => 0, + 'allow_custom' => 0, + 'search_placeholder' => '', + 'allow_null' => 0, + 'ui' => 1, + 'ajax' => 0, + 'placeholder' => '', + ), + 1 => array( + 'key' => 'field_677e6ad862963', + 'label' => __('Arbitrary URL', 'municipio'), + 'name' => 'arbitrary_url', + 'aria-label' => '', + 'type' => 'url', + 'instructions' => '', + 'required' => 1, + 'conditional_logic' => array( + 0 => array( + 0 => array( + 'field' => 'field_677e6a08b2126', + 'operator' => '==', + 'value' => 'arbitrary_url', + ), + ), + ), + 'wrapper' => array( + 'width' => '', + 'class' => '', + 'id' => '', + ), + 'default_value' => '', + 'placeholder' => '', + ), + 2 => array( + 'key' => 'field_677e6b1765fc5', + 'label' => __('Post Type', 'municipio'), + 'name' => 'post_type', + 'aria-label' => '', + 'type' => 'posttype_select', + 'instructions' => '', + 'required' => 0, + 'conditional_logic' => array( + 0 => array( + 0 => array( + 'field' => 'field_677e6a08b2126', + 'operator' => '==', + 'value' => 'post_type', + ), + ), + ), + 'wrapper' => array( + 'width' => '', + 'class' => '', + 'id' => '', + ), + 'default_value' => '', + 'allow_null' => 0, + 'multiple' => 0, + 'placeholder' => '', + 'disabled' => 0, + 'readonly' => 0, + ), + 3 => array( + 'key' => 'field_677e6b8534123', + 'label' => __('Blog', 'municipio'), + 'name' => 'blog_id', + 'aria-label' => '', + 'type' => 'select', + 'instructions' => '', + 'required' => 1, + 'conditional_logic' => array( + 0 => array( + 0 => array( + 'field' => 'field_677e6a08b2126', + 'operator' => '==', + 'value' => 'blog_id', + ), + ), + ), + 'wrapper' => array( + 'width' => '', + 'class' => '', + 'id' => '', + ), + 'choices' => array(), + 'default_value' => false, + 'return_format' => 'value', + 'multiple' => 0, + 'allow_custom' => 0, + 'search_placeholder' => '', + 'allow_null' => 0, + 'ui' => 1, + 'ajax' => 1, + 'placeholder' => '', + ), + ), + 'location' => array( + 0 => array( + 0 => array( + 'param' => 'taxonomy', + 'operator' => '==', + 'value' => 'user_group', + ), + ), + ), + 'menu_order' => 0, + 'position' => 'normal', + 'style' => 'default', + 'label_placement' => 'left', + 'instruction_placement' => 'label', + 'hide_on_screen' => '', + 'active' => true, + 'description' => '', + 'show_in_rest' => 0, + 'acfe_display_title' => '', + 'acfe_autosync' => array( + 0 => 'json', + ), + 'acfe_form' => 0, + 'acfe_meta' => '', + 'acfe_note' => '', +)); + +} \ No newline at end of file diff --git a/library/App.php b/library/App.php index 8e18c17b4..703367643 100644 --- a/library/App.php +++ b/library/App.php @@ -422,6 +422,10 @@ private function setUpMiniOrangeIntegration(): void //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(); } /** diff --git a/library/Bootstrap.php b/library/Bootstrap.php index 112393585..5391f63e8 100644 --- a/library/Bootstrap.php +++ b/library/Bootstrap.php @@ -122,6 +122,7 @@ 'widget-media' => 'group_5b2b70c0bde2f', 'media-attachments' => 'group_650857c9f2cce', 'hidden-validation' => 'group_654a2a57e6897', + 'user-group-url' => 'group_677e6a05e347c', )); $acfExportManager->autoExport($autoExportIds); diff --git a/library/Controller/BaseController.php b/library/Controller/BaseController.php index 9d7ecab10..797345493 100644 --- a/library/Controller/BaseController.php +++ b/library/Controller/BaseController.php @@ -2,6 +2,7 @@ namespace Municipio\Controller; +use WP_Term; use WpService\WpService; use AcfService\AcfService; use Municipio\Helper\FormatObject; @@ -339,6 +340,13 @@ public function __construct( $this->getCurrentUrl(['loggedout' => 'true']) ); + // User group + $this->data['userGroup'] = $this->getCurrentUserGroup(); + $this->data['userGroupUrl'] = $this->getCurrentUserGroupUrl($this->data['userGroup']); + + // User basic details + $this->data['userDetails'] = $this->getUserDetails(); + //User role $this->data['userRole'] = $this->getUserRole(); //TODO: MOVE TO USER HELPER CLASS @@ -436,6 +444,109 @@ public function __construct( $this->init(); } + /** + * Get current user group + * + * @return string|null + */ + private function getCurrentUserGroup(): ?WP_Term + { + //Check login + $user = $this->wpService->wpGetCurrentUser(); + if (!$user) { + return null; + } + + //Check if user has a group + $userGroup = $this->wpService->wpGetObjectTerms($user->ID, 'user_group'); + if (empty($userGroup) || $this->wpService->isWpError($userGroup)) { + return null; + } + + //Only get first item + if (is_array($userGroup)) { + $userGroup = array_shift($userGroup); + } + + return is_a($userGroup, 'WP_Term') ? $userGroup : null; + } + + /** + * Get the current user group URL + * + * @param WP_Term|null $term + * @return string + */ + private function getCurrentUserGroupUrl(?WP_Term $term): ?string + { + // Ensure term exists + if (!$term) { + return null; + } + + // Create the term ID + $termId = 'user_group_' . $term->term_id; + + // Get the selected type of link + $typeOfLink = $this->acfService->getField('user_group_type_of_link', $termId); + + // Return null if the option is disabled + if ($typeOfLink === 'disabled') { + return null; + } + + // Handle arbitrary URL + if ($typeOfLink === 'arbitrary_url') { + return $this->acfService->getField('arbitrary_url', $termId) ?: null; + } + + // Handle post type + if ($typeOfLink === 'post_type') { + $postObject = $this->acfService->getField('post_type', $termId); + if ($postObject && isset($postObject->ID)) { + return get_permalink($postObject->ID); + } + return null; + } + + // Handle blog ID in multisite + if ($typeOfLink === 'blog_id') { + $blogId = $this->acfService->getField('blog_id', $termId); + if ($blogId) { + $blogDetails = $this->wpService->getBlogDetails($blogId); + return (function (?object $details): ?string { + return $details ? '//' . $details->domain . $details->path : null; + })($blogDetails); + } + return null; + } + + // Default case (should not occur) + return null; + } + + /** + * Get the current user details + * + * @return object + */ + private function getUserDetails(): ?object + { + $user = $this->wpService->wpGetCurrentUser(); + + if (!$user) { + return null; + } + + return (object) [ + 'id' => $user->ID, + 'email' => $user->user_email, + 'displayname' => $user->display_name, + 'firstname' => $user->first_name, + 'lastname' => $user->last_name, + ]; + } + /** * Get the current URL with optional query parameters. * diff --git a/library/Integrations/MiniOrange/PopulateUserGroupUrlBlogIdField.php b/library/Integrations/MiniOrange/PopulateUserGroupUrlBlogIdField.php new file mode 100644 index 000000000..071f326d5 --- /dev/null +++ b/library/Integrations/MiniOrange/PopulateUserGroupUrlBlogIdField.php @@ -0,0 +1,36 @@ +wpService->addFilter('acf/load_field/key=field_677e6b8534123', array($this, 'populateUserGroupUrlBlogIdField')); + } + + /** + * Populate the user group url blog id field + * + * @param array $field + */ + public function populateUserGroupUrlBlogIdField($field) + { + if(!$this->wpService->isMultisite()) { + return $field; + } + $field['choices'] = []; + $blogs = $this->wpService->getSites(['number' => 500]) ?? []; + foreach ($blogs as $blog) { + $field['choices'][$blog->blog_id] = 'https://' . $blog->domain . $blog->path; + } + return $field; + } +} diff --git a/views/v3/templates/master.blade.php b/views/v3/templates/master.blade.php index 7a693d169..18fae499b 100644 --- a/views/v3/templates/master.blade.php +++ b/views/v3/templates/master.blade.php @@ -73,6 +73,7 @@ @endif + {{-- Above columns sidebar --}} @hasSection('above')