parent = $parent; } public function nonceAction(): string { return 'abj404_bulkProcess'; } public function nonceArg(): string { return '_wpnonce'; } public function useCheckAdminReferer(): bool { return true; } public function handle(string $action, string &$sub): string { if (!isset($_POST['idnum']) || !is_array($_POST['idnum'])) { $this->parent->getLogger()->debugMessage( "No ID(s) specified for bulk action: " . esc_html($action)); echo sprintf(__("Error: No ID(s) specified for bulk action. (%s)", '404-solution'), esc_html($action)); return ''; } $ids = array(); foreach ($_POST['idnum'] as $rawId) { if (is_scalar($rawId)) { $ids[] = absint($rawId); } } $message = $this->doBulkAction($action, $ids); return $message; } /** * Iterate the given ids and apply the per-row operation implied by the * bulk action verb. Returns a human-readable summary. Public so the * legacy PluginLogicAdminActions::doBulkAction() shim and existing tests * can call it directly without going through handle()'s POST gate. * * @param string $action * @param array $ids * @return string */ public function doBulkAction(string $action, array $ids): string { $message = ""; $logger = $this->parent->getLogger(); $redirectsRepo = $this->parent->getRedirectsRepo(); $logger->debugMessage("In doBulkAction. Action: " . esc_html($action == '' ? '(none)' : $action) . ", ids: " . wp_kses_post((string)json_encode($ids))); if ($action == "bulkignore" || $action == "bulkcaptured" || $action == "bulklater" || $action == "bulk_trash_restore") { $status = 0; if ($action == "bulkignore") { $status = ABJ404_STATUS_IGNORED; } else if ($action == "bulkcaptured") { $status = ABJ404_STATUS_CAPTURED; } else if ($action == "bulklater") { $status = ABJ404_STATUS_LATER; } $count = 0; foreach ($ids as $id) { $s = $redirectsRepo->moveRedirectsToTrash($id, 0); if ($action != "bulk_trash_restore") { $s = $redirectsRepo->updateRedirectTypeStatus($id, (string)$status); } if ($s == "") { $count++; } } if ($action == "bulkignore") { $message = $count . " " . __('URL(s) marked as Ignored.', '404-solution'); } else if ($action == "bulkcaptured") { $message = $count . " " . __('URL(s) marked as Captured.', '404-solution'); } else if ($action == "bulklater") { $message = $count . " " . __('URL(s) marked as Later.', '404-solution'); } else { $message = $count . " " . __('URL(s) restored.', '404-solution'); } } else if ($action == "bulk_trash_delete_permanently") { $count = 0; foreach ($ids as $id) { $redirectsRepo->deleteRedirect(absint($id)); $count ++; } $message = $count . " " . __('URL(s) deleted', '404-solution'); } else if ($action == "bulktrash") { $count = 0; foreach ($ids as $id) { $s = $redirectsRepo->moveRedirectsToTrash($id, 1); if ($s == "") { $count ++; } } $message = $count . " " . __('URL(s) moved to trash', '404-solution'); } else { $logger->errorMessage("Unrecognized bulk action: " . esc_html($action)); echo sprintf(__("Error: Unrecognized bulk action. (%s)", '404-solution'), esc_html($action)); } return $message; } }