fix: paginated region/city pages, no routes before first save, safer structure save

- Add /{slug}/page/N/ rules and keep the page number through 301s
- Route only regions synced to a term, so unsaved presets never claim /europe/ etc.
- Flush rewrites on the request after activation so structure routes survive
- Suggest the existing term slug for new cities (keeps live /tour-destination/ URLs)
- Refuse saves without the editor sentinel; confirm before saving an empty structure
- Show term-sync failures to the admin instead of reporting success

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-29 15:33:01 +03:30
parent f5c4c89f1a
commit 930b7e0534
8 changed files with 161 additions and 28 deletions

View File

@@ -127,23 +127,47 @@ function test_primary_destination_id() {
vc_assert_same( '', VC_Structure::primary_destination_id( array() ) );
}
function vc_fixture_synced() {
$s = vc_fixture_structure();
$s[0]['term_id'] = 11;
$s[1]['term_id'] = 12;
return $s;
}
function test_rewrite_rules() {
vc_assert_same(
array(
'^europe/?$' => 'index.php?viator_destination=europe',
'^americas/?$' => 'index.php?viator_destination=americas',
'^amsterdam/?$' => 'index.php?viator_destination=amsterdam',
'^amsterdam/([^/]+)/?$' => 'index.php?viator_tour=$matches[1]',
'^paris/?$' => 'index.php?viator_destination=paris',
'^paris/([^/]+)/?$' => 'index.php?viator_tour=$matches[1]',
'^boston/?$' => 'index.php?viator_destination=boston',
'^boston/([^/]+)/?$' => 'index.php?viator_tour=$matches[1]',
'^europe/page/([0-9]+)/?$' => 'index.php?viator_destination=europe&paged=$matches[1]',
'^europe/?$' => 'index.php?viator_destination=europe',
'^americas/page/([0-9]+)/?$' => 'index.php?viator_destination=americas&paged=$matches[1]',
'^americas/?$' => 'index.php?viator_destination=americas',
'^amsterdam/page/([0-9]+)/?$' => 'index.php?viator_destination=amsterdam&paged=$matches[1]',
'^amsterdam/?$' => 'index.php?viator_destination=amsterdam',
'^amsterdam/([^/]+)/?$' => 'index.php?viator_tour=$matches[1]',
'^paris/page/([0-9]+)/?$' => 'index.php?viator_destination=paris&paged=$matches[1]',
'^paris/?$' => 'index.php?viator_destination=paris',
'^paris/([^/]+)/?$' => 'index.php?viator_tour=$matches[1]',
'^boston/page/([0-9]+)/?$' => 'index.php?viator_destination=boston&paged=$matches[1]',
'^boston/?$' => 'index.php?viator_destination=boston',
'^boston/([^/]+)/?$' => 'index.php?viator_tour=$matches[1]',
),
VC_Structure::rewrite_rules( vc_fixture_structure() )
VC_Structure::rewrite_rules( vc_fixture_synced() )
);
}
function test_rewrite_rules_empty_region() {
$rules = VC_Structure::rewrite_rules( array( array( 'name' => 'Europe', 'slug' => 'europe', 'term_id' => 0, 'cities' => array() ) ) );
vc_assert_same( array( '^europe/?$' => 'index.php?viator_destination=europe' ), $rules );
$rules = VC_Structure::rewrite_rules( array( array( 'name' => 'Europe', 'slug' => 'europe', 'term_id' => 11, 'cities' => array() ) ) );
vc_assert_same(
array(
'^europe/page/([0-9]+)/?$' => 'index.php?viator_destination=europe&paged=$matches[1]',
'^europe/?$' => 'index.php?viator_destination=europe',
),
$rules
);
}
// Before the structure is ever saved, get() returns the unsynced presets:
// they must not claim /uk/, /europe/, … over existing pages.
function test_rewrite_rules_skip_unsynced_regions() {
vc_assert_same( array(), VC_Structure::rewrite_rules( VC_Structure::default_regions() ) );
}