PluginProbe ʕ •ᴥ•ʔ
Flex Import / 1.8
Flex Import v1.8
2.9 trunk 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8
flex-import / includes / flex-get-templates-api.php
flex-import / includes Last commit date
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
69 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 $args = [
36 'method' => $request_method,
37 'headers' => [
38 'Content-Type' => 'application/json',
39 ],
40 ];
41
42 // POST requests
43 if ($request_method === 'POST') {
44 $args['body'] = json_encode($data);
45 }
46
47 $response = ($request_method === 'POST') ? wp_remote_post($url, $args) : wp_remote_get($url, $args);
48
49 // Check for errors
50 if (is_wp_error($response)) {
51 echo json_encode(['error' => 'Request failed: ' . $response->get_error_message()]);
52 exit;
53 }
54
55 $http_code = wp_remote_retrieve_response_code($response);
56 if ($http_code !== 200) {
57 echo json_encode(['error' => 'HTTP error: ' . $http_code]);
58 exit;
59 }
60
61 // Decode the response body
62 $response_body = wp_remote_retrieve_body($response);
63 $data = json_decode($response_body, true);
64
65 if (json_last_error() !== JSON_ERROR_NONE) {
66 echo json_encode(['error' => 'Invalid JSON format received from the external API']);
67 exit;
68 }
69 echo json_encode(['success' => true, 'data' => $data]);