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

Try: add script attribution data #1629

Draft
wants to merge 10 commits into
base: trunk
Choose a base branch
from

Conversation

adamsilverstein
Copy link
Member

No description provided.

@adamsilverstein adamsilverstein changed the title Try: add script annotations Try: add script attribution data Oct 30, 2024
continue;
}
// Gather the plugin slug, name at relative path.
$plugin_data = $this->get_plugin_data_from_src( $src );
Copy link
Member

Choose a reason for hiding this comment

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

The way that the AMP plugin and Origination project determine which plugin enqueued a script is to wrap each of the action callbacks so that we can capture a list of the enqueued handles before and after an action callback runs, and then any new script handles added can then be attributed to the theme/plugin which is identified as the source for the given function/method via the Reflection API.

In the AMP plugin this is the AMP_Validation_Callback_Wrapper invocable class https://github.com/ampproject/amp-wp/blob/develop/includes/validation/class-amp-validation-callback-wrapper.php

Here's how this is used: https://github.com/ampproject/amp-wp/blob/60a655cdad9e3431b47b2dcc3b2e5ddb86ac5c5e/includes/validation/class-amp-validation-manager.php#L1429-L1490

That wrap_hook_callbacks method runs at the all action.

Copy link
Member Author

Choose a reason for hiding this comment

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

we can capture a list of the enqueued handles

does this also capture manually output 3p scripts (eg. analytics) which are arguably the hardest to attribute?

My approach was to wait and run late on wp_footer (since I'm doing my output there anyway), then parse thru the global script object's "done" property which should include any enqueued scripts.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it adds buffering individually to the output of all actions: https://github.com/ampproject/amp-wp/blob/60a655cdad9e3431b47b2dcc3b2e5ddb86ac5c5e/includes/validation/class-amp-validation-callback-wrapper.php#L110

And then it wraps the output with HTML comments to indicate the origin of the markup:
https://github.com/ampproject/amp-wp/blob/60a655cdad9e3431b47b2dcc3b2e5ddb86ac5c5e/includes/validation/class-amp-validation-manager.php#L1708

The injection of the HTML comments to later discover the source of markup during validation is an additional redirection which probably is not relevant. For your use case, it's probably better to use the HTML Tag Processor in the output buffet callback to add data attributes to the SCRIPT tags to indicate the origin.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I was referring to scripts being printed manually and not enqueued. But yes, 3P enqueued scripts are also attributes to the relevant plugins/themes. By computing a diff of the the registered/enqueued scripts before and after each hook callback invocation we can determine which callback introduced a script, and then we can look up the origin of the callback via the Reflection API to find out the plugin or theme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants