From 4857a5d14cb73d3b39791b90519859a0462b99d8 Mon Sep 17 00:00:00 2001 From: Adnan Haque Date: Sun, 20 Oct 2024 18:49:21 +0300 Subject: [PATCH] Implement partial regeneration --- .../boost/app/lib/critical-css/Regenerate.php | 2 +- .../source-providers/Source_Providers.php | 6 +++--- .../optimizations/cloud-css/Cloud_CSS.php | 16 ++++++++++------ .../cloud-css/Cloud_CSS_Followup.php | 2 +- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/projects/plugins/boost/app/lib/critical-css/Regenerate.php b/projects/plugins/boost/app/lib/critical-css/Regenerate.php index 6b25fdf434c3c..cab4b7fc805ec 100644 --- a/projects/plugins/boost/app/lib/critical-css/Regenerate.php +++ b/projects/plugins/boost/app/lib/critical-css/Regenerate.php @@ -35,7 +35,7 @@ public function start() { // If this is a cloud CSS request, we need to trigger the generation // of the CSS and return the URL to the CSS file. $cloud_css = new Cloud_CSS(); - $cloud_css->regenerate_cloud_css( Cloud_CSS::REGENERATE_REASON_USER_REQUEST ); + $cloud_css->regenerate_cloud_css( Cloud_CSS::REGENERATE_REASON_USER_REQUEST, $cloud_css->get_all_providers() ); Cloud_CSS_Followup::schedule(); } diff --git a/projects/plugins/boost/app/lib/critical-css/source-providers/Source_Providers.php b/projects/plugins/boost/app/lib/critical-css/source-providers/Source_Providers.php index e64b272a7a170..c3f342c97cae9 100644 --- a/projects/plugins/boost/app/lib/critical-css/source-providers/Source_Providers.php +++ b/projects/plugins/boost/app/lib/critical-css/source-providers/Source_Providers.php @@ -113,10 +113,10 @@ public function get_current_request_css() { * * @return array */ - public function get_provider_sources() { + public function get_provider_sources( $context_posts = array() ) { $sources = array(); - $wp_core_provider_urls = WP_Core_Provider::get_critical_source_urls(); + $wp_core_provider_urls = WP_Core_Provider::get_critical_source_urls( $context_posts ); $flat_wp_core_urls = array(); foreach ( $wp_core_provider_urls as $urls ) { $flat_wp_core_urls = array_merge( $flat_wp_core_urls, $urls ); @@ -127,7 +127,7 @@ public function get_provider_sources() { // For each provider, // Gather a list of URLs that are going to be used as Critical CSS source. - foreach ( $provider::get_critical_source_urls() as $group => $urls ) { + foreach ( $provider::get_critical_source_urls( $context_posts ) as $group => $urls ) { if ( empty( $urls ) ) { continue; } diff --git a/projects/plugins/boost/app/modules/optimizations/cloud-css/Cloud_CSS.php b/projects/plugins/boost/app/modules/optimizations/cloud-css/Cloud_CSS.php index 8664505d583cf..64db2088cc0c4 100644 --- a/projects/plugins/boost/app/modules/optimizations/cloud-css/Cloud_CSS.php +++ b/projects/plugins/boost/app/modules/optimizations/cloud-css/Cloud_CSS.php @@ -155,11 +155,11 @@ public function handle_save_post( $post_id, $post ) { // phpcs:ignore VariableAn return; } - $this->regenerate_cloud_css( self::REGENERATE_REASON_SAVE_POST ); + $this->regenerate_cloud_css( self::REGENERATE_REASON_SAVE_POST, $this->get_all_providers( array( $post ) ) ); } - public function regenerate_cloud_css( $reason ) { - $result = $this->generate_cloud_css( $reason, $this->get_existing_sources() ); + public function regenerate_cloud_css( $reason, $providers ) { + $result = $this->generate_cloud_css( $reason, $providers ); if ( is_wp_error( $result ) ) { $state = new Critical_CSS_State(); $state->set_error( $result->get_error_message() )->save(); @@ -171,18 +171,22 @@ public function regenerate_cloud_css( $reason ) { * Called when stored Critical CSS has been invalidated. Triggers a new Cloud CSS request. */ public function handle_critical_css_invalidated() { - $this->regenerate_cloud_css( self::REGENERATE_REASON_INVALIDATED ); + $this->regenerate_cloud_css( self::REGENERATE_REASON_INVALIDATED, $this->get_all_providers() ); Cloud_CSS_Followup::schedule(); } + public function get_all_providers( $context_posts = array() ) { + $source_providers = new Source_Providers(); + return $source_providers->get_provider_sources( $context_posts ); + } + public function get_existing_sources() { $state = new Critical_CSS_State(); $data = $state->get(); if ( ! empty( $data['providers'] ) ) { $providers = $data['providers']; } else { - $source_providers = new Source_Providers(); - $providers = $source_providers->get_provider_sources(); + $providers = $this->get_all_providers(); } return $providers; diff --git a/projects/plugins/boost/app/modules/optimizations/cloud-css/Cloud_CSS_Followup.php b/projects/plugins/boost/app/modules/optimizations/cloud-css/Cloud_CSS_Followup.php index dd97fe0985a8e..197c5ec597a9f 100644 --- a/projects/plugins/boost/app/modules/optimizations/cloud-css/Cloud_CSS_Followup.php +++ b/projects/plugins/boost/app/modules/optimizations/cloud-css/Cloud_CSS_Followup.php @@ -29,7 +29,7 @@ public static function run() { $state = new Critical_CSS_State(); if ( $state->has_errors() ) { $cloud_css = new Cloud_CSS(); - $cloud_css->regenerate_cloud_css( Cloud_CSS::REGENERATE_REASON_FOLLOWUP ); + $cloud_css->regenerate_cloud_css( Cloud_CSS::REGENERATE_REASON_FOLLOWUP, $cloud_css->get_existing_sources() ); } }