Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions admin/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,18 @@ function perflab_render_modules_page() {
* @param array $module_settings Associative array of the module's current settings.
*/
function perflab_render_modules_page_field( $module_slug, $module_data, $module_settings ) {
$base_id = sprintf( 'module_%s', $module_slug );
$base_name = sprintf( '%1$s[%2$s]', PERFLAB_MODULES_SETTING, $module_slug );
$enabled = isset( $module_settings['enabled'] ) && $module_settings['enabled'];
$base_id = sprintf( 'module_%s', $module_slug );
$base_name = sprintf( '%1$s[%2$s]', PERFLAB_MODULES_SETTING, $module_slug );
$enabled = isset( $module_settings['enabled'] ) && $module_settings['enabled'];
$can_load_module = perflab_can_load_module( $module_slug );
$is_standalone_plugin_loaded = perflab_is_standalone_plugin_loaded( $module_slug );
?>
<fieldset>
<legend class="screen-reader-text">
<?php echo esc_html( $module_data['name'] ); ?>
</legend>
<label for="<?php echo esc_attr( "{$base_id}_enabled" ); ?>">
<?php if ( perflab_can_load_module( $module_slug ) ) { ?>
<?php if ( $can_load_module && ! $is_standalone_plugin_loaded ) { ?>
<input type="checkbox" id="<?php echo esc_attr( "{$base_id}_enabled" ); ?>" name="<?php echo esc_attr( "{$base_name}[enabled]" ); ?>" aria-describedby="<?php echo esc_attr( "{$base_id}_description" ); ?>" value="1"<?php checked( $enabled ); ?>>
<?php
if ( $module_data['experimental'] ) {
Expand All @@ -156,7 +158,9 @@ function perflab_render_modules_page_field( $module_slug, $module_data, $module_
<input type="checkbox" id="<?php echo esc_attr( "{$base_id}_enabled" ); ?>" aria-describedby="<?php echo esc_attr( "{$base_id}_description" ); ?>" disabled>
<input type="hidden" name="<?php echo esc_attr( "{$base_name}[enabled]" ); ?>" value="<?php echo $enabled ? '1' : '0'; ?>">
<?php
if ( 'database/sqlite' === $module_slug && file_exists( WP_CONTENT_DIR . '/db.php' ) && ! defined( 'PERFLAB_SQLITE_DB_DROPIN_VERSION' ) ) {
if ( $is_standalone_plugin_loaded ) {
esc_html_e( 'The module cannot be managed with Performance Lab since it is already active as a standalone plugin.', 'performance-lab' );
} elseif ( 'database/sqlite' === $module_slug && file_exists( WP_CONTENT_DIR . '/db.php' ) && ! defined( 'PERFLAB_SQLITE_DB_DROPIN_VERSION' ) ) {
printf(
/* translators: %s: db.php drop-in path */
esc_html__( 'The SQLite module cannot be activated because a different %s drop-in already exists.', 'performance-lab' ),
Expand Down
7 changes: 7 additions & 0 deletions bin/plugin/commands/build-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ exports.handler = async () => {
regex: '@package\\s{1,}performance-lab',
result: '@package ' + pluginSlug,
} );

// Update version constant.
updateModuleDetails( {
pluginPath: buildModulePath,
regex: "[']Performance Lab ['] . PERFLAB_VERSION",
result: `'${ pluginVersion }'`,
} );
} catch ( error ) {
log(
formats.error(
Expand Down
41 changes: 40 additions & 1 deletion load.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ function( $module_settings ) {
* Gets the active and valid performance modules.
*
* @since 1.3.0
* @since n.e.x.t Adds an additional check for standalone plugins.
*
* @param string $module Slug of the module.
* @return bool True if the module is active and valid, otherwise false.
Expand All @@ -179,6 +180,11 @@ function perflab_is_valid_module( $module ) {
return false;
}

// Do not load the module if it can be loaded by a separate plugin.
if ( perflab_is_standalone_plugin_loaded( $module ) ) {
return false;
}

// Do not load module if no longer exists.
$module_file = PERFLAB_PLUGIN_DIR_PATH . 'modules/' . $module . '/load.php';
if ( ! file_exists( $module_file ) ) {
Expand Down Expand Up @@ -248,6 +254,39 @@ function perflab_can_load_module( $module ) {
return (bool) $module();
}

/**
* Checks whether the given module has already been loaded by a separate plugin.
*
* @since n.e.x.t
*
* @param string $module Slug of the 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();
if (
isset( $standalone_plugins_constants[ $module ] ) &&
defined( $standalone_plugins_constants[ $module ] ) &&
! str_starts_with( constant( $standalone_plugins_constants[ $module ] ), 'Performance Lab ' )
) {
return true;
}
return false;
}

/**
* Gets the standalone plugin constants used for each module / plugin.
*
* @since n.e.x.t
*
* @return array Map of module path to version constant used.
*/
function perflab_get_standalone_plugins_constants() {
return array(
'images/webp-uploads' => 'WEBP_UPLOADS_VERSION',
);
}

/**
* Loads the active and valid performance modules.
*
Expand All @@ -262,7 +301,7 @@ function perflab_load_active_and_valid_modules() {
require_once PERFLAB_PLUGIN_DIR_PATH . 'modules/' . $module . '/load.php';
}
}
perflab_load_active_and_valid_modules();
add_action( 'plugins_loaded', 'perflab_load_active_and_valid_modules' );

/**
* Places the Performance Lab's object cache drop-in in the drop-ins folder.
Expand Down
7 changes: 7 additions & 0 deletions modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
* @package performance-lab
*/

// Define the constant.
if ( defined( 'WEBP_UPLOADS_VERSION' ) ) {
return;
}

define( 'WEBP_UPLOADS_VERSION', 'Performance Lab ' . PERFLAB_VERSION );

// Do not load the code if it is already loaded through another means.
if ( function_exists( 'webp_uploads_create_sources_property' ) ) {
return;
Expand Down