| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Resolves the visit link and title for a redirect row destination. |
| 9 |
*/ |
| 10 |
class ABJ_404_Solution_RedirectDestinationLinkResolver { |
| 11 |
|
| 12 |
/** @var ABJ_404_Solution_Logging */ |
| 13 |
private $logger; |
| 14 |
|
| 15 |
public function __construct(ABJ_404_Solution_Logging $logger) { |
| 16 |
$this->logger = $logger; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* @param mixed $rowType |
| 21 |
* @param string $rowFinalDest |
| 22 |
* @return array{link: string, title: string} |
| 23 |
*/ |
| 24 |
public function resolve($rowType, string $rowFinalDest): array { |
| 25 |
$handlers = $this->handlers(); |
| 26 |
$key = is_scalar($rowType) ? (int)$rowType : -1; |
| 27 |
if (isset($handlers[$key])) { |
| 28 |
return $handlers[$key]($rowFinalDest); |
| 29 |
} |
| 30 |
|
| 31 |
// A corrupt redirect row with an unrecognized type is tolerable bad |
| 32 |
// data while rendering the table: we degrade to a bare "Visit" link. |
| 33 |
// Per Defensive Coding Philosophy #8 log this at WARNING level rather |
| 34 |
// than errorMessage() (which fires a production telemetry report). |
| 35 |
$this->logger->warn('Unexpected row type while displaying table: ' . $key); |
| 36 |
return array('link' => '', 'title' => __('Visit', '404-solution') . ' '); |
| 37 |
} |
| 38 |
|
| 39 |
/** @return array<int, callable(string): array{link: string, title: string}> */ |
| 40 |
private function handlers(): array { |
| 41 |
return array( |
| 42 |
ABJ404_TYPE_EXTERNAL => function(string $rowFinalDest): array { |
| 43 |
$title = __('Visit', '404-solution') . ' '; |
| 44 |
if ($rowFinalDest === '') { |
| 45 |
return array('link' => '', 'title' => $title); |
| 46 |
} |
| 47 |
return array('link' => $rowFinalDest, 'title' => $title . $rowFinalDest); |
| 48 |
}, |
| 49 |
ABJ404_TYPE_CAT => function(string $rowFinalDest): array { |
| 50 |
return $this->taxonomyDestination($rowFinalDest, ABJ404_TYPE_CAT, __('Category:', '404-solution')); |
| 51 |
}, |
| 52 |
ABJ404_TYPE_TAG => function(string $rowFinalDest): array { |
| 53 |
return $this->taxonomyDestination($rowFinalDest, ABJ404_TYPE_TAG, __('Tag:', '404-solution')); |
| 54 |
}, |
| 55 |
ABJ404_TYPE_HOME => function(string $rowFinalDest): array { |
| 56 |
return $this->permalinkDestination($rowFinalDest, ABJ404_TYPE_HOME, __('Home Page:', '404-solution') . ' '); |
| 57 |
}, |
| 58 |
ABJ404_TYPE_POST => function(string $rowFinalDest): array { |
| 59 |
if ($rowFinalDest === '') { |
| 60 |
return array('link' => '', 'title' => __('Visit', '404-solution') . ' '); |
| 61 |
} |
| 62 |
return $this->permalinkDestination($rowFinalDest, ABJ404_TYPE_POST, ''); |
| 63 |
}, |
| 64 |
ABJ404_TYPE_404_DISPLAYED => function(string $rowFinalDest): array { |
| 65 |
$result = $this->permalinkDestination($rowFinalDest, ABJ404_TYPE_404_DISPLAYED, ''); |
| 66 |
if ($rowFinalDest == '0') { |
| 67 |
$result['link'] = ''; |
| 68 |
} |
| 69 |
return $result; |
| 70 |
}, |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
/** @return array{link: string, title: string} */ |
| 75 |
private function taxonomyDestination(string $rowFinalDest, int $type, string $label): array { |
| 76 |
if ($rowFinalDest === '') { |
| 77 |
return array('link' => '', 'title' => __('Visit', '404-solution') . ' '); |
| 78 |
} |
| 79 |
return $this->permalinkDestination($rowFinalDest, $type, $label . ' '); |
| 80 |
} |
| 81 |
|
| 82 |
/** @return array{link: string, title: string} */ |
| 83 |
private function permalinkDestination(string $rowFinalDest, int $type, string $titlePrefix): array { |
| 84 |
$permalink = ABJ_404_Solution_PermalinkResolver::permalinkInfoToArray($rowFinalDest . '|' . $type, 0); |
| 85 |
$link = is_string($permalink['link']) ? $permalink['link'] : ''; |
| 86 |
$title = is_string($permalink['title']) ? $permalink['title'] : ''; |
| 87 |
return array('link' => $link, 'title' => __('Visit', '404-solution') . ' ' . $titlePrefix . $title); |
| 88 |
} |
| 89 |
} |
| 90 |
|