feat(wpmc 1.1.0): Redirect::handle reverse-slug lookup on is_404

Replace old triggering-on-page_new semantics with washanyanya-style:
- template_redirect@1 + is_404() trigger
- get_posts(name=$slug, post_type=page, status=[private,publish])
- match candidate.ID against page_unic[i].page_new
- 301 to /<town>/<orig-canonical-path>/

Closes Gitea issue #1.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Vladimir Bryzgalov
2026-05-19 02:22:24 +05:00
parent 9586810507
commit 43e3efc5a2
3 changed files with 332 additions and 54 deletions
+46 -21
View File
@@ -12,60 +12,85 @@ final class Redirect
public static function handle(): void
{
if (\is_admin()) {
if (\is_admin() || (\defined('DOING_AJAX') && \DOING_AJAX)) {
return;
}
if (\defined('DOING_AJAX') && \DOING_AJAX) {
if (!\is_404()) {
return;
}
$main = Helpers::main_town_slug();
if ($main === null) {
return;
}
global $post;
if (!isset($post->ID)) {
$slug = self::extract_last_segment($_SERVER['REQUEST_URI'] ?? '/');
if ($slug === null) {
return;
}
$current_page_id = (int) $post->ID;
$candidates = \get_posts([
'name' => $slug,
'post_type' => 'page',
'post_status' => ['private', 'publish'],
'posts_per_page' => 1,
'no_found_rows' => true,
'suppress_filters' => false,
]);
if (empty($candidates)) {
return;
}
$candidate_id = (int) $candidates[0]->ID;
if (!\function_exists('get_field')) {
return;
}
$page_unic = \get_field('page_unic', 'option');
if (!is_array($page_unic)) {
$rows = \get_field('page_unic', 'option');
if (!is_array($rows)) {
return;
}
$request_uri = $_SERVER['REQUEST_URI'] ?? '/';
foreach ($page_unic as $row) {
foreach ($rows as $row) {
if (!is_array($row)) {
continue;
}
if ((int) ($row['page_new'] ?? 0) !== $current_page_id) {
if ((int) ($row['page_new'] ?? 0) !== $candidate_id) {
continue;
}
$town_term_id = (int) ($row['town'] ?? 0);
if (!$town_term_id) {
$orig_id = (int) ($row['page_old'] ?? 0);
if (!$town_term_id || !$orig_id) {
continue;
}
$term = \get_term_by('id', $town_term_id, 'town');
if (!($term instanceof \WP_Term) || $term->slug === '' || $term->slug === $main) {
continue;
}
$town_slug = $term->slug;
if (preg_match('#^/' . preg_quote($town_slug, '#') . '(/|$)#', $request_uri)) {
return;
}
$orig_id = (int) ($row['page_old'] ?? 0);
if (!$orig_id) {
return;
}
$orig_permalink = Helpers::without_url_filters(static fn () => \get_permalink($orig_id));
$orig_permalink = Helpers::without_url_filters(
static fn () => \get_permalink($orig_id)
);
if (!is_string($orig_permalink) || $orig_permalink === '') {
return;
}
$path_only = Helpers::remove_domain_link($orig_permalink);
$redirect_to = '/' . $town_slug . $path_only;
$redirect_to = '/' . $term->slug . $path_only;
\wp_safe_redirect($redirect_to, 301);
exit;
}
}
private static function extract_last_segment(string $uri): ?string
{
$path = strtok($uri, '?');
if ($path === false) {
return null;
}
$parts = array_values(array_filter(
explode('/', $path),
static fn (string $p) => $p !== ''
));
$last = end($parts);
return $last !== false && $last !== '' ? $last : null;
}
}