getOptions(); $shouldUpdateURL = true; // if we're not supposed to update the URL then don't. if (!array_key_exists('update_suggest_url', $options) || !isset($options['update_suggest_url']) || $options['update_suggest_url'] != 1) { $shouldUpdateURL = false; $debugMessage .= "do not update (update_suggest_url is off), "; } // if the cookie we need isn't set then give up. $updateURLCookieName = ABJ404_PP . '_REQUEST_URI'; $updateURLCookieName .= '_UPDATE_URL'; if (!isset($_REQUEST[$updateURLCookieName]) || empty($_REQUEST[$updateURLCookieName])) { $shouldUpdateURL = false; $debugMessage .= "do not update (no cookie found), "; } $dest404page = (isset($options['dest404page']) ? $options['dest404page'] : ABJ404_TYPE_404_DISPLAYED . '|' . ABJ404_TYPE_404_DISPLAYED); // Check if this is a manual redirect (has query param) - these bypass global 404 page check $queryParamName = ABJ404_PP . '_ref'; $isManualRedirect = isset($_GET[$queryParamName]) && !empty($_GET[$queryParamName]); // if we're not currently loading the custom 404 page then don't change the URL. // Exception: manual redirects to custom 404 pages should always allow URL restoration if ($isManualRedirect) { // Manual redirect - we know we're on a custom 404 page, allow URL restoration $debugMessage .= "ok to update (manual redirect to custom 404 page), "; } else if ($abj404logic->thereIsAUserSpecified404Page($dest404page)) { // get the user specified 404 page. $permalink = ABJ_404_Solution_Functions::permalinkInfoToArray($dest404page, 0, null, $options); // if the last part of the URL does not match the custom 404 page then // don't update the URL. // Strip query string from REQUEST_URI for comparison (query params like abj404_solution_ref) $requestUriPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); if (!$f->endsWithCaseSensitive($permalink['link'], $requestUriPath) && $permalink['status'] != 'trash') { $shouldUpdateURL = false; $debugMessage .= "do not update (not on custom 404 page (" . $permalink['link'] . ")), "; } else { $debugMessage .= "ok to update (displaying custom 404 page (" . $permalink['link'] . ")), "; } } else { // the 404 page is the default 404 page. so we shouldn't change the URL. $shouldUpdateURL = false; $debugMessage .= "do not update (no custom 404 page specified), "; } $content = ''; if ($shouldUpdateURL) { // replace the current URL with the user's actual requested URL. $requestedURL = $_REQUEST[$updateURLCookieName]; $userFriendlyURL = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . esc_url($requestedURL); // Use wp_json_encode to safely encode the URL for JavaScript to prevent XSS $content .= "window.history.replaceState({}, null, " . wp_json_encode($userFriendlyURL) . ");\n"; $debugMessage .= "Updating the URL from " . $_SERVER['REQUEST_URI'] . " to " . esc_url($userFriendlyURL) . ", "; } if ($content != '') { $content = '\n\n"; echo $content; } $debugMessage .= "is404: " . is_404() . ", " . esc_html('auto_redirects: ' . $options['auto_redirects'] . ', auto_score: ' . $options['auto_score'] . ', template_redirect_priority: ' . $options['template_redirect_priority'] . ', auto_cats: ' . $options['auto_cats'] . ', auto_tags: ' . $options['auto_tags'] . ', dest404page: ' . $options['dest404page']) . ", "; $debugMessage .= "is_single(): " . is_single() . " | " . "is_page(): " . is_page() . " | is_feed(): " . is_feed() . " | is_trackback(): " . is_trackback() . " | is_preview(): " . is_preview(); $abj404logging->debugMessage("updateURLbarIfNecessary: " . $debugMessage); } /** * @param array $atts */ static function shortcodePageSuggestions( $atts ) { $abj404logic = ABJ_404_Solution_PluginLogic::getInstance(); $abj404spellChecker = ABJ_404_Solution_SpellChecker::getInstance(); $f = ABJ_404_Solution_Functions::getInstance(); $abj404dao = ABJ_404_Solution_DataAccess::getInstance(); // Attributes $atts = shortcode_atts( array( ), $atts ); $options = $abj404logic->getOptions(); $content = "\n\n"; // get the slug that caused the 404 from the session. $urlRequest = ''; $cookieName = ABJ404_PP . '_REQUEST_URI'; if (isset($_COOKIE[$cookieName]) && !empty($_COOKIE[$cookieName])) { // Normalize URL using centralized function for consistency $urlRequest = $f->normalizeURLForCacheKey($f->normalizeUrlString($_COOKIE[$cookieName])); // delete the cookie because the request was a one-time thing. // we use javascript to delete the cookie because the headers have already been sent. $content .= " \n"; } // we delete the UPDATE_URL cookie here, where the shortcode is used so that it won't // get deleted too early if multiple redirects happen. $updateURLCookieName = ABJ404_PP . '_REQUEST_URI'; $updateURLCookieName .= '_UPDATE_URL'; if (isset($_COOKIE[$updateURLCookieName]) && !empty($_COOKIE[$updateURLCookieName])) { // Use UPDATE_URL cookie as fallback if primary cookie wasn't set // (fixes: manual redirects to custom 404 pages not showing suggestions) if ($urlRequest == '') { // Normalize URL using centralized function for consistency $urlRequest = $f->normalizeURLForCacheKey($f->normalizeUrlString($_COOKIE[$updateURLCookieName])); } // delete the cookie since we're done with it. it's a one-time use thing. $content .= " \n"; } if (isset($_REQUEST[ABJ404_PP]) && isset($_REQUEST[ABJ404_PP][$cookieName])) { // Normalize URL using centralized function for consistency $urlRequest = $f->normalizeURLForCacheKey($f->normalizeUrlString($_REQUEST[ABJ404_PP][$cookieName])); } // Fallback: check for URL passed via query parameter // (fixes: cookies from 301 redirects aren't stored by browsers) $queryParamName = ABJ404_PP . '_ref'; if ($urlRequest == '' && isset($_GET[$queryParamName]) && !empty($_GET[$queryParamName])) { // Normalize URL using centralized function for consistency $urlRequest = $f->normalizeURLForCacheKey($f->normalizeUrlString($_GET[$queryParamName])); } if ($urlRequest == '') { // if no 404 was detected then we don't offer any suggestions return "\n"; } // Check for cached suggestion computation (transient-based) $urlKey = md5($urlRequest); $transientKey = 'abj404_suggest_' . $urlKey; $cachedData = get_transient($transientKey); if ($cachedData !== false) { if (isset($cachedData['status']) && $cachedData['status'] === 'complete') { // Suggestions ready - use cached data $content .= self::renderSuggestionsHTML( isset($cachedData['suggestions']) ? $cachedData['suggestions'] : array(), $urlRequest ); $content .= "\n\n"; return $content; } elseif (isset($cachedData['status']) && $cachedData['status'] === 'pending') { // Still computing - show loading placeholder self::enqueueAsyncPollingScript($urlRequest); $content .= self::renderAsyncPlaceholder($urlRequest, $options); $content .= "\n\n"; return $content; } } // No async data - fall back to synchronous computation $urlSlugOnly = $abj404logic->removeHomeDirectory($urlRequest); // Try cache first (populated by processRedirect() for existing redirects) $permalinkSuggestionsPacket = $abj404spellChecker->getFromPermalinkCache($urlSlugOnly); // If cache miss, compute suggestions if (empty($permalinkSuggestionsPacket) || empty($permalinkSuggestionsPacket[0])) { $permalinkSuggestionsPacket = $abj404spellChecker->findMatchingPosts($urlSlugOnly, @$options['suggest_cats'], @$options['suggest_tags']); } // Ensure suggestions is an array (cache may return stdClass from json_decode) $permalinkSuggestions = isset($permalinkSuggestionsPacket[0]) ? (array)$permalinkSuggestionsPacket[0] : []; $rowType = isset($permalinkSuggestionsPacket[1]) ? $permalinkSuggestionsPacket[1] : 'pages'; $showExtraAdminData = (is_user_logged_in() && $abj404logic->userIsPluginAdmin()); $extraData = null; $extraDataById = []; // <--- New: Array to hold extra data indexed by ID $adminDebugData = []; // <--- New: Array to collect data for JS if ($showExtraAdminData) { // add extra information to the permalinkSuggestionsPacket. for each permalink, // retrieve the post_type, taxonomy, post_author (this is an id not a name), // post_date, post_name (this is the slug), $postIDs = array_keys($permalinkSuggestions); if (!empty($postIDs)) { // for each id remove the part after '|' using substring foreach ($postIDs as $index => $id) { $postIDs[$index] = $f->substr($id, 0, $f->strpos($id, '|')); } $rawExtraData = $abj404dao->getExtraDataToPermalinkSuggestions($postIDs); foreach ($rawExtraData as $dataItem) { $extraDataById['post_id_' . $dataItem['post_id']] = $dataItem; $extraDataById['term_id_' . $dataItem['term_id']] = $dataItem; } } } // allow some HTML. $content .= '