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-api-rest-controller.php

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

220 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 abstract class Weforms_REST_Controller extends WP_REST_Controller {
4
5 public function get_item_permissions_check( $request ) {
6 return $this->get_items_permissions_check( $request );
7 }
8
9 public function get_items_permissions_check( $request ) {
10 if ( ! current_user_can( weforms_form_access_capability() ) ) {
11 return new WP_Error( 'rest_weforms_forbidden_context', __( 'Sorry, you are not allowed','weforms' ), array( 'status' => rest_authorization_required_code() ) );
12 }
13
14 return true;
15 }
16
17 /**
18 * Check if a given request has access to create items
19 *
20 * @param WP_REST_Request $request Full data about the request.
21 * @return WP_Error|bool
22 */
23 public function create_item_permissions_check( $request ) {
24 return $this->get_items_permissions_check( $request );
25 }
26
27 /**
28 * Check if a given request has access to update a specific item
29 *
30 * @param WP_REST_Request $request Full data about the request.
31 * @return WP_Error|bool
32 */
33 public function update_item_permissions_check( $request ) {
34 return $this->get_items_permissions_check( $request );
35 }
36
37 /**
38 * Check if a given request has access to delete a specific item
39 *
40 * @param WP_REST_Request $request Full data about the request.
41 * @return WP_Error|bool
42 */
43 public function delete_item_permissions_check( $request ) {
44 return $this->create_item_permissions_check( $request );
45 }
46
47 /**
48 * Prepare a single user output for response
49 *
50 * @param object $item
51 * @param WP_REST_Request $request Request object.
52 * @param array $additional_fields (optional)
53 *
54 * @return WP_REST_Response $response Response data.
55 */
56 public function prepare_item_for_response( $item, $request, $additional_fields = [] ) {
57 $response = rest_ensure_response( $item );
58 $response = $this->add_links( $response, $item );
59
60 return $response;
61 }
62
63 /**
64 * Adds multiple links to the response.
65 *
66 * @since 1.4.2
67 *
68 * @param object $response
69 * @param object $item
70 *
71 * @return object $response
72 */
73 protected function add_links( $response, $item ) {
74 $response->data['_links'] = $this->prepare_links( $item );
75
76 return $response;
77 }
78
79 /**
80 * Prepare links for the request.
81 *
82 * @since 1.4.2
83 *
84 * @param object $item
85 *
86 * @return array Links for the given user.
87 */
88 protected function prepare_links( $item ) {
89 $links = [
90 'self' => [
91 'href' => rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $item['id'] ) ),
92 ],
93 'collection' => [
94 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
95 ]
96 ];
97
98 return $links;
99 }
100
101 /**
102 * Format item's collection for response
103 *
104 * @since 1.4.2
105 *
106 * @param object $response
107 * @param object $request
108 * @param array $items
109 * @param int $total_items
110 *
111 * @return object
112 */
113 public function format_collection_response( $response, $request, $total_items ) {
114 if ( $total_items === 0 ) {
115 return $response;
116 }
117
118 // Store pagation values for headers then unset for count query.
119 $per_page = (int) ( ! empty( $request['per_page'] ) ? $request['per_page'] : 20 );
120 $page = (int) ( ! empty( $request['page'] ) ? $request['page'] : 1 );
121
122 $response->header( 'X-WP-Total', (int) $total_items );
123
124 $max_pages = ceil( $total_items / $per_page );
125
126 $response->header( 'X-WP-TotalPages', (int) $max_pages );
127 $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
128
129 if ( $page > 1 ) {
130 $prev_page = $page - 1;
131 if ( $prev_page > $max_pages ) {
132 $prev_page = $max_pages;
133 }
134 $prev_link = add_query_arg( 'page', $prev_page, $base );
135 $response->link_header( 'prev', $prev_link );
136 }
137 if ( $max_pages > $page ) {
138
139 $next_page = $page + 1;
140 $next_link = add_query_arg( 'page', $next_page, $base );
141 $response->link_header( 'next', $next_link );
142 }
143
144 return $response;
145 }
146
147 /**
148 * Check form exist or not
149 *
150 * @since 1.4.2
151 *
152 * @param string $param
153 * @param WP_REST_Request $request
154 * @param string $key
155 *
156 * @return boolean
157 */
158 public function is_form_exists( $param, $request, $key ) {
159 global $wpdb;
160
161 $form_id = (int) $request['form_id'];
162
163 $querystr = "
164 SELECT $wpdb->posts.id
165 FROM $wpdb->posts
166 WHERE $wpdb->posts.ID = $form_id
167 AND $wpdb->posts.post_type = 'wpuf_contact_form'
168 ";
169
170 $result = $wpdb->get_var( $querystr );
171
172 if( empty( $result ) ) {
173 return new WP_Error( 'rest_weforms_form', __( 'Form Not Exist','weforms' ), array( 'status' => 404 ) );
174 } else {
175 return true;
176 }
177 }
178
179 /**
180 * Check Entries exist or not
181 *
182 * @since 1.4.2
183 *
184 * @param string $param
185 * @param WP_REST_Request $request
186 * @param string $key
187 *
188 * @return boolean
189 */
190 public function is_entry_exists( $param, $request, $key ) {
191 global $wpdb;
192
193 if( is_array( $request['entry_id'] ) ) {
194 $entry_id = implode( ",", $request['entry_id'] );
195 $querystr = "
196 SELECT $wpdb->weforms_entries.id
197 FROM $wpdb->weforms_entries
198 WHERE $wpdb->weforms_entries.ID IN ( $entry_id )
199 ";
200 } else {
201 $entry_id = (int) $request['entry_id'];
202 $querystr = "
203 SELECT $wpdb->weforms_entries.id
204 FROM $wpdb->weforms_entries
205 WHERE $wpdb->weforms_entries.ID = $entry_id
206 ";
207 }
208
209 $result = $wpdb->get_results( $querystr );
210
211 if ( empty( $result ) ) {
212 return false;
213 } else {
214 return true;
215 }
216
217 return true;
218 }
219 }
220