PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.6.23
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.6.23
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / api / class-weforms-form-notification-controller.php

class-weforms-form-notification-controller.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.6.23, at includes/api/class-weforms-form-notification-controller.php

255 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Settings manager class
5 *
6 * @since 1.4.2
7 */
8 class Weforms_Form_Notification_Controller extends Weforms_REST_Controller {
9
10 /**
11 * Endpoint namespace
12 *
13 * @var string
14 */
15 protected $namespace = 'weforms/v1';
16
17 /**
18 * Route name
19 *
20 * @var string
21 */
22 protected $rest_base = 'forms';
23
24 public function register_routes() {
25 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<form_id>[\d]+)/notifications',
26 [
27 'args' => [
28 'form_id' => [
29 'description' => __( 'Unique identifier for the object', 'weforms' ),
30 'type' => 'integer',
31 'sanitize_callback' => 'absint',
32 'validate_callback' => [ $this, 'is_form_exists' ],
33 'required' => true,
34 ],
35 ],
36 [
37 'methods' => WP_REST_Server::READABLE,
38 'callback' => [ $this, 'get_item_notification' ],
39 'args' => [
40 'context' => $this->get_context_param( [ 'default' => 'view' ] ),
41 ],
42 'permission_callback' => [ $this, 'get_item_permissions_check' ],
43 ],
44 [
45 'methods' => WP_REST_Server::CREATABLE,
46 'callback' => [ $this, 'add_item_notification' ],
47 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
48 'permission_callback' => [ $this, 'get_item_permissions_check' ],
49 ],
50 [
51 'methods' => WP_REST_Server::EDITABLE,
52 'callback' => [ $this, 'update_item_notification' ],
53 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
54 'permission_callback' => [ $this, 'get_item_permissions_check' ],
55 ],
56 ]
57 );
58
59 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<form_id>[\d]+)/notifications',
60 [
61 'args' => [
62 'form_id' => [
63 'description' => __( 'Unique identifier for the object', 'weforms' ),
64 'type' => 'integer',
65 'sanitize_callback' => 'absint',
66 'validate_callback' => [ $this, 'is_form_exists' ],
67 'required' => true,
68 ],
69 'index' => [
70 'description' => __( 'Unique identifier for the object', 'weforms' ),
71 'type' => 'array',
72 'items' => [
73 'type' => 'integer',
74 ],
75 'required' => true,
76 ],
77 ],
78 [
79 'methods' => WP_REST_Server::DELETABLE,
80 'callback' => [ $this, 'delete_item_notification' ],
81 // 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::DELETABLE ),
82 'permission_callback' => [ $this, 'delete_item_permissions_check' ],
83 ],
84 ]
85 );
86 }
87
88 /**
89 * get item notification
90 *
91 * @since 1.4.2
92 *
93 * @param WP_REST_Request $request
94 *
95 * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure
96 */
97 public function get_item_notification( $request ) {
98 $form_id = $request->get_param( 'form_id' );
99 $form = weforms()->form->get( $form_id );
100 $data = $form->get_notifications();
101
102 $response = $this->prepare_response_for_collection( $data, $request );
103 $response = rest_ensure_response( $response );
104 $response->header( 'X-WP-Total', (int) count( $data ) );
105
106 return $response;
107 }
108
109 /**
110 * add item notification
111 *
112 * @since 1.4.2
113 *
114 * @param WP_REST_Request $request
115 *
116 * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure
117 */
118 public function add_item_notification( $request ) {
119 $form_id = $request->get_param( 'form_id' );
120 $notifications = $request->get_param( 'notifications' );
121
122 $data = [
123 'form_id' => $form_id,
124 'notifications' => $notifications,
125 ];
126
127 $form = weforms()->form->get( $form_id );
128 $new_form_notification = array_merge( $form->get_notifications(), $data['notifications'] );
129
130 update_post_meta( $data['form_id'], 'notifications', $new_form_notification );
131
132 $form = weforms()->form->get( $form_id );
133
134 $response_data = [
135 'id' => $form->data->ID,
136 'notifications' => $form->get_notifications(),
137 ];
138
139 $response = rest_ensure_response( $response_data );
140 $response->set_status( 201 );
141
142 return $response;
143 }
144
145 /**
146 * update item integrations
147 *
148 * @since 1.4.2
149 *
150 * @param WP_REST_Request $request
151 *
152 * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure
153 */
154 public function update_item_notification( $request ) {
155 $wpuf_form_id = $request->get_param( 'form_id' );
156 $notifications = $request->get_param( 'notifications' );
157
158 $data = [
159 'form_id' => $wpuf_form_id,
160 'notifications' => $notifications,
161 ];
162
163 $form = weforms()->form->get( $wpuf_form_id );
164 $existing_notifications = $form->get_notifications();
165
166 foreach ( $existing_notifications as $key => $notification ) {
167 if ( array_key_exists( $key, $data['notifications'] ) ) {
168 $existing_notifications[ $key ] = $data['notifications'][$key];
169 }
170 }
171
172 update_post_meta( $data['form_id'], 'notifications', $existing_notifications );
173
174 $form = weforms()->form->get( $wpuf_form_id );
175
176 $response_data = [
177 'id' => $form->data->ID,
178 'notifications' => $form->get_notifications(),
179 ];
180
181 $response = rest_ensure_response( $response_data );
182 $response->set_status( 201 );
183 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $form->id ) ) );
184
185 return $response;
186 }
187
188 /**
189 * delete item notification
190 *
191 * @since 1.4.2
192 *
193 * @param WP_REST_Request $request
194 *
195 * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure
196 */
197 public function delete_item_notification( $request ) {
198 $form_id = $request->get_param( 'form_id' );
199 $notification_ids = $request->get_param( 'index' );
200 $form = weforms()->form->get( $form_id );
201 $form_notification = $form->get_notifications();
202
203 foreach ( $notification_ids as $notification_id ) {
204 unset( $form_notification[ $notification_id ] );
205 }
206
207 update_post_meta( $form_id, 'notifications', array_values( $form_notification ) );
208
209 $form = weforms()->form->get( $form_id );
210
211 $data = [
212 'id' => $form->data->ID,
213 'notifications' => $form->get_notifications(),
214 ];
215
216 $response = $this->prepare_response_for_collection( $data, $request );
217 $response = rest_ensure_response( $response );
218 $response->header( 'X-WP-Total', (int) count( $data['notifications'] ) );
219
220 return $response;
221 }
222
223 /**
224 * Get the Form's schema, conforming to JSON Schema
225 *
226 * @return array
227 */
228 public function get_item_schema() {
229 $schema = [
230 '$schema' => 'http://json-schema.org/draft-04/schema#',
231 'title' => 'forms',
232 'type' => 'object',
233 'properties' => [
234 'form_id' => [
235 'description' => __( 'Unique identifier for the object', 'weforms' ),
236 'type' => 'integer',
237 'sanitize_callback' => 'absint',
238 'validate_callback' => [ $this, 'is_form_exists' ],
239 'context' => [ 'embed', 'view', 'edit' ],
240 'required' => true,
241 'readonly' => true,
242 ],
243 'notifications' => [
244 'description' => __( '', 'weforms' ),
245 'type' => 'object',
246 'context' => [ 'edit', 'view'],
247 'required' => false,
248 ],
249 ],
250 ];
251
252 return $schema;
253 }
254 }
255