site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Remove legacy `getModulesData` JS function and `_googlesitekitLegacyData.modules` global

Open felixarntz opened this issue 5 years ago • 8 comments

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 getModulesData should be refactored to use the getModules or getModule datastore selector instead, using React hooks.
  • The getModulesData function should be entirely removed.
  • The _googlesitekitLegacyData.modules global 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 getModulesData to getModules, note that the data returned from getModules is 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 from settings is needed by components using getModulesData it will need refactoring)
    • All other keys are the same or are only available in the new API

SettingsModules component

  • Remove the useEffect in https://github.com/google/site-kit-wp/blob/3db19cbeca41145aefc6c6c3383ee9b66e8b4121/assets/js/components/settings/SettingsModules.js#L38 and change modulesData to const moduleData = useSelect( ( select ) => select( CORE_MODULES ).getModules() ); (renaming the modulesData to modules as well).

Some component require changing to functional components to use hooks, but this is straightforward:

  • Convert assets/js/components/legacy-notifications/dashboard-setup-alerts.js to 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 the switch statement.
  • 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 componentDidMount logic to a useMount hook. Then, use the getModules() selector to replace the current usage of getModulesData().

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 pass modules as an argument to mapScopesToModuleNames in 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 in get_inline_data in Assets.php (see: https://github.com/google/site-kit-wp/blob/3db19cbeca41145aefc6c6c3383ee9b66e8b4121/includes/Core/Assets/Assets.php#L811-L827)
  • Remove the filter googlesitekit_modules_data from includes/Core/Modules/Modules.php: https://github.com/google/site-kit-wp/blob/3db19cbeca41145aefc6c6c3383ee9b66e8b4121/includes/Core/Modules/Modules.php#L163-L187
  • Remove this function (prepare_info_for_js ) from includes/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 (Note: this is still used but in a different way so should remain.)

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.modules variable are displayed.

Changelog entry

  • N/A

felixarntz avatar Jul 26 '21 22:07 felixarntz

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 avatar Jul 28 '21 23:07 tofumatt

@tofumatt IB overall looks good. A few additional thoughts:

  • The data that getModulesData exposes includes a bit different information than what the getModules datastore 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 the Module::prepare_info_for_js method, the latter of which powers the legacy getModules function in JS.
  • I think it would be good to also completely remove the legacy Module::prepare_info_for_js method (and of course its implementations) as part of this issue, since it'll no longer be useful.

felixarntz avatar Jul 29 '21 17:07 felixarntz

Good call; I've made those updates so this is good for another review 👍🏻

tofumatt avatar Aug 02 '21 16:08 tofumatt

Although @felixarntz previously reviewed this one, in the interest of time I'm leaving it unassigned for someone else to pick up. 🙂

tofumatt avatar Aug 02 '21 16:08 tofumatt

@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?

eugene-manuilov avatar Aug 03 '21 13:08 eugene-manuilov

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

eugene-manuilov avatar Aug 04 '21 12:08 eugene-manuilov

IB :heavy_check_mark:

eugene-manuilov avatar Aug 04 '21 15:08 eugene-manuilov

QA ✅

  • Searched the codebase for getModulesData, which returned no results
  • Ran rg _googlesitekitLegacyData.*modules and rg _googlesitekitLegacyData | rg module using Ripgrep, which returned no results, so _googlesitekitLegacyData.modules appears to have gone from the codebase.
  • _googlesitekitLegacyData.modules was undefined on all pages of the Site Kit plug in, and there were no console errors or warnings relating to it.
  • googlesitekit_modules_data filter has been removed from Modules.php as described in the IB.
  • 'modules', and 'permissions' keys have been removed from the array in get_inline_data in Assets.php as described in the IB.

johnPhillips avatar Sep 01 '21 16:09 johnPhillips