redirectsRepository = $redirectsRepository; $this->matchingEngineOrchestrator = $matchingEngineOrchestrator; $this->notFoundResponse = $notFoundResponse; $this->hitRecorder = $hitRecorder; } /** * @param string $requestedURL * @param string $urlSlugOnly * @param array $options * @param ABJ_404_Solution_FrontendPipelineTrace $trace * @return bool True when a matching engine produced and emitted a redirect. */ function promoteAndRedirectIfMatched( string $requestedURL, string $urlSlugOnly, array $options, ABJ_404_Solution_FrontendPipelineTrace $trace ): bool { $matchRequest = new ABJ_404_Solution_MatchRequest($requestedURL, $urlSlugOnly, $options); $matchResult = $this->matchingEngineOrchestrator->run($matchRequest, $trace); if ($matchResult === null) { return false; } $defaultRedirect = isset($options['default_redirect']) && is_scalar($options['default_redirect']) ? (string)$options['default_redirect'] : ''; $this->redirectsRepository->setupRedirect(ABJ_404_Solution_RedirectSpec::fromArray(array( 'fromURL' => $requestedURL, 'status' => (string)ABJ404_STATUS_AUTO, 'type' => $matchResult->getType(), 'finalDest' => $matchResult->getId(), 'code' => $defaultRedirect, 'disabled' => 0, 'engine' => $matchResult->getEngineName(), 'score' => $matchResult->getScore(), ))); $resolvedLink = $this->resolveMatchLink($matchResult); $this->hitRecorder->record($requestedURL, $resolvedLink, $matchResult->getEngineName(), null, $trace->getSteps()); return $this->notFoundResponse->forceRedirect(esc_url_raw($resolvedLink), (int)$defaultRedirect); } /** * @param ABJ_404_Solution_MatchResult $matchResult * @return string */ private function resolveMatchLink(ABJ_404_Solution_MatchResult $matchResult): string { $resolvedLink = $matchResult->getLink(); if ($matchResult->getId() === '' || $matchResult->getId() === '0') { return $resolvedLink; } $permalink = ABJ_404_Solution_PermalinkResolver::permalinkInfoToArray( $matchResult->getId() . '|' . $matchResult->getType(), 0 ); if (is_array($permalink) && !empty($permalink['link']) && is_string($permalink['link']) && $permalink['link'] !== 'dunno') { return $permalink['link']; } return $resolvedLink; } }