PluginProbe
BeyondWords – AI audio for publishers / 4.5.0
BeyondWords – AI audio for publishers v4.5.0
7.2.0 7.1.0 trunk 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.3.0 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 All 44 releases
speechkit / src / Component / Posts / BulkEdit / BulkEdit.php

BulkEdit.php in BeyondWords – AI audio for publishers 4.5.0, at src/Component/Posts/BulkEdit/BulkEdit.php

296 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 /**
6 * BeyondWords Bulk Edit.
7 *
8 * Text Domain: beyondwords
9 *
10 * @package Beyondwords\Wordpress
11 * @author Stuart McAlpine <[email protected]>
12 * @since 3.0.0
13 */
14
15 namespace Beyondwords\Wordpress\Component\Posts\BulkEdit;
16
17 use Beyondwords\Wordpress\Core\Core;
18 use Beyondwords\Wordpress\Core\CoreUtils;
19 use Beyondwords\Wordpress\Component\Posts\BulkEdit\Notices;
20 use Beyondwords\Wordpress\Component\Settings\SettingsUtils;
21 use Beyondwords\Wordpress\Plugin;
22
23 /**
24 * BulkEdit setup
25 *
26 * @since 3.0.0
27 */
28 class BulkEdit
29 {
30 /**
31 * Init.
32 *
33 * @since 4.0.0
34 */
35 public function init()
36 {
37 add_action('bulk_edit_custom_box', array($this, 'bulkEditCustomBox'), 10, 2);
38 add_action('wp_ajax_save_bulk_edit_beyondwords', array($this, 'saveBulkEdit'));
39
40 add_action('wp_loaded', function () {
41 $postTypes = SettingsUtils::getCompatiblePostTypes();
42
43 if (is_array($postTypes)) {
44 foreach ($postTypes as $postType) {
45 add_filter("bulk_actions-edit-{$postType}", array($this, 'bulkActionsEdit'), 10, 1);
46 add_filter("handle_bulk_actions-edit-{$postType}", array($this, 'handleBulkDeleteAction'), 10, 3);
47 add_filter("handle_bulk_actions-edit-{$postType}", array($this, 'handleBulkGenerateAction'), 10, 3);
48 }
49 }
50 });
51 }
52
53 /**
54 * Adds the meta box container.
55 */
56 public function bulkEditCustomBox($columnName, $postType)
57 {
58 if ($columnName !== 'beyondwords') {
59 return;
60 }
61
62 $postTypes = SettingsUtils::getCompatiblePostTypes();
63
64 if (! in_array($postType, $postTypes)) {
65 return;
66 }
67
68 wp_nonce_field('beyondwords_bulk_edit_nonce', 'beyondwords_bulk_edit');
69
70 ?>
71 <fieldset class="inline-edit-col-right">
72 <div class="inline-edit-col">
73 <div class="inline-edit-group wp-clearfix">
74 <label class="alignleft">
75 <span class="title"><?php _e('BeyondWords', 'speechkit'); ?></span>
76 <select name="beyondwords_generate_audio">
77 <option value="-1"><?php _e('— No change —', 'speechkit'); ?></option>
78 <option value="generate"><?php _e('Generate audio', 'speechkit'); ?></option>
79 <option value="delete"><?php _e('Delete audio', 'speechkit'); ?></option>
80 </select>
81 </label>
82 </div>
83 </div>
84 </fieldset>
85 <?php
86 }
87
88 /**
89 * Save Bulk Edit updates.
90 *
91 * @link https://rudrastyh.com/wordpress/bulk-edit.html
92 */
93 public function saveBulkEdit()
94 {
95 /*
96 * We need to verify this came from the our screen and with proper authorization,
97 * because save_post can be triggered at other times.
98 */
99 if (
100 ! isset($_POST['beyondwords_bulk_edit_nonce']) ||
101 ! wp_verify_nonce(sanitize_text_field($_POST['beyondwords_bulk_edit_nonce']), 'beyondwords_bulk_edit')
102 ) {
103 wp_nonce_ays('');
104 }
105
106 if (! isset($_POST['beyondwords_bulk_edit']) || ! isset($_POST['post_ids'])) {
107 return [];
108 }
109
110 if (is_array($_POST['post_ids']) && count($_POST['post_ids'])) {
111 $postIds = array_map('intval', $_POST['post_ids']);
112 $postIds = array_filter($postIds);
113
114 switch ($_POST['beyondwords_bulk_edit']) {
115 case 'generate':
116 return $this->generateAudioForPosts($postIds);
117 break;
118 case 'delete':
119 return $this->deleteAudioForPosts($postIds);
120 break;
121 }
122 }
123
124 return [];
125 }
126
127 public function generateAudioForPosts($postIds)
128 {
129 if (! is_array($postIds)) {
130 return false;
131 }
132
133 $updatedPostIds = [];
134
135 foreach ($postIds as $postId) {
136 if (! get_post_meta($postId, 'beyondwords_content_id', true)) {
137 update_post_meta($postId, 'beyondwords_generate_audio', '1');
138 }
139 $updatedPostIds[] = $postId;
140 }
141
142 return $updatedPostIds;
143 }
144
145 /**
146 * @SuppressWarnings(PHPMD.LongVariable)
147 */
148 public function deleteAudioForPosts($postIds)
149 {
150 global $beyondwords_wordpress_plugin;
151
152 if (! is_array($postIds)) {
153 return false;
154 }
155
156 $updatedPostIds = [];
157
158 $response = $beyondwords_wordpress_plugin->core->batchDeleteAudioForPosts($postIds);
159
160 if (! $response) {
161 throw new \Exception('Error while bulk deleting audio. Please contact support with reference BULK-NO-RESPONSE.'); // phpcs:ignore Generic.Files.LineLength.TooLong
162 }
163
164 // Now process all posts
165 $keys = CoreUtils::getPostMetaKeys('all');
166
167 foreach ($response as $postId) {
168 foreach ($keys as $key) {
169 delete_post_meta($postId, $key);
170 }
171 $updatedPostIds[] = $postId;
172 }
173
174 return $updatedPostIds;
175 }
176
177 /**
178 *
179 */
180 public function bulkActionsEdit($bulk_array)
181 {
182 $bulk_array['beyondwords_generate_audio'] = __('Generate audio', 'speechkit');
183 $bulk_array['beyondwords_delete_audio'] = __('Delete audio', 'speechkit');
184
185 return $bulk_array;
186 }
187
188 /**
189 * @SuppressWarnings(PHPMD.LongVariable)
190 */
191 public function handleBulkGenerateAction($redirect, $doaction, $objectIds)
192 {
193 if ($doaction !== 'beyondwords_generate_audio') {
194 return $redirect;
195 }
196
197 global $beyondwords_wordpress_plugin;
198
199 // Remove query args
200 $args = [
201 'beyondwords_bulk_generated',
202 'beyondwords_bulk_deleted',
203 'beyondwords_bulk_failed',
204 'beyondwords_bulk_error',
205 ];
206
207 $redirect = remove_query_arg($args, $redirect);
208
209 // Order batch by Post ID asc
210 sort($objectIds);
211
212 $generated = 0;
213 $failed = 0;
214
215 try {
216 // Update all custom fields before attempting any processing
217 foreach ($objectIds as $postId) {
218 update_post_meta($postId, 'beyondwords_generate_audio', '1');
219 }
220
221 // Now process all posts
222 foreach ($objectIds as $postId) {
223 if (
224 $beyondwords_wordpress_plugin instanceof Plugin
225 && $beyondwords_wordpress_plugin->core instanceof Core
226 ) {
227 $response = $beyondwords_wordpress_plugin->core->generateAudioForPost($postId);
228
229 if ($response) {
230 $generated++;
231 } else {
232 $failed++;
233 }
234 } else {
235 throw new \Exception('Error while bulk generating audio. Please contact support with reference BULK-NO-PLUGIN.'); // phpcs:ignore Generic.Files.LineLength.TooLong
236 }
237 }
238 } catch (\Exception $e) {
239 $redirect = add_query_arg('beyondwords_bulk_error', $e->getMessage(), $redirect);
240 }
241
242 // Add $generated & $failed query args into redirect
243 $redirect = add_query_arg('beyondwords_bulk_generated', $generated, $redirect);
244 $redirect = add_query_arg('beyondwords_bulk_failed', $failed, $redirect);
245
246 // Add nonce to redirect url
247 $nonce = wp_create_nonce('beyondwords_bulk_edit_result');
248 $redirect = add_query_arg('beyondwords_bulk_edit_result_nonce', $nonce, $redirect);
249
250 return $redirect;
251 }
252
253 /**
254 *
255 */
256 public function handleBulkDeleteAction($redirect, $doaction, $objectIds)
257 {
258 if ($doaction !== 'beyondwords_delete_audio') {
259 return $redirect;
260 }
261
262 // Remove query args
263 $args = [
264 'beyondwords_bulk_generated',
265 'beyondwords_bulk_deleted',
266 'beyondwords_bulk_failed',
267 'beyondwords_bulk_error',
268 ];
269
270 $redirect = remove_query_arg($args, $redirect);
271
272 // Order batch by Post ID asc
273 sort($objectIds);
274
275 $deleted = 0;
276
277 // Handle "Delete audio" bulk action
278 try {
279 $result = $this->deleteAudioForPosts($objectIds);
280
281 $deleted = count($result);
282
283 // Add $deleted query arg into redirect
284 $redirect = add_query_arg('beyondwords_bulk_deleted', $deleted, $redirect);
285 } catch (\Exception $e) {
286 $redirect = add_query_arg('beyondwords_bulk_error', $e->getMessage(), $redirect);
287 }
288
289 // Add $nonce query arg into redirect
290 $nonce = wp_create_nonce('beyondwords_bulk_edit_result');
291 $redirect = add_query_arg('beyondwords_bulk_edit_result_nonce', $nonce, $redirect);
292
293 return $redirect;
294 }
295 }
296