Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Performance Lab Server Timing Object Cache Drop-In
* Plugin URI: https://github.com/WordPress/performance
* Description: Performance Lab drop-in to register Server-Timing metrics early. This is not a real object cache drop-in and will not override other actual object cache drop-ins.
* Version: 2
* Version: 3
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
* License: GPLv2 or later
Expand Down Expand Up @@ -34,7 +34,7 @@

// Set constant to be able to later check for whether this file was loaded.
if ( ! defined( 'PERFLAB_OBJECT_CACHE_DROPIN_VERSION' ) ) {
define( 'PERFLAB_OBJECT_CACHE_DROPIN_VERSION', 2 );
define( 'PERFLAB_OBJECT_CACHE_DROPIN_VERSION', 3 );
}

if ( ! function_exists( 'perflab_load_server_timing_api_from_dropin' ) ) {
Expand All @@ -58,17 +58,17 @@ function perflab_load_server_timing_api_from_dropin() {

$plugins_dir = defined( 'WP_PLUGIN_DIR' ) ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins';
$plugin_dir = $plugins_dir . '/performance-lab/';
if ( ! file_exists( $plugin_dir . 'server-timing/load.php' ) ) {
if ( ! file_exists( $plugin_dir . 'includes/server-timing/load.php' ) ) {
$plugin_dir = $plugins_dir . '/performance/';
if ( ! file_exists( $plugin_dir . 'server-timing/load.php' ) ) {
if ( ! file_exists( $plugin_dir . 'includes/server-timing/load.php' ) ) {
return;
}
}

require_once $plugin_dir . 'server-timing/class-perflab-server-timing-metric.php';
require_once $plugin_dir . 'server-timing/class-perflab-server-timing.php';
require_once $plugin_dir . 'server-timing/load.php';
require_once $plugin_dir . 'server-timing/defaults.php';
require_once $plugin_dir . 'includes/server-timing/class-perflab-server-timing-metric.php';
require_once $plugin_dir . 'includes/server-timing/class-perflab-server-timing.php';
require_once $plugin_dir . 'includes/server-timing/load.php';
require_once $plugin_dir . 'includes/server-timing/defaults.php';
Comment on lines -61 to +71

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thelovekesh Now that I see this change, moving the files for the Server-Timing API is tricky from a backward compatibility perspective. Here's why:

  • The wp-content/object-cache.php drop-in may already be placed on some sites, from an older Performance Lab version. As such, it would try to load the old paths, which wouldn't work. It wouldn't cause a fatal error because of the checks above, but it just wouldn't load the API.
  • In order to fix that, we'll have to enhance the logic that places that drop-in file (i.e. copies from object-cache.copy.php to the actual drop-in) so that it detects whether the current file is outdated.
  • I'm happy to open another PR for that. However, at a minimum, let's update the drop-in version number in this PR from 2 to 3 (in both the file header and constant).

@felixarntz felixarntz Mar 11, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thelovekesh I've opened #1047 for the purpose of updating an older version of the drop-in, please take a look. Depending which of the two PRs gets merged first, the other one will require a minor update (to sync the version numbers and file paths).

}
}
perflab_load_server_timing_api_from_dropin();
Expand Down
23 changes: 23 additions & 0 deletions includes/site-health/load.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Site Health checks loader.
*
* @package performance-lab
* @since n.e.x.t
*/

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

// Audit Autoloaded Options site health check.
require_once __DIR__ . '/audit-autoloaded-options/helper.php';
require_once __DIR__ . '/audit-autoloaded-options/hooks.php';

// Audit Enqueued Assets site health check.
require_once __DIR__ . '/audit-enqueued-assets/helper.php';
require_once __DIR__ . '/audit-enqueued-assets/hooks.php';

// WebP Support site health check.
require_once __DIR__ . '/webp-support/helper.php';
require_once __DIR__ . '/webp-support/hooks.php';
31 changes: 10 additions & 21 deletions load.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@
define( 'PERFLAB_OBJECT_CACHE_DROPIN_VERSION', false );
}

require_once PERFLAB_PLUGIN_DIR_PATH . 'server-timing/class-perflab-server-timing-metric.php';
require_once PERFLAB_PLUGIN_DIR_PATH . 'server-timing/class-perflab-server-timing.php';
require_once PERFLAB_PLUGIN_DIR_PATH . 'server-timing/load.php';
require_once PERFLAB_PLUGIN_DIR_PATH . 'server-timing/defaults.php';
// Load server-timing API.
require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/server-timing/class-perflab-server-timing-metric.php';
require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/server-timing/class-perflab-server-timing.php';
require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/server-timing/load.php';
require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/server-timing/defaults.php';

// Load site health checks.
require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/site-health/load.php';

/**
* Registers the performance modules setting.
Expand Down Expand Up @@ -122,22 +126,7 @@ function perflab_get_module_settings() {
// Even though a default value is registered for this setting, the default must be explicitly
// passed here, to support scenarios where this function is called before the 'init' action,
// for example when loading the active modules.
$module_settings = (array) get_option( PERFLAB_MODULES_SETTING, perflab_get_modules_setting_default() );

$legacy_module_slugs = array(
'site-health/audit-autoloaded-options' => 'database/audit-autoloaded-options',
'site-health/audit-enqueued-assets' => 'js-and-css/audit-enqueued-assets',
'site-health/webp-support' => 'images/webp-support',
);

foreach ( $legacy_module_slugs as $legacy_slug => $current_slug ) {
if ( isset( $module_settings[ $legacy_slug ] ) ) {
$module_settings[ $current_slug ] = $module_settings[ $legacy_slug ];
unset( $module_settings[ $legacy_slug ] );
}
}

return $module_settings;
return (array) get_option( PERFLAB_MODULES_SETTING, perflab_get_modules_setting_default() );
}

/**
Expand Down Expand Up @@ -424,7 +413,7 @@ function perflab_maybe_set_object_cache_dropin() {
return;
}

$wp_filesystem->copy( PERFLAB_PLUGIN_DIR_PATH . 'server-timing/object-cache.copy.php', $dropin_path );
$wp_filesystem->copy( PERFLAB_PLUGIN_DIR_PATH . 'includes/server-timing/object-cache.copy.php', $dropin_path );
}

// Set timeout of 1 hour before retrying again (only relevant in case the above failed).
Expand Down
19 changes: 0 additions & 19 deletions modules/database/audit-autoloaded-options/can-load.php

This file was deleted.

17 changes: 0 additions & 17 deletions modules/database/audit-autoloaded-options/load.php

This file was deleted.

19 changes: 0 additions & 19 deletions modules/images/webp-support/can-load.php

This file was deleted.

17 changes: 0 additions & 17 deletions modules/images/webp-support/load.php

This file was deleted.

19 changes: 0 additions & 19 deletions modules/js-and-css/audit-enqueued-assets/can-load.php

This file was deleted.

17 changes: 0 additions & 17 deletions modules/js-and-css/audit-enqueued-assets/load.php

This file was deleted.

2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</rule>

<rule ref="WordPress.Files.FileName.NotHyphenatedLowercase">
<exclude-pattern>server-timing/object-cache.copy.php</exclude-pattern>
<exclude-pattern>includes/server-timing/object-cache.copy.php</exclude-pattern>
</rule>

<file>.</file>
Expand Down
3 changes: 1 addition & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ parameters:
- default-enabled-modules.php
- load.php
- module-i18n.php
- modules/
- includes/
- plugins/
- server-timing/
- tests/
- uninstall.php
scanDirectories:
Expand Down
86 changes: 2 additions & 84 deletions tests/load-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,36 +97,6 @@ static function ( $current_default, $option, $passed_default ) use ( &$has_passe
$this->assertEqualSetsWithIndex( $new_value, $settings );
}

/**
* @dataProvider data_legacy_modules
*/
public function test_legacy_module_for_perflab_get_module_settings( $legacy_module_slug, $current_module_slug ) {
$new_value = array( $legacy_module_slug => array( 'enabled' => true ) );
update_option( PERFLAB_MODULES_SETTING, $new_value );

$settings = perflab_get_module_settings();
$this->assertArrayNotHasKey( $legacy_module_slug, $settings, 'The settings do not contain the old legacy module slug in the database' );
$this->assertArrayHasKey( $current_module_slug, $settings, 'The settings contain an updated module slug in the database' );
}

/**
* Data provider for test_legacy_module_for_perflab_get_module_settings().
*
* @return array {
* @type array {
* @type string $legacy_module_slug The legacy module slug.
* @type string $current_module_slug The new/updated module slug.
* }
* }
*/
public function data_legacy_modules() {
return array(
array( 'site-health/audit-autoloaded-options', 'database/audit-autoloaded-options' ),
array( 'site-health/audit-enqueued-assets', 'js-and-css/audit-enqueued-assets' ),
array( 'site-health/webp-support', 'images/webp-support' ),
);
}

public function test_perflab_get_active_modules() {
// Assert that by default there are no active modules.
$active_modules = perflab_get_active_modules();
Expand Down Expand Up @@ -181,45 +151,6 @@ public function test_perflab_render_generator() {
$this->assertStringContainsString( $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/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 ),
);
}

/**
* @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/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 ),
);
}

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 All @@ -230,19 +161,6 @@ public function test_perflab_deactivate_module() {
$this->assertSame( 'deactivated', get_option( 'test_demo_module_activation_status' ) );
}

private function get_expected_default_option() {
// This code is essentially copied over from the perflab_register_modules_setting() function.
$default_enabled_modules = require PERFLAB_PLUGIN_DIR_PATH . 'default-enabled-modules.php';
return array_reduce(
$default_enabled_modules,
static function ( $module_settings, $module_dir ) {
$module_settings[ $module_dir ] = array( 'enabled' => true );
return $module_settings;
},
array()
);
}

public function test_perflab_maybe_set_object_cache_dropin_no_conflict() {
global $wp_filesystem;

Expand All @@ -255,7 +173,7 @@ public function test_perflab_maybe_set_object_cache_dropin_no_conflict() {
// Run function to place drop-in and ensure it exists afterwards.
perflab_maybe_set_object_cache_dropin();
$this->assertTrue( $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) );
$this->assertSame( file_get_contents( PERFLAB_PLUGIN_DIR_PATH . 'server-timing/object-cache.copy.php' ), $wp_filesystem->get_contents( WP_CONTENT_DIR . '/object-cache.php' ) );
$this->assertSame( file_get_contents( PERFLAB_PLUGIN_DIR_PATH . 'includes/server-timing/object-cache.copy.php' ), $wp_filesystem->get_contents( WP_CONTENT_DIR . '/object-cache.php' ) );
}

public function test_perflab_maybe_set_object_cache_dropin_no_conflict_but_failing() {
Expand Down Expand Up @@ -331,6 +249,6 @@ static function () {
WP_Filesystem();

// Simulate that the original object-cache.copy.php file exists.
$wp_filesystem->put_contents( PERFLAB_PLUGIN_DIR_PATH . 'server-timing/object-cache.copy.php', file_get_contents( PERFLAB_PLUGIN_DIR_PATH . 'server-timing/object-cache.copy.php' ) );
$wp_filesystem->put_contents( PERFLAB_PLUGIN_DIR_PATH . 'includes/server-timing/object-cache.copy.php', file_get_contents( PERFLAB_PLUGIN_DIR_PATH . 'includes/server-timing/object-cache.copy.php' ) );
Comment thread
felixarntz marked this conversation as resolved.
}
}