Skip to content

Commit 0e7f285

Browse files
authored
Merge pull request #949 from WordPress/add/standalone-plugin-generator-tag-support
Include standalone plugin slugs in generator tag
2 parents fd23971 + 901a62e commit 0e7f285

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

admin/plugins.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ function perflab_query_plugin_info( string $plugin_slug ) {
4848
* @return array List of WPP standalone plugins as slugs.
4949
*/
5050
function perflab_get_standalone_plugins() {
51-
return array(
52-
'webp-uploads',
53-
'performant-translations',
54-
'dominant-color-images',
51+
return array_keys(
52+
perflab_get_standalone_plugin_version_constants( 'plugins' )
5553
);
5654
}
5755

@@ -65,7 +63,7 @@ function perflab_get_standalone_plugins() {
6563
function perflab_get_active_modules_with_standalone_plugins() {
6664
$modules = perflab_get_module_settings();
6765
return array_filter(
68-
array_keys( perflab_get_standalone_plugins_constants() ),
66+
array_keys( perflab_get_standalone_plugin_version_constants( 'modules' ) ),
6967
static function ( $module ) use ( $modules ) {
7068
return ! empty( $modules[ $module ] ) && $modules[ $module ]['enabled'];
7169
}

load.php

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,23 @@ function perflab_is_valid_module( $module ) {
207207
* This attribute is then used in {@see perflab_render_generator()}.
208208
*
209209
* @since 1.1.0
210+
* @since n.e.x.t The generator tag now includes the active standalone plugin slugs.
210211
*/
211212
function perflab_get_generator_content() {
212213
$active_and_valid_modules = array_filter( perflab_get_active_modules(), 'perflab_is_valid_module' );
213214

215+
$active_plugins = array();
216+
foreach ( perflab_get_standalone_plugin_version_constants( 'plugins' ) as $plugin_slug => $constant_name ) {
217+
if ( defined( $constant_name ) && ! str_starts_with( constant( $constant_name ), 'Performance Lab ' ) ) {
218+
$active_plugins[] = $plugin_slug;
219+
}
220+
}
221+
214222
return sprintf(
215-
'Performance Lab %1$s; modules: %2$s',
223+
'Performance Lab %1$s; modules: %2$s; plugins: %3$s',
216224
PERFLAB_VERSION,
217-
implode( ', ', $active_and_valid_modules )
225+
implode( ', ', $active_and_valid_modules ),
226+
implode( ', ', $active_plugins )
218227
);
219228
}
220229

@@ -276,7 +285,7 @@ function perflab_can_load_module( $module ) {
276285
* @return bool Whether the module has already been loaded by a separate plugin.
277286
*/
278287
function perflab_is_standalone_plugin_loaded( $module ) {
279-
$standalone_plugins_constants = perflab_get_standalone_plugins_constants();
288+
$standalone_plugins_constants = perflab_get_standalone_plugin_version_constants( 'modules' );
280289
if (
281290
isset( $standalone_plugins_constants[ $module ] ) &&
282291
defined( $standalone_plugins_constants[ $module ] ) &&
@@ -288,16 +297,46 @@ function perflab_is_standalone_plugin_loaded( $module ) {
288297
}
289298

290299
/**
291-
* Gets the standalone plugin constants used for each module / plugin.
300+
* Gets the standalone plugin constants used for each module with a standalone plugin.
292301
*
293302
* @since 2.2.0
303+
* @deprecated 2.9.0
294304
*
295305
* @return array Map of module path to version constant used.
296306
*/
297307
function perflab_get_standalone_plugins_constants() {
308+
_deprecated_function( __FUNCTION__, 'Performance Lab 2.9.0', "perflab_get_standalone_plugin_version_constants( 'modules' )" );
309+
return perflab_get_standalone_plugin_version_constants( 'modules' );
310+
}
311+
312+
/**
313+
* Gets the standalone plugin constants used for each available standalone plugin, or module with a standalone plugin.
314+
*
315+
* @since n.e.x.t
316+
*
317+
* @param string $source Optional. Either 'plugins' or 'modules'. Default 'plugins'.
318+
* @return array<string, string> Map of plugin slug / module path and the version constant used.
319+
*/
320+
function perflab_get_standalone_plugin_version_constants( $source = 'plugins' ) {
321+
if ( 'modules' === $source ) {
322+
/*
323+
* This list includes all modules which are also available as standalone plugins,
324+
* as `$module_dir => $version_constant` pairs.
325+
*/
326+
return array(
327+
'images/dominant-color-images' => 'DOMINANT_COLOR_IMAGES_VERSION',
328+
'images/webp-uploads' => 'WEBP_UPLOADS_VERSION',
329+
);
330+
}
331+
332+
/*
333+
* This list includes all standalone plugins that are part of the Performance Lab project,
334+
* as `$plugin_slug => $version_constant` pairs.
335+
*/
298336
return array(
299-
'images/dominant-color-images' => 'DOMINANT_COLOR_IMAGES_VERSION',
300-
'images/webp-uploads' => 'WEBP_UPLOADS_VERSION',
337+
'webp-uploads' => 'WEBP_UPLOADS_VERSION',
338+
'dominant-color-images' => 'DOMINANT_COLOR_IMAGES_VERSION',
339+
'performant-translations' => 'PERFORMANT_TRANSLATIONS_VERSION',
301340
);
302341
}
303342

tests/load-tests.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,15 @@ static function () use ( $active_modules ) {
163163
}
164164
);
165165
$active_modules = array_filter( perflab_get_active_modules(), 'perflab_is_valid_module' );
166-
$expected = 'Performance Lab ' . PERFLAB_VERSION . '; modules: ' . implode( ', ', $active_modules );
166+
$expected = 'Performance Lab ' . PERFLAB_VERSION . '; modules: ' . implode( ', ', $active_modules ) . '; plugins: ';
167167
$content = perflab_get_generator_content();
168168
$this->assertSame( $expected, $content );
169169
}
170170

171171
public function test_perflab_render_generator() {
172172
// Assert generator tag is rendered. Content does not matter, so just use no modules active.
173173
add_filter( 'perflab_active_modules', '__return_empty_array' );
174-
$expected = '<meta name="generator" content="Performance Lab ' . PERFLAB_VERSION . '; modules: ">' . "\n";
174+
$expected = '<meta name="generator" content="Performance Lab ' . PERFLAB_VERSION . '; modules: ; plugins: ">' . "\n";
175175
$output = get_echo( 'perflab_render_generator' );
176176
$this->assertSame( $expected, $output );
177177

0 commit comments

Comments
 (0)