Skip to content

Commit

Permalink
Merge pull request #949 from WordPress/add/standalone-plugin-generato…
Browse files Browse the repository at this point in the history
…r-tag-support

Include standalone plugin slugs in generator tag
  • Loading branch information
felixarntz authored Jan 24, 2024
2 parents fd23971 + 901a62e commit 0e7f285
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
8 changes: 3 additions & 5 deletions admin/plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_plugin_version_constants( 'plugins' )
);
}

Expand All @@ -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_plugin_version_constants( 'modules' ) ),
static function ( $module ) use ( $modules ) {
return ! empty( $modules[ $module ] ) && $modules[ $module ]['enabled'];
}
Expand Down
51 changes: 45 additions & 6 deletions load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_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;
}
}

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 )
);
}

Expand Down Expand Up @@ -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_plugin_version_constants( 'modules' );
if (
isset( $standalone_plugins_constants[ $module ] ) &&
defined( $standalone_plugins_constants[ $module ] ) &&
Expand All @@ -288,16 +297,46 @@ 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
* @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 2.9.0', "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<string, string> 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(
'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',
);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/load-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,15 @@ 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 );
}

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 = '<meta name="generator" content="Performance Lab ' . PERFLAB_VERSION . '; modules: ">' . "\n";
$expected = '<meta name="generator" content="Performance Lab ' . PERFLAB_VERSION . '; modules: ; plugins: ">' . "\n";
$output = get_echo( 'perflab_render_generator' );
$this->assertSame( $expected, $output );

Expand Down

0 comments on commit 0e7f285

Please sign in to comment.