Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow module can-load.php callbacks to return a WP_Error with more information #891

Merged
merged 4 commits into from Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion admin/load.php
Expand Up @@ -141,7 +141,7 @@ function perflab_render_modules_page_field( $module_slug, $module_data, $module_
<?php echo esc_html( $module_data['name'] ); ?>
</legend>
<label for="<?php echo esc_attr( "{$base_id}_enabled" ); ?>">
<?php if ( $can_load_module && ! $is_standalone_plugin_loaded ) { ?>
<?php if ( ! is_wp_error( $can_load_module ) && ! $is_standalone_plugin_loaded ) { ?>
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
<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 @@ -167,6 +167,8 @@ function perflab_render_modules_page_field( $module_slug, $module_data, $module_
<?php
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 ( is_wp_error( $can_load_module ) ) {
echo esc_html( $can_load_module->get_error_message() );
} else {
printf(
/* translators: %s: module name */
Expand Down
10 changes: 8 additions & 2 deletions load.php
Expand Up @@ -197,7 +197,8 @@ function perflab_is_valid_module( $module ) {
}

// Do not load module if it cannot be loaded, e.g. if it was already merged and is available in WordPress core.
return perflab_can_load_module( $module );
$can_load_module = perflab_can_load_module( $module );
return is_wp_error( $can_load_module ) ? false : $can_load_module;
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -237,7 +238,7 @@ function perflab_render_generator() {
* @since 1.3.0
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
*
* @param string $module Slug of the module.
* @return bool Whether the module can be loaded or not.
* @return bool|WP_Error True or false based on whether the module can be loaded or not, or an error on failure.
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
*/
function perflab_can_load_module( $module ) {
$module_load_file = PERFLAB_PLUGIN_DIR_PATH . 'modules/' . $module . '/can-load.php';
Expand All @@ -255,6 +256,11 @@ function perflab_can_load_module( $module ) {
return true;
}

// Check if can load return an error.
if ( is_wp_error( $module() ) ) {
return $module();
}

// Call the closure to determine whether the module can be loaded.
return (bool) $module();
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
19 changes: 19 additions & 0 deletions modules/database/audit-autoloaded-options/can-load.php
@@ -0,0 +1,19 @@
<?php
/**
* Can load function to determine if Site Health module is supported or not.
*
* @since n.e.x.t
* @package performance-lab
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

return static function () {
if ( ! has_filter( 'user_has_cap', 'wp_maybe_grant_site_health_caps' ) ) {
return new WP_Error( 'module_not_loaded', esc_html__( 'The module cannot be loaded with Performance Lab since it is disabled.', 'performance-lab' ) );
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
} else {
return true;
}
};
19 changes: 19 additions & 0 deletions modules/images/webp-support/can-load.php
@@ -0,0 +1,19 @@
<?php
/**
* Can load function to determine if Site Health module is supported or not.
*
* @since n.e.x.t
* @package performance-lab
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

return static function () {
if ( ! has_filter( 'user_has_cap', 'wp_maybe_grant_site_health_caps' ) ) {
return new WP_Error( 'module_not_loaded', esc_html__( 'The module cannot be loaded with Performance Lab since it is disabled.', 'performance-lab' ) );
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
} else {
return true;
}
};
19 changes: 19 additions & 0 deletions modules/js-and-css/audit-enqueued-assets/can-load.php
@@ -0,0 +1,19 @@
<?php
/**
* Can load function to determine if Site Health module is supported or not.
*
* @since n.e.x.t
* @package performance-lab
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

return static function () {
if ( ! has_filter( 'user_has_cap', 'wp_maybe_grant_site_health_caps' ) ) {
return new WP_Error( 'module_not_loaded', esc_html__( 'The module cannot be loaded with Performance Lab since it is disabled.', 'performance-lab' ) );
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
} else {
return true;
}
};
9 changes: 8 additions & 1 deletion tests/admin/load-tests.php
Expand Up @@ -32,6 +32,13 @@ class Admin_Load_Tests extends WP_UnitTestCase {
'focus' => 'images',
'slug' => 'demo-module-3',
),
'check-error/demo-module-4' => array(
'name' => 'Demo Module 4',
'description' => 'This is the description for demo module 4.',
'experimental' => false,
'focus' => 'check-error',
'slug' => 'demo-module-4',
),
);

private static $demo_focus_areas = array(
Expand Down Expand Up @@ -157,7 +164,7 @@ public function test_perflab_load_modules_page() {
array_keys( $wp_settings_fields[ PERFLAB_MODULES_SCREEN ]['js-and-css'] )
);
$this->assertEqualSets(
array( 'something/demo-module-2' ),
array( 'something/demo-module-2', 'check-error/demo-module-4' ),
array_keys( $wp_settings_fields[ PERFLAB_MODULES_SCREEN ]['other'] )
);
}
Expand Down
7 changes: 7 additions & 0 deletions tests/load-tests.php
Expand Up @@ -196,6 +196,7 @@ public function data_perflab_is_valid_module() {
array( '../tests/testdata/demo-modules/js-and-css/demo-module-1', false ),
array( '../tests/testdata/demo-modules/something/demo-module-2', true ),
array( '../tests/testdata/demo-modules/images/demo-module-3', true ),
array( '../tests/testdata/demo-modules/check-error/demo-module-4', false ),
);
}

Expand All @@ -214,6 +215,12 @@ public function data_perflab_can_load_module() {
);
}

public function test_perflab_can_load_module_with_not_loaded_module() {
$can_load_module = perflab_can_load_module( '../tests/testdata/demo-modules/check-error/demo-module-4' );
$this->assertInstanceOf( 'WP_Error', $can_load_module );
$this->assertSame( 'module_not_loaded', $can_load_module->get_error_code() );
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
}

public function test_perflab_activate_module() {
perflab_activate_module( __DIR__ . '/testdata/demo-modules/something/demo-module-2' );
$this->assertSame( 'activated', get_option( 'test_demo_module_activation_status' ) );
Expand Down
10 changes: 10 additions & 0 deletions tests/testdata/demo-modules/check-error/demo-module-4/can-load.php
@@ -0,0 +1,10 @@
<?php
/**
* Can load function to determine if Site Health module is supported or not.
*
* @package performance-lab
*/

return static function () {
return new WP_Error( 'module_not_loaded', esc_html__( 'The module cannot be loaded with Performance Lab since it is disabled.', 'performance-lab' ) );
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
};
10 changes: 10 additions & 0 deletions tests/testdata/demo-modules/check-error/demo-module-4/load.php
@@ -0,0 +1,10 @@
<?php
/**
* Module Name: Demo Module 4
* Description: This is the description for demo module 4.
* Experimental: No
*
* @package performance-lab
*/

// This is a demo module and does nothing.