Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 73 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
EventsController | |
0.00% |
0 / 73 |
|
0.00% |
0 / 4 |
72 | |
0.00% |
0 / 1 |
register_routes | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
2 | |||
get_send_args | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
2 | |||
send | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
send_batch | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | namespace NewfoldLabs\WP\Module\Onboarding\RestApi; |
3 | |
4 | use NewfoldLabs\WP\Module\Onboarding\Data\Events; |
5 | use NewfoldLabs\WP\Module\Onboarding\Permissions; |
6 | use NewfoldLabs\WP\Module\Onboarding\Services\EventService; |
7 | |
8 | /** |
9 | * Controller to send analytics events. |
10 | */ |
11 | class EventsController extends \WP_REST_Controller { |
12 | |
13 | /** |
14 | * The namespace of the controller. |
15 | * |
16 | * @var string |
17 | */ |
18 | protected $namespace = 'newfold-onboarding/v1'; |
19 | |
20 | /** |
21 | * The REST base endpoint. |
22 | * |
23 | * @var string |
24 | */ |
25 | protected $rest_base = '/events'; |
26 | |
27 | /** |
28 | * Register routes that the controller will expose. |
29 | * |
30 | * @return void |
31 | */ |
32 | public function register_routes() { |
33 | \register_rest_route( |
34 | $this->namespace, |
35 | $this->rest_base, |
36 | array( |
37 | array( |
38 | 'methods' => \WP_REST_Server::CREATABLE, |
39 | 'callback' => array( $this, 'send' ), |
40 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
41 | 'args' => $this->get_send_args(), |
42 | ), |
43 | ) |
44 | ); |
45 | |
46 | \register_rest_route( |
47 | $this->namespace, |
48 | $this->rest_base . '/batch', |
49 | array( |
50 | array( |
51 | 'methods' => \WP_REST_Server::CREATABLE, |
52 | 'callback' => array( $this, 'send_batch' ), |
53 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
54 | ), |
55 | ) |
56 | ); |
57 | } |
58 | |
59 | /** |
60 | * Args for a single event. |
61 | * |
62 | * @return array |
63 | */ |
64 | public function get_send_args() { |
65 | return array( |
66 | 'action' => array( |
67 | 'required' => true, |
68 | 'description' => __( 'Event action', 'wp-module-onboarding' ), |
69 | 'type' => 'string', |
70 | 'sanitize_callback' => 'sanitize_title', |
71 | 'validate_callback' => array( EventService::class, 'validate_action' ), |
72 | ), |
73 | 'category' => array( |
74 | 'default' => Events::get_category()[0], |
75 | 'description' => __( 'Event category', 'wp-module-onboarding' ), |
76 | 'type' => 'string', |
77 | 'sanitize_callback' => 'sanitize_title', |
78 | 'validate_callback' => array( EventService::class, 'validate_category' ), |
79 | ), |
80 | 'data' => array( |
81 | 'description' => __( 'Event data', 'wp-module-onboarding' ), |
82 | 'type' => 'object', |
83 | ), |
84 | ); |
85 | } |
86 | |
87 | /** |
88 | * Sends a Hiive Event to the data module API. |
89 | * |
90 | * @param \WP_REST_Request $request The incoming request object. |
91 | * @return \WP_REST_Response|\WP_Error |
92 | */ |
93 | public function send( \WP_REST_Request $request ) { |
94 | return EventService::send( $request->get_params() ); |
95 | } |
96 | |
97 | /** |
98 | * Sends an array of Hiive Events to the data module API programmatically. |
99 | * |
100 | * @param \WP_REST_Request $request The incoming request object. |
101 | * @return \WP_REST_Response|\WP_Error |
102 | */ |
103 | public function send_batch( \WP_REST_Request $request ) { |
104 | $events = $request->get_json_params(); |
105 | if ( ! rest_is_array( $events ) ) { |
106 | return new \WP_Error( |
107 | 'nfd_module_onboarding_error', |
108 | __( 'Request does not contain an array of events.', 'wp-module-onboarding' ) |
109 | ); |
110 | } |
111 | |
112 | $response_errors = array(); |
113 | foreach ( $events as $index => $event ) { |
114 | $response = EventService::send( $event ); |
115 | if ( is_wp_error( $response ) ) { |
116 | array_push( |
117 | $response_errors, |
118 | array( |
119 | 'index' => $index, |
120 | 'data' => $response, |
121 | ) |
122 | ); |
123 | } |
124 | } |
125 | |
126 | if ( ! empty( $response_errors ) ) { |
127 | return new \WP_Error( |
128 | 'nfd_module_onboarding_error', |
129 | __( 'Some events failed.', 'wp-module-onboarding' ), |
130 | array( |
131 | 'data' => $response_errors, |
132 | ) |
133 | ); |
134 | } |
135 | |
136 | return new \WP_REST_Response( |
137 | array(), |
138 | 202 |
139 | ); |
140 | } |
141 | } |