| 1 |
<?php |
| 2 |
require_once '../../../../wp-load.php'; |
| 3 |
|
| 4 |
header('Content-Type: application/json'); |
| 5 |
|
| 6 |
if (!isset($_SERVER['REQUEST_METHOD']) || $_SERVER['REQUEST_METHOD'] !== 'POST') { |
| 7 |
echo json_encode(['error' => 'Invalid request method']); |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
// Get POST data |
| 12 |
$postData = json_decode(file_get_contents('php://input'), true); |
| 13 |
|
| 14 |
// Determine which API to call based on the provided action |
| 15 |
$action = $postData['action'] ?? 'getProducts'; |
| 16 |
$url = ''; |
| 17 |
|
| 18 |
switch ($action) { |
| 19 |
case 'getProducts': |
| 20 |
$url = 'https://license.ovationthemes.com/api/public/getFilteredProducts'; |
| 21 |
$data = [ |
| 22 |
"collectionHandle" => $postData['collectionHandle'] ?? "", |
| 23 |
"productHandle" => $postData['productHandle'] ?? "", |
| 24 |
"paginationParams" => $postData['paginationParams'] ?? [ |
| 25 |
"first" => 12, |
| 26 |
"afterCursor" => null, |
| 27 |
"beforeCursor" => null, |
| 28 |
"reverse" => true |
| 29 |
] |
| 30 |
]; |
| 31 |
|
| 32 |
break; |
| 33 |
case 'getCollections': |
| 34 |
$url = 'https://license.ovationthemes.com/api/public/getCollections'; |
| 35 |
$data = []; // Use an empty array to send a proper POST request |
| 36 |
break; |
| 37 |
default: |
| 38 |
echo json_encode(['error' => 'Invalid action']); |
| 39 |
exit; |
| 40 |
} |
| 41 |
|
| 42 |
// Prepare the request arguments |
| 43 |
$args = [ |
| 44 |
'method' => 'POST', |
| 45 |
'body' => json_encode($data), |
| 46 |
'headers' => [ |
| 47 |
'Content-Type' => 'application/json', |
| 48 |
] |
| 49 |
]; |
| 50 |
|
| 51 |
// Make the request using wp_remote_post |
| 52 |
$response = wp_remote_post($url, $args); |
| 53 |
|
| 54 |
// Check for errors |
| 55 |
if (is_wp_error($response)) { |
| 56 |
echo json_encode(['error' => 'Request failed: ' . $response->get_error_message()]); |
| 57 |
exit; |
| 58 |
} |
| 59 |
|
| 60 |
// Check the response code |
| 61 |
$http_code = wp_remote_retrieve_response_code($response); |
| 62 |
if ($http_code !== 200) { |
| 63 |
echo json_encode(['error' => 'HTTP error: ' . $http_code]); |
| 64 |
exit; |
| 65 |
} |
| 66 |
|
| 67 |
// Get the response body |
| 68 |
$response_body = wp_remote_retrieve_body($response); |
| 69 |
$data = json_decode($response_body, true); |
| 70 |
|
| 71 |
if (json_last_error() !== JSON_ERROR_NONE) { |
| 72 |
echo json_encode(['error' => 'Invalid JSON format received from the external API']); |
| 73 |
exit; |
| 74 |
} |
| 75 |
|
| 76 |
// Output the fetched data as JSON |
| 77 |
echo json_encode($data); |