diff --git a/admin/load.php b/admin/load.php index 0787788317..2b52ff68bc 100644 --- a/admin/load.php +++ b/admin/load.php @@ -135,22 +135,34 @@ function perflab_render_modules_page_field( $module_slug, $module_data, $module_

" class="description"> diff --git a/load.php b/load.php index 83d1146846..10ba39313a 100644 --- a/load.php +++ b/load.php @@ -137,6 +137,30 @@ function( $module_settings ) { return $modules; } +/** + * Gets the active and valid performance modules. + * + * @since n.e.x.t + * + * @param string $module Slug of the module. + * @return bool True if the module is active and valid, otherwise false. + */ +function perflab_is_valid_module( $module ) { + + if ( empty( $module ) ) { + return false; + } + + // Do not load module if no longer exists. + $module_file = plugin_dir_path( __FILE__ ) . 'modules/' . $module . '/load.php'; + if ( ! file_exists( $module_file ) ) { + return false; + } + + // 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 ); +} + /** * Gets the content attribute for the generator tag for the Performance Lab plugin. * @@ -145,12 +169,12 @@ function( $module_settings ) { * @since 1.1.0 */ function perflab_get_generator_content() { - $active_modules = perflab_get_active_modules(); + $active_and_valid_modules = array_filter( perflab_get_active_modules(), 'perflab_is_valid_module' ); return sprintf( 'Performance Lab %1$s; modules: %2$s', PERFLAB_VERSION, - implode( ', ', $active_modules ) + implode( ', ', $active_and_valid_modules ) ); } @@ -169,29 +193,49 @@ function perflab_render_generator() { add_action( 'wp_head', 'perflab_render_generator' ); /** - * Loads the active performance modules. + * Checks whether the given module can be loaded in the current environment. * - * @since 1.0.0 + * @since n.e.x.t + * + * @param string $module Slug of the module. + * @return bool Whether the module can be loaded or not. */ -function perflab_load_active_modules() { - $active_modules = perflab_get_active_modules(); +function perflab_can_load_module( $module ) { + $module_load_file = plugin_dir_path( __FILE__ ) . 'modules/' . $module . '/can-load.php'; + + // If the `can-load.php` file does not exist, assume the module can be loaded. + if ( ! file_exists( $module_load_file ) ) { + return true; + } - if ( empty( $active_modules ) ) { - return; + // Require the file to get the closure for whether the module can load. + $module = require $module_load_file; + + // If the `can-load.php` file is invalid and does not return a closure, assume the module can be loaded. + if ( ! is_callable( $module ) ) { + return true; } - foreach ( $active_modules as $module ) { - // Do not load module if it no longer exists. - $module_file = plugin_dir_path( __FILE__ ) . 'modules/' . $module . '/load.php'; - if ( ! file_exists( $module_file ) ) { - continue; - } + // Call the closure to determine whether the module can be loaded. + return (bool) $module(); +} + +/** + * Loads the active and valid performance modules. + * + * @since 1.0.0 + * @since n.e.x.t Renamed to perflab_load_active_and_valid_modules(). + */ +function perflab_load_active_and_valid_modules() { + $active_and_valid_modules = array_filter( perflab_get_active_modules(), 'perflab_is_valid_module' ); + + foreach ( $active_and_valid_modules as $module ) { - require_once $module_file; + require_once plugin_dir_path( __FILE__ ) . 'modules/' . $module . '/load.php'; } } -perflab_load_active_modules(); +perflab_load_active_and_valid_modules(); // Only load admin integration when in admin. if ( is_admin() ) { diff --git a/modules/images/webp-uploads/can-load.php b/modules/images/webp-uploads/can-load.php new file mode 100644 index 0000000000..5634b2c354 --- /dev/null +++ b/modules/images/webp-uploads/can-load.php @@ -0,0 +1,11 @@ +assertSame( $expected, $content ); } @@ -148,6 +151,38 @@ public function test_perflab_render_generator() { $this->assertContains( $expected, $output ); } + /** + * @dataProvider data_perflab_can_load_module + */ + public function test_perflab_is_valid_module( $dummy_module, $expected_status ) { + $this->assertSame( $expected_status, perflab_is_valid_module( $dummy_module ) ); + } + + public function data_perflab_is_valid_module() { + return array( + array( '', false ), + array( '../tests/testdata/demo-modules/something/non-existing-module', false ), + array( '../tests/testdata/demo-modules/javascript/demo-module-1', false ), + array( '../tests/testdata/demo-modules/something/demo-module-2', true ), + array( '../tests/testdata/demo-modules/images/demo-module-3', true ), + ); + } + + /** + * @dataProvider data_perflab_can_load_module + */ + public function test_perflab_can_load_module( $dummy_module, $expected_status ) { + $this->assertSame( $expected_status, perflab_can_load_module( $dummy_module ) ); + } + + public function data_perflab_can_load_module() { + return array( + array( '../tests/testdata/demo-modules/javascript/demo-module-1', false ), + array( '../tests/testdata/demo-modules/something/demo-module-2', true ), + array( '../tests/testdata/demo-modules/images/demo-module-3', true ), + ); + } + private function get_expected_default_option() { // This code is essentially copied over from the perflab_register_modules_setting() function. $default_enabled_modules = require plugin_dir_path( PERFLAB_MAIN_FILE ) . 'default-enabled-modules.php'; diff --git a/tests/testdata/demo-modules/images/demo-module-3/can-load.php b/tests/testdata/demo-modules/images/demo-module-3/can-load.php new file mode 100644 index 0000000000..a8aabaf890 --- /dev/null +++ b/tests/testdata/demo-modules/images/demo-module-3/can-load.php @@ -0,0 +1,9 @@ +