Skip to content

Commit

Permalink
Merge pull request #891 from WordPress/update/can-load-infrastructure
Browse files Browse the repository at this point in the history
Allow module `can-load.php` callbacks to return a `WP_Error` with more information
  • Loading branch information
felixarntz committed Dec 1, 2023
2 parents 5d77bb0 + 9c05a55 commit 0e10d5a
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 5 deletions.
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 ( $can_load_module && ! is_wp_error( $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 @@ -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
14 changes: 11 additions & 3 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 $can_load_module && ! is_wp_error( $can_load_module );
}

/**
Expand Down Expand Up @@ -235,9 +236,10 @@ function perflab_render_generator() {
* Checks whether the given module can be loaded in the current environment.
*
* @since 1.3.0
* @since n.e.x.t The function may now alternatively return a WP_Error.
*
* @param string $module Slug of the module.
* @return bool Whether the module can be loaded or not.
* @return bool|WP_Error True if the module can be loaded, or false or a WP_Error with more concrete information otherwise.
*/
function perflab_can_load_module( $module ) {
$module_load_file = PERFLAB_PLUGIN_DIR_PATH . 'modules/' . $module . '/can-load.php';
Expand All @@ -256,7 +258,13 @@ function perflab_can_load_module( $module ) {
}

// Call the closure to determine whether the module can be loaded.
return (bool) $module();
$result = $module();

if ( is_wp_error( $result ) ) {
return $result;
}

return (bool) $result;
}

/**
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( 'cannot_load_module', esc_html__( 'The module cannot be loaded since the Site Health feature is disabled.', 'performance-lab' ) );
} 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( 'cannot_load_module', esc_html__( 'The module cannot be loaded since the Site Health feature is disabled.', 'performance-lab' ) );
} 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( 'cannot_load_module', esc_html__( 'The module cannot be loaded since the Site Health feature is disabled.', 'performance-lab' ) );
} 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( 'cannot_load_module', $can_load_module->get_error_code() );
}

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( 'cannot_load_module', esc_html__( 'The module cannot be loaded.', 'performance-lab' ) );
};
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.

0 comments on commit 0e10d5a

Please sign in to comment.