Remove legacy `getModulesData` JS function and `_googlesitekitLegacyData.modules` global
Related to #3647: All usages of the legacy function getModulesData which relies on the _googlesitekitLegacyData.modules global should be refactored, and then those two pieces should be entirely removed.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- All JS components currently relying on
getModulesDatashould be refactored to use thegetModulesorgetModuledatastore selector instead, using React hooks. - The
getModulesDatafunction should be entirely removed. - The
_googlesitekitLegacyData.modulesglobal should be entirely removed, including where it is set in PHP.
Implementation Brief
Update getModules code when changing from getModulesData
- In any place where we change from
getModulesDatatogetModules, note that the data returned fromgetModulesis slightly different. Most of the keys/values/info is the same, but here's a map of the old to new keys:sort(old) =>order(new)screenID(old) => N/A (not needed)autoActivate(old) =>forceActive(new)required(old) =>dependencies(new)settings(old) => N/A (if any data fromsettingsis needed by components usinggetModulesDatait will need refactoring)- All other keys are the same or are only available in the new API
SettingsModules component
- Remove the
useEffectin https://github.com/google/site-kit-wp/blob/3db19cbeca41145aefc6c6c3383ee9b66e8b4121/assets/js/components/settings/SettingsModules.js#L38 and changemodulesDatatoconst moduleData = useSelect( ( select ) => select( CORE_MODULES ).getModules() );(renaming themodulesDatatomodulesas well).
Some component require changing to functional components to use hooks, but this is straightforward:
- Convert
assets/js/components/legacy-notifications/dashboard-setup-alerts.jsto a functional component. Refactor https://github.com/google/site-kit-wp/blob/3db19cbeca41145aefc6c6c3383ee9b66e8b4121/assets/js/components/legacy-notifications/dashboard-setup-alerts.js#L62-L67 to access the module in a hook before theswitchstatement. - Change https://github.com/google/site-kit-wp/blob/3db19cbeca41145aefc6c6c3383ee9b66e8b4121/assets/js/components/legacy-notifications/dashboard-modules-alerts.js#L38 to a functional component, moving the
componentDidMountlogic to auseMounthook. Then, use thegetModules()selector to replace the current usage ofgetModulesData().
Other changes that don't require converting to functional components but require refactoring:
- Change https://github.com/google/site-kit-wp/blob/3db19cbeca41145aefc6c6c3383ee9b66e8b4121/assets/js/components/notifications/UnsatisfiedScopesAlert.js#L61 to select all modules using the
getModules()selector and passmodulesas an argument tomapScopesToModuleNamesin the same file.
Misc changes
- ~Remove
assets/js/modules/optimize/index.legacy.js.~ (This is still required, actually, so should not be removed.) - Remove
assets/js/components/legacy-notifications/data-error.js. - Remove
assets/js/util/index.test.js. - Remove the https://github.com/google/site-kit-wp/blob/3db19cbeca41145aefc6c6c3383ee9b66e8b4121/assets/js/util/index.js#L118 function.
PHP Changes
- Remove
'modules', and'permissions'keys from the array inget_inline_datainAssets.php(see: https://github.com/google/site-kit-wp/blob/3db19cbeca41145aefc6c6c3383ee9b66e8b4121/includes/Core/Assets/Assets.php#L811-L827) - Remove the filter
googlesitekit_modules_datafromincludes/Core/Modules/Modules.php: https://github.com/google/site-kit-wp/blob/3db19cbeca41145aefc6c6c3383ee9b66e8b4121/includes/Core/Modules/Modules.php#L163-L187 Remove this function ((Note: this is still used but in a different way so should remain.)prepare_info_for_js) fromincludes/Core/Modules/Module.php: https://github.com/google/site-kit-wp/blob/47b723d1416fa48b4e9df73b2970e868e1ccece8/includes/Core/Modules/Module.php#L176 as it's no longer used
Test Coverage
- Aside from the tests removed, mentioned above, no test changes should occur. Existing tests should pass.
Visual Regression Changes
- No changes should occur.
QA Brief
- Notifications and Settings pages, mainly, should continue to function as before. This change removes global code set by PHP, so check the developer/browser console during browsing and ensure no errors/warnings related to the
_googlesitekitLegacyData.modulesvariable are displayed.
Changelog entry
- N/A
A bit of the functional conversions/refactoring here might be a bit tricky for anyone less familiar with these files, so marking this as an 11… but it's a high estimate and covers potentially the broad QA requirements of this issue as well.
@tofumatt IB overall looks good. A few additional thoughts:
- The data that
getModulesDataexposes includes a bit different information than what thegetModulesdatastore selector returns - in addition, a few properties use different names between the two. We should double check for that, it would be good if you could add that to the IB. A good way is to compare the REST endpoint to get modules data with theModule::prepare_info_for_jsmethod, the latter of which powers the legacygetModulesfunction in JS. - I think it would be good to also completely remove the legacy
Module::prepare_info_for_jsmethod (and of course its implementations) as part of this issue, since it'll no longer be useful.
Good call; I've made those updates so this is good for another review 👍🏻
Although @felixarntz previously reviewed this one, in the interest of time I'm leaving it unassigned for someone else to pick up. 🙂
@tofumatt the getDataErrorComponent function also relies on getModulesData function but it looks like we don't use it anymore and can delete it. Could you please update IB to delete the file with the getDataErrorComponent function entirely? Plus, could you, please, clarify what exactly needs to be deleted from the Assets.php file?
Remove 'modules', 'locale', and 'permissions' keys from the array in get_inline_data in Assets.php
@tofumatt we can't remove locale because it is used in the getLocale function: https://github.com/google/site-kit-wp/blob/3db19cbeca41145aefc6c6c3383ee9b66e8b4121/assets/js/util/i18n.js#L379-L389
IB :heavy_check_mark:
QA ✅
- Searched the codebase for
getModulesData, which returned no results - Ran
rg _googlesitekitLegacyData.*modulesandrg _googlesitekitLegacyData | rg moduleusing Ripgrep, which returned no results, so_googlesitekitLegacyData.modulesappears to have gone from the codebase. _googlesitekitLegacyData.moduleswasundefinedon all pages of the Site Kit plug in, and there were no console errors or warnings relating to it.googlesitekit_modules_datafilter has been removed fromModules.phpas described in the IB.- 'modules', and 'permissions' keys have been removed from the array in
get_inline_datainAssets.phpas described in the IB.