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

Use React-based component for Stylesheets box on Validated URL screen #6442

Open
wants to merge 58 commits into
base: develop
Choose a base branch
from

Conversation

delawski
Copy link
Collaborator

@delawski delawski commented Jul 2, 2021

Summary

This PR replaces the server-side rendered "Stylesheet" meta box on the Validated URL screen with a React-based component fed via the REST API.

Tasks list (subject to change):

  • Create a new REST endpoint, GET /wp-json/amp/v1/validated-urls/:id/, that returns the validation data stored in a amp_validated_url post type. See REST endpoints for AMP validation errors and validated URLs #5552. The response data structure consists of the following fields:
    • id
    • url
    • date
    • author
    • stylesheets
    • environment
  • Ensure the REST endpoint is secure so that only authorized users are receiving the validation data.
  • Introduce an abstraction layer for the amp_validated_url custom post type in the ValidatedUrlDataProvider class.
  • Update the REST endpoint schema.
  • Implement a new React component that will look similar to the existing "Stylesheets" meta box:
    • Add stylesheets summary table
    • Add stylesheets details table
    • Add CSS code diff component
    • Add Sources component
  • Remove the legacy "Stylesheets" meta box along with code dependencies.
  • Preload validated URL, themes, and plugins data.
  • Incorporate any applicable usability improvements from Update to validator with usability improvements #5191.

Fixes #6441

Checklist

  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@delawski delawski added Developer Tools REST API Issues, features related to REST API WS:UX Work stream for UX/Front-end labels Jul 2, 2021
@delawski delawski force-pushed the add/6441-react-stylesheets-meta-box branch from 8a7b20a to a7b5c64 Compare July 2, 2021 16:31
* @param WP_REST_Request $request Full details about the request.
* @return true|WP_Error True if the request has permission; WP_Error object otherwise.
*/
public function get_item_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was thinking if we could reuse and rename the create_item_permissions_check() method instead of adding a new checker just for the READABLE method. Right now, they are the same.

*/
public function get_validated_url( $request ) {
$post_id = (int) $request['id'];
$validated_url = new ValidatedUrlDataProvider( $post_id );
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I wasn't sure if it's the best practice to instantiate a Provider inside a method like here. Other providers are passed to the controller's __construtor. Or else, maybe the ValidatedUrlDataProvider shouldn't be a Provider in the first place?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Any use of new for something that is not a newable object (i.e. value objects, factories, ...) is not best practice, as we're switching the code over to make use of an injector across the board.

As for your question about the nature of the provider in general, I think the modeling seems to be off.

Best practice would be to have the provider be injected via the constructor, and then have a method that accepts the post ID for which it should provide.
So, following from the name, I'd expect a ValidatedUrlDataProvider to provide a ValidatedUrlData object:

$validated_url_data = $this->validated_url_data_provider->for( $post_id );

In your current design the provider is the data it provides, as it receives the post ID already in its constructor. For multiple post IDs, you'd then need not multiple data objects, but multiple provider objects.

Hope this helps.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Your feedback is very helpful, thanks @schlessera!

I've addressed the modelling problem in 5944bbc. According to your suggestion, there's a new ValidatedUrlData object that's returned by the provider.

The ValidatedUrlData object is a wrapper for the underlying WP_Post object. I'm wondering what would be the best model that would cover a potential data layer change from WP_Post to something else in the future.

Comment on lines 331 to 337
$data = [
'id' => $validated_url->get_id(),
'url' => $validated_url->get_url(),
'date' => $validated_url->get_date(),
'author' => $validated_url->get_author(),
'stylesheets' => $validated_url->get_stylesheets(),
];
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's intentional that there's no single implicit $validated_url->get_data() call. The reasoning is that the REST controller is the place where the response data should be defined explicitly.

@delawski delawski force-pushed the add/6441-react-stylesheets-meta-box branch 3 times, most recently from 20c73be to 0d765cd Compare July 7, 2021 09:28
@delawski delawski marked this pull request as ready for review July 29, 2021 16:51
@codecov
Copy link

codecov bot commented Jul 29, 2021

Codecov Report

Merging #6442 (b9de4d5) into develop (24923a4) will increase coverage by 0.51%.
The diff coverage is 56.80%.

❗ Current head b9de4d5 differs from pull request most recent head 26525f8. Consider uploading reports for the commit 26525f8 to get more accurate results
Impacted file tree graph

@@              Coverage Diff              @@
##             develop    #6442      +/-   ##
=============================================
+ Coverage      75.52%   76.04%   +0.51%     
- Complexity      5987     5999      +12     
=============================================
  Files            190      266      +76     
  Lines          18037    19242    +1205     
=============================================
+ Hits           13623    14633    +1010     
- Misses          4414     4609     +195     
Flag Coverage Δ
javascript 71.99% <58.88%> (?)
php 76.30% <53.62%> (+0.78%) ⬆️
unit 76.30% <53.62%> (+0.78%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...validation/components/amp-document-status/index.js 100.00% <ø> (ø)
...nents/amp-validation-status/status-notification.js 100.00% <ø> (ø)
src/AmpWpPlugin.php 100.00% <ø> (ø)
src/Validation/URLValidationRESTController.php 86.13% <0.00%> (-6.42%) ⬇️
...s/src/components/plugins-context-provider/index.js 7.40% <7.40%> (ø)
...ts/src/components/themes-context-provider/index.js 7.40% <7.40%> (ø)
assets/src/components/source-details/title.js 11.53% <11.53%> (ø)
assets/src/components/source-details/value.js 11.53% <11.53%> (ø)
...components/validated-url-context-provider/index.js 14.28% <14.28%> (ø)
src/Validation/ValidatedUrlData.php 18.89% <18.89%> (ø)
... and 103 more

@github-actions
Copy link
Contributor

github-actions bot commented Jul 29, 2021

Plugin builds for 26525f8 are ready 🛎️!

@delawski delawski force-pushed the add/6441-react-stylesheets-meta-box branch from 84fcf86 to 0403e29 Compare August 17, 2021 12:24
@delawski
Copy link
Collaborator Author

The PR is now rebased onto develop and the conflicts have been resolved. Also, I've removed some bugs related to the Sources component.

Copy link
Collaborator

@schlessera schlessera left a comment

Choose a reason for hiding this comment

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

Some minor things I just noticed...

@@ -15,6 +15,7 @@
'admin.paired_browsing' => \AmpProject\AmpWP\Admin\PairedBrowsing::class,
'admin.plugin_row_meta' => \AmpProject\AmpWP\Admin\PluginRowMeta::class,
'admin.polyfills' => \AmpProject\AmpWP\Admin\Polyfills::class,
'admin.rest_preloader' => \AmpProject\AmpWP\Admin\RESTPreloader::class,
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is missing the second service that was added:

'validated_url_rest_controller' => \AmpProject\AmpWP\Validation\ValidatedUrlRESTController::class,

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch. I've added it back in 26525f8.

src/Validation/ValidatedUrlData.php Outdated Show resolved Hide resolved
src/Validation/ValidatedUrlData.php Outdated Show resolved Hide resolved
@westonruter westonruter added this to the v2.2 milestone Sep 10, 2021
@westonruter westonruter modified the milestones: v2.2, v2.3 Sep 28, 2021
@westonruter westonruter modified the milestones: v2.3, v2.4 Dec 23, 2021
@westonruter westonruter removed this from the v2.4 milestone Apr 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Developer Tools REST API Issues, features related to REST API WS:UX Work stream for UX/Front-end
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Revamp Stylesheets Box on Validated URL Screen
3 participants