Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create frontend providers and widget factory #22008

Open
wants to merge 9 commits into
base: trunk
Choose a base branch
from

Conversation

igorschoester
Copy link
Member

@igorschoester igorschoester commented Jan 30, 2025

Context

Summary

This PR can be summarized in the following changelog entry:

  • Refactors the dashboard widgets to get their behavior via a widget factory.
  • Adds the top pages widget.

Relevant technical choices:

  • Remove inner knowledge of widgets from the dashboard into a widget factory that knows how to supply the widgets with what they need
  • Add data provider to control data knowledge.
  • Add remote data provider to control how to fetch data.
  • I added some comments on my code as food for thought

Test instructions

Test instructions for the acceptance test before the PR gets merged

This PR can be acceptance tested by following these steps:

  • Enable the site kit feature flag
  • Ensure you get Site kit data and have it connected
  • Go to the dashboard
  • Verify you see the widget for "Top 5 most popular content"
  • Verify you get the loading (skeleton loader)
  • Verify you get the no data alert when there is no data (no clue how to do that without changing code)
  • Verify you get the error alert when an error occurs (block in inspector?)

Regression for scores

The score fetching is refactored to go through the data providers.

  • Ensure the score fetching is still functioning as before
  • Turn of the seo analysis feature
  • Verify the seo scores widget is not there
  • Turn off the readability analysis feature
  • Verify the readability widget is not there
  • Turn both features on again
  • Disable indexables (using the filter: add_filter( 'Yoast\WP\SEO\should_index_indexables', '__return_false' );)
  • Verify both score widgets are not there

Relevant test scenarios

  • Changes should be tested with the browser console open
  • Changes should be tested on different posts/pages/taxonomies/custom post types/custom taxonomies
  • Changes should be tested on different editors (Default Block/Gutenberg/Classic/Elementor/other)
  • Changes should be tested on different browsers
  • Changes should be tested on multisite

Test instructions for QA when the code is in the RC

  • QA should use the same steps as above.

QA can test this PR by following these steps:

Impact check

This PR affects the following parts of the plugin, which may require extra testing:

  • regression for score widgets

UI changes

  • This PR changes the UI in the plugin. I have added the 'UI change' label to this PR.

Other environments

  • This PR also affects Shopify. I have added a changelog entry starting with [shopify-seo], added test instructions for Shopify and attached the Shopify label to this PR.

Documentation

  • I have written documentation for this change. For example, comments in the Relevant technical choices, comments in the code, documentation on Confluence / shared Google Drive / Yoast developer portal, or other.

Quality assurance

  • I have tested this code to the best of my abilities.
  • During testing, I had activated all plugins that Yoast SEO provides integrations for.
  • I have added unit tests to verify the code works as intended.
  • If any part of the code is behind a feature flag, my test instructions also cover cases where the feature flag is switched off.
  • I have written this PR in accordance with my team's definition of done.
  • I have checked that the base branch is correctly set.

Innovation

  • No innovation project is applicable for this PR.
  • This PR falls under an innovation project. I have attached the innovation label.
  • I have added my hours to the WBSO document.

Fixes https://github.com/Yoast/reserved-tasks/issues/367

@igorschoester igorschoester added the changelog: non-user-facing Needs to be included in the 'Non-userfacing' category in the changelog label Jan 30, 2025
Comment on lines +26 to +35
/**
* @returns {WidgetTypeInfo[]}
*/
static get widgetTypes() {
return [
{ type: "seoScores" },
{ type: "readabilityScores" },
{ type: "topPages" },
];
}
Copy link
Member Author

@igorschoester igorschoester Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe remove? As it is not needed yet, if ever

@@ -75,11 +83,10 @@ domReady( () => {
element={
<SidebarLayout>
<Dashboard
contentTypes={ contentTypes }
widgetFactory={ widgetFactory }
initialWidgets={ [ "seoScores", "readabilityScores", "topPages" ] }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need feature flag check before adding topPages

const removeWidget = useCallback( ( type ) => {
setWidgets( ( currentWidgets ) => currentWidgets.filter( ( widget ) => widget.type !== type ) );
}, [] );

return (
<>
<PageTitle userName={ userName } features={ features } links={ links } />
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a widget too?

* @returns {JSX.Element|null} The widget or null.
*/
// eslint-disable-next-line no-unused-vars
createWidget( widget, onRemove ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onRemove is not yet used. Should be under the widget menu eventually, right?

Probably nice to have some RemoveWidget base that handles this in the future?

createWidget( widget, onRemove ) {
switch ( widget.type ) {
case "seoScores":
if ( ! this.#dataProvider.hasFeature( "indexables" ) && this.#dataProvider.hasFeature( "seoAnalysis" ) ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this move into the Widget? And is it worth an Widget layer if more need this?

@igorschoester igorschoester force-pushed the 420-create-frontend-data-provider-and-wp-implementation branch 3 times, most recently from 5f59d8d to d53e0ea Compare February 3, 2025 09:42
Comment on lines +61 to +82
/**
* @param {ContentType?} [contentType] The selected content type.
* @param {Term?} [term] The selected term.
* @returns {{contentType: string, taxonomy?: string, term?: string}} The score query parameters.
*/
const getScoreQueryParams = ( contentType, term ) => { // eslint-disable-line complexity
const params = {
contentType: contentType?.name,
};
if ( contentType?.taxonomy?.name && term?.name ) {
params.taxonomy = contentType.taxonomy.name;
params.term = term.name;
}

return params;
};

/**
* @param {?{scores: Score[]}} [data] The data.
* @returns {?Score[]} scores The scores.
*/
const prepareScoreData = ( data ) => data?.scores;
Copy link
Member Author

@igorschoester igorschoester Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These might be moved to the dataProvider or props at a later stage, to actually make it reusable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed and moved into the top-pages-widget

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a friendly hook for more data/error/isPending widgets. However, I'm not sure how caching/debouncing should be incorperated yet.

@Yoast Yoast deleted a comment from coveralls Feb 3, 2025
@igorschoester igorschoester force-pushed the 420-create-frontend-data-provider-and-wp-implementation branch 2 times, most recently from a5b0191 to 7d5224e Compare February 3, 2025 11:20
Initial setup for architecture change
* move data into data provider
* use widget factory to create score widgets
...for the score retrieval
* use base Widget
* use data provider to get the endpoint
* use remote data provider to fetch the scores
* remove endpoint and headers
...to use the data provider.
This fixes a bug where the admin.php would be hardcoded instead of retrieved from WP.
Bonus: a little safeguard against mismatching translation tags.
Add the widget only when site kit is enabled, active and connected.
Though the last part is not yet available, so commented that out and replaced with true for now.
@igorschoester igorschoester force-pushed the 420-create-frontend-data-provider-and-wp-implementation branch 2 times, most recently from dace88b to 3e63593 Compare February 3, 2025 14:23
@igorschoester igorschoester marked this pull request as ready for review February 3, 2025 14:31
@igorschoester igorschoester force-pushed the 420-create-frontend-data-provider-and-wp-implementation branch from 3e63593 to aa075c5 Compare February 3, 2025 14:35
Comment on lines +83 to +85
get( window, "wpseoScriptData.dashboard.siteKitConfiguration.feature_enabled", false ) &&
get( window, "wpseoScriptData.dashboard.siteKitConfiguration.isActive", false ) &&
// get( window, "wpseoScriptData.dashboard.siteKitConfiguration.isConnected", false )
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs adaptation after: d7065dd is merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: non-user-facing Needs to be included in the 'Non-userfacing' category in the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant