Skip to content

Commit

Permalink
feat: add class to use options from blog 1 on subsites. (#1265)
Browse files Browse the repository at this point in the history
* feat: add class to use options from blog 1 on subsites.

* fix: wip

* fix: populate select admin page

* fix: decide to use a field group mapping instead.

* fix: hide fields that cannot be edited on this site

* feat: implement filter functionality in frontend.

* fix: remove static enable.

* fix: lint

* fix: add field configuration

* fix: rename to CommonFieldGroups

* fix: remove unnneccesary comments

* feat: add tests for PopulateCommonFieldGroupSelectTest

* fix: do not have multiple class declaration stacked in test file.

* feat: make more testable & add tests for DisableFieldsThatAreCommonlyManagedOnSubsites

* fix: add public function if wpservice is missing acf_get_fields.

* fix: lint

* fix: add tests & implement contracts in site switcher.

* fix: rename tests with more descriptive names.

* fix: remove seo redirect decorator. Implemented elsewhere.

---------

Co-authored-by: Sebastian Thulin <sebastian.thulin@helsingborg.se>
  • Loading branch information
sebastianthulin and Sebastian Thulin authored Jan 22, 2025
1 parent f6839a0 commit 5dfd224
Show file tree
Hide file tree
Showing 20 changed files with 1,228 additions and 28 deletions.
104 changes: 104 additions & 0 deletions library/AcfFields/json/common-field-groups.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
[{
"key": "group_678e65a73edb3",
"title": "Common Field Groups",
"fields": [
{
"key": "field_678fb6b1caa9e",
"label": "Common Field Groups Feature Description",
"name": "",
"aria-label": "",
"type": "message",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"message": "This feature allows you to select fields that should be manage from the main site id (that is this site). Field groups will not be rendered on the subsites; Instead a link will be rendered to take the user to the main site for editing of the option. \r\n\r\nPlease select the field groups that you want to manage centrally below.",
"new_lines": "wpautop",
"esc_html": 0
},
{
"key": "field_678e65abcf203",
"label": "Common Field Groups",
"name": "sitewide_common_acf_fieldgroups",
"aria-label": "",
"type": "repeater",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"acfe_repeater_stylised_button": 0,
"layout": "table",
"pagination": 0,
"min": 0,
"max": 0,
"collapsed": "",
"button_label": "L\u00e4gg till rad",
"rows_per_page": 20,
"sub_fields": [
{
"key": "field_678e6610d5287",
"label": "Select a fieldgroup",
"name": "sitewide_common_acf_fieldgroup_value",
"aria-label": "",
"type": "select",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"choices": {
"group_678e65a73edb3": "Common Field Groups",
"group_5aa1543e70216": "Report settings"
},
"default_value": false,
"return_format": "value",
"multiple": 0,
"allow_null": 0,
"ui": 0,
"ajax": 0,
"placeholder": "",
"allow_custom": 0,
"search_placeholder": "",
"parent_repeater": "field_678e65abcf203"
}
]
}
],
"location": [
[
{
"param": "options_page",
"operator": "==",
"value": "common-field-groups"
}
]
],
"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": ""
}]

109 changes: 109 additions & 0 deletions library/AcfFields/php/common-field-groups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

if (function_exists('acf_add_local_field_group')) {
acf_add_local_field_group(array(
'key' => 'group_678e65a73edb3',
'title' => 'Common Field Groups',
'fields' => array(
0 => array(
'key' => 'field_678fb6b1caa9e',
'label' => __('Common Field Groups Feature Description', 'municipio'),
'name' => '',
'aria-label' => '',
'type' => 'message',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'message' => __('This feature allows you to select fields that should be manage from the main site id (that is this site). Field groups will not be rendered on the subsites; Instead a link will be rendered to take the user to the main site for editing of the option.
Please select the field groups that you want to manage centrally below.', 'municipio'),
'new_lines' => 'wpautop',
'esc_html' => 0,
),
1 => array(
'key' => 'field_678e65abcf203',
'label' => __('Common Field Groups', 'municipio'),
'name' => 'sitewide_common_acf_fieldgroups',
'aria-label' => '',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'acfe_repeater_stylised_button' => 0,
'layout' => 'table',
'pagination' => 0,
'min' => 0,
'max' => 0,
'collapsed' => '',
'button_label' => __('Lägg till rad', 'municipio'),
'rows_per_page' => 20,
'sub_fields' => array(
0 => array(
'key' => 'field_678e6610d5287',
'label' => __('Select a fieldgroup', 'municipio'),
'name' => 'sitewide_common_acf_fieldgroup_value',
'aria-label' => '',
'type' => 'select',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array(
'group_678e65a73edb3' => __('Common Field Groups', 'municipio'),
'group_5aa1543e70216' => __('Report settings', 'municipio'),
),
'default_value' => false,
'return_format' => 'value',
'multiple' => 0,
'allow_null' => 0,
'ui' => 0,
'ajax' => 0,
'placeholder' => '',
'allow_custom' => 0,
'search_placeholder' => '',
'parent_repeater' => 'field_678e65abcf203',
),
),
),
),
'location' => array(
0 => array(
0 => array(
'param' => 'options_page',
'operator' => '==',
'value' => 'common-field-groups',
),
),
),
'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' => '',
));
}
Expand Down
58 changes: 53 additions & 5 deletions library/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use Municipio\Helper\User\Config\UserConfig;
use Municipio\Helper\User\User;
use Municipio\Helper\SiteSwitcher\SiteSwitcher;
use Municipio\CommonOptions\CommonOptionsConfig;

/**
* Class App
Expand Down Expand Up @@ -102,7 +103,7 @@ public function __construct(
$userHelperConfig,
$userGroupConfig,
new \Municipio\Helper\Term\Term($this->wpService, $this->acfService),
new \Municipio\Helper\SiteSwitcher\SiteSwitcher($this->wpService)
new \Municipio\Helper\SiteSwitcher\SiteSwitcher($this->wpService, $this->acfService)
);

/**
Expand Down Expand Up @@ -367,6 +368,51 @@ public function __construct(
* Broken links
*/
$this->setUpBrokenLinksIntegration();

/**
* Setup common options
*/
$this->setUpCommonFieldGroups();
}

/**
* Set up the common options feature.
*
* This method initializes the common options feature by creating an instance of the
* RegisterCommonOptionsAdminPage class and passing the WordPress service instance.
*
* @return void
*/
private function setUpCommonFieldGroups(): void
{
//Init dependencies
$siteSwitcher = new \Municipio\Helper\SiteSwitcher\SiteSwitcher($this->wpService, $this->acfService);
$config = new \Municipio\CommonFieldGroups\CommonFieldGroupsConfig(
$this->wpService,
$this->acfService,
$siteSwitcher
);

//Check if feature is enabled
if ($config->isEnabled() === false) {
return;
}

//Admin page
$registerCommonFieldGroupsOptionsAdminPage = new \Municipio\CommonFieldGroups\RegisterCommonFieldGroupsOptionsAdminPage($this->wpService, $this->acfService);
$registerCommonFieldGroupsOptionsAdminPage->addHooks();

//Populate admin page fields
$populateCommonFieldGroupSelect = new \Municipio\CommonFieldGroups\PopulateCommonFieldGroupSelect($this->wpService, $this->acfService, $config);
$populateCommonFieldGroupSelect->addHooks();

//Disable fields
$disableFieldsThatAreCommonlyManagedOnSubsites = new \Municipio\CommonFieldGroups\DisableFieldsThatAreCommonlyManagedOnSubsites($this->wpService, $this->acfService, $siteSwitcher, $config);
$disableFieldsThatAreCommonlyManagedOnSubsites->addHooks();

//Modify field choices
$filterGetFieldToRetriveCommonValues = new \Municipio\CommonFieldGroups\FilterGetFieldToRetriveCommonValues($this->wpService, $this->acfService, $siteSwitcher, $config);
$filterGetFieldToRetriveCommonValues->addHooks();
}

/**
Expand Down Expand Up @@ -399,7 +445,7 @@ private function setUpBrokenLinksIntegration(): void
private function setupLoginLogout(): void
{
//Needs setUser to be called before using the user object
$userHelper = new User($this->wpService, $this->acfService, new UserConfig(), new \Municipio\UserGroup\Config\UserGroupConfig($this->wpService), new \Municipio\Helper\Term\Term($this->wpService, $this->acfService), new \Municipio\Helper\SiteSwitcher\SiteSwitcher($this->wpService));
$userHelper = new User($this->wpService, $this->acfService, new UserConfig(), new \Municipio\UserGroup\Config\UserGroupConfig($this->wpService), new \Municipio\Helper\Term\Term($this->wpService, $this->acfService), new \Municipio\Helper\SiteSwitcher\SiteSwitcher($this->wpService, $this->acfService));

$filterAuthUrls = new \Municipio\Admin\Login\RelationalLoginLogourUrls($this->wpService);
$filterAuthUrls->addHooks();
Expand Down Expand Up @@ -440,19 +486,20 @@ private function setupUserGroupFeature(): void
// Setup dependencies
$userGroupRestrictionConfig = new \Municipio\Admin\Private\Config\UserGroupRestrictionConfig();
$userHelperConfig = new \Municipio\Helper\User\Config\UserConfig();

$userHelper = new \Municipio\Helper\User\User(
$this->wpService,
$this->acfService,
$userHelperConfig,
$config,
new \Municipio\Helper\Term\Term($this->wpService, $this->acfService),
new \Municipio\Helper\SiteSwitcher\SiteSwitcher($this->wpService)
new \Municipio\Helper\SiteSwitcher\SiteSwitcher($this->wpService, $this->acfService)
);

$getUserGroupTerms = new \Municipio\Helper\User\GetUserGroupTerms(
$this->wpService,
$config->getUserGroupTaxonomy(),
new \Municipio\Helper\SiteSwitcher\SiteSwitcher($this->wpService)
new \Municipio\Helper\SiteSwitcher\SiteSwitcher($this->wpService, $this->acfService)
);

// Create user group taxonomy
Expand Down Expand Up @@ -501,8 +548,9 @@ private function setUpMiniOrangeIntegration(): void
new \Municipio\Helper\User\Config\UserConfig(),
new \Municipio\UserGroup\Config\UserGroupConfig($this->wpService),
new \Municipio\Helper\Term\Term($this->wpService, $this->acfService),
new \Municipio\Helper\SiteSwitcher\SiteSwitcher($this->wpService)
new \Municipio\Helper\SiteSwitcher\SiteSwitcher($this->wpService, $this->acfService)
);

$termHelper = new \Municipio\Helper\Term\Term($this->wpService, $this->acfService);
$userGroupConfig = new \Municipio\UserGroup\Config\UserGroupConfig($this->wpService);
$config = new \Municipio\Integrations\MiniOrange\Config\MiniOrangeConfig($this->wpService);
Expand Down
1 change: 1 addition & 0 deletions library/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
'hidden-validation' => 'group_654a2a57e6897',
'user-group-url' => 'group_677e6a05e347c',
'post-status-conditional' => 'group_671241997f07d',
'common-field-groups' => 'group_678e65a73edb3',
));

$acfExportManager->autoExport($autoExportIds);
Expand Down
Loading

0 comments on commit 5dfd224

Please sign in to comment.