Automattic\WooCommerce\Blocks\Images
Pexels::refine_returned_images_results()
Refine the results returned by Pexels API.
Method of the class: Pexels{}
No Hooks.
Return
Array|\WP_Error
. The refined images, or WP_Error if the request failed.
Usage
// private - for code of main (parent) class only $result = $this->refine_returned_images_results( $ai_connection, $token, $business_description, $returned_images );
- $ai_connection(Connection) (required)
- The AI connection.
- $token(string) (required)
- The JWT token.
- $business_description(string) (required)
- The business description.
- $returned_images(array) (required)
- The returned images.
Pexels::refine_returned_images_results() Pexels::refine returned images results code WC 9.4.2
private function refine_returned_images_results( $ai_connection, $token, $business_description, $returned_images ) { $image_titles = array(); foreach ( $returned_images as $returned_image ) { if ( isset( $returned_image['title'] ) ) { $image_titles[] = $returned_image['title']; } } $prompt = sprintf( 'Given that you own a store described as "%s", remove from the following JSON all titles that do not represent products that could be sold on your store: %s', $business_description, wp_json_encode( $image_titles ) ); $response = $ai_connection->fetch_ai_response( $token, $prompt, 30 ); if ( is_wp_error( $response ) || ! isset( $response['completion'] ) ) { return $returned_images; } $filtered_image_titles = json_decode( $response['completion'] ); if ( ! is_array( $filtered_image_titles ) ) { $response = $ai_connection->fetch_ai_response( $token, $prompt, 30 ); if ( is_wp_error( $response ) || ! isset( $response['completion'] ) ) { return $returned_images; } $filtered_image_titles = json_decode( $response['completion'] ); } if ( ! is_array( $filtered_image_titles ) ) { return new \WP_Error( 'ai_service_unavailable', __( 'AI Service is unavailable, try again later.', 'woocommerce' ) ); } // Remove the images that are not aligned with the business description. foreach ( $returned_images as $returned_image ) { if ( isset( $returned_image['title'] ) && ! in_array( $returned_image['title'], $filtered_image_titles, true ) ) { unset( $returned_image ); } } return $returned_images; }