"Add roles" placeholder is missing if the role is deleted or `edit_posts` capability is revoked
See original GitHub issueBug Description
Bug bash issue: https://app.asana.com/0/1202258919887896/1202443593793369 please see Asana task for background
Steps To Reproduce:
- Using the User Role Editor plugin, add a custom user role and provide edit_posts capability to that role to be available in the DS settings user roles.
- Set the custom role to a module in the DS settings.
- Delete the custom role.
- Or revoke the edit_posts capability.
- Go to the DS settings modal.
Actual: The module with the custom role set is currently empty, which is expected. However, the Add roles placeholder is missing. That’s due the role is still available in the _googlesitekitDashboardSharingData.settings[module].sharedRoles array. The current logic checks the length of that array and displays the placeholder.
Expected: Add roles placeholder should be visible if the role is deleted or the edit_posts capability is revoked.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- The shared roles of a module must always be a subset of the list of all current shareable roles
- Non-shareable roles should be filtered out accordingly when saving module sharing settings and when reading/loading the value on the backend
Implementation Brief
In includes/Core/Modules/Module_Sharing_Settings.php:
- Create a new protected filter method
filter_shared_roleswith the following logic:- It should receive
shared_rolesas an argument. - Filter the
shared_rolesusing thearray_filter. - Get the role object using the
get_rolemethod, - Return
falseif the role object does not exist. This means that the role was deleted. - Check if the role has the
edit_postscapability. If it does not, returnfalse. - Otherwise, return
true. See: https://github.com/google/site-kit-wp/blob/e87bd022874396b3e876a6a25d1710f03b8b7b75/includes/Core/Assets/Assets.php#L823-L826
- It should receive
For reading data from the backend, the following steps are taken:
- Within the
Module_Sharing_Settings::getmethod, call thefilter_shared_rolesmethod to get the filtered list of shared roles. - Return the filtered list of shared roles within the
sharing_settings.
For saving data to the backend, the following steps are taken:
- Similarly to the above, the
Module_Sharing_Settings::get_sanitize_callbackshould use thefilter_shared_rolesmethod to get the filtered list of shared roles. - Return the filtered list of shared roles within the
sanitized_option.
Test Coverage
- In
tests/phpunit/integration/Core/Modules/Module_Sharing_SettingsTest.php, update thetest_getandtest_get_sanitize_callbacktests to add coverage for the above scenario.
QA Brief
- Follow the provided steps to reproduce, and ensure that the “Add roles” placeholder is present, after deleting/revoking the
edit_postscapability for a custom role.
Changelog entry
- Ensure the user role select always displays properly based on current shareable roles.
Issue Analytics
- State:
- Created a year ago
- Comments:14

Top Related StackOverflow Question
Hi @hussain-t, here’s a bit of commentary on the IB so far.
The
option_anddefault_option_filters are applied after the value is read from the database. So, in order to filter when writing to the DB we should in fact augment theModule_Sharing_Settings::get_sanitize_callbackmethod.Also because
option_/default_option_are applied after reading from the DB, the change you have suggested toget_all_shared_rolesshould not be necessary.Mind you another consideration is, seeing as we will be updating
Module_Sharing_Settings::get_sanitize_callbackto filter on write, maybe we should apply the filter-on-read toModule_Sharing_Settings::getrather than having this filtering logic spread acrossModule_Sharing_SettingsandModule. So we could have a reusablefilter_shared_roleshelper method inModule_Sharing_Settingsand then call that in bothget_sanitize_callbackandget…@aaemnnosttv Sorry, It was a typo. Just updated my comment. Yes there is no error showing anymore. Above mentioned issue is resolved.