loader
1 year ago
widgets
1 year ago
class-template-importer.php
1 year ago
flex-dashboard-post-api.php
1 year ago
flex-get-api.php
1 year ago
flex-get-templates-api.php
1 year ago
fleximp-license-functions.php
1 year ago
flex-get-templates-api.php
70 lines
| 1 | <?php |
| 2 | require_once '../../../../wp-load.php'; |
| 3 | |
| 4 | header('Content-Type: application/json'); |
| 5 | |
| 6 | // Validate the request method |
| 7 | if ($_SERVER['REQUEST_METHOD'] !== 'POST') { |
| 8 | echo json_encode(['error' => 'Invalid request method']); |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | // Decode the input JSON |
| 13 | $postData = json_decode(file_get_contents('php://input'), true); |
| 14 | $action = $postData['action'] ?? 'getTemplates'; |
| 15 | $url = ''; |
| 16 | $request_method = 'GET'; |
| 17 | $data = []; |
| 18 | |
| 19 | switch ($action) { |
| 20 | case 'getTemplates': |
| 21 | $url = FLEXIMP_JSON_DATA_URL . 'flex_theme_get_theme_templates'; |
| 22 | $request_method = 'POST'; |
| 23 | break; |
| 24 | |
| 25 | case 'getCategories': |
| 26 | $url = FLEXIMP_JSON_DATA_URL . 'flex_theme_get_product_categories'; |
| 27 | $request_method = 'GET'; |
| 28 | break; |
| 29 | |
| 30 | default: |
| 31 | echo json_encode(['error' => 'Invalid action']); |
| 32 | exit; |
| 33 | } |
| 34 | |
| 35 | // arguments |
| 36 | $args = [ |
| 37 | 'method' => $request_method, |
| 38 | 'headers' => [ |
| 39 | 'Content-Type' => 'application/json', |
| 40 | ], |
| 41 | ]; |
| 42 | |
| 43 | // POST requests |
| 44 | if ($request_method === 'POST') { |
| 45 | $args['body'] = json_encode($data); |
| 46 | } |
| 47 | |
| 48 | $response = ($request_method === 'POST') ? wp_remote_post($url, $args) : wp_remote_get($url, $args); |
| 49 | |
| 50 | // Check for errors |
| 51 | if (is_wp_error($response)) { |
| 52 | echo json_encode(['error' => 'Request failed: ' . $response->get_error_message()]); |
| 53 | exit; |
| 54 | } |
| 55 | |
| 56 | $http_code = wp_remote_retrieve_response_code($response); |
| 57 | if ($http_code !== 200) { |
| 58 | echo json_encode(['error' => 'HTTP error: ' . $http_code]); |
| 59 | exit; |
| 60 | } |
| 61 | |
| 62 | // Decode the response body |
| 63 | $response_body = wp_remote_retrieve_body($response); |
| 64 | $data = json_decode($response_body, true); |
| 65 | |
| 66 | if (json_last_error() !== JSON_ERROR_NONE) { |
| 67 | echo json_encode(['error' => 'Invalid JSON format received from the external API']); |
| 68 | exit; |
| 69 | } |
| 70 | echo json_encode(['success' => true, 'data' => $data]); |