class-template-importer.php
1 month ago
flex-dashboard-post-api.php
1 month ago
flex-get-api.php
1 month ago
flex-get-templates-api.php
1 month ago
fleximp-license-functions.php
1 month ago
flex-get-templates-api.php
122 lines
| 1 | <?php |
| 2 | require_once '../../../../wp-load.php'; |
| 3 | |
| 4 | header('Content-Type: application/json'); |
| 5 | |
| 6 | if ($_SERVER['REQUEST_METHOD'] !== 'POST') { |
| 7 | echo json_encode(['error' => 'Invalid request method']); |
| 8 | exit; |
| 9 | } |
| 10 | |
| 11 | // Decode the input JSON |
| 12 | $postData = json_decode(file_get_contents('php://input'), true); |
| 13 | $action = $postData['action'] ?? 'getTemplates'; |
| 14 | $url = ''; |
| 15 | $request_method = 'GET'; |
| 16 | $data = []; |
| 17 | |
| 18 | switch ($action) { |
| 19 | case 'getTemplates': |
| 20 | $url = FLEXIMP_JSON_DATA_URL . 'flex_theme_get_theme_templates'; |
| 21 | $request_method = 'POST'; |
| 22 | |
| 23 | $data = [ |
| 24 | 'per_page' => !empty($postData['per_page']) ? (int) $postData['per_page'] : 1, |
| 25 | 'page' => !empty($postData['page']) ? (int) $postData['page'] : 1 |
| 26 | ]; |
| 27 | |
| 28 | if (!empty($postData['product_cat'])) { |
| 29 | $data['product_cat'] = $postData['product_cat']; |
| 30 | } |
| 31 | |
| 32 | if (!empty($postData['search'])) { |
| 33 | $data['search'] = $postData['search']; |
| 34 | } |
| 35 | |
| 36 | if (!empty($postData['free_theme_domain'])) { |
| 37 | $data['free_theme_domain'] = $postData['free_theme_domain']; |
| 38 | } |
| 39 | |
| 40 | if (!empty($postData['free_pro'])) { |
| 41 | $allowed = ['Free', 'Pro']; |
| 42 | $free_pro = $postData['free_pro']; |
| 43 | if (in_array($free_pro, $allowed, true)) { |
| 44 | $data['free_pro'] = $free_pro; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | break; |
| 49 | |
| 50 | case 'getCategories': |
| 51 | $url = FLEXIMP_JSON_DATA_URL . 'flex_theme_get_product_categories'; |
| 52 | $request_method = 'GET'; |
| 53 | break; |
| 54 | |
| 55 | default: |
| 56 | echo json_encode(['error' => 'Invalid action']); |
| 57 | exit; |
| 58 | } |
| 59 | |
| 60 | $args = [ |
| 61 | 'method' => $request_method, |
| 62 | 'headers' => [ |
| 63 | 'Content-Type' => 'application/json', |
| 64 | ], |
| 65 | ]; |
| 66 | |
| 67 | if ($request_method === 'POST') { |
| 68 | $args['body'] = json_encode($data); |
| 69 | } |
| 70 | |
| 71 | $response = ($request_method === 'POST') ? wp_remote_post($url, $args) : wp_remote_get($url, $args); |
| 72 | |
| 73 | if (is_wp_error($response)) { |
| 74 | echo json_encode(['error' => 'Request failed: ' . $response->get_error_message()]); |
| 75 | exit; |
| 76 | } |
| 77 | |
| 78 | $http_code = wp_remote_retrieve_response_code($response); |
| 79 | if ($http_code !== 200) { |
| 80 | echo json_encode(['error' => 'HTTP error: ' . $http_code]); |
| 81 | exit; |
| 82 | } |
| 83 | |
| 84 | $response_body = wp_remote_retrieve_body($response); |
| 85 | $data = json_decode($response_body, true); |
| 86 | |
| 87 | if (json_last_error() !== JSON_ERROR_NONE) { |
| 88 | echo json_encode(['error' => 'Invalid JSON format received from the external API']); |
| 89 | exit; |
| 90 | } |
| 91 | |
| 92 | // Filter free_template based on free_pro parameter for getTemplates action |
| 93 | if ($action === 'getTemplates' && !empty($postData['free_pro'])) { |
| 94 | $requested_type = $postData['free_pro']; // 'Free' or 'Pro' |
| 95 | |
| 96 | // Check if free_template exists in the response |
| 97 | if (isset($data['data']['free_template']) && is_array($data['data']['free_template'])) { |
| 98 | $free_template_type = $data['data']['free_template']['metafields']['free_pro'] ?? ''; |
| 99 | |
| 100 | // If free_template doesn't match requested type, remove it |
| 101 | if ($free_template_type !== $requested_type) { |
| 102 | unset($data['data']['free_template']); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Also check nested structure |
| 107 | if (isset($data['data']['data']['free_template']) && is_array($data['data']['data']['free_template'])) { |
| 108 | $free_template_type = $data['data']['data']['free_template']['metafields']['free_pro'] ?? ''; |
| 109 | |
| 110 | // If free_template doesn't match requested type, remove it |
| 111 | if ($free_template_type !== $requested_type) { |
| 112 | unset($data['data']['data']['free_template']); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | echo json_encode(['success' => true, 'data' => $data]); |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 |