From 0649fefb905c0bb8c634968b72ddcd54c1685ca0 Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Fri, 20 Dec 2024 14:15:44 +0200 Subject: [PATCH 1/7] Fix returning undefined for primaryErrorSet --- .../critical-css/lib/critical-css-errors.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/projects/plugins/boost/app/assets/src/js/features/critical-css/lib/critical-css-errors.ts b/projects/plugins/boost/app/assets/src/js/features/critical-css/lib/critical-css-errors.ts index 52260adb4b0b3..32dbbfb044d2c 100644 --- a/projects/plugins/boost/app/assets/src/js/features/critical-css/lib/critical-css-errors.ts +++ b/projects/plugins/boost/app/assets/src/js/features/critical-css/lib/critical-css-errors.ts @@ -62,12 +62,15 @@ export function getPrimaryErrorSet( cssState: CriticalCssState ): ErrorSet | und return undefined; } - const primaryProviders = [ 'core_front_page', 'core_posts_page' ]; + const isImportantProvider = ( provider: Provider ) => { + return provider.key.includes( 'cornerstone' ) || provider.key === 'core_front_page'; + }; - for ( const key of primaryProviders ) { - const provider = providersWithErrors.find( p => p.key === key ); - if ( provider && provider.errors ) { - return getPrimaryGroupedError( provider.errors ); + for ( const provider of providersWithErrors ) { + if ( isImportantProvider( provider ) ) { + if ( provider.errors ) { + return getPrimaryGroupedError( provider.errors ); + } } } From 9511137da07db8440d06406148781966b9877d0c Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Fri, 20 Dec 2024 14:17:21 +0200 Subject: [PATCH 2/7] Remove front page provider from wp core providers We were removing this with a filter during development of Cornerstone Pages because we had a feature flag. Since it's part of Boost now, we don't need the provider and the filter to remove it. --- .../plugins/boost/app/lib/Cornerstone_Pages.php | 13 ------------- .../source-providers/providers/WP_Core_Provider.php | 7 ------- 2 files changed, 20 deletions(-) diff --git a/projects/plugins/boost/app/lib/Cornerstone_Pages.php b/projects/plugins/boost/app/lib/Cornerstone_Pages.php index 8a659b6f67ae4..463828f648756 100644 --- a/projects/plugins/boost/app/lib/Cornerstone_Pages.php +++ b/projects/plugins/boost/app/lib/Cornerstone_Pages.php @@ -14,7 +14,6 @@ class Cornerstone_Pages implements Has_Setup { public function setup() { $this->register_ds_stores(); - add_filter( 'jetpack_boost_critical_css_providers', array( $this, 'remove_ccss_front_page_provider' ), 10, 2 ); add_filter( 'display_post_states', array( $this, 'add_display_post_states' ), 10, 2 ); add_action( 'init', array( $this, 'set_default_pages' ), 0 ); } @@ -32,18 +31,6 @@ private function register_ds_stores() { jetpack_boost_register_readonly_option( 'cornerstone_pages_properties', array( $this, 'get_properties' ) ); } - public function remove_ccss_front_page_provider( $providers ) { - $filtered_providers = array(); - - foreach ( $providers as $provider ) { - if ( $provider['key'] !== 'core_front_page' ) { - $filtered_providers[] = $provider; - } - } - - return $filtered_providers; - } - private function default_pages() { if ( $this->get_max_pages() === static::FREE_MAX_PAGES ) { return array( '' ); diff --git a/projects/plugins/boost/app/lib/critical-css/source-providers/providers/WP_Core_Provider.php b/projects/plugins/boost/app/lib/critical-css/source-providers/providers/WP_Core_Provider.php index aebaeb8117f76..b24d550d5e952 100644 --- a/projects/plugins/boost/app/lib/critical-css/source-providers/providers/WP_Core_Provider.php +++ b/projects/plugins/boost/app/lib/critical-css/source-providers/providers/WP_Core_Provider.php @@ -30,13 +30,6 @@ public static function get_critical_source_urls( $context_posts = array() ) { $front_page = (int) get_option( 'page_on_front' ); $posts_page = (int) get_option( 'page_for_posts' ); - if ( ! empty( $front_page ) && empty( $context_posts ) ) { - $permalink = get_permalink( $front_page ); - if ( ! empty( $permalink ) ) { - $urls['front_page'] = array( $permalink ); - } - } - $context_post_types = wp_list_pluck( $context_posts, 'post_type' ); $context_post_ids = wp_list_pluck( $context_posts, 'ID' ); From aa41bb3c7e9adecfdcd86fea207349faebcece66 Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Fri, 20 Dec 2024 14:18:55 +0200 Subject: [PATCH 3/7] Remove WP Core provider urls if they are present in cornerstone pages --- .../source-providers/Source_Providers.php | 58 ++++++++++++++----- 1 file changed, 45 insertions(+), 13 deletions(-) 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 47a1d91419365..cf36d4986b0ea 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 @@ -114,24 +114,51 @@ public function get_current_critical_css_key() { return $this->current_critical_css_key; } + /** + * Get a flat array of URLs for a provider. + * + * @param string $provider The provider class name. + * @param array $context_posts The context posts. + * + * @return array + */ + public function get_provider_urls_flat( $provider, $context_posts = array() ) { + $urls = $provider::get_critical_source_urls( $context_posts ); + $flat_urls = array(); + foreach ( $urls as $url ) { + $flat_urls = array_merge( $flat_urls, $url ); + } + return $flat_urls; + } + + /** + * Get the URLs of the important providers. + * Important providers are the ones Boost checks against + * if critical css generation fails + * + * @param array $context_posts The context posts. + * + * @return array + */ + public function get_important_provider_urls( $context_posts = array() ) { + $important_provider_urls = array(); + + $wp_core_provider_urls = $this->get_provider_urls_flat( WP_Core_Provider::class, $context_posts ); + $cornerstone_provider_urls = $this->get_provider_urls_flat( Cornerstone_Provider::class, $context_posts ); + $important_provider_urls = array_merge( $wp_core_provider_urls, $cornerstone_provider_urls ); + + return array_values( array_unique( $important_provider_urls ) ); + } + /** * Get providers sources. * * @return array */ public function get_provider_sources( $context_posts = array() ) { - $sources = array(); - $flat_core_and_cornerstone_urls = array(); - - $wp_core_provider_urls = WP_Core_Provider::get_critical_source_urls( $context_posts ); - foreach ( $wp_core_provider_urls as $urls ) { - $flat_core_and_cornerstone_urls = array_merge( $flat_core_and_cornerstone_urls, $urls ); - } - $cornerstone_provider_urls = Cornerstone_Provider::get_critical_source_urls( $context_posts ); - foreach ( $cornerstone_provider_urls as $urls ) { - $flat_core_and_cornerstone_urls = array_merge( $flat_core_and_cornerstone_urls, $urls ); - } - $flat_core_and_cornerstone_urls = array_values( array_unique( $flat_core_and_cornerstone_urls ) ); + $sources = array(); + $important_provider_urls = $this->get_important_provider_urls( $context_posts ); + $cornerstone_provider_urls = $this->get_provider_urls_flat( Cornerstone_Provider::class, $context_posts ); foreach ( $this->get_providers() as $provider ) { $provider_name = $provider::get_provider_name(); @@ -146,7 +173,12 @@ public function get_provider_sources( $context_posts = array() ) { // This removes core and cornerstone URLs from the list of URLs, // so they don't belong to two separate groups. if ( ! in_array( $provider, array( WP_Core_Provider::class, Cornerstone_Provider::class ), true ) ) { - $urls = array_values( array_diff( $urls, $flat_core_and_cornerstone_urls ) ); + $urls = array_values( array_diff( $urls, $important_provider_urls ) ); + } + + // Remove WP Core URLs if they are in the Cornerstone URLs list. + if ( WP_Core_Provider::class === $provider ) { + $urls = array_values( array_diff( $urls, $cornerstone_provider_urls ) ); } if ( empty( $urls ) ) { From 7e4792ea6df58c7214b7def93a04c6b5a12a50f3 Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Fri, 20 Dec 2024 14:25:44 +0200 Subject: [PATCH 4/7] Remove duplicate blog page if in cornerstone pages list --- .../boost/app/lib/Cornerstone_Pages.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/projects/plugins/boost/app/lib/Cornerstone_Pages.php b/projects/plugins/boost/app/lib/Cornerstone_Pages.php index 463828f648756..2351d9d8b4fc3 100644 --- a/projects/plugins/boost/app/lib/Cornerstone_Pages.php +++ b/projects/plugins/boost/app/lib/Cornerstone_Pages.php @@ -14,6 +14,7 @@ class Cornerstone_Pages implements Has_Setup { public function setup() { $this->register_ds_stores(); + add_filter( 'jetpack_boost_critical_css_providers', array( $this, 'remove_core_posts_page' ), 10, 2 ); add_filter( 'display_post_states', array( $this, 'add_display_post_states' ), 10, 2 ); add_action( 'init', array( $this, 'set_default_pages' ), 0 ); } @@ -31,6 +32,34 @@ private function register_ds_stores() { jetpack_boost_register_readonly_option( 'cornerstone_pages_properties', array( $this, 'get_properties' ) ); } + /** + * Remove the core posts page provider from the list of + * Cornerstone Pages only if there's no static front page, + * but there's a posts page set. + * + * @param array $providers The list of providers. + * + * @return array + */ + public function remove_core_posts_page( $providers ) { + $filtered_providers = array(); + $front_page = (int) get_option( 'page_on_front' ); + $posts_page = (int) get_option( 'page_for_posts' ); + + // Only filter out core_posts_page if there's no static front page but there is a posts page + if ( ! $front_page && $posts_page ) { + foreach ( $providers as $provider ) { + if ( $provider['key'] !== 'core_posts_page' ) { + $filtered_providers[] = $provider; + } + } + + return $filtered_providers; + } + + return $providers; + } + private function default_pages() { if ( $this->get_max_pages() === static::FREE_MAX_PAGES ) { return array( '' ); From 0a5e9fb8a7d5682c3b58ae7938fdcb1f4cf2a55a Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Fri, 20 Dec 2024 14:28:13 +0200 Subject: [PATCH 5/7] add changelog --- .../boost/changelog/update-dedpulicate-source-provider-urls | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 projects/plugins/boost/changelog/update-dedpulicate-source-provider-urls diff --git a/projects/plugins/boost/changelog/update-dedpulicate-source-provider-urls b/projects/plugins/boost/changelog/update-dedpulicate-source-provider-urls new file mode 100644 index 0000000000000..0381006de0460 --- /dev/null +++ b/projects/plugins/boost/changelog/update-dedpulicate-source-provider-urls @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Critical CSS: Make sure URLs used for generation are unique. From ea5f77fe46ce4bc1340056e222b5522d26a7f2a7 Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Fri, 20 Dec 2024 14:42:22 +0200 Subject: [PATCH 6/7] Make sure default cornerstone URLs are always unique --- projects/plugins/boost/app/lib/Cornerstone_Pages.php | 1 + 1 file changed, 1 insertion(+) diff --git a/projects/plugins/boost/app/lib/Cornerstone_Pages.php b/projects/plugins/boost/app/lib/Cornerstone_Pages.php index 2351d9d8b4fc3..8823b6bdd6fe3 100644 --- a/projects/plugins/boost/app/lib/Cornerstone_Pages.php +++ b/projects/plugins/boost/app/lib/Cornerstone_Pages.php @@ -73,6 +73,7 @@ private function default_pages() { $urls = array_unique( array_merge( $homepage, $woocommerce_pages, $yoast_cornerstone_pages ) ); $urls = array_map( 'untrailingslashit', $urls ); + $urls = array_unique( $urls ); return array_slice( $urls, 0, $max_pages ); } From 594c0bfb7c12731df089074aaf7e28fdd8e05186 Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Fri, 20 Dec 2024 14:45:15 +0200 Subject: [PATCH 7/7] Check for unique URLs only once --- projects/plugins/boost/app/lib/Cornerstone_Pages.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/plugins/boost/app/lib/Cornerstone_Pages.php b/projects/plugins/boost/app/lib/Cornerstone_Pages.php index 8823b6bdd6fe3..add62808a5f35 100644 --- a/projects/plugins/boost/app/lib/Cornerstone_Pages.php +++ b/projects/plugins/boost/app/lib/Cornerstone_Pages.php @@ -71,7 +71,7 @@ private function default_pages() { $homepage = array( '' ); - $urls = array_unique( array_merge( $homepage, $woocommerce_pages, $yoast_cornerstone_pages ) ); + $urls = array_merge( $homepage, $woocommerce_pages, $yoast_cornerstone_pages ); $urls = array_map( 'untrailingslashit', $urls ); $urls = array_unique( $urls );