| 1 |
<?php |
| 2 |
|
| 3 |
namespace AATXT\App\Admin\BulkActions; |
| 4 |
|
| 5 |
use AATXT\App\Services\AltTextService; |
| 6 |
|
| 7 |
/** |
| 8 |
* Bulk action for generating alt text for multiple media items. |
| 9 |
* |
| 10 |
* This class encapsulates the logic for processing multiple images |
| 11 |
* and generating alt text for each one using the AltTextService. |
| 12 |
*/ |
| 13 |
final class GenerateAltTextBulkAction implements BulkActionInterface |
| 14 |
{ |
| 15 |
/** |
| 16 |
* Action identifier |
| 17 |
*/ |
| 18 |
private const ACTION_NAME = 'auto_alt_text'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Alt text generation service |
| 22 |
* |
| 23 |
* @var AltTextService |
| 24 |
*/ |
| 25 |
private $altTextService; |
| 26 |
|
| 27 |
/** |
| 28 |
* Constructor |
| 29 |
* |
| 30 |
* @param AltTextService $altTextService Service for generating alt text |
| 31 |
*/ |
| 32 |
public function __construct(AltTextService $altTextService) |
| 33 |
{ |
| 34 |
$this->altTextService = $altTextService; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Execute the bulk action on the given media items. |
| 39 |
* |
| 40 |
* Generates alt text for each media item and updates the post meta. |
| 41 |
* |
| 42 |
* @param array<int> $itemIds Array of media item IDs to process |
| 43 |
* @return BulkActionResult Result containing processing statistics |
| 44 |
*/ |
| 45 |
public function execute(array $itemIds): BulkActionResult |
| 46 |
{ |
| 47 |
$updated = 0; |
| 48 |
|
| 49 |
foreach ($itemIds as $mediaId) { |
| 50 |
$mediaId = (int) $mediaId; |
| 51 |
$altText = $this->altTextService->generateForAttachment($mediaId); |
| 52 |
|
| 53 |
if (!empty($altText)) { |
| 54 |
update_post_meta($mediaId, '_wp_attachment_image_alt', $altText); |
| 55 |
$updated++; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return new BulkActionResult(count($itemIds), $updated); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get the unique name/identifier of this bulk action. |
| 64 |
* |
| 65 |
* @return string The action name |
| 66 |
*/ |
| 67 |
public function getName(): string |
| 68 |
{ |
| 69 |
return self::ACTION_NAME; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get the display label for this bulk action. |
| 74 |
* |
| 75 |
* @return string The translated label |
| 76 |
*/ |
| 77 |
public function getLabel(): string |
| 78 |
{ |
| 79 |
return esc_attr__('Generate Alt Text', 'auto-alt-text'); |
| 80 |
} |
| 81 |
} |
| 82 |
|