| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Handles action 'purgeRedirects': parses the admin form request, delegates |
| 9 |
* the data mutation to the redirects repository, then renders the admin |
| 10 |
* result message. |
| 11 |
*/ |
| 12 |
class ABJ_404_Solution_PurgeRedirectsHandler implements ABJ_404_Solution_AdminActionHandlerInterface { |
| 13 |
|
| 14 |
/** @var ABJ_404_Solution_PluginLogicAdminActions */ |
| 15 |
private $parent; |
| 16 |
|
| 17 |
public function __construct(ABJ_404_Solution_PluginLogicAdminActions $parent) { |
| 18 |
$this->parent = $parent; |
| 19 |
} |
| 20 |
|
| 21 |
public function nonceAction(): string { |
| 22 |
return 'abj404_purgeRedirects'; |
| 23 |
} |
| 24 |
|
| 25 |
public function nonceArg(): string { |
| 26 |
return '_wpnonce'; |
| 27 |
} |
| 28 |
|
| 29 |
public function useCheckAdminReferer(): bool { |
| 30 |
return true; |
| 31 |
} |
| 32 |
|
| 33 |
public function handle(string $action, string &$sub): string { |
| 34 |
$request = $this->parseRequest(); |
| 35 |
if ($request['status'] !== 'ok') { |
| 36 |
return $this->messageForStatus($request['status'], 0); |
| 37 |
} |
| 38 |
|
| 39 |
$result = $this->parent->getRedirectsRepo()->deleteSpecifiedRedirects( |
| 40 |
$request['types'], |
| 41 |
$request['purge_type'] |
| 42 |
); |
| 43 |
|
| 44 |
$status = isset($result['status']) && is_string($result['status']) ? $result['status'] : 'unknown'; |
| 45 |
$rowsAffected = isset($result['rows_affected']) && is_scalar($result['rows_affected']) |
| 46 |
? (int)$result['rows_affected'] |
| 47 |
: 0; |
| 48 |
|
| 49 |
return $this->messageForStatus($status, $rowsAffected); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Parse and validate the purge form POST payload. |
| 54 |
* |
| 55 |
* @return array{status: string, types: array<int, int|string>, purge_type: string} |
| 56 |
*/ |
| 57 |
private function parseRequest(): array { |
| 58 |
if (!array_key_exists('sanity_purge', $_POST) || $_POST['sanity_purge'] != "1") { |
| 59 |
return array('status' => 'missing_sanity_purge', 'types' => array(), 'purge_type' => ''); |
| 60 |
} |
| 61 |
|
| 62 |
if (!isset($_POST['types']) || $_POST['types'] == '') { |
| 63 |
return array('status' => 'missing_types', 'types' => array(), 'purge_type' => ''); |
| 64 |
} |
| 65 |
|
| 66 |
$rawTypes = $_POST['types']; |
| 67 |
if (!is_array($rawTypes)) { |
| 68 |
return array('status' => 'unknown', 'types' => array(), 'purge_type' => ''); |
| 69 |
} |
| 70 |
|
| 71 |
$types = array_map(static function($value): string { |
| 72 |
return sanitize_text_field(is_scalar($value) ? (string)$value : ''); |
| 73 |
}, $rawTypes); |
| 74 |
$purgeType = ABJ_404_Solution_RequestInputNormalizer::readText( |
| 75 |
$_POST, array('name' => 'purgetype')); |
| 76 |
|
| 77 |
if ($purgeType != 'abj404_logs' && $purgeType != 'abj404_redirects') { |
| 78 |
$this->parent->getLogger()->debugMessage("Error: An invalid purge type was selected. Type: " . |
| 79 |
wp_kses_post((string)json_encode($purgeType))); |
| 80 |
return array('status' => 'invalid_purge_type', 'types' => array(), 'purge_type' => ''); |
| 81 |
} |
| 82 |
|
| 83 |
return array('status' => 'ok', 'types' => $types, 'purge_type' => $purgeType); |
| 84 |
} |
| 85 |
|
| 86 |
private function messageForStatus(string $status, int $rowsAffected): string { |
| 87 |
switch ($status) { |
| 88 |
case 'missing_sanity_purge': |
| 89 |
return __('Error: You didn\'t check the I understand checkbox. No purging of records for you!', '404-solution'); |
| 90 |
case 'missing_types': |
| 91 |
return __('Error: No redirect types were selected. No purges will be done.', '404-solution'); |
| 92 |
case 'unknown': |
| 93 |
return __('An unknown error has occurred.', '404-solution'); |
| 94 |
case 'invalid_purge_type': |
| 95 |
return __('Error: An invalid purge type was selected. Exiting.', '404-solution'); |
| 96 |
case 'no_valid_types': |
| 97 |
return __('Error: No valid redirect types were selected. Exiting.', '404-solution'); |
| 98 |
case 'redirects_purged': |
| 99 |
return sprintf(_n('%s redirect entry was moved to the trash.', |
| 100 |
'%s redirect entries were moved to the trash.', $rowsAffected, '404-solution'), $rowsAffected); |
| 101 |
case 'logs_only': |
| 102 |
case 'noop': |
| 103 |
return ''; |
| 104 |
} |
| 105 |
|
| 106 |
return sprintf(__('Error: Unknown purge result: %s', '404-solution'), esc_html($status)); |
| 107 |
} |
| 108 |
} |
| 109 |
|