| @@ -46,8 +46,15 @@ | ||
| 46 | 46 | if ( ! function_exists( 'wp_register_ability' ) ) { |
| 47 | 47 | return; |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | + $media_properties = [ | |
| 51 | + 'media_id' => [ | |
| 52 | + 'type' => 'integer', | |
| 53 | + 'description' => __( 'The WordPress attachment ID to optimize. Provide media_filename or media_url instead when the ID is unknown.', 'imagify' ), | |
| 54 | + ], | |
| 55 | + ] + MediaResolver::get_input_schema_properties(); | |
| 56 | + | |
| 50 | 57 | wp_register_ability( |
| 51 | 58 | 'imagify/optimize-media', |
| 52 | 59 | [ |
| 53 | 60 | 'label' => __( 'Optimize media', 'imagify' ), |
| @@ -54,13 +61,9 @@ | ||
| 54 | 61 | 'description' => __( 'Optimizes a specific media on-demand using Imagify.', 'imagify' ), |
| 55 | 62 | 'category' => 'imagify', |
| 56 | 63 | 'input_schema' => [ |
| 57 | 64 | 'type' => 'object', |
| 58 | - 'properties' => [ | |
| 59 | - 'media_id' => [ | |
| 60 | - 'type' => 'integer', | |
| 61 | - 'description' => __( 'The WordPress attachment ID to optimize.', 'imagify' ), | |
| 62 | - ], | |
| 65 | + 'properties' => $media_properties + [ | |
| 63 | 66 | 'optimization_level' => [ |
| 64 | 67 | 'type' => 'integer', |
| 65 | 68 | 'description' => __( 'Optimization level: 0 (normal), 1 (aggressive), or 2 (ultra). If omitted, uses the global setting.', 'imagify' ), |
| 66 | 69 | 'minimum' => 0, |
| @@ -71,9 +74,8 @@ | ||
| 71 | 74 | 'description' => __( 'Set to true to execute after reviewing the credit-consumption preview returned by a prior call without this flag.', 'imagify' ), |
| 72 | 75 | 'default' => false, |
| 73 | 76 | ], |
| 74 | 77 | ], |
| 75 | - 'required' => [ 'media_id' ], | |
| 76 | 78 | ], |
| 77 | 79 | 'output_schema' => [ |
| 78 | 80 | 'type' => 'object', |
| 79 | 81 | 'properties' => [ |
| @@ -183,12 +185,12 @@ | ||
| 183 | 185 | * @param array $args Input arguments. |
| 184 | 186 | * @return array{status: string, original_size: int|null, optimized_size: int|null, savings_percent: float|null, error_message: string|null} |
| 185 | 187 | */ |
| 186 | 188 | private function do_execute( array $args ): array { |
| 187 | - $media_id = isset( $args['media_id'] ) ? (int) $args['media_id'] : 0; | |
| 189 | + $media_id = MediaResolver::resolve_id( $args ); | |
| 188 | 190 | |
| 189 | - if ( $media_id <= 0 ) { | |
| 190 | - return $this->error_response( 'Invalid or missing media_id.' ); | |
| 191 | + if ( is_wp_error( $media_id ) ) { | |
| 192 | + return $this->error_response( $media_id->get_error_message() ); | |
| 191 | 193 | } |
| 192 | 194 | |
| 193 | 195 | // Verify the attachment exists. |
| 194 | 196 | $post = get_post( $media_id ); |