Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 56 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| EventsController | |
0.00% |
0 / 56 |
|
0.00% |
0 / 3 |
72 | |
0.00% |
0 / 1 |
| send | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| send_batch | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
30 | |||
| get_send_event_args | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace NewfoldLabs\WP\Module\Patterns\Api\Controllers; |
| 3 | |
| 4 | use NewfoldLabs\WP\Module\Patterns\Library\Events; |
| 5 | use NewfoldLabs\WP\Module\Patterns\Api\RemoteRequest; |
| 6 | |
| 7 | /** |
| 8 | * Controller for events. |
| 9 | */ |
| 10 | class EventsController { |
| 11 | |
| 12 | /** |
| 13 | * Return all patterns. |
| 14 | * |
| 15 | * @param WP_REST_Request $request Request object. |
| 16 | */ |
| 17 | public static function send( $request ) { |
| 18 | |
| 19 | $params = $request->get_params(); |
| 20 | |
| 21 | $data = Events::send( $params ); |
| 22 | |
| 23 | if ( \is_wp_error( $data ) ) { |
| 24 | return new \WP_REST_Response( RemoteRequest::format_error_data( $data ), 503 ); |
| 25 | } |
| 26 | |
| 27 | return new \WP_REST_Response( $data ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Sends an array of Hiive Events to the data module API programmatically. |
| 32 | * |
| 33 | * @param \WP_REST_Request $request The incoming request object. |
| 34 | * @return \WP_REST_Response|\WP_Error |
| 35 | */ |
| 36 | public static function send_batch( \WP_REST_Request $request ) { |
| 37 | $events = $request->get_json_params(); |
| 38 | if ( ! rest_is_array( $events ) ) { |
| 39 | |
| 40 | $error = new \WP_Error( |
| 41 | 'nfd_wonder_blocks_error', |
| 42 | __( 'Request does not contain an array of events.', 'nfd-wonder-blocks' ) |
| 43 | ); |
| 44 | |
| 45 | return new \WP_REST_Response( RemoteRequest::format_error_data( $error ), 503 ); |
| 46 | } |
| 47 | |
| 48 | $response_errors = array(); |
| 49 | foreach ( $events as $index => $event ) { |
| 50 | $response = Events::send( $event ); |
| 51 | if ( is_wp_error( $response ) ) { |
| 52 | array_push( |
| 53 | $response_errors, |
| 54 | array( |
| 55 | 'index' => $index, |
| 56 | 'data' => $response, |
| 57 | ) |
| 58 | ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if ( ! empty( $response_errors ) ) { |
| 63 | $error = new \WP_Error( |
| 64 | 'nfd_wonder_blocks_error', |
| 65 | __( 'Some events failed.', 'nfd-wonder-blocks' ), |
| 66 | array( |
| 67 | 'data' => $response_errors, |
| 68 | ) |
| 69 | ); |
| 70 | |
| 71 | return new \WP_REST_Response( RemoteRequest::format_error_data( $error ), 503 ); |
| 72 | } |
| 73 | |
| 74 | return new \WP_REST_Response( |
| 75 | array(), |
| 76 | 202 |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Args for a single event. |
| 82 | * |
| 83 | * @return array |
| 84 | */ |
| 85 | public static function get_send_event_args() { |
| 86 | return array( |
| 87 | 'action' => array( |
| 88 | 'required' => true, |
| 89 | 'description' => __( 'Event action', 'nfd-wonder-blocks' ), |
| 90 | 'type' => 'string', |
| 91 | 'sanitize_callback' => 'sanitize_title', |
| 92 | 'validate_callback' => array( Events::class, 'validate_action' ), |
| 93 | ), |
| 94 | 'category' => array( |
| 95 | 'default' => Events::get_category(), |
| 96 | 'description' => __( 'Event category', 'nfd-wonder-blocks' ), |
| 97 | 'type' => 'string', |
| 98 | 'sanitize_callback' => 'sanitize_title', |
| 99 | 'validate_callback' => array( Events::class, 'validate_category' ), |
| 100 | ), |
| 101 | 'data' => array( |
| 102 | 'description' => __( 'Event data', 'nfd-wonder-blocks' ), |
| 103 | 'type' => 'object', |
| 104 | ), |
| 105 | ); |
| 106 | } |
| 107 | } |