Skip to content

Commit 0c5bc48

Browse files
Introduce perflab_is_module_standalone()
1 parent 97d865e commit 0c5bc48

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

admin/load.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,18 @@ function perflab_render_modules_page() {
126126
* @param array $module_settings Associative array of the module's current settings.
127127
*/
128128
function perflab_render_modules_page_field( $module_slug, $module_data, $module_settings ) {
129-
$base_id = sprintf( 'module_%s', $module_slug );
130-
$base_name = sprintf( '%1$s[%2$s]', PERFLAB_MODULES_SETTING, $module_slug );
131-
$enabled = isset( $module_settings['enabled'] ) && $module_settings['enabled'];
132-
$can_load_module = perflab_can_load_module( $module_slug );
129+
$base_id = sprintf( 'module_%s', $module_slug );
130+
$base_name = sprintf( '%1$s[%2$s]', PERFLAB_MODULES_SETTING, $module_slug );
131+
$enabled = isset( $module_settings['enabled'] ) && $module_settings['enabled'];
132+
$can_load_module = perflab_can_load_module( $module_slug );
133+
$is_module_standalone = perflab_is_module_standalone( $module_slug );
133134
?>
134135
<fieldset>
135136
<legend class="screen-reader-text">
136137
<?php echo esc_html( $module_data['name'] ); ?>
137138
</legend>
138139
<label for="<?php echo esc_attr( "{$base_id}_enabled" ); ?>">
139-
<?php if ( $can_load_module && ! is_wp_error( $can_load_module ) ) { ?>
140+
<?php if ( $can_load_module && ! $is_module_standalone ) { ?>
140141
<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 ); ?>>
141142
<?php
142143
if ( $module_data['experimental'] ) {
@@ -157,8 +158,8 @@ function perflab_render_modules_page_field( $module_slug, $module_data, $module_
157158
<input type="checkbox" id="<?php echo esc_attr( "{$base_id}_enabled" ); ?>" aria-describedby="<?php echo esc_attr( "{$base_id}_description" ); ?>" disabled>
158159
<input type="hidden" name="<?php echo esc_attr( "{$base_name}[enabled]" ); ?>" value="<?php echo $enabled ? '1' : '0'; ?>">
159160
<?php
160-
if ( is_wp_error( $can_load_module ) ) {
161-
echo esc_html( $can_load_module->get_error_message() );
161+
if ( $is_module_standalone ) {
162+
esc_html_e( 'The module cannot be managed with Performance Lab since it is already active as a standalone plugin.', 'performance-lab' );
162163
} elseif ( 'database/sqlite' === $module_slug && file_exists( WP_CONTENT_DIR . '/db.php' ) && ! defined( 'PERFLAB_SQLITE_DB_DROPIN_VERSION' ) ) {
163164
printf(
164165
/* translators: %s: db.php drop-in path */

bin/plugin/commands/build-plugins.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ exports.handler = async () => {
8585
// Update version constant.
8686
updateModuleDetails( {
8787
pluginPath: buildModulePath,
88-
regex: '[\']Performance Lab [\'] . PERFLAB_VERSION',
88+
regex: "[']Performance Lab ['] . PERFLAB_VERSION",
8989
result: `'${ pluginVersion }'`,
9090
} );
9191
} catch ( error ) {

load.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,10 @@ function perflab_render_generator() {
224224
* Checks whether the given module can be loaded in the current environment.
225225
*
226226
* @since 1.3.0
227+
* @since n.e.x.t Adds an additional check for standalone plugins.
227228
*
228229
* @param string $module Slug of the module.
229-
* @return bool|WP_Error Whether the module can be loaded or not, otherwise a WP_Error.
230+
* @return bool Whether the module can be loaded or not.
230231
*/
231232
function perflab_can_load_module( $module ) {
232233
$module_load_file = PERFLAB_PLUGIN_DIR_PATH . 'modules/' . $module . '/can-load.php';
@@ -236,13 +237,9 @@ function perflab_can_load_module( $module ) {
236237
return true;
237238
}
238239

239-
$standalone_plugins_constants = perflab_get_standalone_plugins_constants();
240-
if (
241-
isset( $standalone_plugins_constants[ $module ] ) &&
242-
defined( $standalone_plugins_constants[ $module ] ) &&
243-
! str_starts_with( constant( $standalone_plugins_constants[ $module ] ), 'Performance Lab ' )
244-
) {
245-
return new WP_Error( 'standalone_plugin_activated', __( 'The module cannot be managed with Performance Lab since it is already active as a standalone plugin.', 'performance-lab' ) );
240+
// Do not load the module if it can be loaded by a separate plugin.
241+
if ( perflab_is_module_standalone( $module ) ) {
242+
return false;
246243
}
247244

248245
// Require the file to get the closure for whether the module can load.
@@ -257,6 +254,26 @@ function perflab_can_load_module( $module ) {
257254
return (bool) $module();
258255
}
259256

257+
/**
258+
* Checks whether the given module can be loaded by a separate plugin.
259+
*
260+
* @since n.e.x.t
261+
*
262+
* @param string $module Slug of the module.
263+
* @return bool Whether the module can be loaded by a separate plugin or not.
264+
*/
265+
function perflab_is_module_standalone( $module ) {
266+
$standalone_plugins_constants = perflab_get_standalone_plugins_constants();
267+
if (
268+
isset( $standalone_plugins_constants[ $module ] ) &&
269+
defined( $standalone_plugins_constants[ $module ] ) &&
270+
! str_starts_with( constant( $standalone_plugins_constants[ $module ] ), 'Performance Lab ' )
271+
) {
272+
return true;
273+
}
274+
return false;
275+
}
276+
260277
/**
261278
* Gets the standalone plugin constants used for each module / plugin.
262279
*

0 commit comments

Comments
 (0)