| 1 |
<?php |
| 2 |
|
| 3 |
namespace AATXT\App\Core; |
| 4 |
|
| 5 |
use AATXT\App\Admin\BulkActions\BulkActionHandler; |
| 6 |
use AATXT\App\Admin\MediaLibrary; |
| 7 |
use AATXT\App\Admin\PluginOptions; |
| 8 |
use AATXT\App\Services\AltTextService; |
| 9 |
|
| 10 |
/** |
| 11 |
* Registers all WordPress hooks for the plugin. |
| 12 |
* |
| 13 |
* This class follows the Single Responsibility Principle by handling |
| 14 |
* only the registration of WordPress actions and filters. |
| 15 |
*/ |
| 16 |
final class HooksRegistrar |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Alt text generation service |
| 20 |
* |
| 21 |
* @var AltTextService |
| 22 |
*/ |
| 23 |
private $altTextService; |
| 24 |
|
| 25 |
/** |
| 26 |
* Bulk action handler |
| 27 |
* |
| 28 |
* @var BulkActionHandler |
| 29 |
*/ |
| 30 |
private $bulkActionHandler; |
| 31 |
|
| 32 |
/** |
| 33 |
* Plugin lifecycle handler |
| 34 |
* |
| 35 |
* @var PluginLifecycle |
| 36 |
*/ |
| 37 |
private $lifecycle; |
| 38 |
|
| 39 |
/** |
| 40 |
* Media library handler |
| 41 |
* |
| 42 |
* @var MediaLibrary |
| 43 |
*/ |
| 44 |
private $mediaLibrary; |
| 45 |
|
| 46 |
/** |
| 47 |
* Path to the main plugin file |
| 48 |
* |
| 49 |
* @var string |
| 50 |
*/ |
| 51 |
private $pluginFile; |
| 52 |
|
| 53 |
/** |
| 54 |
* Constructor |
| 55 |
* |
| 56 |
* @param AltTextService $altTextService Service for generating alt text |
| 57 |
* @param BulkActionHandler $bulkActionHandler Handler for bulk actions |
| 58 |
* @param PluginLifecycle $lifecycle Plugin lifecycle handler |
| 59 |
* @param MediaLibrary $mediaLibrary Media library handler |
| 60 |
* @param string $pluginFile Path to the main plugin file |
| 61 |
*/ |
| 62 |
public function __construct( |
| 63 |
AltTextService $altTextService, |
| 64 |
BulkActionHandler $bulkActionHandler, |
| 65 |
PluginLifecycle $lifecycle, |
| 66 |
MediaLibrary $mediaLibrary, |
| 67 |
string $pluginFile |
| 68 |
) { |
| 69 |
$this->altTextService = $altTextService; |
| 70 |
$this->bulkActionHandler = $bulkActionHandler; |
| 71 |
$this->lifecycle = $lifecycle; |
| 72 |
$this->mediaLibrary = $mediaLibrary; |
| 73 |
$this->pluginFile = $pluginFile; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Register all WordPress hooks. |
| 78 |
* |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
public function register(): void |
| 82 |
{ |
| 83 |
// Register admin pages |
| 84 |
PluginOptions::register(); |
| 85 |
$this->mediaLibrary->register(); |
| 86 |
|
| 87 |
// Plugin lifecycle hooks |
| 88 |
register_activation_hook($this->pluginFile, [$this->lifecycle, 'activate']); |
| 89 |
register_deactivation_hook($this->pluginFile, [$this->lifecycle, 'deactivate']); |
| 90 |
|
| 91 |
// Auto-generate alt text on image upload |
| 92 |
add_action('add_attachment', [$this, 'addAltText']); |
| 93 |
|
| 94 |
// Load translations |
| 95 |
add_action('plugins_loaded', [$this, 'loadTextDomain']); |
| 96 |
|
| 97 |
// Add settings link in plugins page |
| 98 |
add_filter( |
| 99 |
'plugin_action_links_auto-alt-text/auto-alt-text.php', |
| 100 |
[$this, 'addSettingsLink'] |
| 101 |
); |
| 102 |
|
| 103 |
// Bulk actions |
| 104 |
add_filter('bulk_actions-upload', [$this->bulkActionHandler, 'getBulkActions']); |
| 105 |
add_action('load-upload.php', [$this->bulkActionHandler, 'handleBulkAction']); |
| 106 |
add_action('admin_notices', [$this->bulkActionHandler, 'displayAdminNotice']); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Generate and save alt text for a newly uploaded attachment. |
| 111 |
* |
| 112 |
* @param int $attachmentId The attachment post ID |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
public function addAltText(int $attachmentId): void |
| 116 |
{ |
| 117 |
$altText = $this->altTextService->generateForAttachment($attachmentId); |
| 118 |
|
| 119 |
if (!empty($altText)) { |
| 120 |
update_post_meta($attachmentId, '_wp_attachment_image_alt', $altText); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Load plugin text domain for translations. |
| 126 |
* |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
public function loadTextDomain(): void |
| 130 |
{ |
| 131 |
load_plugin_textdomain('auto-alt-text', false, AATXT_LANGUAGES_RELATIVE_PATH); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Add settings link to the plugin actions in the plugins list. |
| 136 |
* |
| 137 |
* @param array<string> $links Existing plugin action links |
| 138 |
* @return array<string> Modified plugin action links |
| 139 |
*/ |
| 140 |
public function addSettingsLink(array $links): array |
| 141 |
{ |
| 142 |
$url = esc_url(add_query_arg( |
| 143 |
'page', |
| 144 |
'auto-alt-text-options', |
| 145 |
get_admin_url() . 'admin.php' |
| 146 |
)); |
| 147 |
|
| 148 |
$settingsLink = '<a href="' . $url . '">' . esc_html__('Settings', 'auto-alt-text') . '</a>'; |
| 149 |
$links[] = $settingsLink; |
| 150 |
|
| 151 |
return $links; |
| 152 |
} |
| 153 |
} |
| 154 |
|