Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c3170fe
Added: Mechanism to not load module if core version is available
mukeshpanchal27 Jun 22, 2022
6219d96
Added: New line
mukeshpanchal27 Jun 22, 2022
42a82fc
Fix: Code linting
mukeshpanchal27 Jun 22, 2022
5294f83
Fix: Docblock for functions
mukeshpanchal27 Jun 22, 2022
eef08c5
Update some tests
mukeshpanchal27 Jun 22, 2022
1ee0028
Fix: PHP lint
mukeshpanchal27 Jun 22, 2022
8750d5a
Simplify boolean check for function existence.
felixarntz Jun 22, 2022
f06ba7d
Fix: Text related changes
mukeshpanchal27 Jun 23, 2022
073d872
Update: logic for perflab_can_load_module
mukeshpanchal27 Jun 24, 2022
a0235c8
Fix: Feedback related to disabled checkbox
mukeshpanchal27 Jun 29, 2022
ca1cd0f
Fix: Lint white space issue
mukeshpanchal27 Jun 29, 2022
3f6244d
Perflab_get_active_and_valid_modules() is introduced, and review is i…
mukeshpanchal27 Jun 29, 2022
7c5365c
Fix: Admin settings review
mukeshpanchal27 Jun 30, 2022
7dda6e8
Fix: review of the new function
mukeshpanchal27 Jun 30, 2022
41b2834
Fix: review of tests
mukeshpanchal27 Jun 30, 2022
4d1e220
Added: Unit test for new functions
mukeshpanchal27 Jun 30, 2022
2371a46
Fix: Lint
mukeshpanchal27 Jun 30, 2022
e432764
Unit test for perflab_can_load_module()
mukeshpanchal27 Jul 1, 2022
acd3ed5
Fix: Lint
mukeshpanchal27 Jul 1, 2022
110656c
Feedback implemented
mukeshpanchal27 Jul 4, 2022
d403b7a
Update unit tests in response to the review
mukeshpanchal27 Jul 4, 2022
232d462
Correct lint
mukeshpanchal27 Jul 4, 2022
3388874
Update function name for test
mukeshpanchal27 Jul 4, 2022
a156b1a
Address code review feedback
mukeshpanchal27 Jul 7, 2022
ac6163a
Fix minor lint issue
mukeshpanchal27 Jul 7, 2022
b759790
Address review feedback
mukeshpanchal27 Jul 8, 2022
09c507f
Fix: Minor lind error
mukeshpanchal27 Jul 8, 2022
88eca52
Merge branch 'trunk' into fix/293-mechanism-not-load-module
Jul 8, 2022
e54b7d7
Add test case for file_exists check in perflab_is_valid_module().
Jul 8, 2022
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
44 changes: 28 additions & 16 deletions admin/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,34 @@ 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" ); ?>">
<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'] ) {
printf(
/* translators: %s: module name */
__( 'Enable %s <strong>(experimental)</strong>', 'performance-lab' ),
esc_html( $module_data['name'] )
);
} else {
printf(
/* translators: %s: module name */
__( 'Enable %s', 'performance-lab' ),
esc_html( $module_data['name'] )
);
}
?>
<?php if ( perflab_can_load_module( $module_slug ) ) { ?>
<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'] ) {
printf(
/* translators: %s: module name */
__( 'Enable %s <strong>(experimental)</strong>', 'performance-lab' ),
esc_html( $module_data['name'] )
);
} else {
printf(
/* translators: %s: module name */
__( 'Enable %s', 'performance-lab' ),
esc_html( $module_data['name'] )
);
}
?>
<?php } else { ?>
<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
printf(
/* translators: %s: module name */
__( '%s is already part of your WordPress version and therefore cannot be loaded as part of the plugin.', 'performance-lab' ),
esc_html( $module_data['name'] )
);
?>
<?php } ?>
</label>
<p id="<?php echo esc_attr( "{$base_id}_description" ); ?>" class="description">
<?php echo esc_html( $module_data['description'] ); ?>
Expand Down
76 changes: 60 additions & 16 deletions load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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 )
);
}

Expand All @@ -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 ) {
Comment thread
felixarntz marked this conversation as resolved.
$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
Comment thread
mukeshpanchal27 marked this conversation as resolved.
* @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() ) {
Expand Down
11 changes: 11 additions & 0 deletions modules/images/webp-uploads/can-load.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* Can load function to determine if WebP Uploads module already marge in WordPress core.
*
* @since n.e.x.t
* @package performance-lab
*/

return function() {
return ! function_exists( 'wp_image_use_alternate_mime_types' );
};
45 changes: 40 additions & 5 deletions tests/load-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,18 @@ function( $module_settings ) {

public function test_perflab_get_generator_content() {
// Assert that it returns the current version and active modules.
$dummy_active_modules = array( 'images/a-module', 'object-cache/another-module' );
// For this test, set the active modules to all defaults but the last one.
$active_modules = require plugin_dir_path( PERFLAB_MAIN_FILE ) . 'default-enabled-modules.php';
array_pop( $active_modules );
add_filter(
'perflab_active_modules',
function() use ( $dummy_active_modules ) {
return $dummy_active_modules;
function() use ( $active_modules ) {
return $active_modules;
}
);
$expected = 'Performance Lab ' . PERFLAB_VERSION . '; modules: ' . implode( ', ', $dummy_active_modules );
$content = perflab_get_generator_content();
$active_modules = array_filter( perflab_get_active_modules(), 'perflab_is_valid_module' );
$expected = 'Performance Lab ' . PERFLAB_VERSION . '; modules: ' . implode( ', ', $active_modules );
$content = perflab_get_generator_content();
$this->assertSame( $expected, $content );
}

Expand All @@ -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 ),
Comment thread
felixarntz marked this conversation as resolved.
);
}

/**
* @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';
Expand Down
9 changes: 9 additions & 0 deletions tests/testdata/demo-modules/images/demo-module-3/can-load.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
/**
* Can load function to determine if module already marge in WordPress core.
*
* @since n.e.x.t
* @package performance-lab
*/

return '__return_true';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
/**
* Can load function to determine if module already marge in WordPress core.
*
* @since n.e.x.t
* @package performance-lab
*/

return '__return_false';