HEX
Server: Apache
System: Linux efa57bbe-abb1-400d-2985-3b056fbc2701.secureserver.net 6.1.147-1.el9.elrepo.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Jul 24 12:33:32 EDT 2025 x86_64
User: root (0)
PHP: 8.0.30.4
Disabled: NONE
Upload Files
File: /var/www/wp-content/plugins/pojo-accessibility/modules/remediation/rest/set-alt-text.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;
			}

			$url = esc_url_raw( $request->get_param( 'url' ) );
			$alt_text = sanitize_text_field( $request->get_param( 'alt_text' ) );

			if ( ! $url ) {
				return $this->respond_error_json( [
					'message' => 'Missing required parameters',
					'code' => 'missing_parameters',
				] );
			}

			$attachment_id = attachment_url_to_postid( $url );
			$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',
			] );
		}
	}
}