Skip to content

Commit fa70df4

Browse files
committed
Refactor interactivity-router plugin to improve URL formatting and state management. Introduced a helper function for URL construction in render.php, enhancing navigation link handling. Updated view.js to utilize the new URL formatting logic and added console logs for better state tracking. Incremented asset version in view.asset.php for consistency.
1 parent 77ad31e commit fa70df4

File tree

6 files changed

+93
-28
lines changed

6 files changed

+93
-28
lines changed

plugins/interactivity-router-2f43f8/build/render.php

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,50 @@
1212
*/
1313

1414
$base_url = get_site_url();
15+
$state = array(
16+
'base_url' => $base_url,
17+
'currentSlug' => get_post_field( 'post_name', get_post() ),
18+
'slugs' => array(
19+
'paul-mccartney|Paul',
20+
'john-lennon|John',
21+
'george-harrison|George',
22+
'ringo-starr|Ringo',
23+
),
24+
);
25+
26+
if ( ! function_exists( 'format_url' ) ) {
27+
// Helper function to properly format URLs.
28+
$format_url = function ( $base, $path ) {
29+
// Ensures base URL ends with / .
30+
// Example: "https://example.com" → "https://example.com/" .
31+
$base_with_slash = trailingslashit( $base );
32+
33+
// Removes trailing slashes from path.
34+
// Example: "john-lennon/" → "john-lennon" .
35+
$path_no_trailing = untrailingslashit( $path );
36+
37+
// Removes leading slashes from path.
38+
// Example: "/john-lennon" → "john-lennon" .
39+
$path_clean = ltrim( $path_no_trailing, '/' );
40+
41+
// Combine base URL with cleaned path.
42+
// Result: "https://example.com/" + "john-lennon" = "https://example.com/john-lennon" .
43+
return $base_with_slash . $path_clean;
44+
};
45+
}
46+
if ( $attributes['prev'] ) {
47+
$state['prev'] = $format_url( $base_url, $attributes['prev'] );
48+
}
49+
if ( $attributes['next'] ) {
50+
$state['next'] = $format_url( $base_url, $attributes['next'] );
51+
}
52+
1553
wp_interactivity_state(
1654
'router-2f43f8',
17-
array(
18-
'prev' => $attributes['prev'],
19-
'next' => $attributes['next'],
20-
'base_url' => $base_url,
21-
'currentSlug' => get_post_field( 'post_name', get_post() ),
22-
'slugs' => array(
23-
'paul-mccartney|Paul',
24-
'john-lennon|John',
25-
'george-harrison|George',
26-
'ringo-starr|Ringo',
27-
),
28-
)
55+
$state,
2956
);
3057
?>
58+
3159
<div
3260
<?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>
3361
data-wp-interactive="router-2f43f8"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('@wordpress/interactivity', array('id' => '@wordpress/interactivity-router', 'import' => 'dynamic')), 'version' => 'f490aadf565df2afd7b2', 'type' => 'module');
1+
<?php return array('dependencies' => array('@wordpress/interactivity', array('id' => '@wordpress/interactivity-router', 'import' => 'dynamic')), 'version' => 'a1597cc18fa5c131cccd', 'type' => 'module');

plugins/interactivity-router-2f43f8/build/view.js

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/interactivity-router-2f43f8/build/view.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/interactivity-router-2f43f8/src/render.php

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,50 @@
1212
*/
1313

1414
$base_url = get_site_url();
15+
$state = array(
16+
'base_url' => $base_url,
17+
'currentSlug' => get_post_field( 'post_name', get_post() ),
18+
'slugs' => array(
19+
'paul-mccartney|Paul',
20+
'john-lennon|John',
21+
'george-harrison|George',
22+
'ringo-starr|Ringo',
23+
),
24+
);
25+
26+
if ( ! function_exists( 'format_url' ) ) {
27+
// Helper function to properly format URLs.
28+
$format_url = function ( $base, $path ) {
29+
// Ensures base URL ends with / .
30+
// Example: "https://example.com" → "https://example.com/" .
31+
$base_with_slash = trailingslashit( $base );
32+
33+
// Removes trailing slashes from path.
34+
// Example: "john-lennon/" → "john-lennon" .
35+
$path_no_trailing = untrailingslashit( $path );
36+
37+
// Removes leading slashes from path.
38+
// Example: "/john-lennon" → "john-lennon" .
39+
$path_clean = ltrim( $path_no_trailing, '/' );
40+
41+
// Combine base URL with cleaned path.
42+
// Result: "https://example.com/" + "john-lennon" = "https://example.com/john-lennon" .
43+
return $base_with_slash . $path_clean;
44+
};
45+
}
46+
if ( $attributes['prev'] ) {
47+
$state['prev'] = $format_url( $base_url, $attributes['prev'] );
48+
}
49+
if ( $attributes['next'] ) {
50+
$state['next'] = $format_url( $base_url, $attributes['next'] );
51+
}
52+
1553
wp_interactivity_state(
1654
'router-2f43f8',
17-
array(
18-
'prev' => $attributes['prev'],
19-
'next' => $attributes['next'],
20-
'base_url' => $base_url,
21-
'currentSlug' => get_post_field( 'post_name', get_post() ),
22-
'slugs' => array(
23-
'paul-mccartney|Paul',
24-
'john-lennon|John',
25-
'george-harrison|George',
26-
'ringo-starr|Ringo',
27-
),
28-
)
55+
$state,
2956
);
3057
?>
58+
3159
<div
3260
<?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>
3361
data-wp-interactive="router-2f43f8"

plugins/interactivity-router-2f43f8/src/view.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const prefetchLink = function* ( ref ) {
2020
}
2121
};
2222

23+
const removeSlashes = ( str ) => str.replace( /^\/+|\/+$/g, '' );
24+
2325
const { state } = store( 'router-2f43f8', {
2426
state: {
2527
urlRegionDisplay: window.location.href,
@@ -28,7 +30,8 @@ const { state } = store( 'router-2f43f8', {
2830
},
2931
get itemSlug() {
3032
const ctx = getContext();
31-
return ctx.item.split( '|' )[ 0 ];
33+
const slug = ctx.item.split( '|' )[ 0 ];
34+
return new URL( removeSlashes( slug ), state.base_url ).href;
3235
},
3336
get itemName() {
3437
const ctx = getContext();
@@ -60,6 +63,8 @@ const { state } = store( 'router-2f43f8', {
6063
state.prev = serverState.prev;
6164
state.next = serverState.next;
6265
state.currentSlug = serverState.currentSlug;
66+
console.log( 'Server state: ', serverState );
67+
console.log( 'Client state: ', state );
6368
},
6469
*prefetch() {
6570
const { ref } = getElement();

0 commit comments

Comments
 (0)