Skip to content

Commit

Permalink
Boost: Cornerstone Pages list UI tweaks (#40163)
Browse files Browse the repository at this point in the history
  • Loading branch information
dilirity authored Nov 13, 2024
1 parent bbe16de commit 410917f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -224,19 +225,16 @@ 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 {
url = new URL( line );
} 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 )
Expand Down
Original file line number Diff line number Diff line change
@@ -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 );
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Fix for an unreleased feature.


0 comments on commit 410917f

Please sign in to comment.