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