| 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 |
|