Automattic\WooCommerce\Blocks\Images
Pexels::request()
Make a request to the Pexels API.
Method of the class: Pexels{}
No Hooks.
Return
Array|\WP_Error
. The response body, or WP_Error if the request failed.
Usage
// private - for code of main (parent) class only $result = $this->request( $search_term, $per_page );
- $search_term(string) (required)
- The search term to use.
- $per_page(int)
- The number of images to return.
Default: 100
Pexels::request() Pexels::request code WC 9.4.2
private function request( string $search_term, int $per_page = 100 ) { $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 ); $request->set_param( 'size', 'small' ); $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.', 'woocommerce' ), $error_msg ); } $response = $response_data['media'] ?? $response_data; if ( is_array( $response ) ) { shuffle( $response ); return $response; } return array(); }