PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.4.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.4.1
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Controllers / Rest / IncomingWebhooksController.php
IncomingWebhooksController.php
149 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SureCart\Controllers\Rest;
4
5 use SureCart\Concerns\SanitizesRestParams;
6 use SureCart\Models\IncomingWebhook;
7
8 /**
9 * Handle Price requests through the REST API
10 */
11 class IncomingWebhooksController {
12 use SanitizesRestParams;
13
14 /**
15 * Create a product integration.
16 *
17 * @param \WP_REST_Request $request Rest Request.
18 *
19 * @return \WP_REST_Response|\WP_Error
20 */
21 public function create( \WP_REST_Request $request ) {
22 return IncomingWebhook::create( $request->get_params() );
23 }
24
25 /**
26 * Retry the incoming webhook.
27 *
28 * @param \WP_REST_Request $request Rest Request.
29 *
30 * @return void
31 */
32 public function retry( \WP_REST_Request $request ) {
33 try {
34 $webhook = \SureCart::async()->handle( $request['id'] );
35 } catch ( \Exception $e ) {
36 return new \WP_Error( 'webhook_retry_failed', $e->getMessage() );
37 }
38
39 if ( is_wp_error( $webhook ) ) {
40 return $webhook;
41 }
42
43 if ( empty( $webhook ) ) {
44 return new \WP_Error( 'webhook_retry_failed', 'Webhook retry failed.' );
45 }
46
47 return $webhook->toArray();
48 }
49
50 /**
51 * List all product integrations.
52 *
53 * @param \WP_REST_Request $request Rest Request.
54 *
55 * @return \WP_REST_Response|\WP_Error
56 */
57 public function index( \WP_REST_Request $request ) {
58 $webhook = new IncomingWebhook();
59
60 // processed is false.
61 if ( false === $request->get_param( 'processed' ) ) {
62 $webhook = $webhook->whereNull( 'processed_at' );
63 } elseif ( ! empty( $request->get_param( 'processed' ) ) ) {
64 $webhook = $webhook->whereNotNull( 'processed_at' );
65 }
66
67 // webhook ids.
68 $webhook_ids = $request->get_param( 'webhook_ids' );
69 if ( ! empty( $webhook_ids ) ) {
70 $webhook = $webhook->whereIn( 'webhook_ids', array_map( 'sanitize_text_field', (array) $webhook_ids ) );
71 }
72
73 $total = $webhook->count();
74 $page = $request->get_param( 'page' ) ? $request->get_param( 'page' ) : 1;
75 $per_page = $request->get_param( 'per_page' ) ? $request->get_param( 'per_page' ) : 10;
76
77 // handle pagination.
78 $items = $webhook->paginate(
79 [
80 'page' => $page,
81 'per_page' => $per_page,
82 ]
83 );
84
85 $response = rest_ensure_response( $items );
86 $response->header( 'X-WP-Total', (int) $total );
87 $response->header( 'X-WP-TotalPages', (int) ceil( $total / (int) $per_page ) );
88
89 return $response;
90 }
91
92 /**
93 * Find a specific product integration.
94 *
95 * @param \WP_REST_Request $request Rest Request.
96 *
97 * @return \WP_REST_Response|\WP_Error
98 */
99 public function find( \WP_REST_Request $request ) {
100 $webhook = new IncomingWebhook();
101
102 // processed is false.
103 if ( false === $request->get_param( 'processed' ) ) {
104 $webhook = $webhook->whereNull( 'processed' );
105 } elseif ( ! empty( $request->get_param( 'processed' ) ) ) {
106 $webhook = $webhook->whereNotNull( 'processed' );
107 }
108
109 $webhook_id = $this->sanitizeFilterParam( $request->get_param( 'webhook_id' ) );
110 if ( null !== $webhook_id ) {
111 $webhook = $webhook->where( 'webhook_id', $webhook_id );
112 }
113
114 return $webhook->find( $this->sanitizeFilterParam( $request['id'] ) );
115 }
116
117 /**
118 * Edit model.
119 *
120 * @param \WP_REST_Request $request Rest Request.
121 *
122 * @return \WP_REST_Response|\WP_Error
123 */
124 public function edit( \WP_REST_Request $request ) {
125 return IncomingWebhook::where(
126 array_filter(
127 [
128 'id' => $this->sanitizeFilterParam( $request['id'] ),
129 'webhook_id' => $this->sanitizeFilterParam( $request->get_param( 'webhook_id' ) ),
130 ],
131 function ( $value ) {
132 return null !== $value;
133 }
134 )
135 )->update( array_diff_assoc( $request->get_params(), $request->get_query_params() ) );
136 }
137
138 /**
139 * Delete model.
140 *
141 * @param \WP_REST_Request $request Rest Request.
142 *
143 * @return \WP_REST_Response|\WP_Error
144 */
145 public function delete( \WP_REST_Request $request ) {
146 return IncomingWebhook::delete( $request['id'] );
147 }
148 }
149