feat(wpmc S4): Redirect class 301s on page_unic clone access
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WPMultiCity;
|
||||
|
||||
final class Redirect
|
||||
{
|
||||
public static function register(): void
|
||||
{
|
||||
\add_action('wp', [self::class, 'handle'], 9998);
|
||||
}
|
||||
|
||||
public static function handle(): void
|
||||
{
|
||||
if (\is_admin()) {
|
||||
return;
|
||||
}
|
||||
if (\defined('DOING_AJAX') && \DOING_AJAX) {
|
||||
return;
|
||||
}
|
||||
$main = Helpers::main_town_slug();
|
||||
if ($main === null) {
|
||||
return;
|
||||
}
|
||||
global $post;
|
||||
if (!isset($post->ID)) {
|
||||
return;
|
||||
}
|
||||
$current_page_id = (int) $post->ID;
|
||||
if (!\function_exists('get_field')) {
|
||||
return;
|
||||
}
|
||||
$page_unic = \get_field('page_unic', 'option');
|
||||
if (!is_array($page_unic)) {
|
||||
return;
|
||||
}
|
||||
$request_uri = $_SERVER['REQUEST_URI'] ?? '/';
|
||||
foreach ($page_unic as $row) {
|
||||
if (!is_array($row)) {
|
||||
continue;
|
||||
}
|
||||
if ((int) ($row['page_new'] ?? 0) !== $current_page_id) {
|
||||
continue;
|
||||
}
|
||||
$town_term_id = (int) ($row['town'] ?? 0);
|
||||
if (!$town_term_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 = \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;
|
||||
\wp_safe_redirect($redirect_to, 301);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user