Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
felixarntz Jul 8, 2022
e54b7d7
Add test case for file_exists check in perflab_is_valid_module().
felixarntz 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
31 changes: 20 additions & 11 deletions admin/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,29 +126,38 @@ function perflab_render_modules_page() {
* @param array $module_settings Associative array of the module's current settings.
*/
function perflab_render_modules_page_field( $module_slug, $module_data, $module_settings ) {
$base_id = sprintf( 'module_%s', $module_slug );
$base_name = sprintf( '%1$s[%2$s]', PERFLAB_MODULES_SETTING, $module_slug );
$enabled = isset( $module_settings['enabled'] ) && $module_settings['enabled'];
$base_id = sprintf( 'module_%s', $module_slug );
$base_name = sprintf( '%1$s[%2$s]', PERFLAB_MODULES_SETTING, $module_slug );
$enabled = isset( $module_settings['enabled'] ) && $module_settings['enabled'];
$can_load_module = perflab_can_load_module( $module_slug );
?>
<fieldset>
<legend class="screen-reader-text">
<?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 ); ?>>
<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 echo ( ! $can_load_module ? ' readonly onclick="return false;"' : '' ); ?>>
<?php
if ( $module_data['experimental'] ) {
if ( ! $can_load_module ) {
printf(
/* translators: %s: module name */
__( 'Enable %s <strong>(experimental)</strong>', 'performance-lab' ),
__( '%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'] )
);
} else {
printf(
/* translators: %s: module name */
__( 'Enable %s', 'performance-lab' ),
esc_html( $module_data['name'] )
);
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'] )
);
}
}
?>
</label>
Expand Down
59 changes: 52 additions & 7 deletions load.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,25 @@ function( $module_settings ) {
*/
$modules = apply_filters( 'perflab_active_modules', $modules );

return $modules;
$active_modules = array();
foreach ( $modules as $module ) {

// Do not load module if it marge in WordPress.
$can_load_module = perflab_can_load_module( $module );
if ( ! $can_load_module ) {
continue;
}

// 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;
}

$active_modules[] = $module;
}

return $active_modules;
}

/**
Expand Down Expand Up @@ -168,6 +186,38 @@ function perflab_render_generator() {
}
add_action( 'wp_head', 'perflab_render_generator' );

/**
* Don't load the performance modules if the core version of the module is available.
*
* @since n.e.x.t
*
* @param string $module The name of the module.
* @return bool Whether to load module or not.
*/
function perflab_can_load_module( $module ) {
$module_load_file = plugin_dir_path( __FILE__ ) . 'modules/' . $module . '/can-load.php';

// If the load file does not exist, the feature is not in core and the module can be loaded.
if ( ! file_exists( $module_load_file ) ) {
return true;
}

// Require the file to include the add_filter function.
require_once $module_load_file;

/**
* Filters whether the module can load or not.
*
* You can set this to false in order to disable the module.
*
* @since n.e.x.t
*
* @param bool $can_load_module Whether to load module. default true.
* @param string $module The name of the module.
*/
return apply_filters( 'perflab_can_load_module', true, $module );
}

/**
* Loads the active performance modules.
*
Expand All @@ -181,13 +231,8 @@ function perflab_load_active_modules() {
}

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;
}

require_once $module_file;
require_once plugin_dir_path( __FILE__ ) . 'modules/' . $module . '/load.php';
}
}

Expand Down
30 changes: 30 additions & 0 deletions modules/images/webp-uploads/can-load.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Can load function to determine if WebP Uploads module already marge in WordPress core.
*
* @since n.e.x.t
* @package performance-lab
*/

/**
* Filters whether the module can load or not.
*
* @since n.e.x.t
*
* @param bool $can_load Whether to load module. default true.
* @param string $module The name of the module.
* @return bool Whether to load module or not.
*/
function perflab_check_webp_uploads_core_functions( $can_load, $module ) {

if ( 'images/webp-uploads' !== $module ) {
return $can_load;
}

if ( function_exists( 'wp_image_use_alternate_mime_types' ) ) {
return false;
}

return true;
}
add_filter( 'perflab_can_load_module', 'perflab_check_webp_uploads_core_functions', 10, 2 );
34 changes: 31 additions & 3 deletions tests/load-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,53 @@ function( $module_settings ) {
);
$this->assertSame( $expected_active_modules, $active_modules );

// Assert that option updates affect the active modules correctly.
// Assert that dummy module option doesn't show in active modules.
$new_value = array(
'inactive-module' => array( 'enabled' => false ),
'active-module' => array( 'enabled' => true ),
);
update_option( PERFLAB_MODULES_SETTING, $new_value );
$active_modules = perflab_get_active_modules();
$this->assertSame( array( 'active-module' ), $active_modules );
$this->assertSame( array(), $active_modules );

// Assert that it only allow existing modules.
$new_value = array(
'inactive-module' => array( 'enabled' => false ),
'images/webp-uploads' => array( 'enabled' => true ),
'object-cache/persistent-object-cache-health-check' => array( 'enabled' => true ),
);
update_option( PERFLAB_MODULES_SETTING, $new_value );
$active_modules = perflab_get_active_modules();
$this->assertSame( array( 'images/webp-uploads', 'object-cache/persistent-object-cache-health-check' ), $active_modules );
}

public function test_perflab_get_generator_content() {
// Assert that it returns the current version and active modules.
// Assert that it doesn't returns dummy modules.
$dummy_active_modules = array( 'images/a-module', 'object-cache/another-module' );
add_filter(
'perflab_active_modules',
function() use ( $dummy_active_modules ) {
return $dummy_active_modules;
}
);
$expected = 'Performance Lab ' . PERFLAB_VERSION . '; modules: ';
$content = perflab_get_generator_content();
$this->assertSame( $expected, $content );

// Assert that it returns active modules.
$new_value = array(
'inactive-module' => array( 'enabled' => false ),
'images/webp-uploads' => array( 'enabled' => true ),
'object-cache/persistent-object-cache-health-check' => array( 'enabled' => true ),
);
update_option( PERFLAB_MODULES_SETTING, $new_value );
$dummy_active_modules = array( 'images/webp-uploads', 'object-cache/persistent-object-cache-health-check' );
add_filter(
'perflab_active_modules',
function() use ( $dummy_active_modules ) {
return $dummy_active_modules;
}
);
$expected = 'Performance Lab ' . PERFLAB_VERSION . '; modules: ' . implode( ', ', $dummy_active_modules );
$content = perflab_get_generator_content();
$this->assertSame( $expected, $content );
Expand Down