parent = $parent; } /** * Move a redirect to trash or restore it from trash, depending on * $_GET['trash']. Returns the message to display back to the admin * (or '' when no trash request is present in this hit). * * @return string */ public function handle(): string { $message = ""; if (!isset($_GET['trash'])) { return $message; } if (!is_admin() || !$this->parent->verifyLinkNonce('abj404_trashRedirect')) { return $message; } $trash = ""; if ($_GET['trash'] == 0) { $trash = 0; } else if ($_GET['trash'] == 1) { $trash = 1; } else { $this->parent->getLogger()->errorMessage("Unexpected trash operation: " . esc_html(ABJ_404_Solution_RequestInputNormalizer::normalizeScalar($_GET['trash']))); return __('Error: Bad trash operation specified.', '404-solution'); } $id = ABJ_404_Solution_ExactInteger::read($_GET['id'] ?? null, 1); if ($id === null) { return __('Error:', '404-solution') . ' Invalid redirect ID.'; } $subpage = isset($_GET['subpage']) ? sanitize_text_field(wp_unslash($_GET['subpage'])) : ''; $filter = isset($_GET['filter']) ? intval($_GET['filter']) : 0; $capturedRestore = $trash == 0 && $subpage === 'abj404_captured' && $filter === ABJ404_TRASH_FILTER; try { $message = $capturedRestore ? $this->restoreCapturedRedirect($id) : $this->parent->getRedirectsRepo()->moveRedirectsToTrash($id, $trash); } catch (ABJ_404_Solution_RedirectCycleException $e) { return __('Error:', '404-solution') . ' ' . esc_html($e->getMessage()); } if ($message == "") { if ($trash == 1) { return __('Redirect moved to trash successfully!', '404-solution'); } return __('Redirect restored from trash successfully!', '404-solution'); } if ($trash == 1) { return __('Error: Unable to move redirect to trash.', '404-solution'); } return __('Error:', '404-solution') . ' ' . esc_html($message); } /** Restore both captured fields only when the repository exposes the atomic capability. */ private function restoreCapturedRedirect(int $id): string { $repository = $this->parent->getRedirectsRepo(); if (!method_exists($repository, 'restoreRedirect') || !is_callable(array($repository, 'restoreRedirect'))) { return 'Atomic redirect restore is unavailable for this repository.'; } $message = $repository->restoreRedirect(array('id' => $id, 'status' => ABJ404_STATUS_CAPTURED)); return is_string($message) ? $message : 'Atomic redirect restore returned an invalid response (' . gettype($message) . ').'; } }