From 9c0888f5bb2a91927ea067c68047e618a7bfee7e Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Mon, 27 May 2024 12:05:03 -0400 Subject: [PATCH 01/11] save --- classes/Collector_Assets.php | 15 ++++++++++++++- data/assets.php | 5 +++++ dispatchers/Html.php | 4 ++++ output/html/assets.php | 1 + 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/classes/Collector_Assets.php b/classes/Collector_Assets.php index 105d01432..75cb0656f 100644 --- a/classes/Collector_Assets.php +++ b/classes/Collector_Assets.php @@ -106,6 +106,10 @@ public function process() { 'footer', ); + if ( defined( 'QM_SHOW_ALL_ASSETS' ) && QM_SHOW_ALL_ASSETS ) { + $positions[] = 'registered'; + } + $this->data->counts = array( 'missing' => 0, 'broken' => 0, @@ -125,6 +129,8 @@ public function process() { $broken = array_values( array_diff( $raw->queue, $raw->done ) ); $missing = array_values( array_diff( $raw->queue, array_keys( $raw->registered ) ) ); + $this->data->registered = array_keys( $raw->registered ); + // A broken asset is one which has been deregistered without also being dequeued if ( ! empty( $broken ) ) { foreach ( $broken as $key => $handle ) { @@ -159,6 +165,7 @@ public function process() { $all_dependents = array(); $missing_dependencies = array(); + $processed = array(); foreach ( $positions as $position ) { if ( empty( $this->data->{$position} ) ) { @@ -174,6 +181,10 @@ public function process() { continue; } + if ( 'registered' === $position && in_array( $handle, $processed ) ) { + continue; + } + $all_dependencies = array_merge( $all_dependencies, $dependency->deps ); $dependents = $this->get_dependents( $dependency, $raw ); $all_dependents = array_merge( $all_dependents, $dependents ); @@ -186,7 +197,7 @@ public function process() { $ver = $dependency->ver; } - $warning = ! in_array( $handle, $raw->done, true ); + $warning = ! in_array( $handle, $raw->done, true ) && 'registered' !== $position; if ( $source instanceof WP_Error ) { $display = $source->get_error_message(); @@ -217,6 +228,8 @@ public function process() { $this->data->counts[ $position ]++; $this->data->counts['total']++; + + $processed[] = $handle; } } diff --git a/data/assets.php b/data/assets.php index 5cfa9b340..3e2b92b96 100644 --- a/data/assets.php +++ b/data/assets.php @@ -75,4 +75,9 @@ class QM_Data_Assets extends QM_Data { * @var string */ public $port; + + /** + * @var array + */ + public $registered; } diff --git a/dispatchers/Html.php b/dispatchers/Html.php index 17a848a7e..bbb794324 100644 --- a/dispatchers/Html.php +++ b/dispatchers/Html.php @@ -643,6 +643,10 @@ protected function after_output() { 'label' => __( 'Don\'t specify jQuery as a dependency of Query Monitor. If jQuery isn\'t enqueued then Query Monitor will still operate, but with some reduced functionality.', 'query-monitor' ), 'default' => false, ), + 'QM_SHOW_ALL_ASSETS' => array( + 'label' => __( 'In the Scripts and Styles panels, show every registered asset, in addition to enqueued assets.', 'query-monitor' ), + 'default' => false, + ), 'QM_SHOW_ALL_HOOKS' => array( 'label' => __( 'In the Hooks & Actions panel, show every hook that has an action or filter attached (instead of every action hook that fired during the request).', 'query-monitor' ), 'default' => false, diff --git a/output/html/assets.php b/output/html/assets.php index 499f8c478..6cac47fe6 100644 --- a/output/html/assets.php +++ b/output/html/assets.php @@ -53,6 +53,7 @@ public function output() { 'modules' => __( 'Module', 'query-monitor' ), 'header' => __( 'Header', 'query-monitor' ), 'footer' => __( 'Footer', 'query-monitor' ), + 'registered' => __( 'Registered', 'query-monitor' ), ); $type = $this->collector->get_dependency_type(); From d46ef3f3046d9b692c24d58b400728d6dd4e41a0 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Sat, 27 Jul 2024 00:10:12 -0400 Subject: [PATCH 02/11] save --- classes/Collector_Assets.php | 14 ++++--- output/html/assets.php | 80 +++++++++++++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/classes/Collector_Assets.php b/classes/Collector_Assets.php index 75cb0656f..a710f4044 100644 --- a/classes/Collector_Assets.php +++ b/classes/Collector_Assets.php @@ -106,10 +106,6 @@ public function process() { 'footer', ); - if ( defined( 'QM_SHOW_ALL_ASSETS' ) && QM_SHOW_ALL_ASSETS ) { - $positions[] = 'registered'; - } - $this->data->counts = array( 'missing' => 0, 'broken' => 0, @@ -118,6 +114,11 @@ public function process() { 'total' => 0, ); + if ( defined( 'QM_SHOW_ALL_ASSETS' ) && constant( 'QM_SHOW_ALL_ASSETS' ) ) { + $positions[] = 'registered'; + $this->data->counts['registered'] = 0; + } + foreach ( array( 'header', 'footer' ) as $position ) { if ( empty( $this->data->{$position} ) ) { $this->data->{$position} = array(); @@ -227,7 +228,10 @@ public function process() { ); $this->data->counts[ $position ]++; - $this->data->counts['total']++; + + if ( 'registered' !== $position ) { + $this->data->counts['total']++; + } $processed[] = $handle; } diff --git a/output/html/assets.php b/output/html/assets.php index 6cac47fe6..e94567ef5 100644 --- a/output/html/assets.php +++ b/output/html/assets.php @@ -53,7 +53,6 @@ public function output() { 'modules' => __( 'Module', 'query-monitor' ), 'header' => __( 'Header', 'query-monitor' ), 'footer' => __( 'Footer', 'query-monitor' ), - 'registered' => __( 'Registered', 'query-monitor' ), ); $type = $this->collector->get_dependency_type(); @@ -113,6 +112,70 @@ public function output() { echo ''; $this->after_tabular_output(); + + if ( ! defined( 'QM_SHOW_ALL_ASSETS' ) || empty( constant( 'QM_SHOW_ALL_ASSETS' ) ) ) { + return; + } + + $this->before_tabular_output( $this->collector->id() . '-registered', __( 'Registered', 'query-monitor' ) ); + + /* translators: %s: "styles" or "scripts" */ + $caption_format = esc_html__( 'Missing, broken, and enqueued %s are not included.', 'query-monitor' ); + + printf( + '

%2$s

', + esc_attr( $this->collector->id() . '-registered' ), + sprintf( $caption_format, strtolower( $type_label['plural'] ) ) + ); + + echo ''; + echo ''; + echo '' . esc_html__( 'Position', 'query-monitor' ) . ''; + echo '' . esc_html__( 'Handle', 'query-monitor' ) . ''; + echo ''; + $args = array( + 'prepend' => array( + 'local' => $data->full_host, + ), + ); + echo $this->build_filter( $type . '-host', $hosts, __( 'Host', 'query-monitor' ), $args ); // WPCS: XSS ok. + echo ''; + echo '' . esc_html__( 'Source', 'query-monitor' ) . ''; + echo ''; + echo $this->build_filter( $type . '-dependencies', $data->dependencies, __( 'Dependencies', 'query-monitor' ) ); // WPCS: XSS ok. + echo ''; + echo ''; + echo $this->build_filter( $type . '-dependents', $data->dependents, __( 'Dependents', 'query-monitor' ) ); // WPCS: XSS ok. + echo ''; + echo '' . esc_html__( 'Version', 'query-monitor' ) . ''; + echo ''; + echo ''; + + echo ''; + + if ( ! empty( $data->assets['registered'] ) ) { + foreach ( $data->assets['registered'] as $handle => $asset ) { + $this->dependency_row( $handle, $asset, '' ); + } + } + + echo ''; + + echo ''; + + echo ''; + printf( + '%1$s', + sprintf( + esc_html( $type_label['registered'] ), + '' . esc_html( number_format_i18n( $data->counts['registered'] ) ) . '' + ) + ); + echo ''; + echo ''; + + echo ''; + echo ''; } /** @@ -283,6 +346,21 @@ public function admin_menu( array $menu ) { $id = $this->collector->id(); $menu[ $id ] = $this->menu( $args ); + if ( ! defined( 'QM_SHOW_ALL_ASSETS' ) || empty( constant( 'QM_SHOW_ALL_ASSETS' ) ) ) { + return $menu; + } + + $label = sprintf( + _x( 'Registered (%s)', 'Registered scripts', 'query-monitor' ), + number_format_i18n( $data->counts['registered'] ) + ); + + $menu[ $id ]['children'] = array( array( + 'title' => esc_html( $label ), + 'id' => esc_attr( "query-monitor-{$this->collector->id}-registered" ), + 'href' => esc_attr( '#' . $this->collector->id() . '-registered' ), + ) ); + return $menu; } From 18971439cbcae40c97c4b637394d342a18b74905 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Sat, 27 Jul 2024 00:14:42 -0400 Subject: [PATCH 03/11] save --- dispatchers/Html.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dispatchers/Html.php b/dispatchers/Html.php index bbb794324..80372915b 100644 --- a/dispatchers/Html.php +++ b/dispatchers/Html.php @@ -644,7 +644,7 @@ protected function after_output() { 'default' => false, ), 'QM_SHOW_ALL_ASSETS' => array( - 'label' => __( 'In the Scripts and Styles panels, show every registered asset, in addition to enqueued assets.', 'query-monitor' ), + 'label' => __( 'Add child panel to the Scripts and Styles panels, listing the registered assets.', 'query-monitor' ), 'default' => false, ), 'QM_SHOW_ALL_HOOKS' => array( From 859f390359c17fa404d643c802ddf8c81d22d524 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Sat, 27 Jul 2024 01:16:34 -0400 Subject: [PATCH 04/11] save --- output/html/assets.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/output/html/assets.php b/output/html/assets.php index e94567ef5..74022ec26 100644 --- a/output/html/assets.php +++ b/output/html/assets.php @@ -125,7 +125,7 @@ public function output() { printf( '

%2$s

', esc_attr( $this->collector->id() . '-registered' ), - sprintf( $caption_format, strtolower( $type_label['plural'] ) ) + esc_html( sprintf( $caption_format, strtolower( $type_label['plural'] ) ) ) ); echo ''; @@ -351,15 +351,18 @@ public function admin_menu( array $menu ) { } $label = sprintf( + /* translators: %s: number of registered scripts */ _x( 'Registered (%s)', 'Registered scripts', 'query-monitor' ), number_format_i18n( $data->counts['registered'] ) ); - $menu[ $id ]['children'] = array( array( - 'title' => esc_html( $label ), - 'id' => esc_attr( "query-monitor-{$this->collector->id}-registered" ), - 'href' => esc_attr( '#' . $this->collector->id() . '-registered' ), - ) ); + $menu[ $id ]['children'] = array( + array( + 'title' => esc_html( $label ), + 'id' => esc_attr( "query-monitor-{$this->collector->id}-registered" ), + 'href' => esc_attr( '#' . $this->collector->id() . '-registered' ), + ). + ); return $menu; From 7df7f8bde206c8df11662b17a26d51243532dd8a Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Sat, 27 Jul 2024 01:25:18 -0400 Subject: [PATCH 05/11] save --- output/html/assets.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/output/html/assets.php b/output/html/assets.php index 74022ec26..2f1d02906 100644 --- a/output/html/assets.php +++ b/output/html/assets.php @@ -350,11 +350,9 @@ public function admin_menu( array $menu ) { return $menu; } - $label = sprintf( - /* translators: %s: number of registered scripts */ - _x( 'Registered (%s)', 'Registered scripts', 'query-monitor' ), - number_format_i18n( $data->counts['registered'] ) - ); + /* translators: %s: number of registered scripts */ + $label_format = _x( 'Registered (%s)', 'Registered scripts', 'query-monitor' ); + $label = sprintf( $label_format, number_format_i18n( $data->counts['registered'] ) ); $menu[ $id ]['children'] = array( array( From 029ae89dec3f055ec4e47186c39a6cb6f0a5ceb8 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Sat, 27 Jul 2024 01:36:50 -0400 Subject: [PATCH 06/11] save --- output/html/assets.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/output/html/assets.php b/output/html/assets.php index 2f1d02906..66ea1d1b1 100644 --- a/output/html/assets.php +++ b/output/html/assets.php @@ -359,7 +359,7 @@ public function admin_menu( array $menu ) { 'title' => esc_html( $label ), 'id' => esc_attr( "query-monitor-{$this->collector->id}-registered" ), 'href' => esc_attr( '#' . $this->collector->id() . '-registered' ), - ). + ), ); return $menu; From dd0a55bd034629468c05f5392b60198eeb2f8e51 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Sat, 27 Jul 2024 18:51:06 -0400 Subject: [PATCH 07/11] remove constant requirement for registered assets --- classes/Collector_Assets.php | 7 ++----- dispatchers/Html.php | 4 ---- output/html/assets.php | 10 +--------- 3 files changed, 3 insertions(+), 18 deletions(-) diff --git a/classes/Collector_Assets.php b/classes/Collector_Assets.php index a710f4044..1d6214d18 100644 --- a/classes/Collector_Assets.php +++ b/classes/Collector_Assets.php @@ -104,6 +104,7 @@ public function process() { 'broken', 'header', 'footer', + 'registered', ); $this->data->counts = array( @@ -112,13 +113,9 @@ public function process() { 'header' => 0, 'footer' => 0, 'total' => 0, + 'registered' => 0, ); - if ( defined( 'QM_SHOW_ALL_ASSETS' ) && constant( 'QM_SHOW_ALL_ASSETS' ) ) { - $positions[] = 'registered'; - $this->data->counts['registered'] = 0; - } - foreach ( array( 'header', 'footer' ) as $position ) { if ( empty( $this->data->{$position} ) ) { $this->data->{$position} = array(); diff --git a/dispatchers/Html.php b/dispatchers/Html.php index 80372915b..17a848a7e 100644 --- a/dispatchers/Html.php +++ b/dispatchers/Html.php @@ -643,10 +643,6 @@ protected function after_output() { 'label' => __( 'Don\'t specify jQuery as a dependency of Query Monitor. If jQuery isn\'t enqueued then Query Monitor will still operate, but with some reduced functionality.', 'query-monitor' ), 'default' => false, ), - 'QM_SHOW_ALL_ASSETS' => array( - 'label' => __( 'Add child panel to the Scripts and Styles panels, listing the registered assets.', 'query-monitor' ), - 'default' => false, - ), 'QM_SHOW_ALL_HOOKS' => array( 'label' => __( 'In the Hooks & Actions panel, show every hook that has an action or filter attached (instead of every action hook that fired during the request).', 'query-monitor' ), 'default' => false, diff --git a/output/html/assets.php b/output/html/assets.php index 66ea1d1b1..b751faca5 100644 --- a/output/html/assets.php +++ b/output/html/assets.php @@ -113,14 +113,10 @@ public function output() { $this->after_tabular_output(); - if ( ! defined( 'QM_SHOW_ALL_ASSETS' ) || empty( constant( 'QM_SHOW_ALL_ASSETS' ) ) ) { - return; - } - $this->before_tabular_output( $this->collector->id() . '-registered', __( 'Registered', 'query-monitor' ) ); /* translators: %s: "styles" or "scripts" */ - $caption_format = esc_html__( 'Missing, broken, and enqueued %s are not included.', 'query-monitor' ); + $caption_format = esc_html__( 'Enqueued %s are not included.', 'query-monitor' ); printf( '

%2$s

', @@ -346,10 +342,6 @@ public function admin_menu( array $menu ) { $id = $this->collector->id(); $menu[ $id ] = $this->menu( $args ); - if ( ! defined( 'QM_SHOW_ALL_ASSETS' ) || empty( constant( 'QM_SHOW_ALL_ASSETS' ) ) ) { - return $menu; - } - /* translators: %s: number of registered scripts */ $label_format = _x( 'Registered (%s)', 'Registered scripts', 'query-monitor' ); $label = sprintf( $label_format, number_format_i18n( $data->counts['registered'] ) ); From cc41ed0075d3f3a112e39b6ef429f220575dab20 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sun, 28 Jul 2024 20:35:25 +0100 Subject: [PATCH 08/11] Sort the list of registered assets. --- classes/Collector_Assets.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/classes/Collector_Assets.php b/classes/Collector_Assets.php index 0472b5dad..bbe96911f 100644 --- a/classes/Collector_Assets.php +++ b/classes/Collector_Assets.php @@ -129,6 +129,8 @@ public function process() { $this->data->registered = array_keys( $raw->registered ); + sort( $this->data->registered ); + // A broken asset is one which has been deregistered without also being dequeued if ( ! empty( $broken ) ) { foreach ( $broken as $key => $handle ) { From 383f0c71204aaa4e17214c456972e3dc9dbc26fc Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sun, 28 Jul 2024 20:35:30 +0100 Subject: [PATCH 09/11] Fix this label. --- output/html/assets.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/output/html/assets.php b/output/html/assets.php index b751faca5..d08b2e143 100644 --- a/output/html/assets.php +++ b/output/html/assets.php @@ -163,7 +163,7 @@ public function output() { printf( '%1$s', sprintf( - esc_html( $type_label['registered'] ), + esc_html( $type_label['total'] ), '' . esc_html( number_format_i18n( $data->counts['registered'] ) ) . '' ) ); From bf628d2694bb8635feef7944f7837a5352ae002e Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sun, 28 Jul 2024 20:35:50 +0100 Subject: [PATCH 10/11] No need to show the position column for registered assets. --- output/html/assets.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/output/html/assets.php b/output/html/assets.php index d08b2e143..6c7679c70 100644 --- a/output/html/assets.php +++ b/output/html/assets.php @@ -126,7 +126,6 @@ public function output() { echo ''; echo ''; - echo '' . esc_html__( 'Position', 'query-monitor' ) . ''; echo '' . esc_html__( 'Handle', 'query-monitor' ) . ''; echo ''; $args = array( @@ -161,7 +160,7 @@ public function output() { echo ''; printf( - '%1$s', + '%1$s', sprintf( esc_html( $type_label['total'] ), '' . esc_html( number_format_i18n( $data->counts['registered'] ) ) . '' @@ -221,17 +220,20 @@ protected function dependency_row( $handle, array $asset, $label ) { $type = $this->collector->get_dependency_type(); echo ''; - echo ''; - $warning = QueryMonitor::icon( 'warning' ); + if ( $label !== '' ) { + echo ''; - if ( $asset['warning'] ) { - // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - echo $warning; - } + $warning = QueryMonitor::icon( 'warning' ); - echo esc_html( $label ); - echo ''; + if ( $asset['warning'] ) { + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + echo $warning; + } + + echo esc_html( $label ); + echo ''; + } $host = $asset['host']; $parts = explode( '.', $host ); From c89841722c9aac779b3cb53b88f35598d28c0bf2 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Mon, 29 Jul 2024 09:39:13 -0400 Subject: [PATCH 11/11] fix phpstan error --- output/html/assets.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/output/html/assets.php b/output/html/assets.php index 6c7679c70..4ba3a1836 100644 --- a/output/html/assets.php +++ b/output/html/assets.php @@ -190,11 +190,10 @@ protected function dependency_row( $handle, array $asset, $label ) { $dependents_list = implode( ' ', $asset['dependents'] ); $dependency_output = array(); + $warning = QueryMonitor::icon( 'warning' ); foreach ( $asset['dependencies'] as $dep ) { if ( isset( $data->missing_dependencies[ $dep ] ) ) { - $warning = QueryMonitor::icon( 'warning' ); - $dependency_output[] = sprintf( '%1$s%2$s', $warning, @@ -224,8 +223,6 @@ protected function dependency_row( $handle, array $asset, $label ) { if ( $label !== '' ) { echo ''; - $warning = QueryMonitor::icon( 'warning' ); - if ( $asset['warning'] ) { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $warning;