one-click-accessibility/modules/scanner/rest/generate-alt-text.php
Nirbhay Singh 2eda75935a
Feature/app 2261 bulk alt manager (#512)
* add: preview images for bulk-alt-text

* add: bulk alt text banner

* fix: padding

* add: bulk alt text banner component

* add: icons

* add: support for aborting the fetch request

* add: bulk alt text manager

* update: husky

* fix: optimized images

* update: memoize images

* fix: imports

* add: focustrap

* add: mixpanel events

* update: add logic to save alt text when editing manually

* fix: abort controller reference and cleanup

* fix: improve key stability in image grid rendering

* refactor: add PropTypes validation and memoization to ImageCard component

* feat: add error handling for ImageCard component using ErrorBoundary

* fix: auto selection logic for card with bulk generation

* add: loading and generation states

* refactor: restructure bulk alt text components and enhance progress handling and decentralized logic

* refactor: extract logic from progress bar to hook

* add: quota exceeded alert

* add: promotional banner for generate button

* update: Generate button for selection mode

* fix: button alignment

* update: hide banner for 2 or less images

* fix: box shadow

* add: mixpanel events

* update: rename variable

* add: bulk upgrade url

* update: link handling for upgrade

* fix: box shadow

* add: border radius to dialog

* update: improved accessibility notifications

* fix: height and padding for banner

* update: add more context to image card

* add: ARIA labels

* add: focus on card

* add: decode URI component

* fix: accessibility issues

* add: encoding for file names

* fix: styled components

* update: made progress bar sticky

* update: add logic to disable button on bulk generation

* fix: style

* fix: closing the dialog while generating

* add: error handling

* fix: mark prop as not required

* Fix: Prevent MUI from adding aria-hidden to dialog parent elements [APP-2261]

* add: error handling for quota api error

* fix: prop requirement

* add: close handler to dialog

* fix: ui

* fix: icons

* fix: width of dialog

* fix: width of bulk dialog

---------

Co-authored-by: Kniazevych <pavlok@elementor.red>
2026-02-19 01:12:59 +05:30

91 lines
2 KiB
PHP

<?php
namespace EA11y\Modules\Scanner\Rest;
use EA11y\Modules\Scanner\Classes\Route_Base;
use EA11y\Modules\Scanner\Classes\Utils;
use EA11y\Classes\Utils as Global_Utils;
use EA11y\Classes\Client\Client_Response;
use EA11y\Classes\Exceptions\Quota_Exceeded_Error;
use EA11y\Classes\Exceptions\Quota_API_Error;
use Throwable;
use WP_Error;
use WP_REST_Response;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class Generate_Alt_Text extends Route_Base {
public string $path = 'generate-alt-text';
public function get_methods(): array {
return [ 'POST' ];
}
public function get_name(): string {
return 'generate-alt-text';
}
/**
*
* @return WP_Error|WP_REST_Response
*
*/
public function POST( $request ) {
try {
$error = $this->verify_capability();
if ( $error ) {
return $error;
}
$image = $request->get_param( 'image' );
$svg = $request->get_param( 'svg' );
if ( ! ( $image || $svg ) ) {
return $this->respond_error_json( [
'message' => 'Missing required parameters',
'code' => 'missing_parameters',
] );
}
$src = $image ?? Utils::create_tmp_file_from_png_base64( $svg );
$result = ( new Client_Response(
Global_Utils::get_api_client()->make_request(
'POST',
'ai/image-alt',
[],
[],
false,
$src
)
) )->handle();
return $this->respond_success_json( [
'message' => 'Alt text generated',
'data' => [
'response' => $result->response,
'apiId' => $result->apiId,
],
] );
} catch ( Quota_Exceeded_Error $e ) {
return $this->respond_error_json( [
'message' => 'AI credits quota has been exceeded.',
'code' => 'quota_exceeded',
] );
} catch ( Quota_API_Error $e ) {
return $this->respond_error_json( [
'message' => 'Quota API error. Try again after sometime.',
'code' => 'quota_api_error',
] );
} catch ( Throwable $t ) {
return $this->respond_error_json( [
'message' => $t->getMessage(),
'code' => 'internal_server_error',
] );
}
}
}