-
Notifications
You must be signed in to change notification settings - Fork 107
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
adamsilverstein
wants to merge
10
commits into
WordPress:trunk
Choose a base branch
from
adamsilverstein:add/performance-mark-data
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+385
−5
Draft
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f325567
Add a Perflab_Performance_Marks class
adamsilverstein d9ee3ef
Start adding script performance marks
adamsilverstein cab8e1e
Include correct slug and name in data
adamsilverstein 6bd6396
Fix phpstan errors
adamsilverstein 044d19e
Update plugins/embed-optimizer/hooks.php
adamsilverstein 01296bd
Merge branch 'fix/phpstan' into add/performance-mark-data
adamsilverstein 2315902
Remove unused code
adamsilverstein 193b04c
Attribution for scripts output in wp_head or wp_footer
adamsilverstein a6d4fd9
refine attribution marks
adamsilverstein 3e5b038
Add theme enqueue support, improve attributions
adamsilverstein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
302 changes: 302 additions & 0 deletions
302
plugins/performance-lab/includes/performance-marks/class-perflab-performance-marks.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,302 @@ | ||
<?php | ||
/** | ||
* Server Timing API: Perflab_Performance_Marks class | ||
* | ||
* @package performance-lab | ||
* @since n.e.x.t | ||
*/ | ||
|
||
/** | ||
* Class controlling Dev Tools Performance Marks. | ||
* | ||
* Leverages the Dev Tools extensibility API to add custom performance data. See https://developer.chrome.com/docs/devtools/performance/extension. | ||
* | ||
* @since n.e.x.t | ||
*/ | ||
class Perflab_Performance_Marks { | ||
|
||
/** | ||
* Map of stored metrics that will be added as Dev Tools Performance marks. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @var array<string, array<string, mixed>> | ||
*/ | ||
private $marks = array(); | ||
|
||
/** | ||
* Array of all plugins and their data. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @var array<string, array<string, mixed>> | ||
*/ | ||
private $plugins_data = array(); | ||
|
||
/** | ||
* Initialize the class including plugin data. | ||
* | ||
* @since n.e.x.t | ||
*/ | ||
public function __construct() { | ||
global $wp_scripts; | ||
|
||
if ( ! function_exists( 'get_plugins' ) ) { | ||
require_once ABSPATH . 'wp-admin/includes/plugin.php'; | ||
} | ||
$this->plugins_data = get_plugins(); | ||
} | ||
|
||
/** | ||
* Add a mark to the list of marks. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param string $mark_slug The slug of the mark. | ||
* @param array<string, mixed> $args Arguments for the mark. | ||
*/ | ||
public function add_mark( string $mark_slug, array $args ): void { | ||
$this->marks[ $mark_slug ] = $args; | ||
} | ||
|
||
/** | ||
* Get a mark by its slug. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param string $mark_slug The slug of the mark. | ||
* @return array<string, mixed>|null The mark, or null if not found. | ||
*/ | ||
public function get_mark( string $mark_slug ): ?array { | ||
return $this->marks[ $mark_slug ] ?? null; | ||
} | ||
|
||
/** | ||
* Get all of the marks. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @return array<string, array<string, mixed>> All of the marks. | ||
*/ | ||
public function get_all_marks(): array { | ||
return $this->marks; | ||
} | ||
|
||
/** | ||
* Send the marks to the Dev Tools Performance panel. | ||
* | ||
* Outputs inline JavaScript that uses the Dev Tools Performance API to add marks to the timeline. | ||
* | ||
* This function should be called in the footer after all data has been collected. | ||
* | ||
* @since n.e.x.t | ||
*/ | ||
public function send_marks(): void { | ||
global $wp_scripts; | ||
|
||
// Collect any scripts output directly. | ||
remove_action( 'wp_footer', array( perflab_performance_marks(), 'send_marks' ), 999 ); | ||
$manually_output_scripts = $this->get_manually_output_scripts(); | ||
|
||
// Add the manually output scripts to the marks. | ||
foreach ( $manually_output_scripts as $script ) { | ||
|
||
$this->add_mark( | ||
'script_output::' . $script['slug'], | ||
array( | ||
'path' => $script['path'], | ||
'slug' => $script['slug'], | ||
'name' => $script['name'], | ||
) | ||
); | ||
} | ||
|
||
foreach ( $wp_scripts->done as $handle ) { | ||
$src = $wp_scripts->registered[ $handle ]->src; | ||
if ( false === $src ) { | ||
continue; | ||
} | ||
// Gather the plugin slug, name at relative path. | ||
$plugin_data = $this->get_plugin_data_from_src( $src ); | ||
perflab_performance_marks()->add_mark( | ||
'script_enqueue::' . $handle, | ||
array( | ||
'path' => $plugin_data['path'], | ||
'slug' => $plugin_data['slug'], | ||
'name' => $plugin_data['name'], | ||
) | ||
); | ||
} | ||
if ( empty( $this->marks ) ) { | ||
return; | ||
} | ||
echo ( '<script>' ); | ||
echo 'console.log( "sending marks" );'; | ||
$x = 100; | ||
foreach ( $this->marks as $mark_slug => $mark_args ) { | ||
printf( | ||
'performance.mark( "%s", { detail: %s, startTime: %s } );', | ||
esc_attr( $mark_slug ), | ||
wp_json_encode( $mark_args ), | ||
esc_attr( (string) $x ) | ||
); | ||
++$x; | ||
} | ||
echo( '</script>' ); | ||
} | ||
|
||
/** | ||
* Helper function to get the plugin slug and name when passed a script path. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param string $src The script path. | ||
* @return array<string, string> The plugin slug, name and path. | ||
*/ | ||
private function get_plugin_data_from_src( string $src ): array { | ||
|
||
// Get just the local path for the src (removing the local domain). | ||
$src = str_replace( get_site_url(), '', $src ); | ||
|
||
if ( str_starts_with( $src, '/wp-includes/' ) ) { | ||
return array( | ||
'slug' => 'core', | ||
'name' => 'Core', | ||
'path' => $src, | ||
); | ||
} | ||
|
||
// Extract the slug from $src, eg. "/wp-content/plugins/{slug}/path/to/script.js". | ||
$slugs = explode( '/', $src ); | ||
$slug = $slugs[3]; | ||
|
||
$plugin_data = $this->get_plugin_data_by_slug( $slug ); | ||
|
||
return array( | ||
'slug' => $plugin_data['slug'], | ||
'name' => $plugin_data['name'], | ||
'path' => $src, | ||
); | ||
} | ||
|
||
/** | ||
* Get data for plugin by slug. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param string $slug The plugin slug. | ||
* @return array<string, string> The plugin slug and name. | ||
*/ | ||
private function get_plugin_data_by_slug( string $slug ): array { | ||
if ( '' === $slug ) { | ||
return array( | ||
'slug' => '', | ||
'name' => '', | ||
); | ||
} | ||
foreach ( $this->plugins_data as $plugin_slug => $plugin_data ) { | ||
if ( $slug === $plugin_data['TextDomain'] ) { | ||
return array( | ||
'slug' => $plugin_data['TextDomain'], | ||
'name' => $plugin_data['Name'], | ||
); | ||
} | ||
} | ||
return array( | ||
'slug' => '', | ||
'name' => '', | ||
); | ||
} | ||
|
||
/** | ||
* Get all of the manually output scripts. | ||
* | ||
* Check all plugins hooked to wp_head or wp_footer to see if they are enqueuing scripts. | ||
* Run all hooks using output buffering, then review content for any script handles that are enqueued. | ||
* Use the HTML API to parse for script handle, then add that to the performance marks. | ||
* | ||
* For each script, return the plugin slug and name and the script path. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @return array<int, array<string, bool|string>> Array of script handles with plugin slug and name. | ||
*/ | ||
private function get_manually_output_scripts(): array { | ||
$scripts = array(); | ||
$hooks = array( | ||
'wp_head', | ||
'wp_footer', | ||
); | ||
foreach ( $hooks as $hook ) { | ||
// Get all callbacks hooked on this hook and invoke them one at a time. | ||
$callbacks = $GLOBALS['wp_filter'][ $hook ]; | ||
foreach ( $callbacks as $priority => $sub_callbacks ) { | ||
foreach ( $sub_callbacks as $callback ) { | ||
// Capture the output HTML. | ||
ob_start(); | ||
call_user_func( $callback['function'], array() ); | ||
$html = ob_get_clean(); | ||
// Parse the HTML for any script handles. | ||
if ( empty( $html ) ) { | ||
continue; | ||
} | ||
$processor = new WP_HTML_Tag_Processor( $html ); | ||
while ( $processor->next_tag() ) { | ||
if ( 'SCRIPT' === $processor->get_tag() ) { | ||
$src = $processor->get_attribute( 'src' ); | ||
$plugin_slug = ''; | ||
if ( ! empty( $src ) ) { | ||
if ( is_array( $callback['function'] ) ) { | ||
$class_name = $callback['function'][0]; // Class. | ||
$method_name = $callback['function'][1]; // Method. | ||
try { | ||
$reflection_method = new ReflectionMethod( $class_name, $method_name ); | ||
$file_path = $reflection_method->getFileName(); | ||
$plugin_slug = $this->get_slug_from_path( $file_path ); | ||
} catch ( ReflectionException $e ) { | ||
continue; | ||
} | ||
} else { | ||
$function_name = $callback['function']; | ||
try { | ||
$reflection_function = new ReflectionFunction( $function_name ); | ||
$file_path = $reflection_function->getFileName(); | ||
$plugin_slug = $this->get_slug_from_path( $file_path ); | ||
} catch ( ReflectionException $e ) { | ||
continue; | ||
} | ||
$plugin_data = $this->get_plugin_data_by_slug( $plugin_slug ); | ||
$scripts[] = array( | ||
'path' => $src, | ||
'slug' => empty( $plugin_data['slug'] ) ? 'core' : $plugin_data['slug'], | ||
'name' => empty( $plugin_data['name'] ) ? 'Core' : $plugin_data['name'], | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return $scripts; | ||
} | ||
|
||
/** | ||
* Helper to get the plugin slug from a file path. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param string|false $file_path The file path. | ||
* @return string The plugin slug. | ||
*/ | ||
private function get_slug_from_path( $file_path ): string { | ||
if ( false === $file_path ) { | ||
return ''; | ||
} | ||
$pattern = '#/(?:plugins|themes)/([^/]+)/#'; // Match anything after '/plugins/' or '/themes/' up to the next '/'. | ||
if ( false !== preg_match( $pattern, $file_path, $matches ) ) { | ||
return $matches[1]; | ||
} | ||
return ''; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
plugins/performance-lab/includes/performance-marks/hooks.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
/** | ||
* Performance Marks API hooks file | ||
* | ||
* @package performance-lab | ||
* @since n.e.x.t | ||
*/ | ||
|
||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; // Exit if accessed directly. | ||
} | ||
|
||
/** | ||
* Add performance mark output to the footer. | ||
* | ||
* @since n.e.x.t | ||
*/ | ||
add_action( 'wp_footer', array( perflab_performance_marks(), 'send_marks' ), 999 ); |
40 changes: 40 additions & 0 deletions
40
plugins/performance-lab/includes/performance-marks/load.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
/** | ||
* Performance Marks API integration file | ||
* | ||
* @package performance-lab | ||
* @since n.e.x.t | ||
*/ | ||
|
||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; // Exit if accessed directly. | ||
} | ||
|
||
require_once __DIR__ . '/hooks.php'; | ||
|
||
/** | ||
* Get the instance of the Perflab_Performance_Marks class. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @return Perflab_Performance_Marks The instance of the class. | ||
*/ | ||
function perflab_performance_marks(): Perflab_Performance_Marks { | ||
static $instance; | ||
|
||
if ( ! $instance ) { | ||
$instance = new Perflab_Performance_Marks(); | ||
} | ||
|
||
return $instance; | ||
} | ||
|
||
/** | ||
* Initialize the performance marks API. | ||
* | ||
* @since n.e.x.t | ||
*/ | ||
function perflab_performance_marks_init(): void { | ||
perflab_performance_marks(); | ||
} | ||
add_action( 'wp_loaded', 'perflab_performance_marks_init' ); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.phpHere'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 theall
action.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.