PluginProbe
404 Solution / 4.3.0
404 Solution v4.3.0
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / admin / actions / BulkActionHandler.php

BulkActionHandler.php in 404 Solution 4.3.0, at includes/admin/actions/BulkActionHandler.php

135 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) {
4 exit;
5 }
6
7 /**
8 * Handles any action whose verb starts with 'bulk' (bulktrash,
9 * bulk_trash_delete_permanently, bulk_trash_restore, bulkignore, bulklater,
10 * bulkcaptured). Validates that $_POST['idnum'] is present (echoes an error
11 * and returns '' if not, matching pre-refactor behavior), then iterates ids
12 * and applies the per-row status/trash/delete operation.
13 *
14 * Registered in the dispatcher's prefix-match map (not the exact-match map).
15 *
16 * The per-row dispatch logic previously lived on
17 * PluginLogicAdminActions::doBulkAction() (M201, design-audit-2026-06-02).
18 * That parent method is now a thin shim that calls doBulkAction() here so
19 * existing tests/callers do not break.
20 */
21 class ABJ_404_Solution_BulkActionHandler implements ABJ_404_Solution_AdminActionHandlerInterface {
22
23 /** @var ABJ_404_Solution_PluginLogicAdminActions */
24 private $parent;
25
26 public function __construct(ABJ_404_Solution_PluginLogicAdminActions $parent) {
27 $this->parent = $parent;
28 }
29
30 public function nonceAction(): string {
31 return 'abj404_bulkProcess';
32 }
33
34 public function nonceArg(): string {
35 return '_wpnonce';
36 }
37
38 public function useCheckAdminReferer(): bool {
39 return true;
40 }
41
42 public function handle(string $action, string &$sub): string {
43 if (!isset($_POST['idnum']) || !is_array($_POST['idnum'])) {
44 $this->parent->getLogger()->debugMessage(
45 "No ID(s) specified for bulk action: " . esc_html($action));
46 echo sprintf(__("Error: No ID(s) specified for bulk action. (%s)", '404-solution'),
47 esc_html($action));
48 return '';
49 }
50 $ids = array();
51 foreach ($_POST['idnum'] as $rawId) {
52 if (is_scalar($rawId)) {
53 $ids[] = absint($rawId);
54 }
55 }
56 $message = $this->doBulkAction($action, $ids);
57 return $message;
58 }
59
60 /**
61 * Iterate the given ids and apply the per-row operation implied by the
62 * bulk action verb. Returns a human-readable summary. Public so the
63 * legacy PluginLogicAdminActions::doBulkAction() shim and existing tests
64 * can call it directly without going through handle()'s POST gate.
65 *
66 * @param string $action
67 * @param array<int, int> $ids
68 * @return string
69 */
70 public function doBulkAction(string $action, array $ids): string {
71 $message = "";
72 $logger = $this->parent->getLogger();
73 $redirectsRepo = $this->parent->getRedirectsRepo();
74
75 $logger->debugMessage("In doBulkAction. Action: " .
76 esc_html($action == '' ? '(none)' : $action) . ", ids: " . wp_kses_post((string)json_encode($ids)));
77
78 if ($action == "bulkignore" || $action == "bulkcaptured" || $action == "bulklater" ||
79 $action == "bulk_trash_restore") {
80
81 $status = 0;
82 if ($action == "bulkignore") {
83 $status = ABJ404_STATUS_IGNORED;
84 } else if ($action == "bulkcaptured") {
85 $status = ABJ404_STATUS_CAPTURED;
86 } else if ($action == "bulklater") {
87 $status = ABJ404_STATUS_LATER;
88 }
89
90 $count = 0;
91 foreach ($ids as $id) {
92 $s = $redirectsRepo->moveRedirectsToTrash($id, 0);
93 if ($action != "bulk_trash_restore") {
94 $s = $redirectsRepo->updateRedirectTypeStatus($id, (string)$status);
95 }
96 if ($s == "") {
97 $count++;
98 }
99 }
100 if ($action == "bulkignore") {
101 $message = $count . " " . __('URL(s) marked as Ignored.', '404-solution');
102 } else if ($action == "bulkcaptured") {
103 $message = $count . " " . __('URL(s) marked as Captured.', '404-solution');
104 } else if ($action == "bulklater") {
105 $message = $count . " " . __('URL(s) marked as Later.', '404-solution');
106 } else {
107 $message = $count . " " . __('URL(s) restored.', '404-solution');
108 }
109
110 } else if ($action == "bulk_trash_delete_permanently") {
111 $count = 0;
112 foreach ($ids as $id) {
113 $redirectsRepo->deleteRedirect(absint($id));
114 $count ++;
115 }
116 $message = $count . " " . __('URL(s) deleted', '404-solution');
117
118 } else if ($action == "bulktrash") {
119 $count = 0;
120 foreach ($ids as $id) {
121 $s = $redirectsRepo->moveRedirectsToTrash($id, 1);
122 if ($s == "") {
123 $count ++;
124 }
125 }
126 $message = $count . " " . __('URL(s) moved to trash', '404-solution');
127
128 } else {
129 $logger->errorMessage("Unrecognized bulk action: " . esc_html($action));
130 echo sprintf(__("Error: Unrecognized bulk action. (%s)", '404-solution'), esc_html($action));
131 }
132 return $message;
133 }
134 }
135