one-click-accessibility/modules/remediation/rest/set-alt-text.php
2024-12-29 16:45:20 +02:00

69 lines
1.5 KiB
PHP

<?php
namespace EA11y\Modules\Remediation\rest;
use EA11y\Modules\Remediation\Classes\Route_Base;
use Throwable;
use WP_Error;
use WP_REST_Response;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class Set_Alt_Text extends Route_Base {
const ALT_TEXT_META_KEY = '_wp_attachment_image_alt';
public string $path = 'set-alt-text';
public function get_methods(): array {
return [ 'POST' ];
}
public function get_name(): string {
return 'set-alt-text';
}
/**
*
* @return WP_Error|WP_REST_Response
*
*/
public function POST( $request ) {
try {
$error = $this->verify_capability();
if ( $error ) {
return $error;
}
$attachment_id = $request->get_param( 'attachment_id' );
$alt_text = $request->get_param( 'alt_text' );
if ( ! $attachment_id || ! $alt_text ) {
return $this->respond_error_json( [
'message' => 'Missing required parameters',
'code' => 'missing_parameters',
] );
}
$attachment = get_post( $attachment_id );
if ( ! $attachment ) {
return $this->respond_error_json( [
'message' => 'Attachment not found',
'code' => 'attachment_not_found',
] );
}
update_post_meta( $attachment_id, self::ALT_TEXT_META_KEY, $alt_text );
return $this->respond_success_json( [
'message' => 'Alt text updated',
] );
} catch ( Throwable $t ) {
return $this->respond_error_json( [
'message' => $t->getMessage(),
'code' => 'internal_server_error',
] );
}
}
}