Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
79.41% |
27 / 34 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
WonderBlocks | |
79.41% |
27 / 34 |
|
33.33% |
1 / 3 |
14.47 | |
0.00% |
0 / 1 |
fetch | |
86.21% |
25 / 29 |
|
0.00% |
0 / 1 |
10.26 | |||
is_dev_mode | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
clear_cache | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace NewfoldLabs\WP\Module\Data\WonderBlocks; |
4 | |
5 | use NewfoldLabs\WP\Module\Data\HiiveConnection; |
6 | use NewfoldLabs\WP\Module\Data\WonderBlocks\Requests\Fetch; |
7 | use NewfoldLabs\WP\Module\Data\WonderBlocks\Requests\Request; |
8 | |
9 | /** |
10 | * Class WonderBlocks |
11 | * |
12 | * Handler for WonderBlock requests. |
13 | */ |
14 | class WonderBlocks { |
15 | |
16 | /** |
17 | * Handle a WonderBlock fetch request. |
18 | * |
19 | * @param Fetch $request The request object. |
20 | * @return array|false |
21 | */ |
22 | public static function fetch( Fetch $request ) { |
23 | |
24 | // Generate a unique hash for the request object. |
25 | $hash = $request->get_md5_hash(); |
26 | $endpoint = $request->get_endpoint(); |
27 | // Do not use cache in development mode. |
28 | if ( ! self::is_dev_mode() ) { |
29 | // If the transient exists, return data from the transient. Add endpoint for batch clearing endpoint transients. |
30 | $data = get_transient( "nfd_data_wb_{$endpoint}_{$hash}" ); |
31 | if ( ! empty( $data ) ) { |
32 | return $data; |
33 | } |
34 | } |
35 | |
36 | $url = $request->get_url(); |
37 | if ( empty( $url ) ) { |
38 | return false; |
39 | } |
40 | |
41 | // Populate valid request arguments. |
42 | $args = array( |
43 | 'headers' => array( |
44 | 'X-Hiive-Token' => HiiveConnection::get_auth_token(), |
45 | ), |
46 | 'body' => $request->get_args(), |
47 | 'method' => \WP_REST_Server::READABLE, |
48 | ); |
49 | |
50 | $response = wp_remote_request( |
51 | $url, |
52 | $args |
53 | ); |
54 | if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
55 | return false; |
56 | } |
57 | |
58 | $body = wp_remote_retrieve_body( $response ); |
59 | $data = json_decode( $body, true ); |
60 | |
61 | if ( ! $data || ! is_array( $data ) || ! isset( $data['data'] ) ) { |
62 | return false; |
63 | } |
64 | |
65 | // Cache the response data if specified. |
66 | if ( $request->should_cache() ) { |
67 | set_transient( "nfd_data_wb_{$endpoint}_{$hash}", $data['data'], $request->get_cache_timeout() ); |
68 | } |
69 | |
70 | return $data['data']; |
71 | } |
72 | |
73 | /** |
74 | * Check is the NFD_DATA_WB_DEV_MODE defined and defined as true. |
75 | */ |
76 | protected static function is_dev_mode(): bool { |
77 | return defined( 'NFD_DATA_WB_DEV_MODE' ) |
78 | && constant( 'NFD_DATA_WB_DEV_MODE' ); |
79 | } |
80 | |
81 | /** |
82 | * Clear the cache related a particular request object. |
83 | * |
84 | * @param Request $request An instance of the Request class. |
85 | */ |
86 | public static function clear_cache( Request $request ): bool { |
87 | $endpoint = $request->get_endpoint(); |
88 | $hash = $request->get_md5_hash(); |
89 | return delete_transient( "nfd_data_wb_{$endpoint}_{$hash}" ); |
90 | } |
91 | } |