From 410917f89659035329791482da2907c39dcc3388 Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Wed, 13 Nov 2024 19:26:28 +0200 Subject: [PATCH] Boost: Cornerstone Pages list UI tweaks (#40163) --- .../src/js/features/cornerstone-pages/meta/meta.tsx | 10 ++++------ .../app/assets/src/js/lib/utils/is-same-site-url.ts | 11 +++++++++++ .../boost/app/data-sync/Cornerstone_Pages_Entry.php | 4 +--- .../fix-boost-cornerstone-pages-duplicate-home-urls | 5 +++++ 4 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 projects/plugins/boost/app/assets/src/js/lib/utils/is-same-site-url.ts create mode 100644 projects/plugins/boost/changelog/fix-boost-cornerstone-pages-duplicate-home-urls diff --git a/projects/plugins/boost/app/assets/src/js/features/cornerstone-pages/meta/meta.tsx b/projects/plugins/boost/app/assets/src/js/features/cornerstone-pages/meta/meta.tsx index 172d9655cc4a7..f73749d775ee4 100644 --- a/projects/plugins/boost/app/assets/src/js/features/cornerstone-pages/meta/meta.tsx +++ b/projects/plugins/boost/app/assets/src/js/features/cornerstone-pages/meta/meta.tsx @@ -16,6 +16,7 @@ import { useRegenerationReason } from '$features/critical-css/lib/stores/suggest import { usePremiumFeatures } from '$lib/stores/premium-features'; import { useNavigate } from 'react-router-dom'; import { useRegenerateCriticalCssAction } from '$features/critical-css/lib/stores/critical-css-state'; +import { isSameSiteUrl } from '$lib/utils/is-same-site-url'; const Meta = () => { const [ isExpanded, setIsExpanded ] = useState( false ); @@ -224,6 +225,8 @@ const List: React.FC< ListProps > = ( { throw new Error( message ); } + const siteUrl = new URL( Jetpack_Boost.site.url ); + for ( const line of lines ) { let url: URL | undefined; try { @@ -231,12 +234,7 @@ const List: React.FC< ListProps > = ( { } catch ( e ) { // If the URL is invalid, they have provided a relative URL, which we will allow. } - if ( - url && - ! ( url.origin + url.pathname ).replace( /\/$/, '' ).startsWith( - Jetpack_Boost.site.url.replace( /\/$/, '' ) - ) - ) { + if ( url && ! isSameSiteUrl( url, siteUrl ) ) { throw new Error( /* translators: %s is the URL that didn't match the site URL */ sprintf( __( 'The URL seems to be a different site: %s', 'jetpack-boost' ), line ) diff --git a/projects/plugins/boost/app/assets/src/js/lib/utils/is-same-site-url.ts b/projects/plugins/boost/app/assets/src/js/lib/utils/is-same-site-url.ts new file mode 100644 index 0000000000000..99e913da70e05 --- /dev/null +++ b/projects/plugins/boost/app/assets/src/js/lib/utils/is-same-site-url.ts @@ -0,0 +1,11 @@ +/** + * Returns true if the provided URL is from the same domain as the provided site URL. + * + * @param {URL} url URL to check. + * @param {URL} siteUrl Site URL to compare against. + */ +export function isSameSiteUrl( url: URL, siteUrl: URL ): boolean { + const urlSanitized = url.origin + url.pathname.replace( /\/$/, '' ) + '/'; + const siteUrlSanitized = siteUrl.origin + siteUrl.pathname.replace( /\/$/, '' ) + '/'; + return urlSanitized.startsWith( siteUrlSanitized ); +} diff --git a/projects/plugins/boost/app/data-sync/Cornerstone_Pages_Entry.php b/projects/plugins/boost/app/data-sync/Cornerstone_Pages_Entry.php index adb2690a3db0a..f6b75b42323fb 100644 --- a/projects/plugins/boost/app/data-sync/Cornerstone_Pages_Entry.php +++ b/projects/plugins/boost/app/data-sync/Cornerstone_Pages_Entry.php @@ -28,8 +28,6 @@ public function get( $fallback_value = array() ) { public function set( $value ) { $value = $this->sanitize_value( $value ); - $value = array_map( 'untrailingslashit', $value ); - $updated = update_option( $this->option_key, $value ); if ( $updated ) { ( new Environment_Change_Detector() )->handle_cornerstone_pages_list_update(); @@ -38,7 +36,7 @@ public function set( $value ) { private function sanitize_value( $value ) { if ( is_array( $value ) ) { - $value = array_values( array_unique( array_map( array( $this, 'transform_to_relative' ), $value ) ) ); + $value = array_values( array_unique( array_map( 'untrailingslashit', array_map( array( $this, 'transform_to_relative' ), $value ) ) ) ); } else { $value = array(); } diff --git a/projects/plugins/boost/changelog/fix-boost-cornerstone-pages-duplicate-home-urls b/projects/plugins/boost/changelog/fix-boost-cornerstone-pages-duplicate-home-urls new file mode 100644 index 0000000000000..e8382d9c99e32 --- /dev/null +++ b/projects/plugins/boost/changelog/fix-boost-cornerstone-pages-duplicate-home-urls @@ -0,0 +1,5 @@ +Significance: patch +Type: fixed +Comment: Fix for an unreleased feature. + +