From 54d732ae5b15e973f1ca0299ed10c1fa1d026039 Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Fri, 17 Jan 2025 10:39:47 +0200 Subject: [PATCH] Boost: Reduce unnecessary CSS regenerations (#40891) --- .../providers/Singular_Post_Provider.php | 4 +-- .../providers/Taxonomy_Provider.php | 4 +-- .../optimizations/cloud-css/Cloud_CSS.php | 28 ++++++++++++++++++- ...e-boost-reduce-unnecessary-css-generations | 4 +++ 4 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 projects/plugins/boost/changelog/update-boost-reduce-unnecessary-css-generations diff --git a/projects/plugins/boost/app/lib/critical-css/source-providers/providers/Singular_Post_Provider.php b/projects/plugins/boost/app/lib/critical-css/source-providers/providers/Singular_Post_Provider.php index f258e15095c2d..ffebe55be2f3e 100644 --- a/projects/plugins/boost/app/lib/critical-css/source-providers/providers/Singular_Post_Provider.php +++ b/projects/plugins/boost/app/lib/critical-css/source-providers/providers/Singular_Post_Provider.php @@ -26,14 +26,14 @@ class Singular_Post_Provider extends Provider { * * @var integer */ - const MAX_URLS = 20; + const MAX_URLS = 10; /** * Minimum number of posts to have Critical CSS generated in order for the whole process to be successful. * * @var integer */ - const MIN_SUCCESS_URLS = 10; + const MIN_SUCCESS_URLS = 5; // phpcs:ignore Generic.Commenting.DocComment.MissingShort /** @inheritdoc */ diff --git a/projects/plugins/boost/app/lib/critical-css/source-providers/providers/Taxonomy_Provider.php b/projects/plugins/boost/app/lib/critical-css/source-providers/providers/Taxonomy_Provider.php index 3aea6dc713e60..b628be3521e03 100644 --- a/projects/plugins/boost/app/lib/critical-css/source-providers/providers/Taxonomy_Provider.php +++ b/projects/plugins/boost/app/lib/critical-css/source-providers/providers/Taxonomy_Provider.php @@ -26,14 +26,14 @@ class Taxonomy_Provider extends Provider { * * @var integer */ - const MAX_URLS = 20; + const MAX_URLS = 10; /** * Minimum number of posts to have Critical CSS generated in order for the whole process to be successful. * * @var integer */ - const MIN_SUCCESS_URLS = 10; + const MIN_SUCCESS_URLS = 5; // phpcs:ignore Generic.Commenting.DocComment.MissingShort /** @inheritdoc */ 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 9a06999349920..c522d344c763d 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 @@ -159,7 +159,13 @@ public function handle_save_post( $post_id, $post ) { // phpcs:ignore VariableAn return; } - $this->regenerate_cloud_css( self::REGENERATE_REASON_SAVE_POST, $this->get_all_providers( array( $post ) ) ); + // This checks against the latest providers list, not the list + // stored in the database because newly added posts are always + // included in the providers list that will be used to generate + // the Cloud CSS. + if ( $this->is_post_in_latest_providers_list( $post ) ) { + $this->regenerate_cloud_css( self::REGENERATE_REASON_SAVE_POST, $this->get_all_providers( array( $post ) ) ); + } } public function regenerate_cloud_css( $reason, $providers ) { @@ -171,6 +177,26 @@ public function regenerate_cloud_css( $reason, $providers ) { return $result; } + /** + * Check if the post is in the latest providers list. + * + * @param int|\WP_Post $post The post to check. + * + * @return bool + */ + public function is_post_in_latest_providers_list( $post ) { + $post_link = get_permalink( $post ); + $providers = $this->get_all_providers(); + + foreach ( $providers as $provider ) { + if ( in_array( $post_link, $provider['urls'], true ) ) { + return true; + } + } + + return false; + } + /** * Called when stored Critical CSS has been invalidated. Triggers a new Cloud CSS request. */ diff --git a/projects/plugins/boost/changelog/update-boost-reduce-unnecessary-css-generations b/projects/plugins/boost/changelog/update-boost-reduce-unnecessary-css-generations new file mode 100644 index 0000000000000..a46d7a6f8dd62 --- /dev/null +++ b/projects/plugins/boost/changelog/update-boost-reduce-unnecessary-css-generations @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Critical CSS: Reduce unnecessary regenerations.