Jake Vanderwerf
2 days ago 235ce5716edc2f7cbe80fdccf26eac7269587839
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace JVBase\rest\routes;
 
use JVBase\rest\Rest;
use JVBase\rest\Route;
use WP_REST_Request;
use WP_REST_Response;
use Exception;
 
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
 
class IntegrationsRoutes extends Rest
{
 
    /**
     * Register REST routes
     */
    public function registerRoutes(): void
    {
        Route::for('integrations')
            ->post([$this, 'handleAction'])
            ->args([
                'service'   => 'string|required|enum:'.implode(',',JVB()->getAvailableServices()),
                'action'    => 'string|required',
                'user_id'   => 'int',
                'context'   => 'string|enum:admin,user',
                'data'      => 'array'
            ])
            ->auth('user')
            ->rateLimit(20)
            ->register();
        Route::for('oath/connect')
            ->post([$this, 'initiateOAuth'])
            ->auth('user')
            ->rateLimit(20)
            ->args([
                'service'   => 'string|required',
                'user_id'   => 'int',
                'return_url'=> 'url'
            ])
            ->register();
    }
 
    /**
     * Handle integration actions
     */
    public function handleAction(WP_REST_Request $request): WP_REST_Response
    {
        error_log('[IntegrationsRoutes] handleAction request:'.print_r($request->get_params(), true));
        $service = $request->get_param('service');
        $action = $request->get_param('action');
 
 
        $theUserID = (user_can($request->get_param('user'), 'manage_options')) ? null : $request->get_param('user');
        $integration = JVB()->connect($service, $theUserID);
 
        if (!$integration) {
            return $this->validationError(['message'=>'Invalid service']);
        }
 
        $integration->getCredentials();
 
        // Handle the action
        try {
            $data = $request->get_param('data');
            if (!is_array($data) && empty($data)) {
                $data = null;
            }
            error_log('[IntegrationsRoutes] Calling processAction with data: ' . print_r($data, true));
 
            $result = $integration->processAction($action, $data);
 
            return $this->success($result);
 
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }
 
    public function initiateOAuth(WP_REST_Request $request): WP_REST_Response
    {
        $service = $request->get_param('service');
        $user_id = $request->get_param('user_id');
        $return_url = $request->get_param('return_url');
 
        $integration = JVB()->connect($service, $user_id);
 
        if (!$integration || !$integration->isOAuthService) {
            return $this->validationError(['message'=>'Invalid service']);
        }
 
        $auth_url = $integration->getOAuthUrl($return_url);
 
        if ($auth_url) {
            return $this->success($auth_url);
        }
 
        return $this->error('Failed to generate authorization URL');
    }
}