PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.4.2
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.4.2
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.4.2, at includes/api/class-weforms-form-notification-controller.php

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