Skip to content

Commit

Permalink
feat: generator status
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaucau committed Apr 14, 2024
1 parent 56bdebf commit 866a740
Show file tree
Hide file tree
Showing 13 changed files with 520 additions and 12 deletions.
4 changes: 2 additions & 2 deletions includes/AltGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ public static function generate_alt_text( int $attachment_id ): string|WP_Error
return $completion['choices'][0]['message']['content'] ?? '';
}

public static function generate_and_set_alt_text( int $attachment_id ): ?string {
public static function generate_and_set_alt_text( int $attachment_id ): string|WP_Error|null {
$alt_text = self::generate_alt_text( $attachment_id );
if ( is_wp_error( $alt_text ) ) {
AltGeneratorPlugin::error_log( $alt_text );

return null;
return $alt_text;
}

if ( ! empty( $alt_text ) ) {
Expand Down
12 changes: 11 additions & 1 deletion includes/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function register_routes(): void {
'required' => true,
'type' => 'integer',
],
'save_alt' => [
'required' => false,
'type' => 'boolean',
'default' => false,
],
],
'callback' => [ $this, 'generate_alt_text' ],
'permission_callback' => fn() => current_user_can( 'edit_posts' ),
Expand All @@ -27,8 +32,13 @@ public function register_routes(): void {

public function generate_alt_text( WP_REST_Request $request ): WP_REST_Response|WP_Error {
$attachment_id = $request->get_param( 'attachment_id' );
$save_alt = $request->get_param( 'save_alt' );

$alt_text = AltGenerator::generate_alt_text( $attachment_id );
if ( $save_alt ) {
$alt_text = AltGenerator::generate_and_set_alt_text( $attachment_id );
} else {
$alt_text = AltGenerator::generate_alt_text( $attachment_id );
}

if ( is_wp_error( $alt_text ) ) {
return $alt_text;
Expand Down
255 changes: 255 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
"@wordpress/api-fetch": "^6.52.0",
"@wordpress/block-editor": "^12.23.0",
"@wordpress/components": "^27.3.0",
"@wordpress/core-data": "^6.32.0",
"@wordpress/data": "^9.25.0",
"@wordpress/element": "^5.32.0",
"@wordpress/hooks": "^3.55.0",
"@wordpress/html-entities": "^3.55.0",
"@wordpress/i18n": "^4.55.0",
"@wordpress/notices": "^4.23.0"
}
Expand Down
Loading

0 comments on commit 866a740

Please sign in to comment.