PluginProbe
404 Solution / 4.3.3
404 Solution v4.3.3
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / view / RedirectDestinationResolver.php

RedirectDestinationResolver.php in 404 Solution 4.3.3, at includes/view/RedirectDestinationResolver.php

59 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) {
4 exit;
5 }
6
7 /**
8 * Resolves the edit-form destination view-model from a stored redirect row.
9 *
10 * This is the form's data-prep step, deliberately kept separate from
11 * {@see ABJ_404_Solution_RedirectEditFormPresenter}, which only renders HTML
12 * templates. Given a redirect row and the plugin options it computes the three
13 * values the edit form needs to seed its controls:
14 *
15 * - final the external destination string (only for external redirects)
16 * - pageIDAndType the "{id}|{type}" composite key the redirect-to dropdown uses
17 * - codeSelected the redirect HTTP code, falling back to the configured default
18 *
19 * It encodes redirect-type rules (external vs. page vs. 404-displayed) and the
20 * default-code fallback, so it is pure, dependency-free, and independently
21 * testable.
22 */
23 class ABJ_404_Solution_RedirectDestinationResolver {
24
25 /**
26 * Resolve final destination, pageIDAndType, and redirect code from a redirect row.
27 *
28 * @param array<string, mixed> $redirect
29 * @param array<string, mixed> $options
30 * @return array{final: string, pageIDAndType: string, codeSelected: string}
31 */
32 public function resolveRedirectDestinationInfo(array $redirect, array $options): array {
33 $final = "";
34 $pageIDAndType = "";
35 $redirectTypeRaw = $redirect['type'] ?? '';
36 $redirectType = is_scalar($redirectTypeRaw) ? (string)$redirectTypeRaw : '';
37 $redirectFinalDestRaw = $redirect['final_dest'] ?? 0;
38 $redirectFinalDest = is_scalar($redirectFinalDestRaw) ? (string)$redirectFinalDestRaw : '0';
39 if ($redirectType === (string)ABJ404_TYPE_EXTERNAL) {
40 $final = $redirectFinalDest;
41 $pageIDAndType = ABJ404_TYPE_EXTERNAL . "|" . ABJ404_TYPE_EXTERNAL;
42 } else if ($redirectFinalDest != 0) {
43 $pageIDAndType = $redirectFinalDest . "|" . $redirectType;
44 } else if ($redirectType === (string)ABJ404_TYPE_404_DISPLAYED) {
45 $pageIDAndType = ABJ404_TYPE_404_DISPLAYED . "|" . ABJ404_TYPE_404_DISPLAYED;
46 }
47
48 $rawCode = $redirect['code'] ?? '';
49 if ($rawCode == "") {
50 $rawDefault = $options['default_redirect'] ?? '301';
51 $codeSelected = is_string($rawDefault) ? $rawDefault : '301';
52 } else {
53 $codeSelected = is_string($rawCode) ? $rawCode : '301';
54 }
55
56 return array('final' => $final, 'pageIDAndType' => $pageIDAndType, 'codeSelected' => $codeSelected);
57 }
58 }
59