This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Store Customization > Fetch product images from the Pexels API #11280
Merged
Merged
Changes from all commits
Commits
Show all changes
79 commits
Select commit
Hold shift + click to select a range
178a022
Introduce the ProductUpdater class.
nefeline 6df499c
Update the image assignment for the default products content.
nefeline a754030
Update the default products content.
nefeline d94eab8
Introduce the generate_content and get_placeholder_products methods.
nefeline 884ad30
Merge branch 'trunk' into add/ai-generated-content-for-products
nefeline 8adfc7f
Update the get_placeholder_products method and introduce the new crea…
nefeline aa753fb
Verify the hash of the product content and compare it with the hash o…
nefeline d0688db
Add docblocks and update the structure for the ProductUpdater class.
nefeline 31d5942
Pass the vertical images as a param for the PatternUpdater and the Pr…
nefeline c6dae0b
Update the provided variable to the PatternUpdater class.
nefeline 057fe93
Update the ProductUpdater class to include the requirements for usage…
nefeline 35b0719
remove unnecessary calls to post/product images.
nefeline 9659d40
Update the loop for generating products.
nefeline 11f6eb7
Further adjustments to the Product Updater.
nefeline 18e7b84
Provide the business_description as a param for the generate_content …
nefeline f18ebf5
Merge branch 'trunk' into add/ai-generated-content-for-products
nefeline 8838406
Update the default timeout for the AI connection; update the path to …
nefeline 656fb99
Add new neutral placeholder images for products and patterns.
nefeline 7bed64e
Update the prompt and the logic for the placeholder images.
nefeline cd4fb68
Merge trunk and resolve conflicts.
nefeline e696300
Remove placeholder images.
nefeline 18f39e4
Pass the AI connection as a param and update the get_images_for_patte…
nefeline 0200da5
update the product updater class to rely on Pexels images and add the…
nefeline f97deb9
Remove the unused get_random_images method.
nefeline 648b652
Update the patterns endpoint.
nefeline d5911b1
Delete unused ChatGPTClient class.
nefeline 8a95163
Introduce the new Pexels class.
nefeline 8e85e46
Remove the Verticals references.
nefeline 121d731
Update the reference for the alt description for images.
nefeline 09c22dc
Update the scheduled action to populate patterns and products.
nefeline a965f55
Remove unused Verticals classes.
nefeline b859709
Ensure the Pexels class already returns the array with the expected f…
nefeline d78f2ff
Introduce the select_image_src_based_on_format method.
nefeline 38f3b42
Merge branch 'trunk' into add/ai-generated-content-for-products
nefeline a506ecc
Merge branch 'add/ai-generated-content-for-products' into update/repl…
nefeline 4f331b8
Increase max execution time
nefeline fcd471d
Increase max execution time
nefeline 9cea3a9
Update the request to rely on the WP.com external-media endpoint inst…
nefeline 09df952
Merge branch 'trunk' into add/ai-generated-content-for-products
nefeline 88ab322
Merge branch 'add/ai-generated-content-for-products' into update/repl…
nefeline e5bca84
Improve performance for product content update.
nefeline 39cab0e
Improve quality of images used in products and update queries.
nefeline 806bcf8
Update the products query.
nefeline 88ea1d8
Move the media_sideload_image function dependencies to outside of the…
nefeline c334df4
Update text content.
nefeline d5a91c0
Merge base branch
nefeline a3b02d6
Merge add/ai-generated-content-for-products
nefeline 12d67fa
Introduce the should_update_dummy_product method.
nefeline ee08f23
Update the method to be triggered on scheduled action to return true.
nefeline a5f3f1c
Change the image format for products to improve performance.
nefeline 29b3ba1
Merge branch 'trunk' into add/ai-generated-content-for-products
nefeline 61b3eb5
Merge branch 'add/ai-generated-content-for-products' into update/repl…
nefeline 6a392d1
Merge branch 'trunk' into add/ai-generated-content-for-products
nefeline 0918898
Merge branch 'add/ai-generated-content-for-products' into update/repl…
nefeline 28b0f80
Make portrait the default fallback image format.
nefeline 3da76eb
Address code review.
nefeline 6333f15
bail early if no business description provided.
nefeline 5ddb593
Add an extra safety check in case of query errors.
nefeline 6d36768
Merge branch 'add/ai-generated-content-for-products' into update/repl…
nefeline f97f73d
Address CR.
nefeline 4fcc41e
Merge branch 'trunk' into add/ai-generated-content-for-products
nefeline dfd988a
Merge branch 'add/ai-generated-content-for-products' into update/repl…
nefeline 862e0ec
Merge branch 'trunk' into add/ai-generated-content-for-products
nefeline 7200b1e
Merge branch 'add/ai-generated-content-for-products' into update/repl…
nefeline 1a656dd
Merge trunk and resolve conflicts.
nefeline 8af0567
Merge branch 'trunk' into update/replace-verticals-api-with-pexels
nefeline 114db02
Raise the default memory limit.
nefeline be53ddf
Update the prompt for the search term for images.
nefeline e175385
Make sure the 'woocommerce_blocks_allow_ai_connection' option is set …
nefeline 6d0f99a
Update the prompt for AI generated content in patterns and initialize…
nefeline 7ed6081
Update the prompt for products and introduce the update_dummy_product…
nefeline 3502905
Merge branch 'trunk' into update/replace-verticals-api-with-pexels
nefeline 6c8bfa4
Update the default number of images returned by Pexels.
nefeline 26ba4b4
Update the default fallback for the expected image format.
nefeline a08cae5
Update the prompt for selecting the images.
nefeline f2b243f
Add a character limit to the testimonials.
nefeline 69e0a29
Merge branch 'trunk' into update/replace-verticals-api-with-pexels
nefeline 6a28abc
Increase timeout to fetch products data
nefeline 780df8e
Address code review.
nefeline File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace Automattic\WooCommerce\Blocks\Images; | ||
|
||
use Automattic\WooCommerce\Blocks\AI\Connection; | ||
|
||
/** | ||
* Pexels API client. | ||
* | ||
* @internal | ||
*/ | ||
class Pexels { | ||
nefeline marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* The Pexels API endpoint. | ||
*/ | ||
const EXTERNAL_MEDIA_PEXELS_ENDPOINT = '/wpcom/v2/external-media/list/pexels'; | ||
|
||
/** | ||
* Returns the list of images for the given search criteria. | ||
* | ||
* @param Connection $ai_connection The AI connection. | ||
* @param string $token The JWT token. | ||
* @param string $business_description The business description. | ||
* | ||
* @return array|\WP_Error Array of images, or WP_Error if the request failed. | ||
*/ | ||
public function get_images( $ai_connection, $token, $business_description ) { | ||
$search_term = $this->define_search_term( $ai_connection, $token, $business_description ); | ||
|
||
if ( is_wp_error( $search_term ) ) { | ||
return $search_term; | ||
} | ||
|
||
return $this->request( $search_term ); | ||
} | ||
|
||
/** | ||
* Define the search term to be used on Pexels using the AI endpoint. | ||
* | ||
* The search term is a shorter description of the business. | ||
* | ||
* @param Connection $ai_connection The AI connection. | ||
* @param string $token The JWT token. | ||
* @param string $business_description The business description. | ||
* | ||
* @return mixed|\WP_Error | ||
*/ | ||
private function define_search_term( $ai_connection, $token, $business_description ) { | ||
$prompt = sprintf( 'Based on the description "%s", provide a one-word product description for the store\'s item. Do not include any adjectives or descriptions of the qualities of the product. The returned word should be simple.', $business_description ); | ||
|
||
$response = $ai_connection->fetch_ai_response( $token, $prompt ); | ||
|
||
if ( is_wp_error( $response ) || ! isset( $response['completion'] ) ) { | ||
return new \WP_Error( 'search_term_definition_failed', __( 'The search term definition failed.', 'woo-gutenberg-products-block' ) ); | ||
} | ||
|
||
return $response['completion']; | ||
} | ||
|
||
/** | ||
* Make a request to the Pexels API. | ||
* | ||
* @param string $search_term The search term to use. | ||
* @param int $per_page The number of images to return. | ||
* | ||
* @return array|\WP_Error The response body, or WP_Error if the request failed. | ||
*/ | ||
private function request( string $search_term, int $per_page = 90 ) { | ||
$request = new \WP_REST_Request( 'GET', self::EXTERNAL_MEDIA_PEXELS_ENDPOINT ); | ||
|
||
$request->set_param( 'search', esc_html( $search_term ) ); | ||
$request->set_param( 'number', $per_page ); | ||
|
||
$response = rest_do_request( $request ); | ||
$response_data = $response->get_data(); | ||
|
||
if ( $response->is_error() ) { | ||
$error_msg = [ | ||
'code' => $response->get_status(), | ||
'data' => $response_data, | ||
]; | ||
|
||
return new \WP_Error( 'pexels_api_error', __( 'Request to the Pexels API failed.', 'woo-gutenberg-products-block' ), $error_msg ); | ||
} | ||
|
||
$response = $response_data['media'] ?? $response_data; | ||
|
||
if ( is_array( $response ) ) { | ||
shuffle( $response ); | ||
|
||
return $response; | ||
} | ||
|
||
return array(); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: Should this class be a Singleton? Every time that we get the jwt_token, we do a fetch request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, I'm doing this change on a separate PR 👍 !