* @since 3.0.0 */ namespace Beyondwords\Wordpress\Component\Posts\BulkEdit; use Beyondwords\Wordpress\Core\Core; use Beyondwords\Wordpress\Core\CoreUtils; use Beyondwords\Wordpress\Component\Settings\SettingsUtils; use Beyondwords\Wordpress\Plugin; /** * BulkEdit setup * * @since 3.0.0 */ class BulkEdit { /** * Constructor */ public function __construct() { add_action('bulk_edit_custom_box', array($this, 'bulkEditCustomBox'), 10, 2); add_action('wp_ajax_save_bulk_edit_beyondwords', array($this, 'saveBulkEdit')); add_action('admin_notices', array($this, 'deletedNotice')); add_action('admin_notices', array($this, 'generatedNotice')); add_action('admin_notices', array($this, 'failedNotice')); add_action('admin_notices', array($this, 'errorNotice')); add_action('wp_loaded', function () { $postTypes = SettingsUtils::getSupportedPostTypes(); if (is_array($postTypes)) { foreach ($postTypes as $postType) { add_filter("bulk_actions-edit-{$postType}", array($this, 'bulkActionsEdit'), 10, 1); add_filter("handle_bulk_actions-edit-{$postType}", array($this, 'handleBulkDeleteAction'), 10, 3); add_filter("handle_bulk_actions-edit-{$postType}", array($this, 'handleBulkGenerateAction'), 10, 3); } } }); } /** * Adds the meta box container. */ public function bulkEditCustomBox($columnName, $postType) { if ($columnName !== 'beyondwords') { return; } $postTypes = SettingsUtils::getSupportedPostTypes(); if (! in_array($postType, $postTypes)) { return; } wp_nonce_field('beyondwords_bulk_edit_nonce', 'beyondwords_bulk_edit'); ?>
generateAudioForPosts($postIds); break; case 'delete': return $this->deleteAudioForPosts($postIds); break; } return []; } public function generateAudioForPosts($postIds) { if (! is_array($postIds)) { return false; } $updatedPostIds = []; foreach ($postIds as $postId) { if (! get_post_meta($postId, 'beyondwords_content_id', true)) { update_post_meta($postId, 'beyondwords_generate_audio', '1'); } $updatedPostIds[] = $postId; } return $updatedPostIds; } /** * @SuppressWarnings(PHPMD.LongVariable) */ public function deleteAudioForPosts($postIds) { global $beyondwords_wordpress_plugin; if (! is_array($postIds)) { return false; } $updatedPostIds = []; $response = $beyondwords_wordpress_plugin->core->batchDeleteAudioForPosts($postIds); if (! $response) { throw new \Exception('Error while bulk deleting audio. Please contact support with reference BULK-NO-RESPONSE.'); // phpcs:ignore } // Now process all posts $keys = CoreUtils::getPostMetaKeys('all'); foreach ($response as $postId) { foreach ($keys as $key) { delete_post_meta($postId, $key); } $updatedPostIds[] = $postId; } return $updatedPostIds; } /** * */ public function bulkActionsEdit($bulk_array) { $bulk_array['beyondwords_generate_audio'] = __('Generate audio', 'speechkit'); $bulk_array['beyondwords_delete_audio'] = __('Delete audio', 'speechkit'); return $bulk_array; } /** * @SuppressWarnings(PHPMD.LongVariable) */ public function handleBulkGenerateAction($redirect, $doaction, $objectIds) { if ($doaction !== 'beyondwords_generate_audio') { return $redirect; } global $beyondwords_wordpress_plugin; // Remove query args $args = [ 'beyondwords_bulk_generated', 'beyondwords_bulk_deleted', 'beyondwords_bulk_failed', 'beyondwords_bulk_error', ]; $redirect = remove_query_arg($args, $redirect); // Order batch by Post ID asc sort($objectIds); $generated = 0; $failed = 0; try { // Update all custom fields before attempting any processing foreach ($objectIds as $postId) { update_post_meta($postId, 'beyondwords_generate_audio', '1'); } // Now process all posts foreach ($objectIds as $postId) { if ( $beyondwords_wordpress_plugin instanceof Plugin && $beyondwords_wordpress_plugin->core instanceof Core ) { $response = $beyondwords_wordpress_plugin->core->generateAudioForPost($postId); if ($response) { $generated++; } else { $failed++; } } else { throw new \Exception('Error while bulk generating audio. Please contact support with reference BULK-NO-PLUGIN.'); // phpcs:ignore } } } catch (\Exception $e) { $redirect = add_query_arg('beyondwords_bulk_error', $e->getMessage(), $redirect); } // Add $generated & $failed query args into redirect $redirect = add_query_arg('beyondwords_bulk_generated', $generated, $redirect); $redirect = add_query_arg('beyondwords_bulk_failed', $failed, $redirect); // Add $nonce query arg into redirect $nonce = wp_create_nonce('beyondwords_bulk_edit'); $redirect = add_query_arg('beyondwords_bulk_edit_nonce', $nonce, $redirect); return $redirect; } /** * */ public function handleBulkDeleteAction($redirect, $doaction, $objectIds) { if ($doaction !== 'beyondwords_delete_audio') { return $redirect; } // Remove query args $args = [ 'beyondwords_bulk_generated', 'beyondwords_bulk_deleted', 'beyondwords_bulk_failed', 'beyondwords_bulk_error', ]; $redirect = remove_query_arg($args, $redirect); // Order batch by Post ID asc sort($objectIds); $deleted = 0; // Handle "Delete audio" bulk action try { $result = $this->deleteAudioForPosts($objectIds); $deleted = count($result); // Add $deleted query arg into redirect $redirect = add_query_arg('beyondwords_bulk_deleted', $deleted, $redirect); } catch (\Exception $e) { $redirect = add_query_arg('beyondwords_bulk_error', $e->getMessage(), $redirect); } // Add $nonce query arg into redirect $nonce = wp_create_nonce('beyondwords_bulk_edit'); $redirect = add_query_arg('beyondwords_bulk_edit_nonce', $nonce, $redirect); return $redirect; } /** * @since 4.1.0 */ public function generatedNotice() { if (! isset($_REQUEST['beyondwords_bulk_edit_nonce'])) { return; } if (! wp_verify_nonce(sanitize_text_field($_REQUEST['beyondwords_bulk_edit_nonce']), 'beyondwords_bulk_edit')) { wp_nonce_ays(''); } $count = filter_input(INPUT_GET, 'beyondwords_bulk_generated', FILTER_SANITIZE_NUMBER_INT); if ($count) { $message = sprintf( /* translators: %d is replaced with the number of posts processed */ _n( 'Audio was requested for %d post.', 'Audio was requested for %d posts.', $count, 'speechkit' ), $count ); ?>