PluginProbe
SuperFrete / 3.3.2
SuperFrete v3.3.2
trunk 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4
superfrete / app / Controllers / OAuthController.php

OAuthController.php in SuperFrete 3.3.2, at app/Controllers/OAuthController.php

205 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SuperFrete_API\Controllers;
4
5 use WP_REST_Server;
6 use WP_REST_Request;
7 use WP_REST_Response;
8 use WP_Error;
9
10 if (!defined('ABSPATH')) {
11 exit; // Security check
12 }
13
14 class OAuthController
15 {
16
17 public function __construct()
18 {
19 add_action('rest_api_init', [$this, 'register_routes']);
20
21 // Also register AJAX handlers as backup
22 add_action('wp_ajax_superfrete_oauth_proxy', [$this, 'ajax_oauth_proxy']);
23 add_action('wp_ajax_nopriv_superfrete_oauth_proxy', [$this, 'ajax_oauth_proxy']);
24 }
25
26 /**
27 * Register REST API routes for OAuth proxy
28 */
29 public function register_routes()
30 {
31 // Add debug logging
32 error_log('SuperFrete OAuth: Registering REST API routes');
33
34 // Proxy endpoint for polling OAuth token
35 $result = register_rest_route('superfrete/v1', '/oauth/token', [
36 'methods' => WP_REST_Server::READABLE,
37 'callback' => [$this, 'proxy_oauth_token'],
38 'permission_callback' => '__return_true', // Temporarily allow all for debugging
39 'args' => [
40 'session_id' => [
41 'required' => true,
42 'type' => 'string',
43 'description' => 'OAuth session ID'
44 ]
45 ]
46 ]);
47
48 if ($result) {
49 error_log('SuperFrete OAuth: REST route registered successfully');
50 } else {
51 error_log('SuperFrete OAuth: Failed to register REST route');
52 }
53 }
54
55 /**
56 * AJAX handler for OAuth proxy (backup method)
57 */
58 public function ajax_oauth_proxy()
59 {
60 error_log('SuperFrete OAuth: AJAX proxy called');
61
62 // Check nonce for security
63 if (!isset($_GET['nonce']) || !wp_verify_nonce($_GET['nonce'], 'wp_rest')) {
64 wp_send_json_error('Invalid nonce');
65 return;
66 }
67
68 $session_id = sanitize_text_field($_GET['session_id'] ?? '');
69 error_log('SuperFrete OAuth: AJAX session_id = ' . $session_id);
70
71 if (empty($session_id)) {
72 wp_send_json_error('Session ID is required');
73 return;
74 }
75
76 // Get the headless API URL from settings
77 $api_url = $this->get_headless_api_url();
78 if (!$api_url) {
79 wp_send_json_error('Headless API URL not configured');
80 return;
81 }
82
83 // Make server-side request to headless API
84 $url = $api_url . '/headless/oauth/token?session_id=' . urlencode($session_id);
85 error_log('SuperFrete OAuth: AJAX calling URL = ' . $url);
86
87 $response = wp_remote_get($url, [
88 'timeout' => 30,
89 'headers' => [
90 'Accept' => 'application/json',
91 'User-Agent' => 'SuperFrete-WordPress-Plugin/2.1.4'
92 ]
93 ]);
94
95 if (is_wp_error($response)) {
96 error_log('SuperFrete OAuth: AJAX wp_remote_get error = ' . $response->get_error_message());
97 wp_send_json_error('Failed to connect to headless API: ' . $response->get_error_message());
98 return;
99 }
100
101 $status_code = wp_remote_retrieve_response_code($response);
102 $body = wp_remote_retrieve_body($response);
103
104 error_log('SuperFrete OAuth: AJAX response status = ' . $status_code);
105 error_log('SuperFrete OAuth: AJAX response body = ' . $body);
106
107 // Try to decode JSON response
108 $data = json_decode($body, true);
109 if (json_last_error() !== JSON_ERROR_NONE) {
110 wp_send_json_error('Invalid JSON response from API');
111 return;
112 }
113
114 // Return the data
115 wp_send_json($data);
116 }
117
118 /**
119 * Proxy OAuth token request to headless API
120 *
121 * @param WP_REST_Request $request
122 * @return WP_REST_Response|WP_Error
123 */
124 public function proxy_oauth_token(WP_REST_Request $request)
125 {
126 error_log('SuperFrete OAuth: proxy_oauth_token called');
127
128 $session_id = $request->get_param('session_id');
129 error_log('SuperFrete OAuth: session_id = ' . $session_id);
130
131 if (empty($session_id)) {
132 error_log('SuperFrete OAuth: Missing session_id');
133 return new WP_Error('missing_session_id', 'Session ID is required', ['status' => 400]);
134 }
135
136 // Get the headless API URL from settings
137 $api_url = $this->get_headless_api_url();
138 if (!$api_url) {
139 return new WP_Error('api_url_not_configured', 'Headless API URL not configured', ['status' => 500]);
140 }
141
142 // Make server-side request to headless API
143 $url = $api_url . '/headless/oauth/token?session_id=' . urlencode($session_id);
144
145 $response = wp_remote_get($url, [
146 'timeout' => 30,
147 'headers' => [
148 'Accept' => 'application/json',
149 'User-Agent' => 'SuperFrete-WordPress-Plugin/2.1.4'
150 ]
151 ]);
152
153 if (is_wp_error($response)) {
154 return new WP_Error('api_request_failed', 'Failed to connect to headless API: ' . $response->get_error_message(), ['status' => 500]);
155 }
156
157 $status_code = wp_remote_retrieve_response_code($response);
158 $body = wp_remote_retrieve_body($response);
159
160 // Try to decode JSON response
161 $data = json_decode($body, true);
162 if (json_last_error() !== JSON_ERROR_NONE) {
163 return new WP_Error('invalid_json_response', 'Invalid JSON response from API', ['status' => 500]);
164 }
165
166 // Return the same response structure as the headless API
167 return new WP_REST_Response($data, $status_code);
168 }
169
170 /**
171 * Get headless API URL from WordPress settings
172 *
173 * @return string|null
174 */
175 private function get_headless_api_url()
176 {
177 // Check if we're in sandbox mode
178 $sandbox_mode = get_option('superfrete_sandbox_mode', 'no');
179
180 // Debug logging
181 error_log('SuperFrete OAuth: sandbox_mode setting = ' . $sandbox_mode);
182
183 if ($sandbox_mode === 'yes') {
184 // Development/sandbox environment
185 $api_url = 'https://api.dev.superintegrador.superfrete.com';
186 error_log('SuperFrete OAuth: Using dev API = ' . $api_url);
187 return $api_url;
188 } else {
189 // Production environment
190 $api_url = 'https://api.superintegrador.superfrete.com';
191 error_log('SuperFrete OAuth: Using production API = ' . $api_url);
192 return $api_url;
193 }
194 }
195
196 /**
197 * Check if user has admin permissions
198 *
199 * @return bool
200 */
201 public function check_admin_permissions()
202 {
203 return current_user_can('manage_options');
204 }
205 }