__( 'Delete Media', 'airo-wp' ), 'description' => __( 'Deletes a WordPress media attachment', 'airo-wp' ), 'input_schema' => $this->get_input_schema(), 'output_schema' => $this->get_output_schema(), 'execute_callback' => array( $this, 'execute' ), 'permission_callback' => array( $this, 'check_permissions' ), 'category' => 'media-management', ) ); } /** * Execute the tool. * * @param array $input Input parameters. * @return array Delete result or error. */ public function execute( array $input ): array { $media_id = ! empty( $input['media_id'] ) ? (int) $input['media_id'] : 0; if ( empty( $media_id ) ) { return array( 'success' => false, 'message' => __( 'Media ID is required', 'airo-wp' ), ); } $post = get_post( $media_id ); if ( ! $post ) { return array( 'success' => false, /* translators: %d: Media ID */ 'message' => sprintf( __( 'Media with ID %d not found', 'airo-wp' ), $media_id ), ); } if ( 'attachment' !== $post->post_type ) { return array( 'success' => false, /* translators: %d: Post ID */ 'message' => sprintf( __( 'Post with ID %d is not a media attachment', 'airo-wp' ), $media_id ), ); } if ( ! current_user_can( 'delete_post', $media_id ) ) { return array( 'success' => false, 'message' => __( 'You do not have permission to delete this media item', 'airo-wp' ), ); } $media_info = array( 'id' => $post->ID, 'title' => $post->post_title, 'url' => wp_get_attachment_url( $media_id ), ); $deleted = wp_delete_attachment( $media_id, true ); if ( false === $deleted || null === $deleted ) { return array( 'success' => false, 'message' => __( 'Failed to delete media attachment', 'airo-wp' ), ); } return array( 'success' => true, /* translators: %d: Media ID */ 'message' => sprintf( __( 'Successfully deleted media with ID %d', 'airo-wp' ), $media_id ), 'deleted_media' => $media_info, ); } /** * Input JSON Schema. * * @return array */ private function get_input_schema(): array { return array( 'type' => 'object', 'properties' => array( 'media_id' => array( 'type' => 'integer', 'description' => __( 'The ID of the media attachment to delete', 'airo-wp' ), 'minimum' => 1, ), ), 'required' => array( 'media_id' ), ); } /** * Output JSON Schema. * * @return array */ private function get_output_schema(): array { return $this->build_output_schema( __( 'Media deletion result', 'airo-wp' ), array( 'deleted_media' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'integer', 'description' => __( 'The deleted media attachment ID', 'airo-wp' ), ), 'title' => array( 'type' => 'string', 'description' => __( 'The title of the deleted media', 'airo-wp' ), ), 'url' => array( 'type' => 'string', 'description' => __( 'The URL of the deleted media', 'airo-wp' ), ), ), 'description' => __( 'Information about the deleted media (only present on success)', 'airo-wp' ), ), ) ); } }