PluginProbe
Auto Alt Text / 2.7.0
Auto Alt Text v2.7.0
3.0.3 2.8.2 1.3.1 1.3.2 2.0.0 2.1.0 2.1.1 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.7.0 2.8.0 2.8.1 All 28 releases
auto-alt-text / src / App / Admin / BulkActions / BulkActionHandler.php

BulkActionHandler.php in Auto Alt Text 2.7.0, at src/App/Admin/BulkActions/BulkActionHandler.php

189 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AATXT\App\Admin\BulkActions;
4
5 use AATXT\Config\Constants;
6
7 /**
8 * Handler for managing and executing bulk actions in the media library.
9 *
10 * This class is responsible for:
11 * - Registering bulk actions with WordPress
12 * - Handling bulk action execution
13 * - Displaying admin notices with results
14 */
15 final class BulkActionHandler
16 {
17 /**
18 * Registered bulk actions
19 *
20 * @var array<string, BulkActionInterface>
21 */
22 private $actions;
23
24 /**
25 * Constructor
26 */
27 public function __construct()
28 {
29 $this->actions = [];
30 }
31
32 /**
33 * Register a bulk action.
34 *
35 * @param BulkActionInterface $action The bulk action to register
36 * @return void
37 */
38 public function register(BulkActionInterface $action): void
39 {
40 $this->actions[$action->getName()] = $action;
41 }
42
43 /**
44 * Get all registered action names and labels for WordPress bulk actions filter.
45 *
46 * @param array<string, string> $actions Existing WordPress bulk actions
47 * @return array<string, string> Modified bulk actions array
48 */
49 public function getBulkActions(array $actions): array
50 {
51 foreach ($this->actions as $action) {
52 $actions[$action->getName()] = $action->getLabel();
53 }
54
55 return $actions;
56 }
57
58 /**
59 * Handle bulk action execution from media library.
60 *
61 * Checks for the current action, verifies permissions and nonce,
62 * executes the action, and redirects with results.
63 *
64 * @return void
65 */
66 public function handleBulkAction(): void
67 {
68 if (!current_user_can('upload_files')) {
69 return;
70 }
71
72 $wpListTable = _get_list_table('WP_Media_List_Table');
73 $currentAction = $wpListTable->current_action();
74
75 if (!isset($this->actions[$currentAction])) {
76 return;
77 }
78
79 // Protect against CSRF attacks
80 check_admin_referer('bulk-media');
81
82 $mediaIds = isset($_REQUEST['media']) ? (array) $_REQUEST['media'] : [];
83
84 if (empty($mediaIds)) {
85 return;
86 }
87
88 $action = $this->actions[$currentAction];
89 $result = $action->execute($mediaIds);
90
91 // Redirect with result data
92 $redirectArgs = [
93 'bulk_action_name' => $currentAction,
94 'mediaSelected' => $result->getTotal(),
95 'mediaUpdated' => $result->getUpdated(),
96 ];
97
98 // Preserve Media Library pagination when redirecting after bulk action
99 $sendback = wp_get_referer();
100 if (empty($sendback)) {
101 $sendback = admin_url('upload.php');
102 }
103
104 $sendback = remove_query_arg([
105 'action',
106 'action2',
107 'media',
108 '_wpnonce',
109 '_wp_http_referer',
110 'auto_alt_text',
111 'mediaSelected',
112 'mediaUpdated',
113 ], $sendback);
114
115 $sendback = add_query_arg($redirectArgs, $sendback);
116
117 wp_safe_redirect($sendback);
118 exit();
119 }
120
121 /**
122 * Display admin notice after bulk action execution.
123 *
124 * Shows success, warning, or error notice based on results.
125 *
126 * @return void
127 */
128 public function displayAdminNotice(): void
129 {
130 if (!isset($_REQUEST['bulk_action_name'])) {
131 return;
132 }
133
134 $actionName = sanitize_text_field($_REQUEST['bulk_action_name']);
135
136 if (!isset($this->actions[$actionName])) {
137 return;
138 }
139
140 $mediaSelected = isset($_REQUEST['mediaSelected']) ? intval($_REQUEST['mediaSelected']) : 0;
141 $mediaUpdated = isset($_REQUEST['mediaUpdated']) ? intval($_REQUEST['mediaUpdated']) : 0;
142
143 $result = new BulkActionResult($mediaSelected, $mediaUpdated);
144
145 $this->renderNotice($result);
146 }
147
148 /**
149 * Render the appropriate admin notice based on the result.
150 *
151 * @param BulkActionResult $result The bulk action result
152 * @return void
153 */
154 private function renderNotice(BulkActionResult $result): void
155 {
156 $errorLogDisclaimer = __('Take a look at the', 'auto-alt-text')
157 . ' <a href="' . esc_url(menu_page_url(Constants::AATXT_PLUGIN_OPTION_LOG_PAGE_SLUG, false)) . '">'
158 . __('error log', 'auto-alt-text') . '</a>.';
159
160 if ($result->hasNoUpdates()) {
161 printf(
162 '<div id="message" class="notice notice-error is-dismissible"><p>%s %s</p></div>',
163 esc_html__('No Alt Text has been set.', 'auto-alt-text'),
164 wp_kses_post($errorLogDisclaimer)
165 );
166 } elseif ($result->isComplete()) {
167 printf(
168 '<div id="message" class="updated notice is-dismissible"><p>%s</p></div>',
169 sprintf(
170 /* translators: %s = number of images processed */
171 esc_html__('The Alt Text has been set for %s media.', 'auto-alt-text'),
172 number_format_i18n($result->getUpdated())
173 )
174 );
175 } else {
176 printf(
177 '<div id="message" class="notice notice-warning is-dismissible"><p>%s</p></div>',
178 sprintf(
179 /* translators: 1 = updated count, 2 = selected count, 3 = disclaimer HTML */
180 __('The Alt Text has been set for %1$s of %2$s media. %3$s', 'auto-alt-text'),
181 number_format_i18n($result->getUpdated()),
182 number_format_i18n($result->getTotal()),
183 wp_kses_post($errorLogDisclaimer)
184 )
185 );
186 }
187 }
188 }
189