|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Plugin Name: Podcast Download Canonical Guard |
| 4 | + * Description: Prevents Seriously Simple Podcasting plugin redirect loops on missing podcast download URLs by disabling canonical redirects for 404 responses on /podcast-download/ paths. |
| 5 | + * Version: 0.1.0 |
| 6 | + * Author: Jordan |
| 7 | + * Author URI: https://managingwp.io/ |
| 8 | + * Type: snippet |
| 9 | + * Status: Complete |
| 10 | + */ |
| 11 | + |
| 12 | +define('PODCAST_CANONICAL_GUARD_DEBUG', apply_filters('podcast_canonical_guard_debug', false)); |
| 13 | + |
| 14 | +if (!function_exists('podcast_canonical_guard_parse_route')) { |
| 15 | + function podcast_canonical_guard_parse_route($url_or_path) |
| 16 | + { |
| 17 | + $path = wp_parse_url($url_or_path, PHP_URL_PATH); |
| 18 | + |
| 19 | + if ($path === null && is_string($url_or_path) && strpos($url_or_path, '/') === 0) { |
| 20 | + $path = preg_replace('#\?.*$#', '', $url_or_path); |
| 21 | + } |
| 22 | + |
| 23 | + if (!$path || strpos($path, '/podcast-download/') === false) { |
| 24 | + return null; |
| 25 | + } |
| 26 | + |
| 27 | + $normalized_path = untrailingslashit($path); |
| 28 | + |
| 29 | + if (!preg_match('#^/podcast-download/(\d+)/([^/]+)\.mp3$#i', $normalized_path, $matches)) { |
| 30 | + return null; |
| 31 | + } |
| 32 | + |
| 33 | + return [ |
| 34 | + 'path' => $path, |
| 35 | + 'normalized_path' => $normalized_path, |
| 36 | + 'post_id' => absint($matches[1]), |
| 37 | + 'requested_slug' => sanitize_title($matches[2]), |
| 38 | + ]; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +if (!function_exists('podcast_canonical_guard_send_404')) { |
| 43 | + function podcast_canonical_guard_send_404($requested_path, $reason = '', $status = 404) |
| 44 | + { |
| 45 | + if (PODCAST_CANONICAL_GUARD_DEBUG) { |
| 46 | + error_log(sprintf('[Podcast Canonical Guard] Forced %d for %s (%s)', $status, $requested_path, $reason ?: 'unspecified')); |
| 47 | + } |
| 48 | + |
| 49 | + global $wp_query; |
| 50 | + |
| 51 | + if ($wp_query instanceof WP_Query) { |
| 52 | + $wp_query->set_404(); |
| 53 | + } |
| 54 | + |
| 55 | + status_header($status); |
| 56 | + nocache_headers(); |
| 57 | + |
| 58 | + if (!headers_sent()) { |
| 59 | + header('Content-Type: text/plain; charset=utf-8'); |
| 60 | + } |
| 61 | + |
| 62 | + echo sprintf("Podcast download unavailable (%d).", $status); |
| 63 | + |
| 64 | + exit; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +add_filter('redirect_canonical', function ($redirect_url, $requested_url) { |
| 69 | + // Bail early if WordPress has not parsed the request yet. |
| 70 | + if (!did_action('parse_request')) { |
| 71 | + return $redirect_url; |
| 72 | + } |
| 73 | + |
| 74 | + $requested = podcast_canonical_guard_parse_route($requested_url); |
| 75 | + |
| 76 | + if (!$requested) { |
| 77 | + return $redirect_url; |
| 78 | + } |
| 79 | + |
| 80 | + // Allow other components to short-circuit this behaviour entirely. |
| 81 | + if (false === apply_filters('podcast_canonical_guard_should_intercept', true, $requested['path'], $redirect_url, $requested_url)) { |
| 82 | + return $redirect_url; |
| 83 | + } |
| 84 | + |
| 85 | + $redirect_host = $redirect_url ? wp_parse_url($redirect_url, PHP_URL_HOST) : null; |
| 86 | + $requested_host = wp_parse_url($requested_url, PHP_URL_HOST); |
| 87 | + |
| 88 | + $redirect = $redirect_url ? podcast_canonical_guard_parse_route($redirect_url) : null; |
| 89 | + |
| 90 | + $hosts_match = !$redirect_host || !$requested_host || strcasecmp($redirect_host, $requested_host) === 0; |
| 91 | + $paths_match = $redirect && $redirect['normalized_path'] === $requested['normalized_path']; |
| 92 | + |
| 93 | + $should_block = ($hosts_match && $paths_match) || is_404(); |
| 94 | + |
| 95 | + if (!$should_block) { |
| 96 | + return $redirect_url; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Fires when a canonical redirect is being suppressed for a podcast download URL. |
| 101 | + * |
| 102 | + * @param string|null $redirect_url The URL WordPress planned to redirect to. |
| 103 | + * @param string $requested_url The original requested URL. |
| 104 | + * @param string $path Parsed path portion of the requested URL. |
| 105 | + */ |
| 106 | + do_action('podcast_canonical_guard_blocked_redirect', $redirect_url, $requested_url, $requested['path']); |
| 107 | + |
| 108 | + if (PODCAST_CANONICAL_GUARD_DEBUG) { |
| 109 | + error_log(sprintf('[Podcast Canonical Guard] Suppressed redirect for path: %s (was %s)', $requested_url, $redirect_url)); |
| 110 | + } |
| 111 | + |
| 112 | + return false; |
| 113 | +}, 10, 2); |
| 114 | + |
| 115 | +add_action('template_redirect', function () { |
| 116 | + $request_uri = isset($_SERVER['REQUEST_URI']) ? wp_unslash((string) $_SERVER['REQUEST_URI']) : ''; |
| 117 | + |
| 118 | + if ($request_uri === '') { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + $route = podcast_canonical_guard_parse_route($request_uri); |
| 123 | + |
| 124 | + if (!$route) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + $post_id = $route['post_id']; |
| 129 | + $requested_slug = $route['requested_slug']; |
| 130 | + $path = $route['path']; |
| 131 | + |
| 132 | + if (!$post_id) { |
| 133 | + podcast_canonical_guard_send_404($path, 'missing-id'); |
| 134 | + } |
| 135 | + |
| 136 | + $post = get_post($post_id); |
| 137 | + |
| 138 | + if (!$post || 'publish' !== $post->post_status) { |
| 139 | + podcast_canonical_guard_send_404($path, 'missing-post'); |
| 140 | + } |
| 141 | + |
| 142 | + $canonical_slug = sanitize_title($post->post_name); |
| 143 | + |
| 144 | + if (strcasecmp($canonical_slug, $requested_slug) !== 0) { |
| 145 | + podcast_canonical_guard_send_404($path, 'slug-mismatch', 410); |
| 146 | + } |
| 147 | +}, 0); |
0 commit comments