Skip to content

Conversation

@kidunot89
Copy link
Collaborator

@kidunot89 kidunot89 commented Jul 8, 2024

What does this implement/fix? Explain your changes.

  • Modifies the enqueuedScripts, and enqueuedStyles field on the ContentNode to better simulate the rendering of a content node a WordPress theme, as well as sorting the results in the correct order for rendering.
    The primary purpose of this change is to enable and simplify the process of render an editor page in a headless application on a 1:1 scale with the WordPress theme. CSS/JS scripts and all.
  • ScriptLoadingGroupEnum enum type implemented and assigned as the return type for the EnqueuedScript type's new location field.
  • Adds the group field to the EnqueuedAsset type.
  • Adds the location field to the EnqueuedScript type.
  • All self::IS_NULL, self::IS_FALSY, self::NOT_NULL, and self::NOT_FALSY calls in tests replaced with static::IS_NULL, static::IS_FALSY, static::NOT_NULL and static::NOT_FALSY to ensure the youngest definition is always returned. Moved to chore: WPGraphQLTestCase constant usage updated for extendability #3191

Does this close any currently open issues?

Any relevant logs, error output, GraphiQL screenshots, etc?

Any other comments?

Where has this been tested?

Operating System:

WordPress Version:

@kidunot89 kidunot89 requested review from jasonbahl and justlevine July 8, 2024 21:55
'description' => __( 'The loading strategy to use on the script tag', 'wp-graphql' ),
'resolve' => static function ( \_WP_Dependency $script ) {
if ( ! isset( $script->extra['group'] ) ) {
return 0;

Choose a reason for hiding this comment

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

Avoid too many return statements within this method.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this be on the interface?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, defaulting to HEADER only seems true for scripts, but per the above link, a false value is NO_GROUP. Not sure if that's a helpful demarcation for non-script assets, but it does respect WP as source-of-truth

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Gotcha, I've copied the group field to the EnqueuedAsset type with a return type to Integer. Then renamed the group field to location in the EnqueuedScript file.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure I follow the change. What is the difference between group and location?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

group is the group level that can be applied to all _WP_Dependencies, and location is the enum representation of this value exclusive to scripts being either HEADER for 0 and FOOTER for 1.

*
* @return array
*/
public static function get_enqueued_scripts_handles( $queue ) {

Choose a reason for hiding this comment

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

Similar blocks of code found in 2 locations. Consider refactoring.

return array_values( array_unique( $handles ) );
}

public static function get_enqueued_styles_handles( $queue ) {

Choose a reason for hiding this comment

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

Similar blocks of code found in 2 locations. Consider refactoring.

*
* @return array
*/
public static function get_enqueued_scripts_handles( $queue ) {

Choose a reason for hiding this comment

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

Function get_enqueued_scripts_handles has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.

'resolve' => static function ( $source, $args, $context, $info ) {
// Simulate WP template rendering
ob_start();
wp_head();
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will be incompatible with eventual FSE support, and break several existing WPGraphQL extensions (e.g. seo). I haven't reviewed the PR for real, so without knowing the full goal I gotta hope there's a different way to accomplish this.

Copy link
Collaborator Author

@kidunot89 kidunot89 Jul 11, 2024

Choose a reason for hiding this comment

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

We'll maybe not the FSE but plain-old Gutenburg and editor pages, yes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also it runs on the resolvers for both enqueuedStyles and enqueuedScripts, which means these do_action() and apply_filters() are getting triggered multiple times too... What's the performance hit with this change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nothing that I've noticed, but I'm just using WC and some WC premium plugins, and while WC emits a lot of localize scripts for their pages I've haven't seen any slow downs in the query speed due to this change.

@justlevine justlevine added the compat: breaking change This is a breaking change to existing functionality label Jul 11, 2024
'description' => __( 'The loading group to which this asset belongs.', 'wp-graphql' ),
'resolve' => static function ( $asset ) {
if ( ! isset( $asset->extra['group'] ) ) {
return 0;

Choose a reason for hiding this comment

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

Avoid too many return statements within this method.

return 0;
}

return absint( $asset->extra['group'] );

Choose a reason for hiding this comment

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

Avoid too many return statements within this method.

*
* @return string[]
*/
public static function get_enqueued_asset_handles( array $queue, $wp_assets ) {

Choose a reason for hiding this comment

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

Function get_enqueued_asset_handles has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.

@coveralls
Copy link

coveralls commented Jul 11, 2024

Coverage Status

coverage: 84.205% (+0.03%) from 84.172%
when pulling f6adc45 on kidunot89:feat/enqueued-asset-changes
into 66aaf9a on wp-graphql:develop.

@kidunot89 kidunot89 force-pushed the feat/enqueued-asset-changes branch from 1f70926 to af5bbc5 Compare July 25, 2024 23:11
*
* @return string[]
*/
public static function get_enqueued_asset_handles( array $queue, $wp_assets ) {

Choose a reason for hiding this comment

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

Function get_enqueued_asset_handles has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.

@kidunot89 kidunot89 force-pushed the feat/enqueued-asset-changes branch from af5bbc5 to 6188d78 Compare August 7, 2024 18:30
*
* @return string[]
*/
public static function flatten_enqueued_assets_list( array $queue, $wp_assets ) {

Choose a reason for hiding this comment

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

Function flatten_enqueued_assets_list has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.

@kidunot89 kidunot89 force-pushed the feat/enqueued-asset-changes branch from 3872ec5 to d7b71a5 Compare August 8, 2024 18:47
@kidunot89 kidunot89 requested a review from justlevine August 9, 2024 21:35
@qlty-cloud-legacy
Copy link

Code Climate has analyzed commit f6adc45 and detected 8 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 8

View more on Code Climate.

justlevine added a commit to justlevine/wp-graphql that referenced this pull request Aug 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compat: breaking change This is a breaking change to existing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants