-
Notifications
You must be signed in to change notification settings - Fork 235
Feature/8103 add settings changes detection #8140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Miraeld
wants to merge
11
commits into
feature/ri-recommendations
Choose a base branch
from
feature/8103-add-settings-changes-detection
base: feature/ri-recommendations
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+520
−49
Open
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4a005db
Add SettingsSubscriber for recommendations update on settings change
Miraeld fb6e64d
Register SettingsSubscriber in ServiceProvider and Plugin
Miraeld a0660a4
Add unit tests for SettingsSubscriber and DataManager
Miraeld d6dec2c
Add integration tests for SettingsSubscriber
Miraeld 4a2cfb9
fix feedback
Miraeld c0edafa
fix tests
Miraeld 5ac81d8
tests
Miraeld 45be0b3
fix stan
Miraeld b1086d5
Use standard WordPress hook and unified options list
Miraeld 40042fa
Update tests to use update_option_wp_rocket_settings hook
Miraeld b5547fe
Remove duplicate DataManager methods that belong to Task 2.1
Miraeld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
inc/Engine/Admin/RocketInsights/Recommendations/SettingsSubscriber.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| namespace WP_Rocket\Engine\Admin\RocketInsights\Recommendations; | ||
|
|
||
| use WP_Rocket\Event_Management\Subscriber_Interface; | ||
| use WP_Rocket\Logger\LoggerAware; | ||
| use WP_Rocket\Logger\LoggerAwareInterface; | ||
|
|
||
| /** | ||
| * Recommendations Settings Subscriber. | ||
| * | ||
| * Detects WP Rocket settings changes and triggers recommendation updates. | ||
| */ | ||
| class SettingsSubscriber implements Subscriber_Interface, LoggerAwareInterface { | ||
| use LoggerAware; | ||
|
|
||
| /** | ||
| * Recommendations data manager instance. | ||
| * | ||
| * @var DataManager | ||
| */ | ||
| private $data_manager; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
| * @param DataManager $data_manager Data manager instance. | ||
| */ | ||
| public function __construct( DataManager $data_manager ) { | ||
| $this->data_manager = $data_manager; | ||
| } | ||
|
|
||
| /** | ||
| * Return an array of events that this subscriber wants to listen to. | ||
| * | ||
| * @return array | ||
| */ | ||
| public static function get_subscribed_events(): array { | ||
| return [ | ||
| 'rocket_after_save_options' => [ 'maybe_fetch_after_settings_change', 10, 2 ], | ||
| ]; | ||
Miraeld marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Maybe fetch recommendations after settings change. | ||
| * | ||
| * Only fetches if: | ||
| * - Status is completed or failed | ||
| * - Changed settings affect recommendations | ||
| * - Metrics hash would change | ||
| * | ||
| * @param array $old_options Previous settings. | ||
| * @param array $new_options New settings. | ||
| * @return void | ||
| */ | ||
| public function maybe_fetch_after_settings_change( array $old_options, array $new_options ): void { | ||
| // Check current status. | ||
| $status = $this->data_manager->get_status(); | ||
|
|
||
| // Only proceed if recommendations exist. | ||
| if ( ! in_array( $status, [ 'completed', 'failed' ], true ) ) { | ||
| $this->logger::debug( | ||
| 'Recommendations: Settings changed but status not ready', | ||
| [ 'status' => $status ] | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| // Check if relevant settings changed. | ||
| if ( ! $this->has_relevant_changes( $old_options, $new_options ) ) { | ||
| $this->logger::debug( 'Recommendations: Settings changed but none affect recommendations' ); | ||
| return; | ||
| } | ||
|
|
||
| // Check if hash would change (prevents redundant fetch). | ||
| if ( ! $this->data_manager->should_fetch_recommendations() ) { | ||
| $this->logger::debug( 'Recommendations: Settings changed but hash unchanged' ); | ||
| return; | ||
| } | ||
|
|
||
| $this->logger::info( 'Recommendations: Relevant settings changed, fetching new recommendations' ); | ||
|
|
||
| // Fetch new recommendations. | ||
| $this->data_manager->fetch_recommendations(); | ||
| } | ||
|
|
||
| /** | ||
| * Check if changed settings affect recommendations. | ||
| * | ||
| * @param array $old_options Previous settings. | ||
| * @param array $new_options New settings. | ||
| * @return bool True if relevant changes detected. | ||
| */ | ||
| private function has_relevant_changes( array $old_options, array $new_options ): bool { | ||
| // Get list of recommendation-related options. | ||
| $relevant_keys = [ | ||
Miraeld marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 'minify_css', | ||
| 'minify_js', | ||
| 'minify_concatenate_css', | ||
| 'minify_concatenate_js', | ||
| 'defer_all_js', | ||
| 'delay_js', | ||
| 'async_css', | ||
| 'critical_css', | ||
| 'remove_unused_css', | ||
| 'lazyload', | ||
| 'lazyload_iframes', | ||
| 'lazyload_youtube', | ||
| 'image_dimensions', | ||
| 'cdn', | ||
| 'do_caching_mobile_files', | ||
| 'cache_logged_user', | ||
| 'cache_webp', | ||
| 'manual_preload', | ||
| 'sitemap_preload', | ||
| 'control_heartbeat', | ||
| 'minify_google_fonts', | ||
| ]; | ||
|
|
||
| // Check if any relevant setting changed. | ||
| foreach ( $relevant_keys as $key ) { | ||
| $old_value = $old_options[ $key ] ?? false; | ||
| $new_value = $new_options[ $key ] ?? false; | ||
|
|
||
| if ( $old_value !== $new_value ) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...es/inc/Engine/Admin/RocketInsights/Recommendations/DataManager/HasRequiredMetricsTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| <?php | ||
|
|
||
| return [ | ||
| 'test_data' => [ | ||
| 'shouldReturnTrueWhenAllMetricsExist' => [ | ||
| 'config' => [ | ||
| 'global_score_data' => [ | ||
| 'score' => 75, | ||
| 'average_metrics' => [ | ||
| 'lcp' => 2.5, | ||
| 'ttfb' => 0.8, | ||
| 'cls' => 0.1, | ||
| 'tbt' => 200, | ||
| ], | ||
| ], | ||
| ], | ||
| 'expected' => [ | ||
| 'has_metrics' => true, | ||
| ], | ||
| ], | ||
|
|
||
| 'shouldReturnFalseWhenAverageMetricsNull' => [ | ||
| 'config' => [ | ||
| 'global_score_data' => [ | ||
| 'score' => 75, | ||
| ], | ||
| ], | ||
| 'expected' => [ | ||
| 'has_metrics' => false, | ||
| ], | ||
| ], | ||
|
|
||
| 'shouldReturnFalseWhenAverageMetricsEmpty' => [ | ||
| 'config' => [ | ||
| 'global_score_data' => [ | ||
| 'score' => 75, | ||
| 'average_metrics' => [], | ||
| ], | ||
| ], | ||
| 'expected' => [ | ||
| 'has_metrics' => false, | ||
| ], | ||
| ], | ||
|
|
||
| 'shouldReturnFalseWhenMissingLCP' => [ | ||
| 'config' => [ | ||
| 'global_score_data' => [ | ||
| 'score' => 75, | ||
| 'average_metrics' => [ | ||
| 'ttfb' => 0.8, | ||
| 'cls' => 0.1, | ||
| 'tbt' => 200, | ||
| ], | ||
| ], | ||
| ], | ||
| 'expected' => [ | ||
| 'has_metrics' => false, | ||
| ], | ||
| ], | ||
|
|
||
| 'shouldReturnFalseWhenMissingTTFB' => [ | ||
| 'config' => [ | ||
| 'global_score_data' => [ | ||
| 'score' => 75, | ||
| 'average_metrics' => [ | ||
| 'lcp' => 2.5, | ||
| 'cls' => 0.1, | ||
| 'tbt' => 200, | ||
| ], | ||
| ], | ||
| ], | ||
| 'expected' => [ | ||
| 'has_metrics' => false, | ||
| ], | ||
| ], | ||
|
|
||
| 'shouldReturnFalseWhenMissingCLS' => [ | ||
| 'config' => [ | ||
| 'global_score_data' => [ | ||
| 'score' => 75, | ||
| 'average_metrics' => [ | ||
| 'lcp' => 2.5, | ||
| 'ttfb' => 0.8, | ||
| 'tbt' => 200, | ||
| ], | ||
| ], | ||
| ], | ||
| 'expected' => [ | ||
| 'has_metrics' => false, | ||
| ], | ||
| ], | ||
|
|
||
| 'shouldReturnFalseWhenMissingTBT' => [ | ||
| 'config' => [ | ||
| 'global_score_data' => [ | ||
| 'score' => 75, | ||
| 'average_metrics' => [ | ||
| 'lcp' => 2.5, | ||
| 'ttfb' => 0.8, | ||
| 'cls' => 0.1, | ||
| ], | ||
| ], | ||
| ], | ||
| 'expected' => [ | ||
| 'has_metrics' => false, | ||
| ], | ||
| ], | ||
| ], | ||
| ]; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.