| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Handles captured-row status link actions such as ignore and organize later. |
| 9 |
*/ |
| 10 |
class ABJ_404_Solution_StatusLinkActionHandler { |
| 11 |
|
| 12 |
/** @var ABJ_404_Solution_PluginLogicAdminActions */ |
| 13 |
private $parent; |
| 14 |
|
| 15 |
public function __construct(ABJ_404_Solution_PluginLogicAdminActions $parent) { |
| 16 |
$this->parent = $parent; |
| 17 |
} |
| 18 |
|
| 19 |
/** @return string */ |
| 20 |
public function handleIgnoreAction(): string { |
| 21 |
return $this->handleStatusUpdate('ignore', 'abj404_ignore404', ABJ404_STATUS_IGNORED, 'ignore', 'ignored'); |
| 22 |
} |
| 23 |
|
| 24 |
/** @return string */ |
| 25 |
public function handleLaterAction(): string { |
| 26 |
return $this->handleStatusUpdate('later', 'abj404_organizeLater', ABJ404_STATUS_LATER, 'organize later', 'organize later'); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* @param string $paramName The $_GET parameter name. |
| 31 |
* @param string $nonceAction The nonce action name. |
| 32 |
* @param int $activeStatus The status constant to use when action=1. |
| 33 |
* @param string $errorActionName Action name for error messages. |
| 34 |
* @param string $successActionName Action name for success messages. |
| 35 |
* @return string |
| 36 |
*/ |
| 37 |
private function handleStatusUpdate($paramName, $nonceAction, $activeStatus, $errorActionName, $successActionName): string { |
| 38 |
if (!isset($_GET[$paramName])) { |
| 39 |
return ''; |
| 40 |
} |
| 41 |
|
| 42 |
if (!is_admin() || !$this->parent->verifyLinkNonce($nonceAction)) { |
| 43 |
return ''; |
| 44 |
} |
| 45 |
|
| 46 |
$operation = trim(ABJ_404_Solution_RequestInputNormalizer::normalizeScalar($_GET[$paramName] ?? '')); |
| 47 |
if ($operation !== '0' && $operation !== '1') { |
| 48 |
$this->parent->getLogger()->debugMessage("Unexpected {$errorActionName} operation: " . |
| 49 |
esc_html($operation)); |
| 50 |
return sprintf(__('Error: Bad %s operation specified.', '404-solution'), $errorActionName); |
| 51 |
} |
| 52 |
|
| 53 |
$id = isset($_GET['id']) && is_scalar($_GET['id']) ? (string)$_GET['id'] : ''; |
| 54 |
if ($id === '' || !$this->parent->getFunctions()->regexMatch('[0-9]+', $id)) { |
| 55 |
return ''; |
| 56 |
} |
| 57 |
|
| 58 |
$newstatus = $operation === '1' ? $activeStatus : ABJ404_STATUS_CAPTURED; |
| 59 |
$message = $this->parent->getRedirectsRepo()->updateRedirectTypeStatus(absint($id), (string)$newstatus); |
| 60 |
if ($message == '') { |
| 61 |
if ($newstatus == ABJ404_STATUS_CAPTURED) { |
| 62 |
return sprintf(__('Removed 404 URL from %s list successfully!', '404-solution'), $successActionName); |
| 63 |
} |
| 64 |
return sprintf(__('404 URL marked as %s successfully!', '404-solution'), $successActionName); |
| 65 |
} |
| 66 |
|
| 67 |
if ($newstatus == ABJ404_STATUS_CAPTURED) { |
| 68 |
return sprintf(__('Error: unable to remove URL from %s list', '404-solution'), $successActionName); |
| 69 |
} |
| 70 |
return sprintf(__('Error: unable to mark URL as %s', '404-solution'), $successActionName); |
| 71 |
} |
| 72 |
} |
| 73 |
|