From c52d7597b86f34df14f857c70768a3d07db7d38b Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 18 Jan 2024 15:11:03 -0800 Subject: [PATCH 1/5] Include standalone plugin slugs in generator tag. --- admin/plugins.php | 8 +++----- load.php | 33 ++++++++++++++++++++++++++------- tests/load-tests.php | 2 +- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/admin/plugins.php b/admin/plugins.php index 4baf479067..feca3b2614 100644 --- a/admin/plugins.php +++ b/admin/plugins.php @@ -48,10 +48,8 @@ function perflab_query_plugin_info( string $plugin_slug ) { * @return array List of WPP standalone plugins as slugs. */ function perflab_get_standalone_plugins() { - return array( - 'webp-uploads', - 'performant-translations', - 'dominant-color-images', + return array_keys( + perflab_get_standalone_plugins_constants( 'plugins' ) ); } @@ -65,7 +63,7 @@ function perflab_get_standalone_plugins() { function perflab_get_active_modules_with_standalone_plugins() { $modules = perflab_get_module_settings(); return array_filter( - array_keys( perflab_get_standalone_plugins_constants() ), + array_keys( perflab_get_standalone_plugins_constants( 'modules' ) ), static function ( $module ) use ( $modules ) { return ! empty( $modules[ $module ] ) && $modules[ $module ]['enabled']; } diff --git a/load.php b/load.php index 5d1d9970d9..c6e0f9cd91 100644 --- a/load.php +++ b/load.php @@ -207,14 +207,23 @@ function perflab_is_valid_module( $module ) { * This attribute is then used in {@see perflab_render_generator()}. * * @since 1.1.0 + * @since n.e.x.t The generator tag now includes the active standalone plugin slugs. */ function perflab_get_generator_content() { $active_and_valid_modules = array_filter( perflab_get_active_modules(), 'perflab_is_valid_module' ); + $active_plugins = array(); + foreach ( perflab_get_standalone_plugins_constants( 'plugins' ) as $plugin_slug => $constant_name ) { + if ( defined( $constant_name ) && ! str_starts_with( constant( $constant_name ), 'Performance Lab ' ) ) { + $active_plugins[] = $plugin_slug; + } + } + return sprintf( - 'Performance Lab %1$s; modules: %2$s', + 'Performance Lab %1$s; modules: %2$s; plugins: %3$s', PERFLAB_VERSION, - implode( ', ', $active_and_valid_modules ) + implode( ', ', $active_and_valid_modules ), + implode( ', ', $active_plugins ) ); } @@ -276,7 +285,7 @@ function perflab_can_load_module( $module ) { * @return bool Whether the module has already been loaded by a separate plugin. */ function perflab_is_standalone_plugin_loaded( $module ) { - $standalone_plugins_constants = perflab_get_standalone_plugins_constants(); + $standalone_plugins_constants = perflab_get_standalone_plugins_constants( 'modules' ); if ( isset( $standalone_plugins_constants[ $module ] ) && defined( $standalone_plugins_constants[ $module ] ) && @@ -291,13 +300,23 @@ function perflab_is_standalone_plugin_loaded( $module ) { * Gets the standalone plugin constants used for each module / plugin. * * @since 2.2.0 + * @since n.e.x.t The `$type` parameter was added. * - * @return array Map of module path to version constant used. + * @param string $type Optional. Either 'modules' or 'plugins'. Default 'modules'. + * @return array Map of module path / plugin slug and the version constant used. */ -function perflab_get_standalone_plugins_constants() { +function perflab_get_standalone_plugins_constants( $type = 'modules' ) { + if ( 'modules' === $type ) { + return array( + 'images/dominant-color-images' => 'DOMINANT_COLOR_IMAGES_VERSION', + 'images/webp-uploads' => 'WEBP_UPLOADS_VERSION', + ); + } + return array( - 'images/dominant-color-images' => 'DOMINANT_COLOR_IMAGES_VERSION', - 'images/webp-uploads' => 'WEBP_UPLOADS_VERSION', + 'webp-uploads' => 'WEBP_UPLOADS_VERSION', + 'dominant-color-images' => 'DOMINANT_COLOR_IMAGES_VERSION', + 'performant-translations' => 'PERFORMANT_TRANSLATIONS_VERSION', ); } diff --git a/tests/load-tests.php b/tests/load-tests.php index 19c8a9a88f..c98eda795f 100644 --- a/tests/load-tests.php +++ b/tests/load-tests.php @@ -163,7 +163,7 @@ static function () use ( $active_modules ) { } ); $active_modules = array_filter( perflab_get_active_modules(), 'perflab_is_valid_module' ); - $expected = 'Performance Lab ' . PERFLAB_VERSION . '; modules: ' . implode( ', ', $active_modules ); + $expected = 'Performance Lab ' . PERFLAB_VERSION . '; modules: ' . implode( ', ', $active_modules ) . '; plugins: '; $content = perflab_get_generator_content(); $this->assertSame( $expected, $content ); } From ccf3220bbc92965fbd3dd3a39234a11c4fafd539 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 18 Jan 2024 15:22:22 -0800 Subject: [PATCH 2/5] Update additional test. --- tests/load-tests.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/load-tests.php b/tests/load-tests.php index c98eda795f..d2be1eec03 100644 --- a/tests/load-tests.php +++ b/tests/load-tests.php @@ -171,7 +171,7 @@ static function () use ( $active_modules ) { public function test_perflab_render_generator() { // Assert generator tag is rendered. Content does not matter, so just use no modules active. add_filter( 'perflab_active_modules', '__return_empty_array' ); - $expected = '' . "\n"; + $expected = '' . "\n"; $output = get_echo( 'perflab_render_generator' ); $this->assertSame( $expected, $output ); From ead22c82a647f9473e6ec444e513dee9cf5e802b Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Mon, 22 Jan 2024 14:43:07 -0800 Subject: [PATCH 3/5] Use more precise type doc. Co-authored-by: Weston Ruter --- load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/load.php b/load.php index c6e0f9cd91..3eaca92313 100644 --- a/load.php +++ b/load.php @@ -303,7 +303,7 @@ function perflab_is_standalone_plugin_loaded( $module ) { * @since n.e.x.t The `$type` parameter was added. * * @param string $type Optional. Either 'modules' or 'plugins'. Default 'modules'. - * @return array Map of module path / plugin slug and the version constant used. + * @return array Map of module path / plugin slug and the version constant used. */ function perflab_get_standalone_plugins_constants( $type = 'modules' ) { if ( 'modules' === $type ) { From 8181c68ae8806ecd89c047d76318044ae24b01f8 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Mon, 22 Jan 2024 16:21:44 -0800 Subject: [PATCH 4/5] Introduce new function instead of trying to reuse old one, and add more explanation via code comments. --- admin/plugins.php | 4 ++-- load.php | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/admin/plugins.php b/admin/plugins.php index feca3b2614..b5a7fd9e7b 100644 --- a/admin/plugins.php +++ b/admin/plugins.php @@ -49,7 +49,7 @@ function perflab_query_plugin_info( string $plugin_slug ) { */ function perflab_get_standalone_plugins() { return array_keys( - perflab_get_standalone_plugins_constants( 'plugins' ) + perflab_get_standalone_plugin_version_constants( 'plugins' ) ); } @@ -63,7 +63,7 @@ function perflab_get_standalone_plugins() { function perflab_get_active_modules_with_standalone_plugins() { $modules = perflab_get_module_settings(); return array_filter( - array_keys( perflab_get_standalone_plugins_constants( 'modules' ) ), + array_keys( perflab_get_standalone_plugin_version_constants( 'modules' ) ), static function ( $module ) use ( $modules ) { return ! empty( $modules[ $module ] ) && $modules[ $module ]['enabled']; } diff --git a/load.php b/load.php index 3eaca92313..5c2d37cd76 100644 --- a/load.php +++ b/load.php @@ -213,7 +213,7 @@ function perflab_get_generator_content() { $active_and_valid_modules = array_filter( perflab_get_active_modules(), 'perflab_is_valid_module' ); $active_plugins = array(); - foreach ( perflab_get_standalone_plugins_constants( 'plugins' ) as $plugin_slug => $constant_name ) { + foreach ( perflab_get_standalone_plugin_version_constants( 'plugins' ) as $plugin_slug => $constant_name ) { if ( defined( $constant_name ) && ! str_starts_with( constant( $constant_name ), 'Performance Lab ' ) ) { $active_plugins[] = $plugin_slug; } @@ -285,7 +285,7 @@ function perflab_can_load_module( $module ) { * @return bool Whether the module has already been loaded by a separate plugin. */ function perflab_is_standalone_plugin_loaded( $module ) { - $standalone_plugins_constants = perflab_get_standalone_plugins_constants( 'modules' ); + $standalone_plugins_constants = perflab_get_standalone_plugin_version_constants( 'modules' ); if ( isset( $standalone_plugins_constants[ $module ] ) && defined( $standalone_plugins_constants[ $module ] ) && @@ -297,22 +297,42 @@ function perflab_is_standalone_plugin_loaded( $module ) { } /** - * Gets the standalone plugin constants used for each module / plugin. + * Gets the standalone plugin constants used for each module with a standalone plugin. * * @since 2.2.0 - * @since n.e.x.t The `$type` parameter was added. + * @deprecated n.e.x.t * - * @param string $type Optional. Either 'modules' or 'plugins'. Default 'modules'. - * @return array Map of module path / plugin slug and the version constant used. + * @return array Map of module path to version constant used. */ -function perflab_get_standalone_plugins_constants( $type = 'modules' ) { - if ( 'modules' === $type ) { +function perflab_get_standalone_plugins_constants() { + _deprecated_function( __FUNCTION__, 'Performance Lab n.e.x.t', "perflab_get_standalone_plugin_version_constants( 'modules' )" ); + return perflab_get_standalone_plugin_version_constants( 'modules' ); +} + +/** + * Gets the standalone plugin constants used for each available standalone plugin, or module with a standalone plugin. + * + * @since n.e.x.t + * + * @param string $source Optional. Either 'plugins' or 'modules'. Default 'plugins'. + * @return array Map of plugin slug / module path and the version constant used. + */ +function perflab_get_standalone_plugin_version_constants( $source = 'plugins' ) { + if ( 'modules' === $source ) { + /* + * This list includes all modules which are also available as standalone plugins, + * as `$module_dir => $version_constant` pairs. + */ return array( 'images/dominant-color-images' => 'DOMINANT_COLOR_IMAGES_VERSION', 'images/webp-uploads' => 'WEBP_UPLOADS_VERSION', ); } + /* + * This list includes all standalone plugins that are part of the Performance Lab project, + * as `$plugin_slug => $version_constant` pairs. + */ return array( 'webp-uploads' => 'WEBP_UPLOADS_VERSION', 'dominant-color-images' => 'DOMINANT_COLOR_IMAGES_VERSION', From 901a62e3a8f31482a4511f7ac744c5a0c1eab070 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 24 Jan 2024 15:35:08 -0800 Subject: [PATCH 5/5] Replace n.e.x.t with 2.9.0. --- load.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/load.php b/load.php index 5c2d37cd76..ce20b2e267 100644 --- a/load.php +++ b/load.php @@ -300,12 +300,12 @@ function perflab_is_standalone_plugin_loaded( $module ) { * Gets the standalone plugin constants used for each module with a standalone plugin. * * @since 2.2.0 - * @deprecated n.e.x.t + * @deprecated 2.9.0 * * @return array Map of module path to version constant used. */ function perflab_get_standalone_plugins_constants() { - _deprecated_function( __FUNCTION__, 'Performance Lab n.e.x.t', "perflab_get_standalone_plugin_version_constants( 'modules' )" ); + _deprecated_function( __FUNCTION__, 'Performance Lab 2.9.0', "perflab_get_standalone_plugin_version_constants( 'modules' )" ); return perflab_get_standalone_plugin_version_constants( 'modules' ); }