| 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 |
} |
| 234 |
|