← All changes
|
includes/api/class-weforms-forms-controller.php
+1649
-1649
1.6.2
→
1.4.8
View file →
| @@ -1,1649 +1,1649 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -class Weforms_Forms_Controller extends Weforms_REST_Controller { | |
| 4 | - | |
| 5 | - /** | |
| 6 | - * Endpoint namespace | |
| 7 | - * | |
| 8 | - * @var string | |
| 9 | - */ | |
| 10 | - protected $namespace = 'weforms/v1'; | |
| 11 | - | |
| 12 | - /** | |
| 13 | - * Route name | |
| 14 | - * | |
| 15 | - * @var string | |
| 16 | - */ | |
| 17 | - protected $rest_base = 'forms'; | |
| 18 | - | |
| 19 | - /** | |
| 20 | - * Register all routes releated with forms | |
| 21 | - * | |
| 22 | - * @return void | |
| 23 | - */ | |
| 24 | - public function register_routes() { | |
| 25 | - register_rest_route( | |
| 26 | - $this->namespace, | |
| 27 | - '/' . $this->rest_base, | |
| 28 | - [ | |
| 29 | - [ | |
| 30 | - 'methods' => WP_REST_Server::READABLE, | |
| 31 | - 'callback' => [ $this, 'get_items' ], | |
| 32 | - 'args' => $this->get_collection_params(), | |
| 33 | - 'permission_callback' => [ $this, 'get_items_permissions_check' ], | |
| 34 | - ], | |
| 35 | - [ | |
| 36 | - 'methods' => WP_REST_Server::CREATABLE, | |
| 37 | - 'callback' => [ $this, 'create_item' ], | |
| 38 | - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), | |
| 39 | - 'permission_callback' => [ $this, 'create_item_permissions_check' ], | |
| 40 | - ], | |
| 41 | - [ | |
| 42 | - 'methods' => WP_REST_Server::DELETABLE, | |
| 43 | - 'callback' => [ $this, 'bulk_delete_form' ], | |
| 44 | - 'permission_callback' => [ $this, 'delete_item_permissions_check' ], | |
| 45 | - 'args' => [ | |
| 46 | - 'ids' => [ | |
| 47 | - 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 48 | - 'type' => 'array', | |
| 49 | - 'items' => [ | |
| 50 | - 'type' => 'integer', | |
| 51 | - ], | |
| 52 | - 'validate_callback' => [ $this, 'is_bulk_delete_form_exists' ], | |
| 53 | - 'required' => true, | |
| 54 | - ], | |
| 55 | - ], | |
| 56 | - ], | |
| 57 | - ] | |
| 58 | - ); | |
| 59 | - | |
| 60 | - register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<form_id>\d+)', [ | |
| 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 | - ], | |
| 70 | - [ | |
| 71 | - 'methods' => WP_REST_Server::READABLE, | |
| 72 | - 'callback' => [ $this, 'get_item' ], | |
| 73 | - 'args' => [ | |
| 74 | - 'context' => $this->get_context_param( [ 'default' => 'view' ] ), | |
| 75 | - ], | |
| 76 | - 'permission_callback' => [ $this, 'get_item_permissions_check' ], | |
| 77 | - ], | |
| 78 | - [ | |
| 79 | - 'methods' => WP_REST_Server::DELETABLE, | |
| 80 | - 'callback' => [ $this, 'delete_item' ], | |
| 81 | - 'args' => [ | |
| 82 | - 'force' => [ | |
| 83 | - 'type' => 'boolean', | |
| 84 | - 'default' => false, | |
| 85 | - 'description' => __( 'Whether to bypass trash and force deletion.', 'weforms' ), | |
| 86 | - ], | |
| 87 | - ], | |
| 88 | - 'permission_callback' => [ $this, 'delete_item_permissions_check' ], | |
| 89 | - ], | |
| 90 | - [ | |
| 91 | - 'methods' => WP_REST_Server::EDITABLE, | |
| 92 | - 'callback' => [ $this, 'update_item' ], | |
| 93 | - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), | |
| 94 | - 'permission_callback' => [ $this, 'update_item_permissions_check' ], | |
| 95 | - ], | |
| 96 | - ] ); | |
| 97 | - | |
| 98 | - register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<form_id>[\d]+)' . '/entries/', | |
| 99 | - [ | |
| 100 | - 'args' => [ | |
| 101 | - 'form_id' => [ | |
| 102 | - 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 103 | - 'type' => 'integer', | |
| 104 | - 'sanitize_callback' => 'absint', | |
| 105 | - 'validate_callback' => [ $this, 'is_form_exists' ], | |
| 106 | - 'required' => true, | |
| 107 | - ], | |
| 108 | - ], | |
| 109 | - | |
| 110 | - [ | |
| 111 | - 'methods' => WP_REST_Server::CREATABLE, | |
| 112 | - 'callback' => [ $this, 'save_entry' ], | |
| 113 | - 'permission_callback' => [ $this, 'submit_permissions_check' ], | |
| 114 | - ], | |
| 115 | - [ | |
| 116 | - 'methods' => WP_REST_Server::READABLE, | |
| 117 | - 'callback' => [ $this, 'get_entry' ], | |
| 118 | - 'permission_callback' => [ $this, 'get_item_permissions_check' ], | |
| 119 | - 'args' => $this->get_collection_params(), | |
| 120 | - ], | |
| 121 | - ] | |
| 122 | - ); | |
| 123 | - | |
| 124 | - register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<form_id>[\d]+)' . '/duplicate/', [ | |
| 125 | - 'args' => [ | |
| 126 | - 'form_id' => [ | |
| 127 | - 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 128 | - 'key' => 'integer', | |
| 129 | - 'sanitize_callback' => 'absint', | |
| 130 | - 'validate_callback' => [ $this, 'is_form_exists' ], | |
| 131 | - 'required' => true, | |
| 132 | - ], | |
| 133 | - ], | |
| 134 | - [ | |
| 135 | - 'methods' => WP_REST_Server::CREATABLE, | |
| 136 | - 'callback' => [ $this, 'duplicate_form' ], | |
| 137 | - 'permission_callback' => [ $this, 'create_item_permissions_check' ], | |
| 138 | - ], | |
| 139 | - ] ); | |
| 140 | - | |
| 141 | - register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<form_id>[\d]+)' . '/export_entries/', [ | |
| 142 | - 'args' => [ | |
| 143 | - 'form_id' => [ | |
| 144 | - 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 145 | - 'type' => 'integer', | |
| 146 | - 'sanitize_callback' => 'absint', | |
| 147 | - 'validate_callback' => [ $this, 'is_form_exists' ], | |
| 148 | - 'required' => true, | |
| 149 | - ], | |
| 150 | - ], | |
| 151 | - | |
| 152 | - [ | |
| 153 | - 'methods' => WP_REST_Server::READABLE, | |
| 154 | - 'callback' => [ $this, 'export_form_entries' ], | |
| 155 | - 'permission_callback' => [ $this, 'get_items_permissions_check' ], | |
| 156 | - ], | |
| 157 | - ] ); | |
| 158 | - | |
| 159 | - register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<form_id>[\d]+)' . '/entries/(?P<entry_id>[\d]+)', | |
| 160 | - [ | |
| 161 | - 'args' => [ | |
| 162 | - 'form_id' => [ | |
| 163 | - 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 164 | - 'type' => 'integer', | |
| 165 | - 'sanitize_callback' => 'absint', | |
| 166 | - 'validate_callback' => [ $this, 'is_form_exists' ], | |
| 167 | - 'required' => true, | |
| 168 | - ], | |
| 169 | - 'entry_id' => [ | |
| 170 | - 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 171 | - 'type' => 'integer', | |
| 172 | - 'sanitize_callback' => 'absint', | |
| 173 | - 'validate_callback' => [ $this, 'is_entry_exists' ], | |
| 174 | - 'required' => true, | |
| 175 | - ], | |
| 176 | - 'context' => $this->get_context_param( [ 'default' => 'view' ] ), | |
| 177 | - ], | |
| 178 | - [ | |
| 179 | - 'methods' => WP_REST_Server::READABLE, | |
| 180 | - 'callback' => [ $this, 'get_entry_details' ], | |
| 181 | - 'permission_callback' => [ $this, 'get_item_permissions_check' ], | |
| 182 | - ], | |
| 183 | - ] | |
| 184 | - ); | |
| 185 | - | |
| 186 | - register_rest_route( $this->namespace, '/' . $this->rest_base . '/import', [ | |
| 187 | - [ | |
| 188 | - 'methods' => WP_REST_Server::CREATABLE, | |
| 189 | - 'callback' => [ $this, 'import_forms' ], | |
| 190 | - 'permission_callback' => [ $this, 'create_item_permissions_check' ], | |
| 191 | - ], | |
| 192 | - ] ); | |
| 193 | - | |
| 194 | - register_rest_route( $this->namespace, '/' . $this->rest_base . '/export', [ | |
| 195 | - 'args' => [ | |
| 196 | - 'ids' => [ | |
| 197 | - 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 198 | - 'type' => 'array', | |
| 199 | - 'validate_callback' => [ $this, 'is_bulk_delete_form_exists' ], | |
| 200 | - 'required' => false, | |
| 201 | - ], | |
| 202 | - ], | |
| 203 | - [ | |
| 204 | - 'methods' => WP_REST_Server::READABLE, | |
| 205 | - 'callback' => [ $this, 'export_forms' ], | |
| 206 | - 'permission_callback' => [ $this, 'get_item_permissions_check' ], | |
| 207 | - ], | |
| 208 | - ] ); | |
| 209 | - } | |
| 210 | - | |
| 211 | - /** | |
| 212 | - * Check Template Exist | |
| 213 | - * | |
| 214 | - * @param string $param | |
| 215 | - * @param WP_REST_Request $request | |
| 216 | - * @param string $key | |
| 217 | - * | |
| 218 | - * @return bool | |
| 219 | - **/ | |
| 220 | - public function is_template_exists( $param, $request, $key ) { | |
| 221 | - $templates = weforms()->templates->get_templates(); | |
| 222 | - | |
| 223 | - return (bool) array_key_exists( $param, $templates ); | |
| 224 | - } | |
| 225 | - | |
| 226 | - public function is_field_exists( $param, $request, $key ) { | |
| 227 | - $fields = $request['field_id']; | |
| 228 | - $fields = array_filter( $fields, 'is_numeric' ); | |
| 229 | - | |
| 230 | - if ( empty( $fields ) ) { | |
| 231 | - return false; | |
| 232 | - } | |
| 233 | - | |
| 234 | - return true; | |
| 235 | - } | |
| 236 | - | |
| 237 | - public function save_check( $request ) { | |
| 238 | - $form = weforms()->form->get( (int) $request['form_id'] ); | |
| 239 | - $form_error = new WP_Error(); | |
| 240 | - $form_settings = $form->get_settings(); | |
| 241 | - $form_fields = $form->get_fields(); | |
| 242 | - $form_entries = weforms_get_form_entries( $form->id, [ 'number' => '', 'offset' => '' ] ); | |
| 243 | - | |
| 244 | - if ( !$form_fields ) { | |
| 245 | - $form_error->add( 'rest_weforms_form_fields', __( 'No form fields found!', 'weforms' ), [ 'status' => 404 ] ); | |
| 246 | - } else { | |
| 247 | - if ( $form->has_field( 'recaptcha' ) ) { | |
| 248 | - $validate_Captcha = $this->validate_reCaptcha( $request ); | |
| 249 | - | |
| 250 | - if ( $validate_Captcha == false ) { | |
| 251 | - $form_error->add( 'rest_weforms_form', __( 'reCAPTCHA validation failed', 'weforms' ), [ 'status' => 404 ] ); | |
| 252 | - } | |
| 253 | - } | |
| 254 | - | |
| 255 | - $entry_fields = []; | |
| 256 | - | |
| 257 | - foreach ( $form_fields as $key => $field ) { | |
| 258 | - if ( $field['wpuf_cond']['condition_status'] == 'yes' ) { | |
| 259 | - $logic = []; | |
| 260 | - $cond_fields = $field['wpuf_cond']['cond_field']; | |
| 261 | - $cond_operators = $field['wpuf_cond']['cond_operator']; | |
| 262 | - $cond_options = $field['wpuf_cond']['cond_option']; | |
| 263 | - | |
| 264 | - $required = false; | |
| 265 | - | |
| 266 | - foreach ( $cond_fields as $cond_key => $value ) { | |
| 267 | - $operator = $cond_operators[$cond_key]; | |
| 268 | - $selected_value = $request[$value]; | |
| 269 | - $logic_value = $cond_options[$cond_key]; | |
| 270 | - | |
| 271 | - if ( $field['wpuf_cond']['cond_logic'] == 'all' ) { | |
| 272 | - if ( ( $operator == '=' && $selected_value == $logic_value ) || ( $operator == '!=' && $selected_value != $logic_value ) ) { | |
| 273 | - $required = true; | |
| 274 | - } else { | |
| 275 | - $required = false; | |
| 276 | - break; | |
| 277 | - } | |
| 278 | - } | |
| 279 | - | |
| 280 | - if ( $field['wpuf_cond']['cond_logic'] == 'any' ) { | |
| 281 | - if ( ( $operator == '=' && $selected_value == $logic_value ) || ( $operator == '!=' && $selected_value != $logic_value ) ) { | |
| 282 | - $required = true; | |
| 283 | - break; | |
| 284 | - } else { | |
| 285 | - $required = false; | |
| 286 | - } | |
| 287 | - } | |
| 288 | - } | |
| 289 | - | |
| 290 | - if ( $required ) { | |
| 291 | - if ( isset( $field['required'] ) && $field['required'] == 'yes' && empty( $request->get_param( $field['name'] ) ) ) { | |
| 292 | - $form_error->add( 'rest_weforms_form_required_field', __( $field['name'] . ' Required Field Missing', 'weforms' ), [ 'status' => 404 ] ); | |
| 293 | - } | |
| 294 | - } | |
| 295 | - } elseif ( ( isset( $field['required'] ) && $field['required'] == 'yes' && empty( $request->get_param( $field['name'] ) ) ) ) { | |
| 296 | - $form_error->add( 'rest_weforms_form_required_field', __( $field['name'] . ' Required Field Missing', 'weforms' ), [ 'status' => 404 ] ); | |
| 297 | - } | |
| 298 | - | |
| 299 | - $ignore_list = apply_filters( 'wefroms_entry_ignore_list', [ | |
| 300 | - 'recaptcha', 'step_start', 'section_break', | |
| 301 | - ] ); | |
| 302 | - | |
| 303 | - if ( in_array( $field['template'], $ignore_list ) ) { | |
| 304 | - continue; | |
| 305 | - } | |
| 306 | - | |
| 307 | - if ( !empty( $field['name'] ) ) { | |
| 308 | - $entry_fields[ $field['name'] ] = $request->get_param( $field['name'] ); | |
| 309 | - } | |
| 310 | - | |
| 311 | - if ( !array_key_exists( $field['template'], $form_fields ) ) { | |
| 312 | - continue; | |
| 313 | - } | |
| 314 | - } | |
| 315 | - | |
| 316 | - foreach ( $entry_fields as $field_key => $field_value ) { | |
| 317 | - $duplicate_check = false; | |
| 318 | - $field_label = 'This'; | |
| 319 | - | |
| 320 | - foreach ( $form_fields as $form_field ) { | |
| 321 | - if ( in_array( $form_field['template'], [ 'text_field', 'website_url', 'numeric_text_field', 'email_address' ] ) && $form_field['name'] == $field_key && isset( $form_field['duplicate'] ) && 'no' == $form_field['duplicate'] ) { | |
| 322 | - $duplicate_check = true; | |
| 323 | - $field_label = $form_field['label']; | |
| 324 | - } | |
| 325 | - } | |
| 326 | - | |
| 327 | - if ( $duplicate_check ) { | |
| 328 | - foreach ( $form_entries as $entry ) { | |
| 329 | - $existing = weforms_get_entry_meta( $entry->id, $field_key, true ); | |
| 330 | - | |
| 331 | - if ( $existing && $field_value == $existing ) { | |
| 332 | - $form_error->add( 'rest_weforms_form_unique_entry', __( 'field requires a unique entry and has already been used.', 'weforms' ), [ 'status' => 404 ] ); | |
| 333 | - } | |
| 334 | - } | |
| 335 | - } | |
| 336 | - } | |
| 337 | - | |
| 338 | - foreach ( $form_fields as $key => $field ) { | |
| 339 | - //skip custom html field as it is not saved | |
| 340 | - if ( 'custom_html' == $field['template'] ) { | |
| 341 | - continue; | |
| 342 | - } | |
| 343 | - | |
| 344 | - //skip recaptcha field as it is not saved | |
| 345 | - if ( 'recaptcha' == $field['template'] ) { | |
| 346 | - continue; | |
| 347 | - } | |
| 348 | - | |
| 349 | - if ( !empty( $field['name'] ) ) { | |
| 350 | - $value = $request[ $field['name'] ]; | |
| 351 | - } | |
| 352 | - | |
| 353 | - if ( 'file_upload' === $field['template'] ) { | |
| 354 | - $file = $request->get_param( 'wpuf_files' ); | |
| 355 | - $file_ids = $file[ $field['name'] ]; | |
| 356 | - | |
| 357 | - foreach ( $file_ids as $key => $file_id ) { | |
| 358 | - if ( !wp_get_attachment_url( $file_id ) ) { | |
| 359 | - $form_error->add( 'rest_weforms_form_file', __( 'File Not Found', 'weforms' ), [ 'status' => 404 ] ); | |
| 360 | - } | |
| 361 | - } | |
| 362 | - } | |
| 363 | - | |
| 364 | - if ( 'image_upload' === $field['template'] ) { | |
| 365 | - $image = $request->get_param( 'wpuf_files' ); | |
| 366 | - $image_ids = $image[ $field['name'] ]; | |
| 367 | - | |
| 368 | - foreach ( $image_ids as $key => $image_id ) { | |
| 369 | - if ( !wp_get_attachment_url( $image_id ) ) { | |
| 370 | - $form_error->add( 'rest_weforms_form_image', __( 'Image Not Found', 'weforms' ), [ 'status' => 404 ] ); | |
| 371 | - } | |
| 372 | - } | |
| 373 | - } | |
| 374 | - | |
| 375 | - if ( 'date_field' === $field['template'] ) { | |
| 376 | - $date_format = $field['format']; | |
| 377 | - | |
| 378 | - $possible_date= [ | |
| 379 | - 'dd-mm-yy' => 'd-m-y', | |
| 380 | - 'yy-mm-dd' => 'y-m-d', | |
| 381 | - 'mm-dd-yy' => 'm-d-y', | |
| 382 | - 'dd/mm/yy' => 'd/m/y', | |
| 383 | - 'yy/mm/dd' => 'y/m/d', | |
| 384 | - 'mm/dd/yy' => 'm/d/y', | |
| 385 | - 'dd.mm.yy' => 'd/m/y', | |
| 386 | - 'yy.mm.dd' => 'y/m/d', | |
| 387 | - 'mm.dd.yy' => 'm/d/y', | |
| 388 | - ]; | |
| 389 | - | |
| 390 | - foreach ( $possible_date as $key => $date ) { | |
| 391 | - if ( strcmp( $key, $date_format ) == 0 ) { | |
| 392 | - $date_format = $date; | |
| 393 | - break; | |
| 394 | - } | |
| 395 | - } | |
| 396 | - | |
| 397 | - $d = DateTime::createFromFormat( $date_format, $value ); | |
| 398 | - | |
| 399 | - if ( !( $d && ( $d->format( $date_format ) == $value ) ) ) { | |
| 400 | - $form_error->add( 'rest_weforms_form_date_field', __( 'Date Field Is not Valid', 'weforms' ), [ 'status' => 404 ] ); | |
| 401 | - } | |
| 402 | - } | |
| 403 | - | |
| 404 | - if ( 'email_address' === $field['template'] ) { | |
| 405 | - if ( !$this->email_validation( $value ) ) { | |
| 406 | - $form_error->add( 'rest_weforms_form_email', __( 'Invalid Email Address', 'weforms' ), [ 'status' => 404 ] ); | |
| 407 | - } | |
| 408 | - } | |
| 409 | - | |
| 410 | - if ( 'website_url' === $field['template'] ) { | |
| 411 | - if ( !filter_var( $value, FILTER_VALIDATE_URL ) ) { | |
| 412 | - $form_error->add( 'rest_weforms_form_url', __( 'Invalid Url Address', 'weforms' ), [ 'status' => 404 ] ); | |
| 413 | - } | |
| 414 | - } | |
| 415 | - | |
| 416 | - if ( 'numeric_text_field' === $field['template'] ) { | |
| 417 | - if ( !is_numeric( $value ) ) { | |
| 418 | - $form_error->add( 'rest_weforms_form_number', __( 'Number Field Should be numberic value ', 'weforms' ), [ 'status' => 404 ] ); | |
| 419 | - } | |
| 420 | - } | |
| 421 | - | |
| 422 | - if ( 'single_product' === $field['template'] ) { | |
| 423 | - if ( !$value ) { | |
| 424 | - $value = []; | |
| 425 | - } | |
| 426 | - | |
| 427 | - $value['price'] = isset( $value['price'] ) ? floatval( $value['price'] ) : 0; | |
| 428 | - $value['quantity'] = isset( $value['quantity'] ) ? floatval( $value['quantity'] ) : 0; | |
| 429 | - $quantity = isset( $field['quantity'] ) ? $field['quantity'] : []; | |
| 430 | - $price = isset( $field['price'] ) ? $field['price'] : []; | |
| 431 | - | |
| 432 | - if ( isset( $price['is_flexible'] ) && $price['is_flexible'] ) { | |
| 433 | - $min = isset( $price['min'] ) ? floatval( $price['min'] ) : 0; | |
| 434 | - $max = isset( $price['max'] ) ? floatval( $price['max'] ) : 0; | |
| 435 | - | |
| 436 | - if ( $value['price'] < $min ) { | |
| 437 | - return new WP_Error( 'rest_weforms_form_price', sprintf( __( '%s price must be equal or greater than %s', | |
| 438 | - $field['weforms'], | |
| 439 | - $min, | |
| 440 | - 'weforms' | |
| 441 | - ) | |
| 442 | - ), [ 'status' => 404 ] ); | |
| 443 | - } | |
| 444 | - | |
| 445 | - if ( $max && $value['price'] > $max ) { | |
| 446 | - $form_error->add( 'rest_weforms_form_price', sprintf( __( '%s price must be equal or less than %s', | |
| 447 | - $field['weforms'], | |
| 448 | - $max, | |
| 449 | - 'weforms' | |
| 450 | - ) | |
| 451 | - ), [ 'status' => 404 ] ); | |
| 452 | - } | |
| 453 | - } | |
| 454 | - | |
| 455 | - if ( isset( $quantity['status'] ) && $quantity['status'] ) { | |
| 456 | - $min = isset( $quantity['min'] ) ? floatval( $quantity['min'] ) : 0; | |
| 457 | - $max = isset( $quantity['max'] ) ? floatval( $quantity['max'] ) : 0; | |
| 458 | - | |
| 459 | - if ( $value['quantity'] < $min ) { | |
| 460 | - $form_error->add( 'rest_weforms_form_quantity', sprintf( __( | |
| 461 | - '%s quantity must be equal or greater than %s', | |
| 462 | - $field['weforms'], | |
| 463 | - $min, | |
| 464 | - 'weforms' | |
| 465 | - ) | |
| 466 | - ), [ 'status' => 404 ] ); | |
| 467 | - } | |
| 468 | - | |
| 469 | - if ( $max && $value['quantity'] > $max ) { | |
| 470 | - $form_error->add( 'rest_weforms_form_quantity', sprintf( __( | |
| 471 | - '%s quantity must be equal or less than %s', | |
| 472 | - $field['weforms'], | |
| 473 | - $max, | |
| 474 | - 'weforms' | |
| 475 | - ) | |
| 476 | - ), [ 'status' => 404 ] ); | |
| 477 | - } | |
| 478 | - } | |
| 479 | - } | |
| 480 | - } | |
| 481 | - } | |
| 482 | - | |
| 483 | - if ( count( $form_error->get_error_messages() ) > 0 ) { | |
| 484 | - return $form_error; | |
| 485 | - } else { | |
| 486 | - return true; | |
| 487 | - } | |
| 488 | - } | |
| 489 | - | |
| 490 | - /** | |
| 491 | - * reCaptcha Validation | |
| 492 | - * | |
| 493 | - * @since 1.4.2 | |
| 494 | - * | |
| 495 | - * @return void | |
| 496 | - */ | |
| 497 | - public function validate_reCaptcha( $request ) { | |
| 498 | - if ( class_exists( 'WPUF_ReCaptcha' ) ) { | |
| 499 | - $recaptcha_class = 'WPUF_ReCaptcha'; | |
| 500 | - } else { | |
| 501 | - if ( !function_exists( 'recaptcha_get_html' ) ) { | |
| 502 | - require_once WEFORMS_INCLUDES . '/library/reCaptcha/recaptchalib.php'; | |
| 503 | - } | |
| 504 | - | |
| 505 | - require_once WEFORMS_INCLUDES . '/library/reCaptcha/recaptchalib_noCaptcha.php'; | |
| 506 | - | |
| 507 | - $recaptcha_class = 'Weforms_ReCaptcha'; | |
| 508 | - } | |
| 509 | - | |
| 510 | - $invisible = isset( $request['g-recaptcha-response'] ) ? false : true; | |
| 511 | - | |
| 512 | - $recaptcha_settings = weforms_get_settings( 'recaptcha' ); | |
| 513 | - $secret = isset( $recaptcha_settings->secret ) ? $recaptcha_settings->secret : ''; | |
| 514 | - | |
| 515 | - if ( ! $invisible ) { | |
| 516 | - $response = null; | |
| 517 | - $reCaptcha = new $recaptcha_class( $secret ); | |
| 518 | - $remote_ADDR = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; | |
| 519 | - $recaptcha_response = isset( $_SERVER['g-recaptcha-response'] ) ? sanitize_text_field( wp_unslash( $request['g-recaptcha-response'] ) ) : ''; | |
| 520 | - | |
| 521 | - $resp = $reCaptcha->verifyResponse( | |
| 522 | - $remote_ADDR, | |
| 523 | - $recaptcha_response | |
| 524 | - ); | |
| 525 | - | |
| 526 | - if ( !$resp->success ) { | |
| 527 | - return false; | |
| 528 | - } | |
| 529 | - } else { | |
| 530 | - $recap_challenge = isset( $request['recaptcha_challenge_field'] ) ? $request['recaptcha_challenge_field'] : ''; | |
| 531 | - $recap_response = isset( $request['recaptcha_response_field'] ) ? $request['recaptcha_response_field'] : ''; | |
| 532 | - $resp = recaptcha_check_answer( $secret, $remote_ADDR, $recap_challenge, $recap_response ); | |
| 533 | - | |
| 534 | - if ( !$resp->is_valid ) { | |
| 535 | - ob_clean(); | |
| 536 | - | |
| 537 | - return false; | |
| 538 | - } | |
| 539 | - } | |
| 540 | - | |
| 541 | - return true; | |
| 542 | - } | |
| 543 | - | |
| 544 | - public function weforms_api_insert_entry( $args, $fields = [] ) { | |
| 545 | - global $wpdb; | |
| 546 | - | |
| 547 | - $browser = weforms_get_browser(); | |
| 548 | - | |
| 549 | - $defaults = [ | |
| 550 | - 'form_id' => 0, | |
| 551 | - 'user_id' => get_current_user_id(), | |
| 552 | - 'user_ip' => ip2long( weforms_get_client_ip() ), | |
| 553 | - 'user_device' => $browser['name'] . '/' . $browser['platform'], | |
| 554 | - 'created_at' => current_time( 'mysql' ), | |
| 555 | - ]; | |
| 556 | - | |
| 557 | - $r = wp_parse_args( $args, $defaults ); | |
| 558 | - | |
| 559 | - if ( !$r['form_id'] ) { | |
| 560 | - return new WP_Error( 'no-form-id', __( 'No form ID was found.', 'weforms' ) ); | |
| 561 | - } | |
| 562 | - | |
| 563 | - if ( !$fields ) { | |
| 564 | - return new WP_Error( 'no-fields', __( 'No form fields were found.', 'weforms' ) ); | |
| 565 | - } | |
| 566 | - | |
| 567 | - $success = $wpdb->insert( $wpdb->weforms_entries, $r ); | |
| 568 | - | |
| 569 | - if ( is_wp_error( $success ) || !$success ) { | |
| 570 | - return new WP_Error( 'could-not-create', __( 'Could not create an entry', 'weforms' ), [ 'status' => 404 ] ); | |
| 571 | - } | |
| 572 | - | |
| 573 | - $entry_id = $wpdb->insert_id; | |
| 574 | - | |
| 575 | - foreach ( $fields as $key => $value ) { | |
| 576 | - weforms_add_entry_meta( $entry_id, $key, $value ); | |
| 577 | - } | |
| 578 | - | |
| 579 | - return $entry_id; | |
| 580 | - } | |
| 581 | - | |
| 582 | - /** | |
| 583 | - * Save and existing form | |
| 584 | - * | |
| 585 | - * @since 1.4.2 | |
| 586 | - * | |
| 587 | - * @param array $data Contains form_fields, form_settings, form_settings_key data | |
| 588 | - * | |
| 589 | - * @return bool | |
| 590 | - */ | |
| 591 | - public function weforms_api_save( $data ) { | |
| 592 | - $saved_wpuf_inputs = []; | |
| 593 | - | |
| 594 | - wp_update_post( [ 'ID' => $data['form_id'], 'post_status' => 'publish', 'post_title' => $data['post_title'] ] ); | |
| 595 | - | |
| 596 | - $existing_wpuf_input_ids = get_children( [ | |
| 597 | - 'post_parent' => $data['form_id'], | |
| 598 | - 'post_status' => 'publish', | |
| 599 | - 'post_type' => 'wpuf_input', | |
| 600 | - 'numberposts' => '-1', | |
| 601 | - 'orderby' => 'menu_order', | |
| 602 | - 'order' => 'ASC', | |
| 603 | - 'fields' => 'ids', | |
| 604 | - ] ); | |
| 605 | - | |
| 606 | - $new_wpuf_input_ids = []; | |
| 607 | - | |
| 608 | - if ( !empty( $data['form_fields'] ) ) { | |
| 609 | - foreach ( $data['form_fields'] as $order => $field ) { | |
| 610 | - if ( !empty( $field['is_new'] ) ) { | |
| 611 | - unset( $field['is_new'] ); | |
| 612 | - unset( $field['id'] ); | |
| 613 | - | |
| 614 | - $field_id = 0; | |
| 615 | - } else { | |
| 616 | - $field_id = $field['id']; | |
| 617 | - } | |
| 618 | - | |
| 619 | - $field_id = weforms_insert_form_field( $data['form_id'], $field, $field_id, $order ); | |
| 620 | - | |
| 621 | - $new_wpuf_input_ids[] = $field_id; | |
| 622 | - | |
| 623 | - $field['id'] = $field_id; | |
| 624 | - | |
| 625 | - $saved_wpuf_inputs[] = $field; | |
| 626 | - } | |
| 627 | - } | |
| 628 | - | |
| 629 | - $form = weforms()->form->get( $data['form_id'] ); | |
| 630 | - $new_form_settings = array_merge( $data['form_settings'], array_diff_key( $form->get_settings(), $data['form_settings'] ) ); | |
| 631 | - $new_form_notifications = array_merge( $data['notifications'], array_diff_key( $form->get_notifications(), $data['notifications'] ) ); | |
| 632 | - $new_form_integrations = array_merge( $data['integrations'], array_diff_key( $form->get_integrations(), $data['integrations'] ) ); | |
| 633 | - | |
| 634 | - update_post_meta( $data['form_id'], $data['form_settings_key'], $new_form_settings ); | |
| 635 | - update_post_meta( $data['form_id'], 'notifications', $new_form_notifications ); | |
| 636 | - update_post_meta( $data['form_id'], 'integrations', $new_form_integrations ); | |
| 637 | - update_post_meta( $data['form_id'], '_weforms_version', WEFORMS_VERSION ); | |
| 638 | - | |
| 639 | - return $saved_wpuf_inputs; | |
| 640 | - } | |
| 641 | - | |
| 642 | - /** | |
| 643 | - * Bulk Delete Form Exist | |
| 644 | - * | |
| 645 | - * @since 1.4.2 | |
| 646 | - * | |
| 647 | - * @param array $param | |
| 648 | - * @param WP_REST_Request $request Full details about the request | |
| 649 | - * @param string $key | |
| 650 | - * | |
| 651 | - * @return bool | |
| 652 | - **/ | |
| 653 | - public function is_bulk_delete_form_exists( $param, $request, $key ) { | |
| 654 | - global $wpdb; | |
| 655 | - $forms = $request['ids']; | |
| 656 | - | |
| 657 | - if ( is_array( $forms ) ) { | |
| 658 | - $forms = array_filter( $forms, 'is_numeric' ); | |
| 659 | - | |
| 660 | - if ( empty( $forms ) ) { | |
| 661 | - return false; | |
| 662 | - } | |
| 663 | - | |
| 664 | - $form_id = implode( ',', $forms ); | |
| 665 | - $querystr = " SELECT $wpdb->posts.id | |
| 666 | - FROM $wpdb->posts | |
| 667 | - WHERE $wpdb->posts.post_type = 'wpuf_contact_form' | |
| 668 | - AND $wpdb->posts.ID IN ( $form_id ) | |
| 669 | - "; | |
| 670 | - } else { | |
| 671 | - $form_id = (int) $forms; | |
| 672 | - $querystr = " SELECT $wpdb->posts.id | |
| 673 | - FROM $wpdb->posts | |
| 674 | - WHERE $wpdb->posts.post_type = 'wpuf_contact_form' | |
| 675 | - AND $wpdb->posts.ID = $form_id | |
| 676 | - "; | |
| 677 | - } | |
| 678 | - | |
| 679 | - $result = $wpdb->get_results( $querystr ); | |
| 680 | - | |
| 681 | - if ( empty( $result ) ) { | |
| 682 | - return false; | |
| 683 | - } else { | |
| 684 | - return true; | |
| 685 | - } | |
| 686 | - } | |
| 687 | - | |
| 688 | - public function email_validation( $value ) { | |
| 689 | - return ( !preg_match( "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $value ) ) ? false : true; | |
| 690 | - } | |
| 691 | - | |
| 692 | - public function get_items( $request ) { | |
| 693 | - $args = [ | |
| 694 | - 'posts_per_page' => isset( $request['per_page'] ) ? intval( $request['per_page'] ) : 10, | |
| 695 | - 'paged' => isset( $request['page'] ) ? absint( $request['page'] ) : 1, | |
| 696 | - 'order' => isset( $request['order'] ) ? $request['order'] : 'DESC', | |
| 697 | - 'orderby' => isset( $request['orderby'] ) ? $request['orderby'] : 'post_date', | |
| 698 | - 'post_status' => isset( $request['status'] ) ? $request['status'] : 'any', | |
| 699 | - 's' => isset( $request['search'] ) ? $request['search'] : '', | |
| 700 | - ]; | |
| 701 | - | |
| 702 | - $forms_array = []; | |
| 703 | - | |
| 704 | - $defaults = [ | |
| 705 | - 'post_type' => 'wpuf_contact_form', | |
| 706 | - 'post_status' => [ 'publish', 'draft', 'pending' ], | |
| 707 | - ]; | |
| 708 | - | |
| 709 | - $args = wp_parse_args( $args, $defaults ); | |
| 710 | - $query = new WP_Query( $args ); | |
| 711 | - $forms = $query->get_posts(); | |
| 712 | - $formatted_items = []; | |
| 713 | - | |
| 714 | - if ( $forms ) { | |
| 715 | - foreach ( $forms as $form_obj ) { | |
| 716 | - $form = new WeForms_Form( $form_obj ); | |
| 717 | - $form_author = $form->data->post_author; | |
| 718 | - $form_status = $form->is_api_form_submission_open(); | |
| 719 | - $data = []; | |
| 720 | - $data['id'] = $form->id; | |
| 721 | - $data['name'] = $form->name; | |
| 722 | - $data['created_by'] = get_the_author_meta( 'user_nicename', $form_author ); | |
| 723 | - $data['created_on'] = $form->data->post_date; | |
| 724 | - $data['status'] = $form_status['type']; | |
| 725 | - $data['status_info'] = isset( $form_status['message'] ) ? $form_status['message'] : ''; | |
| 726 | - $data['fields'] = count( $form->get_fields() ); | |
| 727 | - $data['entries'] = $form->num_form_entries(); | |
| 728 | - $data['views'] = $form->num_form_views(); | |
| 729 | - $data['payments'] = $form->num_form_payments(); | |
| 730 | - $data = $this->prepare_item_for_response( (array) $data, $request ); | |
| 731 | - $formatted_items[] = $this->prepare_response_for_collection( $data ); | |
| 732 | - } | |
| 733 | - } | |
| 734 | - | |
| 735 | - $contact_forms = apply_filters( 'weforms_rest_get_contact_forms', $formatted_items ); | |
| 736 | - $response = rest_ensure_response( $contact_forms ); | |
| 737 | - $response = $this->format_collection_response( $response, $request, (int) $query->found_posts ); | |
| 738 | - | |
| 739 | - return $response; | |
| 740 | - } | |
| 741 | - | |
| 742 | - public function get_item( $request ) { | |
| 743 | - $form_id = absint( $request['form_id'] ); | |
| 744 | - $form = weforms()->form->get( $form_id ); | |
| 745 | - $form_status = $form->is_api_form_submission_open(); | |
| 746 | - $form_author = $form->data->post_author; | |
| 747 | - | |
| 748 | - $data = [ | |
| 749 | - 'id' => $form->data->ID, | |
| 750 | - 'name' => $form->name, | |
| 751 | - 'created_by' => get_the_author_meta( 'user_nicename', $form_author ), | |
| 752 | - 'created_on' => $form->data->post_date, | |
| 753 | - 'status' => $form_status['type'], | |
| 754 | - 'status_info' => isset( $form_status['message'] ) ? $form_status['message'] : '', | |
| 755 | - 'fields' => $form->get_fields(), | |
| 756 | - 'settings' => $form->get_settings(), | |
| 757 | - 'notifications' => $form->get_notifications(), | |
| 758 | - 'integrations' => $form->get_integrations(), | |
| 759 | - ]; | |
| 760 | - | |
| 761 | - $data = $this->prepare_item_for_response( $data, $request ); | |
| 762 | - $response = rest_ensure_response( $data ); | |
| 763 | - | |
| 764 | - return $response; | |
| 765 | - } | |
| 766 | - | |
| 767 | - public function create_item( $request ) { | |
| 768 | - $template = isset( $request['template'] ) ? sanitize_text_field( $request['template'] ) : ''; | |
| 769 | - $name = isset( $request['name'] ) ? sanitize_text_field( $request['name'] ) : ''; | |
| 770 | - $setting = isset( $request['settings'] ) ? $request['settings'] : []; | |
| 771 | - $notifications = isset( $request['notifications'] ) ? $request['notifications'] : []; | |
| 772 | - $fields = isset( $request['fields'] ) ? $request['fields'] : []; | |
| 773 | - $integrations = isset( $request['integrations'] ) ? $request['integrations'] : []; | |
| 774 | - | |
| 775 | - $default_form_settings = weforms_get_default_form_settings(); | |
| 776 | - $default_form_notification = [ weforms_get_default_form_notification() ]; | |
| 777 | - $integration_list = weforms()->integrations->get_integration_js_settings(); | |
| 778 | - $field_list = weforms()->fields->get_fields(); | |
| 779 | - | |
| 780 | - if ( isset( $template ) && !empty( $template ) ) { | |
| 781 | - $form_id = weforms()->templates->create( $template ); | |
| 782 | - } elseif ( !empty( $name ) ) { | |
| 783 | - if ( !empty( $fields ) ) { | |
| 784 | - foreach ( $fields as $key => $field ) { | |
| 785 | - $f = in_array( $field['template'], array_keys( $field_list ) ); | |
| 786 | - | |
| 787 | - if ( empty( $f ) ) { | |
| 788 | - unset( $fields[ $key ] ); | |
| 789 | - } | |
| 790 | - } | |
| 791 | - } | |
| 792 | - | |
| 793 | - $fields = array_intersect_key( $fields, $field_list ); | |
| 794 | - $form_id = weforms()->form->create( $name, $fields ); | |
| 795 | - | |
| 796 | - if ( is_wp_error( $form_id ) ) { | |
| 797 | - return new WP_Error( 'rest_invalid_data', __( 'Could not create the form', 'weforms' ), [ 'status' => 404 ] ); | |
| 798 | - } | |
| 799 | - | |
| 800 | - $new_form_settings = array_diff_key( $default_form_settings, $setting ); | |
| 801 | - $new_form_notification = array_diff_key( $default_form_notification, $notifications ); | |
| 802 | - $new_form_integrations = array_intersect_key( $integrations, $integration_list ); | |
| 803 | - | |
| 804 | - if ( !class_exists( 'WeForms_Pro' ) ) { | |
| 805 | - $new_form_integrations = array_udiff_assoc( $new_form_integrations, $integration_list, | |
| 806 | - function ( $item, $item_list ) { | |
| 807 | - if ( isset( $item_list['pro'] ) && $item_list['pro'] == true ) { | |
| 808 | - return 0; | |
| 809 | - } | |
| 810 | - | |
| 811 | - return $item; | |
| 812 | - } | |
| 813 | - ); | |
| 814 | - } | |
| 815 | - | |
| 816 | - update_post_meta( $form_id, 'wpuf_form_settings', $new_form_settings ); | |
| 817 | - update_post_meta( $form_id, 'notifications', $new_form_notification ); | |
| 818 | - update_post_meta( $form_id, 'integrations', $new_form_integrations ); | |
| 819 | - } | |
| 820 | - | |
| 821 | - if ( is_wp_error( $form_id ) || is_null( $form_id ) ) { | |
| 822 | - return new WP_Error( 'rest_weforms_invalid_data', __( 'Could not create the form', 'weforms' ), [ 'status' => 404 ] ); | |
| 823 | - } | |
| 824 | - | |
| 825 | - $form = weforms()->form->get( $form_id ); | |
| 826 | - | |
| 827 | - $data = [ | |
| 828 | - 'id' => $form->data->ID, | |
| 829 | - 'name' => $form->name, | |
| 830 | - 'fields' => $form->get_fields(), | |
| 831 | - 'settings' => $form->get_settings(), | |
| 832 | - 'notifications' => $form->get_notifications(), | |
| 833 | - 'integrations' => $form->get_integrations(), | |
| 834 | - ]; | |
| 835 | - | |
| 836 | - $response = $this->prepare_item_for_response( $data, $request ); | |
| 837 | - $response = rest_ensure_response( $response ); | |
| 838 | - $response->set_status( 200 ); | |
| 839 | - $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $form->id ) ) ); | |
| 840 | - | |
| 841 | - return $response; | |
| 842 | - } | |
| 843 | - | |
| 844 | - public function update_item( $request ) { | |
| 845 | - $data = []; | |
| 846 | - $form_id = $request['form_id']; | |
| 847 | - $data['form_id'] = $form_id; | |
| 848 | - $data['form_settings_key'] = 'wpuf_form_settings'; | |
| 849 | - | |
| 850 | - if ( isset( $request['name'] ) && !empty( $request['name'] ) ) { | |
| 851 | - $data['post_title'] = $request['name']; | |
| 852 | - } | |
| 853 | - | |
| 854 | - if ( isset( $request['settings'] ) && !empty( $request['settings'] ) ) { | |
| 855 | - $data['form_settings'] = $request['settings']; | |
| 856 | - } | |
| 857 | - | |
| 858 | - if ( isset( $request['fields'] ) && !empty( $request['fields'] ) ) { | |
| 859 | - $data['form_fields'] = $request['fields']; | |
| 860 | - } | |
| 861 | - | |
| 862 | - if ( isset( $request['notifications'] ) && !empty( $request['notifications'] ) ) { | |
| 863 | - $data['notifications'] = $request['notifications']; | |
| 864 | - } | |
| 865 | - | |
| 866 | - if ( isset( $request['integrations'] ) && !empty( $request['integrations'] ) ) { | |
| 867 | - $data['integrations'] = $request['integrations']; | |
| 868 | - } | |
| 869 | - | |
| 870 | - $this->weforms_api_update( $data ); | |
| 871 | - | |
| 872 | - $form = weforms()->form->get( $form_id ); | |
| 873 | - | |
| 874 | - $response_data = [ | |
| 875 | - 'id' => $form->data->ID, | |
| 876 | - 'name' => $form->name, | |
| 877 | - 'fields' => $form->get_fields(), | |
| 878 | - 'settings' => $form->get_settings(), | |
| 879 | - 'notifications' => $form->get_notifications(), | |
| 880 | - 'integrations' => $form->get_integrations(), | |
| 881 | - ]; | |
| 882 | - | |
| 883 | - $response = $this->prepare_item_for_response( $response_data, $request ); | |
| 884 | - $response = rest_ensure_response( $response ); | |
| 885 | - $response->set_status( 201 ); | |
| 886 | - $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $form->id ) ) ); | |
| 887 | - | |
| 888 | - return $response; | |
| 889 | - } | |
| 890 | - | |
| 891 | - public function delete_item( $request ) { | |
| 892 | - $form_array = []; | |
| 893 | - $form_id = $request['form_id']; | |
| 894 | - $form = $this->delete( $form_id ); | |
| 895 | - | |
| 896 | - if ( is_wp_error( $form ) ) { | |
| 897 | - return new WP_Error( 'error', __( 'Could not delete the form', 'weforms' ), [ 'status' => 404 ] ); | |
| 898 | - } | |
| 899 | - | |
| 900 | - $form_array['id'] = $form_id; | |
| 901 | - $form_array['message'] = __( 'form deleted successfully', 'weforms' ); | |
| 902 | - $form_array['data']['status'] = 200; | |
| 903 | - | |
| 904 | - $response = $this->prepare_response_for_collection( $form_array, $request ); | |
| 905 | - $response = rest_ensure_response( $response ); | |
| 906 | - | |
| 907 | - return $response; | |
| 908 | - } | |
| 909 | - | |
| 910 | - /** | |
| 911 | - * Duplicate Form | |
| 912 | - * | |
| 913 | - * @since 1.4.2 | |
| 914 | - * | |
| 915 | - * @param WP_REST_Request $request full details about the request | |
| 916 | - * | |
| 917 | - * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure | |
| 918 | - **/ | |
| 919 | - public function duplicate_form( $request ) { | |
| 920 | - $form_id = $request['form_id']; | |
| 921 | - $form = weforms()->form->get( $form_id ); | |
| 922 | - | |
| 923 | - if ( empty( $form ) ) { | |
| 924 | - return new WP_Error( 'error', __( 'Could not duplicate the form', 'weforms' ), [ 'status' => 404 ] ); | |
| 925 | - } | |
| 926 | - | |
| 927 | - $form_id = weforms()->form->create( $form->name, $form->get_fields() ); | |
| 928 | - | |
| 929 | - $data = [ | |
| 930 | - 'form_id' => absint( $form_id ), | |
| 931 | - 'post_title' => sanitize_text_field( $form->name ) . ' (#' . $form_id . ')', | |
| 932 | - 'form_fields' => weforms()->form->get( $form_id )->get_fields(), | |
| 933 | - 'form_settings' => $form->get_settings(), | |
| 934 | - 'form_settings_key' => 'wpuf_form_settings', | |
| 935 | - 'notifications' => $form->get_notifications(), | |
| 936 | - 'integrations' => $form->get_integrations(), | |
| 937 | - ]; | |
| 938 | - | |
| 939 | - $form_fields = weforms()->form->save( $data ); | |
| 940 | - $form = weforms()->form->get( $form_id ); | |
| 941 | - $form->settings = $form->get_settings(); | |
| 942 | - | |
| 943 | - $response_data = [ | |
| 944 | - 'id' => $form->data->ID, | |
| 945 | - 'name' => $form->name, | |
| 946 | - 'fields' => $form->get_fields(), | |
| 947 | - 'settings' => $form->get_settings(), | |
| 948 | - 'notifications' => $form->get_notifications(), | |
| 949 | - 'integrations' => $form->get_integrations(), | |
| 950 | - ]; | |
| 951 | - | |
| 952 | - $response = $this->prepare_item_for_response( $response_data, $request ); | |
| 953 | - $response = rest_ensure_response( $response ); | |
| 954 | - $response->set_status( 201 ); | |
| 955 | - $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $form->id ) ) ); | |
| 956 | - | |
| 957 | - return $response; | |
| 958 | - } | |
| 959 | - | |
| 960 | - /** | |
| 961 | - * Prepare a single user output for response | |
| 962 | - * | |
| 963 | - * @param object $item | |
| 964 | - * @param WP_REST_Request $request request object | |
| 965 | - * @param array $additional_fields (optional) | |
| 966 | - * | |
| 967 | - * @return WP_REST_Response $response response data | |
| 968 | - */ | |
| 969 | - public function prepare_item_for_response( $item, $request, $additional_fields = [] ) { | |
| 970 | - $response = rest_ensure_response( $item ); | |
| 971 | - $response = $this->add_links( $response, $item ); | |
| 972 | - | |
| 973 | - return $response; | |
| 974 | - } | |
| 975 | - | |
| 976 | - /** | |
| 977 | - * Get the Form's schema, conforming to JSON Schema | |
| 978 | - * | |
| 979 | - * @return array | |
| 980 | - */ | |
| 981 | - public function get_item_schema() { | |
| 982 | - $schema = [ | |
| 983 | - '$schema' => 'http://json-schema.org/draft-04/schema#', | |
| 984 | - 'title' => 'forms', | |
| 985 | - 'type' => 'object', | |
| 986 | - 'properties' => [ | |
| 987 | - 'form_id' => [ | |
| 988 | - 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 989 | - 'type' => 'integer', | |
| 990 | - 'sanitize_callback' => 'absint', | |
| 991 | - 'validate_callback' => [ $this, 'is_form_exists' ], | |
| 992 | - 'context' => [ 'embed', 'view', 'edit' ], | |
| 993 | - 'required' => true, | |
| 994 | - 'readonly' => true, | |
| 995 | - ], | |
| 996 | - 'name' => [ | |
| 997 | - 'description' => __( '', 'weforms' ), | |
| 998 | - 'type' => 'string', | |
| 999 | - 'context' => [ 'edit', 'view'], | |
| 1000 | - 'sanitize_callback' => 'sanitize_text_field', | |
| 1001 | - 'required' => false, | |
| 1002 | - ], | |
| 1003 | - 'template' => [ | |
| 1004 | - 'required' => false, | |
| 1005 | - 'type' => 'string', | |
| 1006 | - 'description' => 'template name', | |
| 1007 | - 'sanitize_callback' => 'sanitize_text_field', | |
| 1008 | - 'validate_callback' => [ $this, 'is_template_exists' ], | |
| 1009 | - ], | |
| 1010 | - 'settings' => [ | |
| 1011 | - 'description' => __( '', 'weforms' ), | |
| 1012 | - 'type' => 'object', | |
| 1013 | - 'context' => [ 'edit', 'view'], | |
| 1014 | - 'required' => false, | |
| 1015 | - ], | |
| 1016 | - 'notifications' => [ | |
| 1017 | - 'description' => __( '', 'weforms' ), | |
| 1018 | - 'type' => 'object', | |
| 1019 | - 'context' => [ 'edit', 'view' ], | |
| 1020 | - 'required' => false, | |
| 1021 | - ], | |
| 1022 | - 'fields' => [ | |
| 1023 | - 'description' => __( '', 'weforms' ), | |
| 1024 | - 'type' => 'object', | |
| 1025 | - 'context' => [ 'edit', 'view' ], | |
| 1026 | - 'required' => false, | |
| 1027 | - ], | |
| 1028 | - 'integrations' => [ | |
| 1029 | - 'description' => __( '', 'weforms' ), | |
| 1030 | - 'context' => [ 'edit', 'view' ], | |
| 1031 | - 'type' => 'object', | |
| 1032 | - 'required' => false, | |
| 1033 | - ], | |
| 1034 | - ], | |
| 1035 | - ]; | |
| 1036 | - | |
| 1037 | - return $schema; | |
| 1038 | - } | |
| 1039 | - | |
| 1040 | - public function get_collection_params() { | |
| 1041 | - $query_params = parent::get_collection_params(); | |
| 1042 | - $query_params['context']['default'] = 'view'; | |
| 1043 | - | |
| 1044 | - $schema = $this->get_item_schema(); | |
| 1045 | - $schema_properties = $schema['properties']; | |
| 1046 | - | |
| 1047 | - return $query_params; | |
| 1048 | - } | |
| 1049 | - | |
| 1050 | - /** | |
| 1051 | - * Delete a form with it's input fields | |
| 1052 | - * | |
| 1053 | - * @since 1.4.2 | |
| 1054 | - * | |
| 1055 | - * @param int $form_id | |
| 1056 | - * @param bool $force | |
| 1057 | - * | |
| 1058 | - * @return void | |
| 1059 | - */ | |
| 1060 | - public function delete( $form_id, $force = true ) { | |
| 1061 | - global $wpdb; | |
| 1062 | - | |
| 1063 | - $form = weforms()->form->get( $form_id ); | |
| 1064 | - | |
| 1065 | - if ( !$form->id ) { | |
| 1066 | - return new WP_Error( 'form_not_found', __( 'Form not found', 'weforms' ) ); | |
| 1067 | - } | |
| 1068 | - | |
| 1069 | - $wp_post = wp_delete_post( $form_id, $force ); | |
| 1070 | - | |
| 1071 | - if ( !$wp_post ) { | |
| 1072 | - return new WP_Error( 'unable_to_delete', __( 'Unable to delete form', 'weforms' ) ); | |
| 1073 | - } | |
| 1074 | - | |
| 1075 | - // delete form inputs as WP doesn't know the relationship | |
| 1076 | - return $wpdb->delete( $wpdb->posts, | |
| 1077 | - [ | |
| 1078 | - 'post_parent' => $form_id, | |
| 1079 | - 'post_type' => 'wpuf_input', | |
| 1080 | - ] | |
| 1081 | - ); | |
| 1082 | - | |
| 1083 | - return $form; | |
| 1084 | - } | |
| 1085 | - | |
| 1086 | - /** | |
| 1087 | - * Bulk Delete Form | |
| 1088 | - * | |
| 1089 | - * @since 1.3.9 | |
| 1090 | - * | |
| 1091 | - * @param WP_REST_Request $request | |
| 1092 | - * | |
| 1093 | - * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure | |
| 1094 | - */ | |
| 1095 | - public function bulk_delete_form( $request ) { | |
| 1096 | - $form_ids = $request['ids']; | |
| 1097 | - | |
| 1098 | - if ( empty( $form_ids ) ) { | |
| 1099 | - return new WP_Error( 'error', __( 'No form ids provided!', 'weforms' ), [ 'status' => 404 ] ); | |
| 1100 | - } | |
| 1101 | - | |
| 1102 | - $response = []; | |
| 1103 | - | |
| 1104 | - foreach ( $form_ids as $form_id ) { | |
| 1105 | - $status = $this->delete( $form_id ); | |
| 1106 | - | |
| 1107 | - if ( is_wp_error( $status ) ) { | |
| 1108 | - $response[$form_id] = $status->get_error_message(); | |
| 1109 | - } else { | |
| 1110 | - $response[$form_id] = __( 'form deleted successfully ', 'weforms' ); | |
| 1111 | - } | |
| 1112 | - } | |
| 1113 | - | |
| 1114 | - $response = $this->prepare_response_for_collection( $response ); | |
| 1115 | - $response = rest_ensure_response( $response ); | |
| 1116 | - | |
| 1117 | - return $response; | |
| 1118 | - } | |
| 1119 | - | |
| 1120 | - /** | |
| 1121 | - * Update and existing form | |
| 1122 | - * | |
| 1123 | - * @since 1.4.2 | |
| 1124 | - * | |
| 1125 | - * @param array $data Contains form_fields, form_settings, form_settings_key data | |
| 1126 | - * | |
| 1127 | - * @return bool | |
| 1128 | - */ | |
| 1129 | - public function weforms_api_update( $data ) { | |
| 1130 | - $saved_wpuf_inputs = []; | |
| 1131 | - | |
| 1132 | - if ( isset( $data['post_title'] ) && !empty( $data['post_title'] ) ) { | |
| 1133 | - wp_update_post( | |
| 1134 | - [ | |
| 1135 | - 'ID' => $data['form_id'], | |
| 1136 | - 'post_status' => 'publish', | |
| 1137 | - 'post_title' => $data['post_title'], | |
| 1138 | - ] | |
| 1139 | - ); | |
| 1140 | - } | |
| 1141 | - | |
| 1142 | - $existing_wpuf_input_ids = get_children( [ | |
| 1143 | - 'post_parent' => $data['form_id'], | |
| 1144 | - 'post_status' => 'publish', | |
| 1145 | - 'post_type' => 'wpuf_input', | |
| 1146 | - 'numberposts' => '-1', | |
| 1147 | - 'orderby' => 'menu_order', | |
| 1148 | - 'order' => 'ASC', | |
| 1149 | - 'fields' => 'ids', | |
| 1150 | - ] ); | |
| 1151 | - | |
| 1152 | - $new_wpuf_input_ids = []; | |
| 1153 | - | |
| 1154 | - if ( !empty( $data['form_fields'] ) ) { | |
| 1155 | - foreach ( $data['form_fields'] as $order => $field ) { | |
| 1156 | - if ( !empty( $field['is_new'] ) ) { | |
| 1157 | - unset( $field['is_new'] ); | |
| 1158 | - unset( $field['id'] ); | |
| 1159 | - | |
| 1160 | - $field_id = 0; | |
| 1161 | - } else { | |
| 1162 | - $field_id = $field['id']; | |
| 1163 | - } | |
| 1164 | - | |
| 1165 | - $field_id = weforms_insert_form_field( $data['form_id'], $field, $field_id, $order ); | |
| 1166 | - | |
| 1167 | - $new_wpuf_input_ids[] = $field_id; | |
| 1168 | - | |
| 1169 | - $field['id'] = $field_id; | |
| 1170 | - | |
| 1171 | - $saved_wpuf_inputs[] = $field; | |
| 1172 | - } | |
| 1173 | - } | |
| 1174 | - | |
| 1175 | - $form = weforms()->form->get( $data['form_id'] ); | |
| 1176 | - | |
| 1177 | - if ( isset( $data['form_settings'] ) && !empty( $data['form_settings'] ) ) { | |
| 1178 | - $new_form_settings = array_merge( $data['form_settings'], array_diff_key( $form->get_settings(), $data['form_settings'] ) ); | |
| 1179 | - update_post_meta( $data['form_id'], 'wpuf_form_settings', $new_form_settings ); | |
| 1180 | - } | |
| 1181 | - | |
| 1182 | - if ( isset( $data['notifications'] ) && !empty( $data['notifications'] ) ) { | |
| 1183 | - $existing_notifications = $form->get_notifications(); | |
| 1184 | - | |
| 1185 | - foreach ( $existing_notifications as $key => $notification ) { | |
| 1186 | - if ( array_key_exists( $key, $data['notifications'] ) ) { | |
| 1187 | - $existing_notifications[ $key ] = $data['notifications'][$key]; | |
| 1188 | - } | |
| 1189 | - } | |
| 1190 | - | |
| 1191 | - update_post_meta( $data['form_id'], 'notifications', $existing_notifications ); | |
| 1192 | - } | |
| 1193 | - | |
| 1194 | - if ( isset( $data['integrations'] ) && !empty( $data['integrations'] ) ) { | |
| 1195 | - $integration_list = weforms()->integrations->get_integration_js_settings(); | |
| 1196 | - $form_integration = $form->get_integrations(); | |
| 1197 | - $integrations = $data['integrations']; | |
| 1198 | - $integrations = array_intersect_key( $integrations, $integration_list ); | |
| 1199 | - | |
| 1200 | - if ( !class_exists( 'WeForms_Pro' ) ) { | |
| 1201 | - $integrations = array_udiff_assoc( $integrations, $integration_list, | |
| 1202 | - function ( $item, $item_list ) { | |
| 1203 | - if ( isset( $item_list['pro'] ) && $item_list['pro'] == true ) { | |
| 1204 | - return 0; | |
| 1205 | - } | |
| 1206 | - | |
| 1207 | - return $item; | |
| 1208 | - } | |
| 1209 | - ); | |
| 1210 | - } | |
| 1211 | - | |
| 1212 | - $new_form_integrations = array_merge( $integrations, array_diff_key( $form->get_integrations(), $integrations ) ); | |
| 1213 | - | |
| 1214 | - update_post_meta( $data['form_id'], 'integrations', $new_form_integrations ); | |
| 1215 | - } | |
| 1216 | - | |
| 1217 | - update_post_meta( $data['form_id'], '_weforms_version', WEFORMS_VERSION ); | |
| 1218 | - | |
| 1219 | - return $saved_wpuf_inputs; | |
| 1220 | - } | |
| 1221 | - | |
| 1222 | - /** | |
| 1223 | - * Import Forms | |
| 1224 | - * | |
| 1225 | - * @since 1.4.2 | |
| 1226 | - * | |
| 1227 | - * @param WP_REST_Request $request | |
| 1228 | - * | |
| 1229 | - * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure | |
| 1230 | - **/ | |
| 1231 | - public function import_forms( $request ) { | |
| 1232 | - $files = $request->get_file_params(); | |
| 1233 | - | |
| 1234 | - if ( !$files ) { | |
| 1235 | - return new WP_Error( 'rest_invalid_file', __( 'No file found to import.', 'weforms' ), [ 'status' => 404] ); | |
| 1236 | - } | |
| 1237 | - | |
| 1238 | - $file_ext = pathinfo( $files['file']['name'], PATHINFO_EXTENSION ); | |
| 1239 | - | |
| 1240 | - if ( !class_exists( 'WeForms_Admin_Tools' ) ) { | |
| 1241 | - require_once WEFORMS_INCLUDES . '/admin/class-admin-tools.php'; | |
| 1242 | - } | |
| 1243 | - | |
| 1244 | - if ( ( $file_ext == 'json' ) && ( $files['file']['size'] < 500000 ) ) { | |
| 1245 | - $status = WeForms_Admin_Tools::import_json_file( $files['file']['tmp_name'] ); | |
| 1246 | - | |
| 1247 | - if ( $status ) { | |
| 1248 | - $response_data = []; | |
| 1249 | - $response_data['message'] = __( 'The forms have been imported successfully!', 'weforms' ); | |
| 1250 | - $response_data['status'] = 200; | |
| 1251 | - | |
| 1252 | - $response = $this->prepare_response_for_collection( $response_data ); | |
| 1253 | - $response = rest_ensure_response( $response ); | |
| 1254 | - | |
| 1255 | - return $response; | |
| 1256 | - } else { | |
| 1257 | - return new WP_Error( 'rest_invalid_file', __( 'Something went wrong importing the file.', 'weforms' ), [ 'status' => 404 ] ); | |
| 1258 | - } | |
| 1259 | - } else { | |
| 1260 | - return new WP_Error( 'rest_invalid_file', __( 'Invalid file or file size too big.', 'weforms' ), [ 'status' => 404 ] ); | |
| 1261 | - } | |
| 1262 | - } | |
| 1263 | - | |
| 1264 | - /** | |
| 1265 | - * Save Form Entry from Frontend | |
| 1266 | - * | |
| 1267 | - * @since 1.4.2 | |
| 1268 | - * | |
| 1269 | - * @param WP_REST_Request $request full details about the request | |
| 1270 | - * | |
| 1271 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure | |
| 1272 | - **/ | |
| 1273 | - public function save_entry( $request ) { | |
| 1274 | - $form_id = $request['form_id']; | |
| 1275 | - $page_id = isset( $request['page_id'] ) ? intval( $request['page_id'] ) : 0; | |
| 1276 | - $form = weforms()->form->get( $form_id ); | |
| 1277 | - $form_settings = $form->get_settings(); | |
| 1278 | - $form_fields = $form->get_fields(); | |
| 1279 | - $entry_fields = $form->prepare_entries( $request ); | |
| 1280 | - $entry_fields = apply_filters( 'weforms_before_entry_submission', $entry_fields, $form, $form_settings, $form_fields ); | |
| 1281 | - | |
| 1282 | - $entry_id = $this->weforms_api_insert_entry( [ | |
| 1283 | - 'form_id' => $form_id, | |
| 1284 | - ], $entry_fields ); | |
| 1285 | - | |
| 1286 | - if ( is_wp_error( $entry_id ) ) { | |
| 1287 | - return new WP_Error( 'rest_invalid_data', $entry_id->get_error_message(), [ 'status' => 404 ] ); | |
| 1288 | - } | |
| 1289 | - | |
| 1290 | - // redirect URL | |
| 1291 | - $show_message = false; | |
| 1292 | - $redirect_to = false; | |
| 1293 | - | |
| 1294 | - if ( $form_settings['redirect_to'] == 'page' ) { | |
| 1295 | - $redirect_to = get_permalink( $form_settings['page_id'] ); | |
| 1296 | - } elseif ( $form_settings['redirect_to'] == 'url' ) { | |
| 1297 | - $redirect_to = $form_settings['url']; | |
| 1298 | - } elseif ( $form_settings['redirect_to'] == 'same' ) { | |
| 1299 | - $show_message = true; | |
| 1300 | - } else { | |
| 1301 | - $redirect_to = get_permalink( $post_id ); | |
| 1302 | - } | |
| 1303 | - | |
| 1304 | - // Fire a hook for integration | |
| 1305 | - do_action( 'weforms_entry_submission', $entry_id, $form_id, $page_id, $form_settings ); | |
| 1306 | - | |
| 1307 | - $field_search = $field_replace = []; | |
| 1308 | - | |
| 1309 | - foreach ( $form_fields as $r_field ) { | |
| 1310 | - $field_search[] = '{' . $r_field['name'] . '}'; | |
| 1311 | - | |
| 1312 | - if ( $r_field['template'] == 'name_field' ) { | |
| 1313 | - $field_replace[] = implode( ' ', explode( '|', $entry_fields[$r_field['name']] ) ); | |
| 1314 | - } else { | |
| 1315 | - $field_replace[] = isset( $entry_fields[$r_field['name']] ) ? $entry_fields[$r_field['name']] : ''; | |
| 1316 | - } | |
| 1317 | - } | |
| 1318 | - | |
| 1319 | - $message = str_replace( $field_search, $field_replace, $form_settings['message'] ); | |
| 1320 | - | |
| 1321 | - $notification = new WeForms_Notification( [ | |
| 1322 | - 'form_id' => $form_id, | |
| 1323 | - 'page_id' => $page_id, | |
| 1324 | - 'entry_id' => $entry_id, | |
| 1325 | - ] ); | |
| 1326 | - | |
| 1327 | - $notification->send_notifications(); | |
| 1328 | - | |
| 1329 | - $entry_data = weforms_get_entry_data( $entry_id ); | |
| 1330 | - | |
| 1331 | - $response_data = []; | |
| 1332 | - $response_data['id'] = $entry_id; | |
| 1333 | - $response_data['message'] = 'Entries Created Successfully'; | |
| 1334 | - $response_data['success'] = true; | |
| 1335 | - $response_data['redirect_to'] = $redirect_to; | |
| 1336 | - $response_data['show_message'] = $show_message; | |
| 1337 | - $response_data['message'] = $message; | |
| 1338 | - $response_data['data'] = $entry_data['data']; | |
| 1339 | - $response_data['form_id'] = $form_id; | |
| 1340 | - $response_data['entry_id'] = $entry_id; | |
| 1341 | - | |
| 1342 | - $response = apply_filters( 'weforms_entry_submission_response', $response_data ); | |
| 1343 | - $response = $this->prepare_response_for_collection( $response_data ); | |
| 1344 | - $response = rest_ensure_response( $response ); | |
| 1345 | - | |
| 1346 | - return $response; | |
| 1347 | - } | |
| 1348 | - | |
| 1349 | - /** | |
| 1350 | - * Export Form Entries | |
| 1351 | - * | |
| 1352 | - **/ | |
| 1353 | - public function export_form_entries( $request ) { | |
| 1354 | - $form_id = $request['form_id']; | |
| 1355 | - | |
| 1356 | - if ( !$form_id ) { | |
| 1357 | - return new WP_Error( 'rest_weforms_invalid_form', __( 'Form id not exists', 'weforms' ) ); | |
| 1358 | - } | |
| 1359 | - | |
| 1360 | - $entry_array = []; | |
| 1361 | - $columns = weforms_get_entry_columns( $form_id, false ); | |
| 1362 | - $total_entries = weforms_count_form_entries( $form_id ); | |
| 1363 | - $entries = weforms_get_form_entries( $form_id, [ | |
| 1364 | - 'number' => $total_entries, | |
| 1365 | - 'offset' => 0, | |
| 1366 | - ] ); | |
| 1367 | - $extra_columns = [ | |
| 1368 | - 'ip_address' => __( 'IP Address', 'weforms' ), | |
| 1369 | - 'created_at' => __( 'Date', 'weforms' ), | |
| 1370 | - ]; | |
| 1371 | - | |
| 1372 | - $columns = array_merge( [ 'id' => 'Entry ID' ], $columns, $extra_columns ); | |
| 1373 | - | |
| 1374 | - foreach ( $entries as $entry ) { | |
| 1375 | - $temp = []; | |
| 1376 | - | |
| 1377 | - foreach ( $columns as $column_id => $label ) { | |
| 1378 | - switch ( $column_id ) { | |
| 1379 | - case 'id': | |
| 1380 | - $temp[ $column_id ] = $entry->id; | |
| 1381 | - break; | |
| 1382 | - | |
| 1383 | - case 'ip_address': | |
| 1384 | - $temp[ $column_id ] = $entry->ip_address; | |
| 1385 | - break; | |
| 1386 | - | |
| 1387 | - case 'created_at': | |
| 1388 | - $temp[ $column_id ] = $entry->created_at; | |
| 1389 | - break; | |
| 1390 | - | |
| 1391 | - default: | |
| 1392 | - $value = weforms_get_entry_meta( $entry->id, $column_id, true ); | |
| 1393 | - $value = weforms_get_pain_text( $value ); | |
| 1394 | - $temp[ $column_id ] = str_replace( WeForms::$field_separator, ' ', $value ); | |
| 1395 | - break; | |
| 1396 | - } | |
| 1397 | - } | |
| 1398 | - | |
| 1399 | - $entry_array[] = $temp; | |
| 1400 | - } | |
| 1401 | - | |
| 1402 | - error_reporting( 0 ); | |
| 1403 | - | |
| 1404 | - if ( ob_get_contents() ) { | |
| 1405 | - ob_clean(); | |
| 1406 | - } | |
| 1407 | - | |
| 1408 | - $blogname = sanitize_title( strtolower( str_replace( ' ', '-', get_option( 'blogname' ) ) ) ); | |
| 1409 | - $file_name = $blogname . '-weforms-entries-' . time() . '.csv'; | |
| 1410 | - | |
| 1411 | - // force download | |
| 1412 | - header( 'Content-Type: application/force-download' ); | |
| 1413 | - header( 'Content-Type: application/octet-stream' ); | |
| 1414 | - header( 'Content-Type: application/download' ); | |
| 1415 | - | |
| 1416 | - // disposition / encoding on response body | |
| 1417 | - header( "Content-Disposition: attachment;filename={$file_name}" ); | |
| 1418 | - header( 'Content-Transfer-Encoding: binary' ); | |
| 1419 | - | |
| 1420 | - $handle = fopen( 'php://output', 'w' ); | |
| 1421 | - | |
| 1422 | - //handle UTF-8 chars conversion for CSV | |
| 1423 | - fprintf( $handle, chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ) ); | |
| 1424 | - | |
| 1425 | - // put the column headers | |
| 1426 | - fputcsv( $handle, array_values( $columns ) ); | |
| 1427 | - | |
| 1428 | - // put the entry values | |
| 1429 | - foreach ( $entry_array as $row ) { | |
| 1430 | - fputcsv( $handle, $row ); | |
| 1431 | - } | |
| 1432 | - | |
| 1433 | - fclose( $handle ); | |
| 1434 | - | |
| 1435 | - exit; | |
| 1436 | - } | |
| 1437 | - | |
| 1438 | - public function submit_permissions_check( $request ) { | |
| 1439 | - if ( !is_weforms_api_allowed_guest_submission() ) { | |
| 1440 | - return new WP_Error( 'rest_cannot_submit_entry', __( 'Sorry, you have no permission to submit this form.', 'weforms' ), [ 'status' => rest_authorization_required_code() ] ); | |
| 1441 | - } | |
| 1442 | - | |
| 1443 | - $form = weforms()->form->get( (int) $request['form_id'] ); | |
| 1444 | - $form_is_open = $form->is_submission_open(); | |
| 1445 | - | |
| 1446 | - if ( is_wp_error( $form_is_open ) ) { | |
| 1447 | - return new WP_Error( 'rest_weforms_form_permission', $form_is_open->get_error_message(), [ 'status' => 404 ] ); | |
| 1448 | - } | |
| 1449 | - | |
| 1450 | - $form_validations = $this->save_check( $request ); | |
| 1451 | - | |
| 1452 | - if ( is_wp_error( $form_validations ) ) { | |
| 1453 | - $form_message = []; | |
| 1454 | - | |
| 1455 | - foreach ( $form_validations->get_error_messages() as $error ) { | |
| 1456 | - $form_message[] = $error; | |
| 1457 | - } | |
| 1458 | - | |
| 1459 | - return new WP_Error( 'error', $form_message, [ 'status' => 404 ] ); | |
| 1460 | - } | |
| 1461 | - | |
| 1462 | - return true; | |
| 1463 | - } | |
| 1464 | - | |
| 1465 | - /** | |
| 1466 | - * Get Form Entries | |
| 1467 | - * | |
| 1468 | - * @since 1.4.2 | |
| 1469 | - * | |
| 1470 | - * @param WP_REST_Request $request | |
| 1471 | - * | |
| 1472 | - * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure | |
| 1473 | - */ | |
| 1474 | - public function get_entry( $request ) { | |
| 1475 | - $form_id = isset( $request['form_id'] ) ? intval( $request['form_id'] ) : 0; | |
| 1476 | - $current_page = isset( $request['page'] ) ? intval( $request['page'] ) : 1; | |
| 1477 | - $per_page = isset( $request['per_page'] ) ? intval( $request['per_page'] ) : 10; | |
| 1478 | - $status = isset( $request['status'] ) ? $request['status'] : 'publish'; | |
| 1479 | - $offset = ( $current_page - 1 ) * $per_page; | |
| 1480 | - | |
| 1481 | - if ( !$form_id ) { | |
| 1482 | - return new WP_Error( 'rest_invalid_form', __( 'Please provide a form id', 'weforms' ), [ 'status' => 404 ] ); | |
| 1483 | - } | |
| 1484 | - | |
| 1485 | - $entries = weforms_get_form_entries( | |
| 1486 | - $form_id, [ | |
| 1487 | - 'number' => $per_page, | |
| 1488 | - 'offset' => $offset, | |
| 1489 | - 'status' => $status, | |
| 1490 | - ] | |
| 1491 | - ); | |
| 1492 | - | |
| 1493 | - $columns = weforms_get_entry_columns( $form_id ); | |
| 1494 | - $total_entries = weforms_count_form_entries( $form_id, $status ); | |
| 1495 | - | |
| 1496 | - array_map( | |
| 1497 | - function ( $entry ) use ( $columns,$form_id ) { | |
| 1498 | - $entry_id = $entry->id; | |
| 1499 | - $entry->fields = []; | |
| 1500 | - $entry->links = rest_url( sprintf( '%s/%s/%d/%s/%d', $this->namespace, $this->rest_base, $form_id, 'entries', $entry->id ) ); | |
| 1501 | - | |
| 1502 | - foreach ( $columns as $meta_key => $label ) { | |
| 1503 | - $value = weforms_get_entry_meta( $entry_id, $meta_key, true ); | |
| 1504 | - $entry->fields[ $meta_key ] = str_replace( WeForms::$field_separator, ' ', $value ); | |
| 1505 | - } | |
| 1506 | - }, $entries | |
| 1507 | - ); | |
| 1508 | - | |
| 1509 | - $entries = apply_filters( 'weforms_get_entries', $entries, $form_id ); | |
| 1510 | - | |
| 1511 | - $response = $entries; | |
| 1512 | - $max_pages = ceil( $total_entries / $per_page ); | |
| 1513 | - $response = $this->prepare_response_for_collection( $response ); | |
| 1514 | - $response = rest_ensure_response( $response ); | |
| 1515 | - | |
| 1516 | - $response->header( 'X-WP-Total', (int) $total_entries ); | |
| 1517 | - $response->header( 'X-WP-TotalPages', (int) $max_pages ); | |
| 1518 | - | |
| 1519 | - return $response; | |
| 1520 | - } | |
| 1521 | - | |
| 1522 | - /** | |
| 1523 | - * Get Single Entry Details | |
| 1524 | - * | |
| 1525 | - * @since 1.4.2 | |
| 1526 | - * | |
| 1527 | - * @param WP_REST_Request $request full details about the request | |
| 1528 | - * | |
| 1529 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure | |
| 1530 | - **/ | |
| 1531 | - public function get_entry_details( $request ) { | |
| 1532 | - $form_id = $request['form_id']; | |
| 1533 | - $entry_id = $request['entry_id']; | |
| 1534 | - $form = weforms()->form->get( $form_id ); | |
| 1535 | - $form_settings = $form->get_settings(); | |
| 1536 | - $entry = $form->entries()->get( $entry_id ); | |
| 1537 | - $fields = $entry->get_fields(); | |
| 1538 | - | |
| 1539 | - $fields_data = array_map( function ( $field ) { | |
| 1540 | - return [ | |
| 1541 | - 'name' => $field['name'], | |
| 1542 | - 'label' => $field['label'], | |
| 1543 | - 'value' => $field['value'], | |
| 1544 | - ]; | |
| 1545 | - }, $fields ); | |
| 1546 | - | |
| 1547 | - $metadata = $entry->get_metadata(); | |
| 1548 | - $payment = $entry->get_payment_data(); | |
| 1549 | - | |
| 1550 | - if ( isset( $payment->payment_data ) && is_serialized( $payment->payment_data ) ) { | |
| 1551 | - $payment->payment_data = unserialize( $payment->payment_data ); | |
| 1552 | - } | |
| 1553 | - | |
| 1554 | - $has_empty = false; | |
| 1555 | - $answers = []; | |
| 1556 | - $respondentPoints = isset( $form_settings['total_points'] ) ? floatval( $form_settings['total_points'] ) : 0; | |
| 1557 | - | |
| 1558 | - foreach ( $fields as $key => $field ) { | |
| 1559 | - if ( $form_settings['quiz_form'] == 'yes' ) { | |
| 1560 | - $selectedAnswers = isset( $field['selected_answers'] ) ? $field['selected_answers'] : ''; | |
| 1561 | - $givenAnswer = isset( $field['value'] ) ? $field['value'] : ''; | |
| 1562 | - $options = isset( $field['options'] ) ? $field['options'] : ''; | |
| 1563 | - $template = $field['template']; | |
| 1564 | - $fieldPoints = isset( $field['points'] ) ? floatval( $field['points'] ) : 0; | |
| 1565 | - | |
| 1566 | - if ( $template == 'radio_field' || $template == 'dropdown_field' ) { | |
| 1567 | - $answers[$field['name']] = true; | |
| 1568 | - | |
| 1569 | - if ( empty( $givenAnswer ) ) { | |
| 1570 | - $answers[$field['name']] = false; | |
| 1571 | - $respondentPoints -= $fieldPoints; | |
| 1572 | - } else { | |
| 1573 | - foreach ( $options as $key => $value ) { | |
| 1574 | - if ( $givenAnswer == $value ) { | |
| 1575 | - if ( $key != $selectedAnswers ) { | |
| 1576 | - $answers[$field['name']] = false; | |
| 1577 | - $respondentPoints -= $fieldPoints; | |
| 1578 | - } | |
| 1579 | - } | |
| 1580 | - } | |
| 1581 | - } | |
| 1582 | - } elseif ( $template == 'checkbox_field' || $template == 'multiple_select' ) { | |
| 1583 | - $answers[$field['name']] = true; | |
| 1584 | - $userAnswer = []; | |
| 1585 | - | |
| 1586 | - foreach ( $options as $key => $value ) { | |
| 1587 | - foreach ( $givenAnswer as $answer ) { | |
| 1588 | - if ( $value == $answer ) { | |
| 1589 | - $userAnswer[] = $key; | |
| 1590 | - } | |
| 1591 | - } | |
| 1592 | - } | |
| 1593 | - | |
| 1594 | - $userAnswer = implode( '|', $userAnswer ); | |
| 1595 | - $rightAnswers = implode( '|', $selectedAnswers ); | |
| 1596 | - | |
| 1597 | - if ( $userAnswer != $rightAnswers || empty( $userAnswer ) ) { | |
| 1598 | - $answers[$field['name']] = false; | |
| 1599 | - $respondentPoints -= $fieldPoints; | |
| 1600 | - } | |
| 1601 | - } | |
| 1602 | - } elseif ( empty( $field['value'] ) ) { | |
| 1603 | - $has_empty = true; | |
| 1604 | - break; | |
| 1605 | - } | |
| 1606 | - } | |
| 1607 | - | |
| 1608 | - $response = [ | |
| 1609 | - 'fields' => $fields_data, | |
| 1610 | - 'meta_data' => $metadata, | |
| 1611 | - 'payment_data' => $payment, | |
| 1612 | - 'has_empty' => $has_empty, | |
| 1613 | - 'respondent_points' => $respondentPoints, | |
| 1614 | - 'answers' => $answers, | |
| 1615 | - ]; | |
| 1616 | - | |
| 1617 | - $response = $this->prepare_response_for_collection( $response ); | |
| 1618 | - $response = rest_ensure_response( $response ); | |
| 1619 | - $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d/%s/%d', $this->namespace, $this->rest_base, $form->id, 'entries', $entry->id ) ) ); | |
| 1620 | - | |
| 1621 | - return $response; | |
| 1622 | - } | |
| 1623 | - | |
| 1624 | - /** | |
| 1625 | - * Export forms to JSON | |
| 1626 | - * | |
| 1627 | - * @param WP_REST_Request $request | |
| 1628 | - * | |
| 1629 | - * @return json | |
| 1630 | - **/ | |
| 1631 | - public function export_forms( $request ) { | |
| 1632 | - $export_type = isset( $request['type'] ) ? $request['type'] : 'selected'; | |
| 1633 | - $selected = isset( $request['ids'] ) ? array_map( 'absint', $request['ids'] ) : []; | |
| 1634 | - | |
| 1635 | - if ( !class_exists( 'WeForms_Admin_Tools' ) ) { | |
| 1636 | - require_once WEFORMS_INCLUDES . '/admin/class-admin-tools.php'; | |
| 1637 | - } | |
| 1638 | - | |
| 1639 | - switch ( $export_type ) { | |
| 1640 | - case 'all': | |
| 1641 | - WeForms_Admin_Tools::export_to_json(); | |
| 1642 | - break; | |
| 1643 | - | |
| 1644 | - case 'selected': | |
| 1645 | - WeForms_Admin_Tools::export_to_json( $selected ); | |
| 1646 | - break; | |
| 1647 | - } | |
| 1648 | - } | |
| 1649 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +class Weforms_Forms_Controller extends Weforms_REST_Controller { | |
| 4 | + | |
| 5 | + /** | |
| 6 | + * Endpoint namespace | |
| 7 | + * | |
| 8 | + * @var string | |
| 9 | + */ | |
| 10 | + protected $namespace = 'weforms/v1'; | |
| 11 | + | |
| 12 | + /** | |
| 13 | + * Route name | |
| 14 | + * | |
| 15 | + * @var string | |
| 16 | + */ | |
| 17 | + protected $rest_base = 'forms'; | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * Register all routes releated with forms | |
| 21 | + * | |
| 22 | + * @return void | |
| 23 | + */ | |
| 24 | + public function register_routes() { | |
| 25 | + register_rest_route( | |
| 26 | + $this->namespace, | |
| 27 | + '/' . $this->rest_base, | |
| 28 | + [ | |
| 29 | + [ | |
| 30 | + 'methods' => WP_REST_Server::READABLE, | |
| 31 | + 'callback' => [ $this, 'get_items' ], | |
| 32 | + 'args' => $this->get_collection_params(), | |
| 33 | + 'permission_callback' => [ $this, 'get_items_permissions_check' ], | |
| 34 | + ], | |
| 35 | + [ | |
| 36 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 37 | + 'callback' => [ $this, 'create_item' ], | |
| 38 | + 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), | |
| 39 | + 'permission_callback' => [ $this, 'create_item_permissions_check' ], | |
| 40 | + ], | |
| 41 | + [ | |
| 42 | + 'methods' => WP_REST_Server::DELETABLE, | |
| 43 | + 'callback' => [ $this, 'bulk_delete_form' ], | |
| 44 | + 'permission_callback' => [ $this, 'delete_item_permissions_check' ], | |
| 45 | + 'args' => [ | |
| 46 | + 'ids' => [ | |
| 47 | + 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 48 | + 'type' => 'array', | |
| 49 | + 'items' => [ | |
| 50 | + 'type' => 'integer', | |
| 51 | + ], | |
| 52 | + 'validate_callback' => [ $this, 'is_bulk_delete_form_exists' ], | |
| 53 | + 'required' => true, | |
| 54 | + ], | |
| 55 | + ], | |
| 56 | + ], | |
| 57 | + ] | |
| 58 | + ); | |
| 59 | + | |
| 60 | + register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<form_id>\d+)', [ | |
| 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 | + ], | |
| 70 | + [ | |
| 71 | + 'methods' => WP_REST_Server::READABLE, | |
| 72 | + 'callback' => [ $this, 'get_item' ], | |
| 73 | + 'args' => [ | |
| 74 | + 'context' => $this->get_context_param( [ 'default' => 'view' ] ), | |
| 75 | + ], | |
| 76 | + 'permission_callback' => [ $this, 'get_item_permissions_check' ], | |
| 77 | + ], | |
| 78 | + [ | |
| 79 | + 'methods' => WP_REST_Server::DELETABLE, | |
| 80 | + 'callback' => [ $this, 'delete_item' ], | |
| 81 | + 'args' => [ | |
| 82 | + 'force' => [ | |
| 83 | + 'type' => 'boolean', | |
| 84 | + 'default' => false, | |
| 85 | + 'description' => __( 'Whether to bypass trash and force deletion.', 'weforms' ), | |
| 86 | + ], | |
| 87 | + ], | |
| 88 | + 'permission_callback' => [ $this, 'delete_item_permissions_check' ], | |
| 89 | + ], | |
| 90 | + [ | |
| 91 | + 'methods' => WP_REST_Server::EDITABLE, | |
| 92 | + 'callback' => [ $this, 'update_item' ], | |
| 93 | + 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), | |
| 94 | + 'permission_callback' => [ $this, 'update_item_permissions_check' ], | |
| 95 | + ], | |
| 96 | + ] ); | |
| 97 | + | |
| 98 | + register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<form_id>[\d]+)' . '/entries/', | |
| 99 | + [ | |
| 100 | + 'args' => [ | |
| 101 | + 'form_id' => [ | |
| 102 | + 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 103 | + 'type' => 'integer', | |
| 104 | + 'sanitize_callback' => 'absint', | |
| 105 | + 'validate_callback' => [ $this, 'is_form_exists' ], | |
| 106 | + 'required' => true, | |
| 107 | + ], | |
| 108 | + ], | |
| 109 | + | |
| 110 | + [ | |
| 111 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 112 | + 'callback' => [ $this, 'save_entry' ], | |
| 113 | + 'permission_callback' => [ $this, 'submit_permissions_check' ], | |
| 114 | + ], | |
| 115 | + [ | |
| 116 | + 'methods' => WP_REST_Server::READABLE, | |
| 117 | + 'callback' => [ $this, 'get_entry' ], | |
| 118 | + 'permission_callback' => [ $this, 'get_item_permissions_check' ], | |
| 119 | + 'args' => $this->get_collection_params(), | |
| 120 | + ], | |
| 121 | + ] | |
| 122 | + ); | |
| 123 | + | |
| 124 | + register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<form_id>[\d]+)' . '/duplicate/', [ | |
| 125 | + 'args' => [ | |
| 126 | + 'form_id' => [ | |
| 127 | + 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 128 | + 'key' => 'integer', | |
| 129 | + 'sanitize_callback' => 'absint', | |
| 130 | + 'validate_callback' => [ $this, 'is_form_exists' ], | |
| 131 | + 'required' => true, | |
| 132 | + ], | |
| 133 | + ], | |
| 134 | + [ | |
| 135 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 136 | + 'callback' => [ $this, 'duplicate_form' ], | |
| 137 | + 'permission_callback' => [ $this, 'create_item_permissions_check' ], | |
| 138 | + ], | |
| 139 | + ] ); | |
| 140 | + | |
| 141 | + register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<form_id>[\d]+)' . '/export_entries/', [ | |
| 142 | + 'args' => [ | |
| 143 | + 'form_id' => [ | |
| 144 | + 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 145 | + 'type' => 'integer', | |
| 146 | + 'sanitize_callback' => 'absint', | |
| 147 | + 'validate_callback' => [ $this, 'is_form_exists' ], | |
| 148 | + 'required' => true, | |
| 149 | + ], | |
| 150 | + ], | |
| 151 | + | |
| 152 | + [ | |
| 153 | + 'methods' => WP_REST_Server::READABLE, | |
| 154 | + 'callback' => [ $this, 'export_form_entries' ], | |
| 155 | + 'permission_callback' => [ $this, 'get_items_permissions_check' ], | |
| 156 | + ], | |
| 157 | + ] ); | |
| 158 | + | |
| 159 | + register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<form_id>[\d]+)' . '/entries/(?P<entry_id>[\d]+)', | |
| 160 | + [ | |
| 161 | + 'args' => [ | |
| 162 | + 'form_id' => [ | |
| 163 | + 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 164 | + 'type' => 'integer', | |
| 165 | + 'sanitize_callback' => 'absint', | |
| 166 | + 'validate_callback' => [ $this, 'is_form_exists' ], | |
| 167 | + 'required' => true, | |
| 168 | + ], | |
| 169 | + 'entry_id' => [ | |
| 170 | + 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 171 | + 'type' => 'integer', | |
| 172 | + 'sanitize_callback' => 'absint', | |
| 173 | + 'validate_callback' => [ $this, 'is_entry_exists' ], | |
| 174 | + 'required' => true, | |
| 175 | + ], | |
| 176 | + 'context' => $this->get_context_param( [ 'default' => 'view' ] ), | |
| 177 | + ], | |
| 178 | + [ | |
| 179 | + 'methods' => WP_REST_Server::READABLE, | |
| 180 | + 'callback' => [ $this, 'get_entry_details' ], | |
| 181 | + 'permission_callback' => [ $this, 'get_item_permissions_check' ], | |
| 182 | + ], | |
| 183 | + ] | |
| 184 | + ); | |
| 185 | + | |
| 186 | + register_rest_route( $this->namespace, '/' . $this->rest_base . '/import', [ | |
| 187 | + [ | |
| 188 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 189 | + 'callback' => [ $this, 'import_forms' ], | |
| 190 | + 'permission_callback' => [ $this, 'create_item_permissions_check' ], | |
| 191 | + ], | |
| 192 | + ] ); | |
| 193 | + | |
| 194 | + register_rest_route( $this->namespace, '/' . $this->rest_base . '/export', [ | |
| 195 | + 'args' => [ | |
| 196 | + 'ids' => [ | |
| 197 | + 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 198 | + 'type' => 'array', | |
| 199 | + 'validate_callback' => [ $this, 'is_bulk_delete_form_exists' ], | |
| 200 | + 'required' => false, | |
| 201 | + ], | |
| 202 | + ], | |
| 203 | + [ | |
| 204 | + 'methods' => WP_REST_Server::READABLE, | |
| 205 | + 'callback' => [ $this, 'export_forms' ], | |
| 206 | + 'permission_callback' => [ $this, 'get_item_permissions_check' ], | |
| 207 | + ], | |
| 208 | + ] ); | |
| 209 | + } | |
| 210 | + | |
| 211 | + /** | |
| 212 | + * Check Template Exist | |
| 213 | + * | |
| 214 | + * @param string $param | |
| 215 | + * @param WP_REST_Request $request | |
| 216 | + * @param string $key | |
| 217 | + * | |
| 218 | + * @return bool | |
| 219 | + **/ | |
| 220 | + public function is_template_exists( $param, $request, $key ) { | |
| 221 | + $templates = weforms()->templates->get_templates(); | |
| 222 | + | |
| 223 | + return (bool) array_key_exists( $param, $templates ); | |
| 224 | + } | |
| 225 | + | |
| 226 | + public function is_field_exists( $param, $request, $key ) { | |
| 227 | + $fields = $request['field_id']; | |
| 228 | + $fields = array_filter( $fields, 'is_numeric' ); | |
| 229 | + | |
| 230 | + if ( empty( $fields ) ) { | |
| 231 | + return false; | |
| 232 | + } | |
| 233 | + | |
| 234 | + return true; | |
| 235 | + } | |
| 236 | + | |
| 237 | + public function save_check( $request ) { | |
| 238 | + $form = weforms()->form->get( (int) $request['form_id'] ); | |
| 239 | + $form_error = new WP_Error(); | |
| 240 | + $form_settings = $form->get_settings(); | |
| 241 | + $form_fields = $form->get_fields(); | |
| 242 | + $form_entries = weforms_get_form_entries( $form->id, [ 'number' => '', 'offset' => '' ] ); | |
| 243 | + | |
| 244 | + if ( !$form_fields ) { | |
| 245 | + $form_error->add( 'rest_weforms_form_fields', __( 'No form fields found!', 'weforms' ), [ 'status' => 404 ] ); | |
| 246 | + } else { | |
| 247 | + if ( $form->has_field( 'recaptcha' ) ) { | |
| 248 | + $validate_Captcha = $this->validate_reCaptcha( $request ); | |
| 249 | + | |
| 250 | + if ( $validate_Captcha == false ) { | |
| 251 | + $form_error->add( 'rest_weforms_form', __( 'reCAPTCHA validation failed', 'weforms' ), [ 'status' => 404 ] ); | |
| 252 | + } | |
| 253 | + } | |
| 254 | + | |
| 255 | + $entry_fields = []; | |
| 256 | + | |
| 257 | + foreach ( $form_fields as $key => $field ) { | |
| 258 | + if ( $field['wpuf_cond']['condition_status'] == 'yes' ) { | |
| 259 | + $logic = []; | |
| 260 | + $cond_fields = $field['wpuf_cond']['cond_field']; | |
| 261 | + $cond_operators = $field['wpuf_cond']['cond_operator']; | |
| 262 | + $cond_options = $field['wpuf_cond']['cond_option']; | |
| 263 | + | |
| 264 | + $required = false; | |
| 265 | + | |
| 266 | + foreach ( $cond_fields as $cond_key => $value ) { | |
| 267 | + $operator = $cond_operators[$cond_key]; | |
| 268 | + $selected_value = $request[$value]; | |
| 269 | + $logic_value = $cond_options[$cond_key]; | |
| 270 | + | |
| 271 | + if ( $field['wpuf_cond']['cond_logic'] == 'all' ) { | |
| 272 | + if ( ( $operator == '=' && $selected_value == $logic_value ) || ( $operator == '!=' && $selected_value != $logic_value ) ) { | |
| 273 | + $required = true; | |
| 274 | + } else { | |
| 275 | + $required = false; | |
| 276 | + break; | |
| 277 | + } | |
| 278 | + } | |
| 279 | + | |
| 280 | + if ( $field['wpuf_cond']['cond_logic'] == 'any' ) { | |
| 281 | + if ( ( $operator == '=' && $selected_value == $logic_value ) || ( $operator == '!=' && $selected_value != $logic_value ) ) { | |
| 282 | + $required = true; | |
| 283 | + break; | |
| 284 | + } else { | |
| 285 | + $required = false; | |
| 286 | + } | |
| 287 | + } | |
| 288 | + } | |
| 289 | + | |
| 290 | + if ( $required ) { | |
| 291 | + if ( isset( $field['required'] ) && $field['required'] == 'yes' && empty( $request->get_param( $field['name'] ) ) ) { | |
| 292 | + $form_error->add( 'rest_weforms_form_required_field', __( $field['name'] . ' Required Field Missing', 'weforms' ), [ 'status' => 404 ] ); | |
| 293 | + } | |
| 294 | + } | |
| 295 | + } elseif ( ( isset( $field['required'] ) && $field['required'] == 'yes' && empty( $request->get_param( $field['name'] ) ) ) ) { | |
| 296 | + $form_error->add( 'rest_weforms_form_required_field', __( $field['name'] . ' Required Field Missing', 'weforms' ), [ 'status' => 404 ] ); | |
| 297 | + } | |
| 298 | + | |
| 299 | + $ignore_list = apply_filters( 'wefroms_entry_ignore_list', [ | |
| 300 | + 'recaptcha', 'step_start', 'section_break', | |
| 301 | + ] ); | |
| 302 | + | |
| 303 | + if ( in_array( $field['template'], $ignore_list ) ) { | |
| 304 | + continue; | |
| 305 | + } | |
| 306 | + | |
| 307 | + if ( !empty( $field['name'] ) ) { | |
| 308 | + $entry_fields[ $field['name'] ] = $request->get_param( $field['name'] ); | |
| 309 | + } | |
| 310 | + | |
| 311 | + if ( !array_key_exists( $field['template'], $form_fields ) ) { | |
| 312 | + continue; | |
| 313 | + } | |
| 314 | + } | |
| 315 | + | |
| 316 | + foreach ( $entry_fields as $field_key => $field_value ) { | |
| 317 | + $duplicate_check = false; | |
| 318 | + $field_label = 'This'; | |
| 319 | + | |
| 320 | + foreach ( $form_fields as $form_field ) { | |
| 321 | + if ( in_array( $form_field['template'], [ 'text_field', 'website_url', 'numeric_text_field', 'email_address' ] ) && $form_field['name'] == $field_key && isset( $form_field['duplicate'] ) && 'no' == $form_field['duplicate'] ) { | |
| 322 | + $duplicate_check = true; | |
| 323 | + $field_label = $form_field['label']; | |
| 324 | + } | |
| 325 | + } | |
| 326 | + | |
| 327 | + if ( $duplicate_check ) { | |
| 328 | + foreach ( $form_entries as $entry ) { | |
| 329 | + $existing = weforms_get_entry_meta( $entry->id, $field_key, true ); | |
| 330 | + | |
| 331 | + if ( $existing && $field_value == $existing ) { | |
| 332 | + $form_error->add( 'rest_weforms_form_unique_entry', __( 'field requires a unique entry and has already been used.', 'weforms' ), [ 'status' => 404 ] ); | |
| 333 | + } | |
| 334 | + } | |
| 335 | + } | |
| 336 | + } | |
| 337 | + | |
| 338 | + foreach ( $form_fields as $key => $field ) { | |
| 339 | + //skip custom html field as it is not saved | |
| 340 | + if ( 'custom_html' == $field['template'] ) { | |
| 341 | + continue; | |
| 342 | + } | |
| 343 | + | |
| 344 | + //skip recaptcha field as it is not saved | |
| 345 | + if ( 'recaptcha' == $field['template'] ) { | |
| 346 | + continue; | |
| 347 | + } | |
| 348 | + | |
| 349 | + if ( !empty( $field['name'] ) ) { | |
| 350 | + $value = $request[ $field['name'] ]; | |
| 351 | + } | |
| 352 | + | |
| 353 | + if ( 'file_upload' === $field['template'] ) { | |
| 354 | + $file = $request->get_param( 'wpuf_files' ); | |
| 355 | + $file_ids = $file[ $field['name'] ]; | |
| 356 | + | |
| 357 | + foreach ( $file_ids as $key => $file_id ) { | |
| 358 | + if ( !wp_get_attachment_url( $file_id ) ) { | |
| 359 | + $form_error->add( 'rest_weforms_form_file', __( 'File Not Found', 'weforms' ), [ 'status' => 404 ] ); | |
| 360 | + } | |
| 361 | + } | |
| 362 | + } | |
| 363 | + | |
| 364 | + if ( 'image_upload' === $field['template'] ) { | |
| 365 | + $image = $request->get_param( 'wpuf_files' ); | |
| 366 | + $image_ids = $image[ $field['name'] ]; | |
| 367 | + | |
| 368 | + foreach ( $image_ids as $key => $image_id ) { | |
| 369 | + if ( !wp_get_attachment_url( $image_id ) ) { | |
| 370 | + $form_error->add( 'rest_weforms_form_image', __( 'Image Not Found', 'weforms' ), [ 'status' => 404 ] ); | |
| 371 | + } | |
| 372 | + } | |
| 373 | + } | |
| 374 | + | |
| 375 | + if ( 'date_field' === $field['template'] ) { | |
| 376 | + $date_format = $field['format']; | |
| 377 | + | |
| 378 | + $possible_date= [ | |
| 379 | + 'dd-mm-yy' => 'd-m-y', | |
| 380 | + 'yy-mm-dd' => 'y-m-d', | |
| 381 | + 'mm-dd-yy' => 'm-d-y', | |
| 382 | + 'dd/mm/yy' => 'd/m/y', | |
| 383 | + 'yy/mm/dd' => 'y/m/d', | |
| 384 | + 'mm/dd/yy' => 'm/d/y', | |
| 385 | + 'dd.mm.yy' => 'd/m/y', | |
| 386 | + 'yy.mm.dd' => 'y/m/d', | |
| 387 | + 'mm.dd.yy' => 'm/d/y', | |
| 388 | + ]; | |
| 389 | + | |
| 390 | + foreach ( $possible_date as $key => $date ) { | |
| 391 | + if ( strcmp( $key, $date_format ) == 0 ) { | |
| 392 | + $date_format = $date; | |
| 393 | + break; | |
| 394 | + } | |
| 395 | + } | |
| 396 | + | |
| 397 | + $d = DateTime::createFromFormat( $date_format, $value ); | |
| 398 | + | |
| 399 | + if ( !( $d && ( $d->format( $date_format ) == $value ) ) ) { | |
| 400 | + $form_error->add( 'rest_weforms_form_date_field', __( 'Date Field Is not Valid', 'weforms' ), [ 'status' => 404 ] ); | |
| 401 | + } | |
| 402 | + } | |
| 403 | + | |
| 404 | + if ( 'email_address' === $field['template'] ) { | |
| 405 | + if ( !$this->email_validation( $value ) ) { | |
| 406 | + $form_error->add( 'rest_weforms_form_email', __( 'Invalid Email Address', 'weforms' ), [ 'status' => 404 ] ); | |
| 407 | + } | |
| 408 | + } | |
| 409 | + | |
| 410 | + if ( 'website_url' === $field['template'] ) { | |
| 411 | + if ( !filter_var( $value, FILTER_VALIDATE_URL ) ) { | |
| 412 | + $form_error->add( 'rest_weforms_form_url', __( 'Invalid Url Address', 'weforms' ), [ 'status' => 404 ] ); | |
| 413 | + } | |
| 414 | + } | |
| 415 | + | |
| 416 | + if ( 'numeric_text_field' === $field['template'] ) { | |
| 417 | + if ( !is_numeric( $value ) ) { | |
| 418 | + $form_error->add( 'rest_weforms_form_number', __( 'Number Field Should be numberic value ', 'weforms' ), [ 'status' => 404 ] ); | |
| 419 | + } | |
| 420 | + } | |
| 421 | + | |
| 422 | + if ( 'single_product' === $field['template'] ) { | |
| 423 | + if ( !$value ) { | |
| 424 | + $value = []; | |
| 425 | + } | |
| 426 | + | |
| 427 | + $value['price'] = isset( $value['price'] ) ? floatval( $value['price'] ) : 0; | |
| 428 | + $value['quantity'] = isset( $value['quantity'] ) ? floatval( $value['quantity'] ) : 0; | |
| 429 | + $quantity = isset( $field['quantity'] ) ? $field['quantity'] : []; | |
| 430 | + $price = isset( $field['price'] ) ? $field['price'] : []; | |
| 431 | + | |
| 432 | + if ( isset( $price['is_flexible'] ) && $price['is_flexible'] ) { | |
| 433 | + $min = isset( $price['min'] ) ? floatval( $price['min'] ) : 0; | |
| 434 | + $max = isset( $price['max'] ) ? floatval( $price['max'] ) : 0; | |
| 435 | + | |
| 436 | + if ( $value['price'] < $min ) { | |
| 437 | + return new WP_Error( 'rest_weforms_form_price', sprintf( __( '%s price must be equal or greater than %s', | |
| 438 | + $field['weforms'], | |
| 439 | + $min, | |
| 440 | + 'weforms' | |
| 441 | + ) | |
| 442 | + ), [ 'status' => 404 ] ); | |
| 443 | + } | |
| 444 | + | |
| 445 | + if ( $max && $value['price'] > $max ) { | |
| 446 | + $form_error->add( 'rest_weforms_form_price', sprintf( __( '%s price must be equal or less than %s', | |
| 447 | + $field['weforms'], | |
| 448 | + $max, | |
| 449 | + 'weforms' | |
| 450 | + ) | |
| 451 | + ), [ 'status' => 404 ] ); | |
| 452 | + } | |
| 453 | + } | |
| 454 | + | |
| 455 | + if ( isset( $quantity['status'] ) && $quantity['status'] ) { | |
| 456 | + $min = isset( $quantity['min'] ) ? floatval( $quantity['min'] ) : 0; | |
| 457 | + $max = isset( $quantity['max'] ) ? floatval( $quantity['max'] ) : 0; | |
| 458 | + | |
| 459 | + if ( $value['quantity'] < $min ) { | |
| 460 | + $form_error->add( 'rest_weforms_form_quantity', sprintf( __( | |
| 461 | + '%s quantity must be equal or greater than %s', | |
| 462 | + $field['weforms'], | |
| 463 | + $min, | |
| 464 | + 'weforms' | |
| 465 | + ) | |
| 466 | + ), [ 'status' => 404 ] ); | |
| 467 | + } | |
| 468 | + | |
| 469 | + if ( $max && $value['quantity'] > $max ) { | |
| 470 | + $form_error->add( 'rest_weforms_form_quantity', sprintf( __( | |
| 471 | + '%s quantity must be equal or less than %s', | |
| 472 | + $field['weforms'], | |
| 473 | + $max, | |
| 474 | + 'weforms' | |
| 475 | + ) | |
| 476 | + ), [ 'status' => 404 ] ); | |
| 477 | + } | |
| 478 | + } | |
| 479 | + } | |
| 480 | + } | |
| 481 | + } | |
| 482 | + | |
| 483 | + if ( count( $form_error->get_error_messages() ) > 0 ) { | |
| 484 | + return $form_error; | |
| 485 | + } else { | |
| 486 | + return true; | |
| 487 | + } | |
| 488 | + } | |
| 489 | + | |
| 490 | + /** | |
| 491 | + * reCaptcha Validation | |
| 492 | + * | |
| 493 | + * @since 1.4.2 | |
| 494 | + * | |
| 495 | + * @return void | |
| 496 | + */ | |
| 497 | + public function validate_reCaptcha( $request ) { | |
| 498 | + if ( class_exists( 'WPUF_ReCaptcha' ) ) { | |
| 499 | + $recaptcha_class = 'WPUF_ReCaptcha'; | |
| 500 | + } else { | |
| 501 | + if ( !function_exists( 'recaptcha_get_html' ) ) { | |
| 502 | + require_once WEFORMS_INCLUDES . '/library/reCaptcha/recaptchalib.php'; | |
| 503 | + } | |
| 504 | + | |
| 505 | + require_once WEFORMS_INCLUDES . '/library/reCaptcha/recaptchalib_noCaptcha.php'; | |
| 506 | + | |
| 507 | + $recaptcha_class = 'Weforms_ReCaptcha'; | |
| 508 | + } | |
| 509 | + | |
| 510 | + $invisible = isset( $request['g-recaptcha-response'] ) ? false : true; | |
| 511 | + | |
| 512 | + $recaptcha_settings = weforms_get_settings( 'recaptcha' ); | |
| 513 | + $secret = isset( $recaptcha_settings->secret ) ? $recaptcha_settings->secret : ''; | |
| 514 | + | |
| 515 | + if ( ! $invisible ) { | |
| 516 | + $response = null; | |
| 517 | + $reCaptcha = new $recaptcha_class( $secret ); | |
| 518 | + $remote_ADDR = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; | |
| 519 | + $recaptcha_response = isset( $_SERVER['g-recaptcha-response'] ) ? sanitize_text_field( wp_unslash( $request['g-recaptcha-response'] ) ) : ''; | |
| 520 | + | |
| 521 | + $resp = $reCaptcha->verifyResponse( | |
| 522 | + $remote_ADDR, | |
| 523 | + $recaptcha_response | |
| 524 | + ); | |
| 525 | + | |
| 526 | + if ( !$resp->success ) { | |
| 527 | + return false; | |
| 528 | + } | |
| 529 | + } else { | |
| 530 | + $recap_challenge = isset( $request['recaptcha_challenge_field'] ) ? $request['recaptcha_challenge_field'] : ''; | |
| 531 | + $recap_response = isset( $request['recaptcha_response_field'] ) ? $request['recaptcha_response_field'] : ''; | |
| 532 | + $resp = recaptcha_check_answer( $secret, $remote_ADDR, $recap_challenge, $recap_response ); | |
| 533 | + | |
| 534 | + if ( !$resp->is_valid ) { | |
| 535 | + ob_clean(); | |
| 536 | + | |
| 537 | + return false; | |
| 538 | + } | |
| 539 | + } | |
| 540 | + | |
| 541 | + return true; | |
| 542 | + } | |
| 543 | + | |
| 544 | + public function weforms_api_insert_entry( $args, $fields = [] ) { | |
| 545 | + global $wpdb; | |
| 546 | + | |
| 547 | + $browser = weforms_get_browser(); | |
| 548 | + | |
| 549 | + $defaults = [ | |
| 550 | + 'form_id' => 0, | |
| 551 | + 'user_id' => get_current_user_id(), | |
| 552 | + 'user_ip' => ip2long( weforms_get_client_ip() ), | |
| 553 | + 'user_device' => $browser['name'] . '/' . $browser['platform'], | |
| 554 | + 'created_at' => current_time( 'mysql' ), | |
| 555 | + ]; | |
| 556 | + | |
| 557 | + $r = wp_parse_args( $args, $defaults ); | |
| 558 | + | |
| 559 | + if ( !$r['form_id'] ) { | |
| 560 | + return new WP_Error( 'no-form-id', __( 'No form ID was found.', 'weforms' ) ); | |
| 561 | + } | |
| 562 | + | |
| 563 | + if ( !$fields ) { | |
| 564 | + return new WP_Error( 'no-fields', __( 'No form fields were found.', 'weforms' ) ); | |
| 565 | + } | |
| 566 | + | |
| 567 | + $success = $wpdb->insert( $wpdb->weforms_entries, $r ); | |
| 568 | + | |
| 569 | + if ( is_wp_error( $success ) || !$success ) { | |
| 570 | + return new WP_Error( 'could-not-create', __( 'Could not create an entry', 'weforms' ), [ 'status' => 404 ] ); | |
| 571 | + } | |
| 572 | + | |
| 573 | + $entry_id = $wpdb->insert_id; | |
| 574 | + | |
| 575 | + foreach ( $fields as $key => $value ) { | |
| 576 | + weforms_add_entry_meta( $entry_id, $key, $value ); | |
| 577 | + } | |
| 578 | + | |
| 579 | + return $entry_id; | |
| 580 | + } | |
| 581 | + | |
| 582 | + /** | |
| 583 | + * Save and existing form | |
| 584 | + * | |
| 585 | + * @since 1.4.2 | |
| 586 | + * | |
| 587 | + * @param array $data Contains form_fields, form_settings, form_settings_key data | |
| 588 | + * | |
| 589 | + * @return bool | |
| 590 | + */ | |
| 591 | + public function weforms_api_save( $data ) { | |
| 592 | + $saved_wpuf_inputs = []; | |
| 593 | + | |
| 594 | + wp_update_post( [ 'ID' => $data['form_id'], 'post_status' => 'publish', 'post_title' => $data['post_title'] ] ); | |
| 595 | + | |
| 596 | + $existing_wpuf_input_ids = get_children( [ | |
| 597 | + 'post_parent' => $data['form_id'], | |
| 598 | + 'post_status' => 'publish', | |
| 599 | + 'post_type' => 'wpuf_input', | |
| 600 | + 'numberposts' => '-1', | |
| 601 | + 'orderby' => 'menu_order', | |
| 602 | + 'order' => 'ASC', | |
| 603 | + 'fields' => 'ids', | |
| 604 | + ] ); | |
| 605 | + | |
| 606 | + $new_wpuf_input_ids = []; | |
| 607 | + | |
| 608 | + if ( !empty( $data['form_fields'] ) ) { | |
| 609 | + foreach ( $data['form_fields'] as $order => $field ) { | |
| 610 | + if ( !empty( $field['is_new'] ) ) { | |
| 611 | + unset( $field['is_new'] ); | |
| 612 | + unset( $field['id'] ); | |
| 613 | + | |
| 614 | + $field_id = 0; | |
| 615 | + } else { | |
| 616 | + $field_id = $field['id']; | |
| 617 | + } | |
| 618 | + | |
| 619 | + $field_id = weforms_insert_form_field( $data['form_id'], $field, $field_id, $order ); | |
| 620 | + | |
| 621 | + $new_wpuf_input_ids[] = $field_id; | |
| 622 | + | |
| 623 | + $field['id'] = $field_id; | |
| 624 | + | |
| 625 | + $saved_wpuf_inputs[] = $field; | |
| 626 | + } | |
| 627 | + } | |
| 628 | + | |
| 629 | + $form = weforms()->form->get( $data['form_id'] ); | |
| 630 | + $new_form_settings = array_merge( $data['form_settings'], array_diff_key( $form->get_settings(), $data['form_settings'] ) ); | |
| 631 | + $new_form_notifications = array_merge( $data['notifications'], array_diff_key( $form->get_notifications(), $data['notifications'] ) ); | |
| 632 | + $new_form_integrations = array_merge( $data['integrations'], array_diff_key( $form->get_integrations(), $data['integrations'] ) ); | |
| 633 | + | |
| 634 | + update_post_meta( $data['form_id'], $data['form_settings_key'], $new_form_settings ); | |
| 635 | + update_post_meta( $data['form_id'], 'notifications', $new_form_notifications ); | |
| 636 | + update_post_meta( $data['form_id'], 'integrations', $new_form_integrations ); | |
| 637 | + update_post_meta( $data['form_id'], '_weforms_version', WEFORMS_VERSION ); | |
| 638 | + | |
| 639 | + return $saved_wpuf_inputs; | |
| 640 | + } | |
| 641 | + | |
| 642 | + /** | |
| 643 | + * Bulk Delete Form Exist | |
| 644 | + * | |
| 645 | + * @since 1.4.2 | |
| 646 | + * | |
| 647 | + * @param array $param | |
| 648 | + * @param WP_REST_Request $request Full details about the request | |
| 649 | + * @param string $key | |
| 650 | + * | |
| 651 | + * @return bool | |
| 652 | + **/ | |
| 653 | + public function is_bulk_delete_form_exists( $param, $request, $key ) { | |
| 654 | + global $wpdb; | |
| 655 | + $forms = $request['ids']; | |
| 656 | + | |
| 657 | + if ( is_array( $forms ) ) { | |
| 658 | + $forms = array_filter( $forms, 'is_numeric' ); | |
| 659 | + | |
| 660 | + if ( empty( $forms ) ) { | |
| 661 | + return false; | |
| 662 | + } | |
| 663 | + | |
| 664 | + $form_id = implode( ',', $forms ); | |
| 665 | + $querystr = " SELECT $wpdb->posts.id | |
| 666 | + FROM $wpdb->posts | |
| 667 | + WHERE $wpdb->posts.post_type = 'wpuf_contact_form' | |
| 668 | + AND $wpdb->posts.ID IN ( $form_id ) | |
| 669 | + "; | |
| 670 | + } else { | |
| 671 | + $form_id = (int) $forms; | |
| 672 | + $querystr = " SELECT $wpdb->posts.id | |
| 673 | + FROM $wpdb->posts | |
| 674 | + WHERE $wpdb->posts.post_type = 'wpuf_contact_form' | |
| 675 | + AND $wpdb->posts.ID = $form_id | |
| 676 | + "; | |
| 677 | + } | |
| 678 | + | |
| 679 | + $result = $wpdb->get_results( $querystr ); | |
| 680 | + | |
| 681 | + if ( empty( $result ) ) { | |
| 682 | + return false; | |
| 683 | + } else { | |
| 684 | + return true; | |
| 685 | + } | |
| 686 | + } | |
| 687 | + | |
| 688 | + public function email_validation( $value ) { | |
| 689 | + return ( !preg_match( "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $value ) ) ? false : true; | |
| 690 | + } | |
| 691 | + | |
| 692 | + public function get_items( $request ) { | |
| 693 | + $args = [ | |
| 694 | + 'posts_per_page' => isset( $request['per_page'] ) ? intval( $request['per_page'] ) : 10, | |
| 695 | + 'paged' => isset( $request['page'] ) ? absint( $request['page'] ) : 1, | |
| 696 | + 'order' => isset( $request['order'] ) ? $request['order'] : 'DESC', | |
| 697 | + 'orderby' => isset( $request['orderby'] ) ? $request['orderby'] : 'post_date', | |
| 698 | + 'post_status' => isset( $request['status'] ) ? $request['status'] : 'any', | |
| 699 | + 's' => isset( $request['search'] ) ? $request['search'] : '', | |
| 700 | + ]; | |
| 701 | + | |
| 702 | + $forms_array = []; | |
| 703 | + | |
| 704 | + $defaults = [ | |
| 705 | + 'post_type' => 'wpuf_contact_form', | |
| 706 | + 'post_status' => [ 'publish', 'draft', 'pending' ], | |
| 707 | + ]; | |
| 708 | + | |
| 709 | + $args = wp_parse_args( $args, $defaults ); | |
| 710 | + $query = new WP_Query( $args ); | |
| 711 | + $forms = $query->get_posts(); | |
| 712 | + $formatted_items = []; | |
| 713 | + | |
| 714 | + if ( $forms ) { | |
| 715 | + foreach ( $forms as $form_obj ) { | |
| 716 | + $form = new WeForms_Form( $form_obj ); | |
| 717 | + $form_author = $form->data->post_author; | |
| 718 | + $form_status = $form->is_api_form_submission_open(); | |
| 719 | + $data = []; | |
| 720 | + $data['id'] = $form->id; | |
| 721 | + $data['name'] = $form->name; | |
| 722 | + $data['created_by'] = get_the_author_meta( 'user_nicename', $form_author ); | |
| 723 | + $data['created_on'] = $form->data->post_date; | |
| 724 | + $data['status'] = $form_status['type']; | |
| 725 | + $data['status_info'] = isset( $form_status['message'] ) ? $form_status['message'] : ''; | |
| 726 | + $data['fields'] = count( $form->get_fields() ); | |
| 727 | + $data['entries'] = $form->num_form_entries(); | |
| 728 | + $data['views'] = $form->num_form_views(); | |
| 729 | + $data['payments'] = $form->num_form_payments(); | |
| 730 | + $data = $this->prepare_item_for_response( (array) $data, $request ); | |
| 731 | + $formatted_items[] = $this->prepare_response_for_collection( $data ); | |
| 732 | + } | |
| 733 | + } | |
| 734 | + | |
| 735 | + $contact_forms = apply_filters( 'weforms_rest_get_contact_forms', $formatted_items ); | |
| 736 | + $response = rest_ensure_response( $contact_forms ); | |
| 737 | + $response = $this->format_collection_response( $response, $request, (int) $query->found_posts ); | |
| 738 | + | |
| 739 | + return $response; | |
| 740 | + } | |
| 741 | + | |
| 742 | + public function get_item( $request ) { | |
| 743 | + $form_id = absint( $request['form_id'] ); | |
| 744 | + $form = weforms()->form->get( $form_id ); | |
| 745 | + $form_status = $form->is_api_form_submission_open(); | |
| 746 | + $form_author = $form->data->post_author; | |
| 747 | + | |
| 748 | + $data = [ | |
| 749 | + 'id' => $form->data->ID, | |
| 750 | + 'name' => $form->name, | |
| 751 | + 'created_by' => get_the_author_meta( 'user_nicename', $form_author ), | |
| 752 | + 'created_on' => $form->data->post_date, | |
| 753 | + 'status' => $form_status['type'], | |
| 754 | + 'status_info' => isset( $form_status['message'] ) ? $form_status['message'] : '', | |
| 755 | + 'fields' => $form->get_fields(), | |
| 756 | + 'settings' => $form->get_settings(), | |
| 757 | + 'notifications' => $form->get_notifications(), | |
| 758 | + 'integrations' => $form->get_integrations(), | |
| 759 | + ]; | |
| 760 | + | |
| 761 | + $data = $this->prepare_item_for_response( $data, $request ); | |
| 762 | + $response = rest_ensure_response( $data ); | |
| 763 | + | |
| 764 | + return $response; | |
| 765 | + } | |
| 766 | + | |
| 767 | + public function create_item( $request ) { | |
| 768 | + $template = isset( $request['template'] ) ? sanitize_text_field( $request['template'] ) : ''; | |
| 769 | + $name = isset( $request['name'] ) ? sanitize_text_field( $request['name'] ) : ''; | |
| 770 | + $setting = isset( $request['settings'] ) ? $request['settings'] : []; | |
| 771 | + $notifications = isset( $request['notifications'] ) ? $request['notifications'] : []; | |
| 772 | + $fields = isset( $request['fields'] ) ? $request['fields'] : []; | |
| 773 | + $integrations = isset( $request['integrations'] ) ? $request['integrations'] : []; | |
| 774 | + | |
| 775 | + $default_form_settings = weforms_get_default_form_settings(); | |
| 776 | + $default_form_notification = [ weforms_get_default_form_notification() ]; | |
| 777 | + $integration_list = weforms()->integrations->get_integration_js_settings(); | |
| 778 | + $field_list = weforms()->fields->get_fields(); | |
| 779 | + | |
| 780 | + if ( isset( $template ) && !empty( $template ) ) { | |
| 781 | + $form_id = weforms()->templates->create( $template ); | |
| 782 | + } elseif ( !empty( $name ) ) { | |
| 783 | + if ( !empty( $fields ) ) { | |
| 784 | + foreach ( $fields as $key => $field ) { | |
| 785 | + $f = in_array( $field['template'], array_keys( $field_list ) ); | |
| 786 | + | |
| 787 | + if ( empty( $f ) ) { | |
| 788 | + unset( $fields[ $key ] ); | |
| 789 | + } | |
| 790 | + } | |
| 791 | + } | |
| 792 | + | |
| 793 | + $fields = array_intersect_key( $fields, $field_list ); | |
| 794 | + $form_id = weforms()->form->create( $name, $fields ); | |
| 795 | + | |
| 796 | + if ( is_wp_error( $form_id ) ) { | |
| 797 | + return new WP_Error( 'rest_invalid_data', __( 'Could not create the form', 'weforms' ), [ 'status' => 404 ] ); | |
| 798 | + } | |
| 799 | + | |
| 800 | + $new_form_settings = array_diff_key( $default_form_settings, $setting ); | |
| 801 | + $new_form_notification = array_diff_key( $default_form_notification, $notifications ); | |
| 802 | + $new_form_integrations = array_intersect_key( $integrations, $integration_list ); | |
| 803 | + | |
| 804 | + if ( !class_exists( 'WeForms_Pro' ) ) { | |
| 805 | + $new_form_integrations = array_udiff_assoc( $new_form_integrations, $integration_list, | |
| 806 | + function ( $item, $item_list ) { | |
| 807 | + if ( isset( $item_list['pro'] ) && $item_list['pro'] == true ) { | |
| 808 | + return 0; | |
| 809 | + } | |
| 810 | + | |
| 811 | + return $item; | |
| 812 | + } | |
| 813 | + ); | |
| 814 | + } | |
| 815 | + | |
| 816 | + update_post_meta( $form_id, 'wpuf_form_settings', $new_form_settings ); | |
| 817 | + update_post_meta( $form_id, 'notifications', $new_form_notification ); | |
| 818 | + update_post_meta( $form_id, 'integrations', $new_form_integrations ); | |
| 819 | + } | |
| 820 | + | |
| 821 | + if ( is_wp_error( $form_id ) || is_null( $form_id ) ) { | |
| 822 | + return new WP_Error( 'rest_weforms_invalid_data', __( 'Could not create the form', 'weforms' ), [ 'status' => 404 ] ); | |
| 823 | + } | |
| 824 | + | |
| 825 | + $form = weforms()->form->get( $form_id ); | |
| 826 | + | |
| 827 | + $data = [ | |
| 828 | + 'id' => $form->data->ID, | |
| 829 | + 'name' => $form->name, | |
| 830 | + 'fields' => $form->get_fields(), | |
| 831 | + 'settings' => $form->get_settings(), | |
| 832 | + 'notifications' => $form->get_notifications(), | |
| 833 | + 'integrations' => $form->get_integrations(), | |
| 834 | + ]; | |
| 835 | + | |
| 836 | + $response = $this->prepare_item_for_response( $data, $request ); | |
| 837 | + $response = rest_ensure_response( $response ); | |
| 838 | + $response->set_status( 200 ); | |
| 839 | + $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $form->id ) ) ); | |
| 840 | + | |
| 841 | + return $response; | |
| 842 | + } | |
| 843 | + | |
| 844 | + public function update_item( $request ) { | |
| 845 | + $data = []; | |
| 846 | + $form_id = $request['form_id']; | |
| 847 | + $data['form_id'] = $form_id; | |
| 848 | + $data['form_settings_key'] = 'wpuf_form_settings'; | |
| 849 | + | |
| 850 | + if ( isset( $request['name'] ) && !empty( $request['name'] ) ) { | |
| 851 | + $data['post_title'] = $request['name']; | |
| 852 | + } | |
| 853 | + | |
| 854 | + if ( isset( $request['settings'] ) && !empty( $request['settings'] ) ) { | |
| 855 | + $data['form_settings'] = $request['settings']; | |
| 856 | + } | |
| 857 | + | |
| 858 | + if ( isset( $request['fields'] ) && !empty( $request['fields'] ) ) { | |
| 859 | + $data['form_fields'] = $request['fields']; | |
| 860 | + } | |
| 861 | + | |
| 862 | + if ( isset( $request['notifications'] ) && !empty( $request['notifications'] ) ) { | |
| 863 | + $data['notifications'] = $request['notifications']; | |
| 864 | + } | |
| 865 | + | |
| 866 | + if ( isset( $request['integrations'] ) && !empty( $request['integrations'] ) ) { | |
| 867 | + $data['integrations'] = $request['integrations']; | |
| 868 | + } | |
| 869 | + | |
| 870 | + $this->weforms_api_update( $data ); | |
| 871 | + | |
| 872 | + $form = weforms()->form->get( $form_id ); | |
| 873 | + | |
| 874 | + $response_data = [ | |
| 875 | + 'id' => $form->data->ID, | |
| 876 | + 'name' => $form->name, | |
| 877 | + 'fields' => $form->get_fields(), | |
| 878 | + 'settings' => $form->get_settings(), | |
| 879 | + 'notifications' => $form->get_notifications(), | |
| 880 | + 'integrations' => $form->get_integrations(), | |
| 881 | + ]; | |
| 882 | + | |
| 883 | + $response = $this->prepare_item_for_response( $response_data, $request ); | |
| 884 | + $response = rest_ensure_response( $response ); | |
| 885 | + $response->set_status( 201 ); | |
| 886 | + $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $form->id ) ) ); | |
| 887 | + | |
| 888 | + return $response; | |
| 889 | + } | |
| 890 | + | |
| 891 | + public function delete_item( $request ) { | |
| 892 | + $form_array = []; | |
| 893 | + $form_id = $request['form_id']; | |
| 894 | + $form = $this->delete( $form_id ); | |
| 895 | + | |
| 896 | + if ( is_wp_error( $form ) ) { | |
| 897 | + return new WP_Error( 'error', __( 'Could not delete the form', 'weforms' ), [ 'status' => 404 ] ); | |
| 898 | + } | |
| 899 | + | |
| 900 | + $form_array['id'] = $form_id; | |
| 901 | + $form_array['message'] = __( 'form deleted successfully', 'weforms' ); | |
| 902 | + $form_array['data']['status'] = 200; | |
| 903 | + | |
| 904 | + $response = $this->prepare_response_for_collection( $form_array, $request ); | |
| 905 | + $response = rest_ensure_response( $response ); | |
| 906 | + | |
| 907 | + return $response; | |
| 908 | + } | |
| 909 | + | |
| 910 | + /** | |
| 911 | + * Duplicate Form | |
| 912 | + * | |
| 913 | + * @since 1.4.2 | |
| 914 | + * | |
| 915 | + * @param WP_REST_Request $request full details about the request | |
| 916 | + * | |
| 917 | + * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure | |
| 918 | + **/ | |
| 919 | + public function duplicate_form( $request ) { | |
| 920 | + $form_id = $request['form_id']; | |
| 921 | + $form = weforms()->form->get( $form_id ); | |
| 922 | + | |
| 923 | + if ( empty( $form ) ) { | |
| 924 | + return new WP_Error( 'error', __( 'Could not duplicate the form', 'weforms' ), [ 'status' => 404 ] ); | |
| 925 | + } | |
| 926 | + | |
| 927 | + $form_id = weforms()->form->create( $form->name, $form->get_fields() ); | |
| 928 | + | |
| 929 | + $data = [ | |
| 930 | + 'form_id' => absint( $form_id ), | |
| 931 | + 'post_title' => sanitize_text_field( $form->name ) . ' (#' . $form_id . ')', | |
| 932 | + 'form_fields' => weforms()->form->get( $form_id )->get_fields(), | |
| 933 | + 'form_settings' => $form->get_settings(), | |
| 934 | + 'form_settings_key' => 'wpuf_form_settings', | |
| 935 | + 'notifications' => $form->get_notifications(), | |
| 936 | + 'integrations' => $form->get_integrations(), | |
| 937 | + ]; | |
| 938 | + | |
| 939 | + $form_fields = weforms()->form->save( $data ); | |
| 940 | + $form = weforms()->form->get( $form_id ); | |
| 941 | + $form->settings = $form->get_settings(); | |
| 942 | + | |
| 943 | + $response_data = [ | |
| 944 | + 'id' => $form->data->ID, | |
| 945 | + 'name' => $form->name, | |
| 946 | + 'fields' => $form->get_fields(), | |
| 947 | + 'settings' => $form->get_settings(), | |
| 948 | + 'notifications' => $form->get_notifications(), | |
| 949 | + 'integrations' => $form->get_integrations(), | |
| 950 | + ]; | |
| 951 | + | |
| 952 | + $response = $this->prepare_item_for_response( $response_data, $request ); | |
| 953 | + $response = rest_ensure_response( $response ); | |
| 954 | + $response->set_status( 201 ); | |
| 955 | + $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $form->id ) ) ); | |
| 956 | + | |
| 957 | + return $response; | |
| 958 | + } | |
| 959 | + | |
| 960 | + /** | |
| 961 | + * Prepare a single user output for response | |
| 962 | + * | |
| 963 | + * @param object $item | |
| 964 | + * @param WP_REST_Request $request request object | |
| 965 | + * @param array $additional_fields (optional) | |
| 966 | + * | |
| 967 | + * @return WP_REST_Response $response response data | |
| 968 | + */ | |
| 969 | + public function prepare_item_for_response( $item, $request, $additional_fields = [] ) { | |
| 970 | + $response = rest_ensure_response( $item ); | |
| 971 | + $response = $this->add_links( $response, $item ); | |
| 972 | + | |
| 973 | + return $response; | |
| 974 | + } | |
| 975 | + | |
| 976 | + /** | |
| 977 | + * Get the Form's schema, conforming to JSON Schema | |
| 978 | + * | |
| 979 | + * @return array | |
| 980 | + */ | |
| 981 | + public function get_item_schema() { | |
| 982 | + $schema = [ | |
| 983 | + '$schema' => 'http://json-schema.org/draft-04/schema#', | |
| 984 | + 'title' => 'forms', | |
| 985 | + 'type' => 'object', | |
| 986 | + 'properties' => [ | |
| 987 | + 'form_id' => [ | |
| 988 | + 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 989 | + 'type' => 'integer', | |
| 990 | + 'sanitize_callback' => 'absint', | |
| 991 | + 'validate_callback' => [ $this, 'is_form_exists' ], | |
| 992 | + 'context' => [ 'embed', 'view', 'edit' ], | |
| 993 | + 'required' => true, | |
| 994 | + 'readonly' => true, | |
| 995 | + ], | |
| 996 | + 'name' => [ | |
| 997 | + 'description' => __( '', 'weforms' ), | |
| 998 | + 'type' => 'string', | |
| 999 | + 'context' => [ 'edit', 'view'], | |
| 1000 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 1001 | + 'required' => false, | |
| 1002 | + ], | |
| 1003 | + 'template' => [ | |
| 1004 | + 'required' => false, | |
| 1005 | + 'type' => 'string', | |
| 1006 | + 'description' => 'template name', | |
| 1007 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 1008 | + 'validate_callback' => [ $this, 'is_template_exists' ], | |
| 1009 | + ], | |
| 1010 | + 'settings' => [ | |
| 1011 | + 'description' => __( '', 'weforms' ), | |
| 1012 | + 'type' => 'object', | |
| 1013 | + 'context' => [ 'edit', 'view'], | |
| 1014 | + 'required' => false, | |
| 1015 | + ], | |
| 1016 | + 'notifications' => [ | |
| 1017 | + 'description' => __( '', 'weforms' ), | |
| 1018 | + 'type' => 'object', | |
| 1019 | + 'context' => [ 'edit', 'view' ], | |
| 1020 | + 'required' => false, | |
| 1021 | + ], | |
| 1022 | + 'fields' => [ | |
| 1023 | + 'description' => __( '', 'weforms' ), | |
| 1024 | + 'type' => 'object', | |
| 1025 | + 'context' => [ 'edit', 'view' ], | |
| 1026 | + 'required' => false, | |
| 1027 | + ], | |
| 1028 | + 'integrations' => [ | |
| 1029 | + 'description' => __( '', 'weforms' ), | |
| 1030 | + 'context' => [ 'edit', 'view' ], | |
| 1031 | + 'type' => 'object', | |
| 1032 | + 'required' => false, | |
| 1033 | + ], | |
| 1034 | + ], | |
| 1035 | + ]; | |
| 1036 | + | |
| 1037 | + return $schema; | |
| 1038 | + } | |
| 1039 | + | |
| 1040 | + public function get_collection_params() { | |
| 1041 | + $query_params = parent::get_collection_params(); | |
| 1042 | + $query_params['context']['default'] = 'view'; | |
| 1043 | + | |
| 1044 | + $schema = $this->get_item_schema(); | |
| 1045 | + $schema_properties = $schema['properties']; | |
| 1046 | + | |
| 1047 | + return $query_params; | |
| 1048 | + } | |
| 1049 | + | |
| 1050 | + /** | |
| 1051 | + * Delete a form with it's input fields | |
| 1052 | + * | |
| 1053 | + * @since 1.4.2 | |
| 1054 | + * | |
| 1055 | + * @param int $form_id | |
| 1056 | + * @param bool $force | |
| 1057 | + * | |
| 1058 | + * @return void | |
| 1059 | + */ | |
| 1060 | + public function delete( $form_id, $force = true ) { | |
| 1061 | + global $wpdb; | |
| 1062 | + | |
| 1063 | + $form = weforms()->form->get( $form_id ); | |
| 1064 | + | |
| 1065 | + if ( !$form->id ) { | |
| 1066 | + return new WP_Error( 'form_not_found', __( 'Form not found', 'weforms' ) ); | |
| 1067 | + } | |
| 1068 | + | |
| 1069 | + $wp_post = wp_delete_post( $form_id, $force ); | |
| 1070 | + | |
| 1071 | + if ( !$wp_post ) { | |
| 1072 | + return new WP_Error( 'unable_to_delete', __( 'Unable to delete form', 'weforms' ) ); | |
| 1073 | + } | |
| 1074 | + | |
| 1075 | + // delete form inputs as WP doesn't know the relationship | |
| 1076 | + return $wpdb->delete( $wpdb->posts, | |
| 1077 | + [ | |
| 1078 | + 'post_parent' => $form_id, | |
| 1079 | + 'post_type' => 'wpuf_input', | |
| 1080 | + ] | |
| 1081 | + ); | |
| 1082 | + | |
| 1083 | + return $form; | |
| 1084 | + } | |
| 1085 | + | |
| 1086 | + /** | |
| 1087 | + * Bulk Delete Form | |
| 1088 | + * | |
| 1089 | + * @since 1.3.9 | |
| 1090 | + * | |
| 1091 | + * @param WP_REST_Request $request | |
| 1092 | + * | |
| 1093 | + * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure | |
| 1094 | + */ | |
| 1095 | + public function bulk_delete_form( $request ) { | |
| 1096 | + $form_ids = $request['ids']; | |
| 1097 | + | |
| 1098 | + if ( empty( $form_ids ) ) { | |
| 1099 | + return new WP_Error( 'error', __( 'No form ids provided!', 'weforms' ), [ 'status' => 404 ] ); | |
| 1100 | + } | |
| 1101 | + | |
| 1102 | + $response = []; | |
| 1103 | + | |
| 1104 | + foreach ( $form_ids as $form_id ) { | |
| 1105 | + $status = $this->delete( $form_id ); | |
| 1106 | + | |
| 1107 | + if ( is_wp_error( $status ) ) { | |
| 1108 | + $response[$form_id] = $status->get_error_message(); | |
| 1109 | + } else { | |
| 1110 | + $response[$form_id] = __( 'form deleted successfully ', 'weforms' ); | |
| 1111 | + } | |
| 1112 | + } | |
| 1113 | + | |
| 1114 | + $response = $this->prepare_response_for_collection( $response ); | |
| 1115 | + $response = rest_ensure_response( $response ); | |
| 1116 | + | |
| 1117 | + return $response; | |
| 1118 | + } | |
| 1119 | + | |
| 1120 | + /** | |
| 1121 | + * Update and existing form | |
| 1122 | + * | |
| 1123 | + * @since 1.4.2 | |
| 1124 | + * | |
| 1125 | + * @param array $data Contains form_fields, form_settings, form_settings_key data | |
| 1126 | + * | |
| 1127 | + * @return bool | |
| 1128 | + */ | |
| 1129 | + public function weforms_api_update( $data ) { | |
| 1130 | + $saved_wpuf_inputs = []; | |
| 1131 | + | |
| 1132 | + if ( isset( $data['post_title'] ) && !empty( $data['post_title'] ) ) { | |
| 1133 | + wp_update_post( | |
| 1134 | + [ | |
| 1135 | + 'ID' => $data['form_id'], | |
| 1136 | + 'post_status' => 'publish', | |
| 1137 | + 'post_title' => $data['post_title'], | |
| 1138 | + ] | |
| 1139 | + ); | |
| 1140 | + } | |
| 1141 | + | |
| 1142 | + $existing_wpuf_input_ids = get_children( [ | |
| 1143 | + 'post_parent' => $data['form_id'], | |
| 1144 | + 'post_status' => 'publish', | |
| 1145 | + 'post_type' => 'wpuf_input', | |
| 1146 | + 'numberposts' => '-1', | |
| 1147 | + 'orderby' => 'menu_order', | |
| 1148 | + 'order' => 'ASC', | |
| 1149 | + 'fields' => 'ids', | |
| 1150 | + ] ); | |
| 1151 | + | |
| 1152 | + $new_wpuf_input_ids = []; | |
| 1153 | + | |
| 1154 | + if ( !empty( $data['form_fields'] ) ) { | |
| 1155 | + foreach ( $data['form_fields'] as $order => $field ) { | |
| 1156 | + if ( !empty( $field['is_new'] ) ) { | |
| 1157 | + unset( $field['is_new'] ); | |
| 1158 | + unset( $field['id'] ); | |
| 1159 | + | |
| 1160 | + $field_id = 0; | |
| 1161 | + } else { | |
| 1162 | + $field_id = $field['id']; | |
| 1163 | + } | |
| 1164 | + | |
| 1165 | + $field_id = weforms_insert_form_field( $data['form_id'], $field, $field_id, $order ); | |
| 1166 | + | |
| 1167 | + $new_wpuf_input_ids[] = $field_id; | |
| 1168 | + | |
| 1169 | + $field['id'] = $field_id; | |
| 1170 | + | |
| 1171 | + $saved_wpuf_inputs[] = $field; | |
| 1172 | + } | |
| 1173 | + } | |
| 1174 | + | |
| 1175 | + $form = weforms()->form->get( $data['form_id'] ); | |
| 1176 | + | |
| 1177 | + if ( isset( $data['form_settings'] ) && !empty( $data['form_settings'] ) ) { | |
| 1178 | + $new_form_settings = array_merge( $data['form_settings'], array_diff_key( $form->get_settings(), $data['form_settings'] ) ); | |
| 1179 | + update_post_meta( $data['form_id'], 'wpuf_form_settings', $new_form_settings ); | |
| 1180 | + } | |
| 1181 | + | |
| 1182 | + if ( isset( $data['notifications'] ) && !empty( $data['notifications'] ) ) { | |
| 1183 | + $existing_notifications = $form->get_notifications(); | |
| 1184 | + | |
| 1185 | + foreach ( $existing_notifications as $key => $notification ) { | |
| 1186 | + if ( array_key_exists( $key, $data['notifications'] ) ) { | |
| 1187 | + $existing_notifications[ $key ] = $data['notifications'][$key]; | |
| 1188 | + } | |
| 1189 | + } | |
| 1190 | + | |
| 1191 | + update_post_meta( $data['form_id'], 'notifications', $existing_notifications ); | |
| 1192 | + } | |
| 1193 | + | |
| 1194 | + if ( isset( $data['integrations'] ) && !empty( $data['integrations'] ) ) { | |
| 1195 | + $integration_list = weforms()->integrations->get_integration_js_settings(); | |
| 1196 | + $form_integration = $form->get_integrations(); | |
| 1197 | + $integrations = $data['integrations']; | |
| 1198 | + $integrations = array_intersect_key( $integrations, $integration_list ); | |
| 1199 | + | |
| 1200 | + if ( !class_exists( 'WeForms_Pro' ) ) { | |
| 1201 | + $integrations = array_udiff_assoc( $integrations, $integration_list, | |
| 1202 | + function ( $item, $item_list ) { | |
| 1203 | + if ( isset( $item_list['pro'] ) && $item_list['pro'] == true ) { | |
| 1204 | + return 0; | |
| 1205 | + } | |
| 1206 | + | |
| 1207 | + return $item; | |
| 1208 | + } | |
| 1209 | + ); | |
| 1210 | + } | |
| 1211 | + | |
| 1212 | + $new_form_integrations = array_merge( $integrations, array_diff_key( $form->get_integrations(), $integrations ) ); | |
| 1213 | + | |
| 1214 | + update_post_meta( $data['form_id'], 'integrations', $new_form_integrations ); | |
| 1215 | + } | |
| 1216 | + | |
| 1217 | + update_post_meta( $data['form_id'], '_weforms_version', WEFORMS_VERSION ); | |
| 1218 | + | |
| 1219 | + return $saved_wpuf_inputs; | |
| 1220 | + } | |
| 1221 | + | |
| 1222 | + /** | |
| 1223 | + * Import Forms | |
| 1224 | + * | |
| 1225 | + * @since 1.4.2 | |
| 1226 | + * | |
| 1227 | + * @param WP_REST_Request $request | |
| 1228 | + * | |
| 1229 | + * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure | |
| 1230 | + **/ | |
| 1231 | + public function import_forms( $request ) { | |
| 1232 | + $files = $request->get_file_params(); | |
| 1233 | + | |
| 1234 | + if ( !$files ) { | |
| 1235 | + return new WP_Error( 'rest_invalid_file', __( 'No file found to import.', 'weforms' ), [ 'status' => 404] ); | |
| 1236 | + } | |
| 1237 | + | |
| 1238 | + $file_ext = pathinfo( $files['file']['name'], PATHINFO_EXTENSION ); | |
| 1239 | + | |
| 1240 | + if ( !class_exists( 'WeForms_Admin_Tools' ) ) { | |
| 1241 | + require_once WEFORMS_INCLUDES . '/admin/class-admin-tools.php'; | |
| 1242 | + } | |
| 1243 | + | |
| 1244 | + if ( ( $file_ext == 'json' ) && ( $files['file']['size'] < 500000 ) ) { | |
| 1245 | + $status = WeForms_Admin_Tools::import_json_file( $files['file']['tmp_name'] ); | |
| 1246 | + | |
| 1247 | + if ( $status ) { | |
| 1248 | + $response_data = []; | |
| 1249 | + $response_data['message'] = __( 'The forms have been imported successfully!', 'weforms' ); | |
| 1250 | + $response_data['status'] = 200; | |
| 1251 | + | |
| 1252 | + $response = $this->prepare_response_for_collection( $response_data ); | |
| 1253 | + $response = rest_ensure_response( $response ); | |
| 1254 | + | |
| 1255 | + return $response; | |
| 1256 | + } else { | |
| 1257 | + return new WP_Error( 'rest_invalid_file', __( 'Something went wrong importing the file.', 'weforms' ), [ 'status' => 404 ] ); | |
| 1258 | + } | |
| 1259 | + } else { | |
| 1260 | + return new WP_Error( 'rest_invalid_file', __( 'Invalid file or file size too big.', 'weforms' ), [ 'status' => 404 ] ); | |
| 1261 | + } | |
| 1262 | + } | |
| 1263 | + | |
| 1264 | + /** | |
| 1265 | + * Save Form Entry from Frontend | |
| 1266 | + * | |
| 1267 | + * @since 1.4.2 | |
| 1268 | + * | |
| 1269 | + * @param WP_REST_Request $request full details about the request | |
| 1270 | + * | |
| 1271 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure | |
| 1272 | + **/ | |
| 1273 | + public function save_entry( $request ) { | |
| 1274 | + $form_id = $request['form_id']; | |
| 1275 | + $page_id = isset( $request['page_id'] ) ? intval( $request['page_id'] ) : 0; | |
| 1276 | + $form = weforms()->form->get( $form_id ); | |
| 1277 | + $form_settings = $form->get_settings(); | |
| 1278 | + $form_fields = $form->get_fields(); | |
| 1279 | + $entry_fields = $form->prepare_entries( $request ); | |
| 1280 | + $entry_fields = apply_filters( 'weforms_before_entry_submission', $entry_fields, $form, $form_settings, $form_fields ); | |
| 1281 | + | |
| 1282 | + $entry_id = $this->weforms_api_insert_entry( [ | |
| 1283 | + 'form_id' => $form_id, | |
| 1284 | + ], $entry_fields ); | |
| 1285 | + | |
| 1286 | + if ( is_wp_error( $entry_id ) ) { | |
| 1287 | + return new WP_Error( 'rest_invalid_data', $entry_id->get_error_message(), [ 'status' => 404 ] ); | |
| 1288 | + } | |
| 1289 | + | |
| 1290 | + // redirect URL | |
| 1291 | + $show_message = false; | |
| 1292 | + $redirect_to = false; | |
| 1293 | + | |
| 1294 | + if ( $form_settings['redirect_to'] == 'page' ) { | |
| 1295 | + $redirect_to = get_permalink( $form_settings['page_id'] ); | |
| 1296 | + } elseif ( $form_settings['redirect_to'] == 'url' ) { | |
| 1297 | + $redirect_to = $form_settings['url']; | |
| 1298 | + } elseif ( $form_settings['redirect_to'] == 'same' ) { | |
| 1299 | + $show_message = true; | |
| 1300 | + } else { | |
| 1301 | + $redirect_to = get_permalink( $post_id ); | |
| 1302 | + } | |
| 1303 | + | |
| 1304 | + // Fire a hook for integration | |
| 1305 | + do_action( 'weforms_entry_submission', $entry_id, $form_id, $page_id, $form_settings ); | |
| 1306 | + | |
| 1307 | + $field_search = $field_replace = []; | |
| 1308 | + | |
| 1309 | + foreach ( $form_fields as $r_field ) { | |
| 1310 | + $field_search[] = '{' . $r_field['name'] . '}'; | |
| 1311 | + | |
| 1312 | + if ( $r_field['template'] == 'name_field' ) { | |
| 1313 | + $field_replace[] = implode( ' ', explode( '|', $entry_fields[$r_field['name']] ) ); | |
| 1314 | + } else { | |
| 1315 | + $field_replace[] = isset( $entry_fields[$r_field['name']] ) ? $entry_fields[$r_field['name']] : ''; | |
| 1316 | + } | |
| 1317 | + } | |
| 1318 | + | |
| 1319 | + $message = str_replace( $field_search, $field_replace, $form_settings['message'] ); | |
| 1320 | + | |
| 1321 | + $notification = new WeForms_Notification( [ | |
| 1322 | + 'form_id' => $form_id, | |
| 1323 | + 'page_id' => $page_id, | |
| 1324 | + 'entry_id' => $entry_id, | |
| 1325 | + ] ); | |
| 1326 | + | |
| 1327 | + $notification->send_notifications(); | |
| 1328 | + | |
| 1329 | + $entry_data = weforms_get_entry_data( $entry_id ); | |
| 1330 | + | |
| 1331 | + $response_data = []; | |
| 1332 | + $response_data['id'] = $entry_id; | |
| 1333 | + $response_data['message'] = 'Entries Created Successfully'; | |
| 1334 | + $response_data['success'] = true; | |
| 1335 | + $response_data['redirect_to'] = $redirect_to; | |
| 1336 | + $response_data['show_message'] = $show_message; | |
| 1337 | + $response_data['message'] = $message; | |
| 1338 | + $response_data['data'] = $entry_data['data']; | |
| 1339 | + $response_data['form_id'] = $form_id; | |
| 1340 | + $response_data['entry_id'] = $entry_id; | |
| 1341 | + | |
| 1342 | + $response = apply_filters( 'weforms_entry_submission_response', $response_data ); | |
| 1343 | + $response = $this->prepare_response_for_collection( $response_data ); | |
| 1344 | + $response = rest_ensure_response( $response ); | |
| 1345 | + | |
| 1346 | + return $response; | |
| 1347 | + } | |
| 1348 | + | |
| 1349 | + /** | |
| 1350 | + * Export Form Entries | |
| 1351 | + * | |
| 1352 | + **/ | |
| 1353 | + public function export_form_entries( $request ) { | |
| 1354 | + $form_id = $request['form_id']; | |
| 1355 | + | |
| 1356 | + if ( !$form_id ) { | |
| 1357 | + return new WP_Error( 'rest_weforms_invalid_form', __( 'Form id not exists', 'weforms' ) ); | |
| 1358 | + } | |
| 1359 | + | |
| 1360 | + $entry_array = []; | |
| 1361 | + $columns = weforms_get_entry_columns( $form_id, false ); | |
| 1362 | + $total_entries = weforms_count_form_entries( $form_id ); | |
| 1363 | + $entries = weforms_get_form_entries( $form_id, [ | |
| 1364 | + 'number' => $total_entries, | |
| 1365 | + 'offset' => 0, | |
| 1366 | + ] ); | |
| 1367 | + $extra_columns = [ | |
| 1368 | + 'ip_address' => __( 'IP Address', 'weforms' ), | |
| 1369 | + 'created_at' => __( 'Date', 'weforms' ), | |
| 1370 | + ]; | |
| 1371 | + | |
| 1372 | + $columns = array_merge( [ 'id' => 'Entry ID' ], $columns, $extra_columns ); | |
| 1373 | + | |
| 1374 | + foreach ( $entries as $entry ) { | |
| 1375 | + $temp = []; | |
| 1376 | + | |
| 1377 | + foreach ( $columns as $column_id => $label ) { | |
| 1378 | + switch ( $column_id ) { | |
| 1379 | + case 'id': | |
| 1380 | + $temp[ $column_id ] = $entry->id; | |
| 1381 | + break; | |
| 1382 | + | |
| 1383 | + case 'ip_address': | |
| 1384 | + $temp[ $column_id ] = $entry->ip_address; | |
| 1385 | + break; | |
| 1386 | + | |
| 1387 | + case 'created_at': | |
| 1388 | + $temp[ $column_id ] = $entry->created_at; | |
| 1389 | + break; | |
| 1390 | + | |
| 1391 | + default: | |
| 1392 | + $value = weforms_get_entry_meta( $entry->id, $column_id, true ); | |
| 1393 | + $value = weforms_get_pain_text( $value ); | |
| 1394 | + $temp[ $column_id ] = str_replace( WeForms::$field_separator, ' ', $value ); | |
| 1395 | + break; | |
| 1396 | + } | |
| 1397 | + } | |
| 1398 | + | |
| 1399 | + $entry_array[] = $temp; | |
| 1400 | + } | |
| 1401 | + | |
| 1402 | + error_reporting( 0 ); | |
| 1403 | + | |
| 1404 | + if ( ob_get_contents() ) { | |
| 1405 | + ob_clean(); | |
| 1406 | + } | |
| 1407 | + | |
| 1408 | + $blogname = sanitize_title( strtolower( str_replace( ' ', '-', get_option( 'blogname' ) ) ) ); | |
| 1409 | + $file_name = $blogname . '-weforms-entries-' . time() . '.csv'; | |
| 1410 | + | |
| 1411 | + // force download | |
| 1412 | + header( 'Content-Type: application/force-download' ); | |
| 1413 | + header( 'Content-Type: application/octet-stream' ); | |
| 1414 | + header( 'Content-Type: application/download' ); | |
| 1415 | + | |
| 1416 | + // disposition / encoding on response body | |
| 1417 | + header( "Content-Disposition: attachment;filename={$file_name}" ); | |
| 1418 | + header( 'Content-Transfer-Encoding: binary' ); | |
| 1419 | + | |
| 1420 | + $handle = fopen( 'php://output', 'w' ); | |
| 1421 | + | |
| 1422 | + //handle UTF-8 chars conversion for CSV | |
| 1423 | + fprintf( $handle, chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ) ); | |
| 1424 | + | |
| 1425 | + // put the column headers | |
| 1426 | + fputcsv( $handle, array_values( $columns ) ); | |
| 1427 | + | |
| 1428 | + // put the entry values | |
| 1429 | + foreach ( $entry_array as $row ) { | |
| 1430 | + fputcsv( $handle, $row ); | |
| 1431 | + } | |
| 1432 | + | |
| 1433 | + fclose( $handle ); | |
| 1434 | + | |
| 1435 | + exit; | |
| 1436 | + } | |
| 1437 | + | |
| 1438 | + public function submit_permissions_check( $request ) { | |
| 1439 | + if ( !is_weforms_api_allowed_guest_submission() ) { | |
| 1440 | + return new WP_Error( 'rest_cannot_submit_entry', __( 'Sorry, you have no permission to submit this form.', 'weforms' ), [ 'status' => rest_authorization_required_code() ] ); | |
| 1441 | + } | |
| 1442 | + | |
| 1443 | + $form = weforms()->form->get( (int) $request['form_id'] ); | |
| 1444 | + $form_is_open = $form->is_submission_open(); | |
| 1445 | + | |
| 1446 | + if ( is_wp_error( $form_is_open ) ) { | |
| 1447 | + return new WP_Error( 'rest_weforms_form_permission', $form_is_open->get_error_message(), [ 'status' => 404 ] ); | |
| 1448 | + } | |
| 1449 | + | |
| 1450 | + $form_validations = $this->save_check( $request ); | |
| 1451 | + | |
| 1452 | + if ( is_wp_error( $form_validations ) ) { | |
| 1453 | + $form_message = []; | |
| 1454 | + | |
| 1455 | + foreach ( $form_validations->get_error_messages() as $error ) { | |
| 1456 | + $form_message[] = $error; | |
| 1457 | + } | |
| 1458 | + | |
| 1459 | + return new WP_Error( 'error', $form_message, [ 'status' => 404 ] ); | |
| 1460 | + } | |
| 1461 | + | |
| 1462 | + return true; | |
| 1463 | + } | |
| 1464 | + | |
| 1465 | + /** | |
| 1466 | + * Get Form Entries | |
| 1467 | + * | |
| 1468 | + * @since 1.4.2 | |
| 1469 | + * | |
| 1470 | + * @param WP_REST_Request $request | |
| 1471 | + * | |
| 1472 | + * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure | |
| 1473 | + */ | |
| 1474 | + public function get_entry( $request ) { | |
| 1475 | + $form_id = isset( $request['form_id'] ) ? intval( $request['form_id'] ) : 0; | |
| 1476 | + $current_page = isset( $request['page'] ) ? intval( $request['page'] ) : 1; | |
| 1477 | + $per_page = isset( $request['per_page'] ) ? intval( $request['per_page'] ) : 10; | |
| 1478 | + $status = isset( $request['status'] ) ? $request['status'] : 'publish'; | |
| 1479 | + $offset = ( $current_page - 1 ) * $per_page; | |
| 1480 | + | |
| 1481 | + if ( !$form_id ) { | |
| 1482 | + return new WP_Error( 'rest_invalid_form', __( 'Please provide a form id', 'weforms' ), [ 'status' => 404 ] ); | |
| 1483 | + } | |
| 1484 | + | |
| 1485 | + $entries = weforms_get_form_entries( | |
| 1486 | + $form_id, [ | |
| 1487 | + 'number' => $per_page, | |
| 1488 | + 'offset' => $offset, | |
| 1489 | + 'status' => $status, | |
| 1490 | + ] | |
| 1491 | + ); | |
| 1492 | + | |
| 1493 | + $columns = weforms_get_entry_columns( $form_id ); | |
| 1494 | + $total_entries = weforms_count_form_entries( $form_id, $status ); | |
| 1495 | + | |
| 1496 | + array_map( | |
| 1497 | + function ( $entry ) use ( $columns,$form_id ) { | |
| 1498 | + $entry_id = $entry->id; | |
| 1499 | + $entry->fields = []; | |
| 1500 | + $entry->links = rest_url( sprintf( '%s/%s/%d/%s/%d', $this->namespace, $this->rest_base, $form_id, 'entries', $entry->id ) ); | |
| 1501 | + | |
| 1502 | + foreach ( $columns as $meta_key => $label ) { | |
| 1503 | + $value = weforms_get_entry_meta( $entry_id, $meta_key, true ); | |
| 1504 | + $entry->fields[ $meta_key ] = str_replace( WeForms::$field_separator, ' ', $value ); | |
| 1505 | + } | |
| 1506 | + }, $entries | |
| 1507 | + ); | |
| 1508 | + | |
| 1509 | + $entries = apply_filters( 'weforms_get_entries', $entries, $form_id ); | |
| 1510 | + | |
| 1511 | + $response = $entries; | |
| 1512 | + $max_pages = ceil( $total_entries / $per_page ); | |
| 1513 | + $response = $this->prepare_response_for_collection( $response ); | |
| 1514 | + $response = rest_ensure_response( $response ); | |
| 1515 | + | |
| 1516 | + $response->header( 'X-WP-Total', (int) $total_entries ); | |
| 1517 | + $response->header( 'X-WP-TotalPages', (int) $max_pages ); | |
| 1518 | + | |
| 1519 | + return $response; | |
| 1520 | + } | |
| 1521 | + | |
| 1522 | + /** | |
| 1523 | + * Get Single Entry Details | |
| 1524 | + * | |
| 1525 | + * @since 1.4.2 | |
| 1526 | + * | |
| 1527 | + * @param WP_REST_Request $request full details about the request | |
| 1528 | + * | |
| 1529 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure | |
| 1530 | + **/ | |
| 1531 | + public function get_entry_details( $request ) { | |
| 1532 | + $form_id = $request['form_id']; | |
| 1533 | + $entry_id = $request['entry_id']; | |
| 1534 | + $form = weforms()->form->get( $form_id ); | |
| 1535 | + $form_settings = $form->get_settings(); | |
| 1536 | + $entry = $form->entries()->get( $entry_id ); | |
| 1537 | + $fields = $entry->get_fields(); | |
| 1538 | + | |
| 1539 | + $fields_data = array_map( function ( $field ) { | |
| 1540 | + return [ | |
| 1541 | + 'name' => $field['name'], | |
| 1542 | + 'label' => $field['label'], | |
| 1543 | + 'value' => $field['value'], | |
| 1544 | + ]; | |
| 1545 | + }, $fields ); | |
| 1546 | + | |
| 1547 | + $metadata = $entry->get_metadata(); | |
| 1548 | + $payment = $entry->get_payment_data(); | |
| 1549 | + | |
| 1550 | + if ( isset( $payment->payment_data ) && is_serialized( $payment->payment_data ) ) { | |
| 1551 | + $payment->payment_data = unserialize( $payment->payment_data ); | |
| 1552 | + } | |
| 1553 | + | |
| 1554 | + $has_empty = false; | |
| 1555 | + $answers = []; | |
| 1556 | + $respondentPoints = isset( $form_settings['total_points'] ) ? floatval( $form_settings['total_points'] ) : 0; | |
| 1557 | + | |
| 1558 | + foreach ( $fields as $key => $field ) { | |
| 1559 | + if ( $form_settings['quiz_form'] == 'yes' ) { | |
| 1560 | + $selectedAnswers = isset( $field['selected_answers'] ) ? $field['selected_answers'] : ''; | |
| 1561 | + $givenAnswer = isset( $field['value'] ) ? $field['value'] : ''; | |
| 1562 | + $options = isset( $field['options'] ) ? $field['options'] : ''; | |
| 1563 | + $template = $field['template']; | |
| 1564 | + $fieldPoints = isset( $field['points'] ) ? floatval( $field['points'] ) : 0; | |
| 1565 | + | |
| 1566 | + if ( $template == 'radio_field' || $template == 'dropdown_field' ) { | |
| 1567 | + $answers[$field['name']] = true; | |
| 1568 | + | |
| 1569 | + if ( empty( $givenAnswer ) ) { | |
| 1570 | + $answers[$field['name']] = false; | |
| 1571 | + $respondentPoints -= $fieldPoints; | |
| 1572 | + } else { | |
| 1573 | + foreach ( $options as $key => $value ) { | |
| 1574 | + if ( $givenAnswer == $value ) { | |
| 1575 | + if ( $key != $selectedAnswers ) { | |
| 1576 | + $answers[$field['name']] = false; | |
| 1577 | + $respondentPoints -= $fieldPoints; | |
| 1578 | + } | |
| 1579 | + } | |
| 1580 | + } | |
| 1581 | + } | |
| 1582 | + } elseif ( $template == 'checkbox_field' || $template == 'multiple_select' ) { | |
| 1583 | + $answers[$field['name']] = true; | |
| 1584 | + $userAnswer = []; | |
| 1585 | + | |
| 1586 | + foreach ( $options as $key => $value ) { | |
| 1587 | + foreach ( $givenAnswer as $answer ) { | |
| 1588 | + if ( $value == $answer ) { | |
| 1589 | + $userAnswer[] = $key; | |
| 1590 | + } | |
| 1591 | + } | |
| 1592 | + } | |
| 1593 | + | |
| 1594 | + $userAnswer = implode( '|', $userAnswer ); | |
| 1595 | + $rightAnswers = implode( '|', $selectedAnswers ); | |
| 1596 | + | |
| 1597 | + if ( $userAnswer != $rightAnswers || empty( $userAnswer ) ) { | |
| 1598 | + $answers[$field['name']] = false; | |
| 1599 | + $respondentPoints -= $fieldPoints; | |
| 1600 | + } | |
| 1601 | + } | |
| 1602 | + } elseif ( empty( $field['value'] ) ) { | |
| 1603 | + $has_empty = true; | |
| 1604 | + break; | |
| 1605 | + } | |
| 1606 | + } | |
| 1607 | + | |
| 1608 | + $response = [ | |
| 1609 | + 'fields' => $fields_data, | |
| 1610 | + 'meta_data' => $metadata, | |
| 1611 | + 'payment_data' => $payment, | |
| 1612 | + 'has_empty' => $has_empty, | |
| 1613 | + 'respondent_points' => $respondentPoints, | |
| 1614 | + 'answers' => $answers, | |
| 1615 | + ]; | |
| 1616 | + | |
| 1617 | + $response = $this->prepare_response_for_collection( $response ); | |
| 1618 | + $response = rest_ensure_response( $response ); | |
| 1619 | + $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d/%s/%d', $this->namespace, $this->rest_base, $form->id, 'entries', $entry->id ) ) ); | |
| 1620 | + | |
| 1621 | + return $response; | |
| 1622 | + } | |
| 1623 | + | |
| 1624 | + /** | |
| 1625 | + * Export forms to JSON | |
| 1626 | + * | |
| 1627 | + * @param WP_REST_Request $request | |
| 1628 | + * | |
| 1629 | + * @return json | |
| 1630 | + **/ | |
| 1631 | + public function export_forms( $request ) { | |
| 1632 | + $export_type = isset( $request['type'] ) ? $request['type'] : 'selected'; | |
| 1633 | + $selected = isset( $request['ids'] ) ? array_map( 'absint', $request['ids'] ) : []; | |
| 1634 | + | |
| 1635 | + if ( !class_exists( 'WeForms_Admin_Tools' ) ) { | |
| 1636 | + require_once WEFORMS_INCLUDES . '/admin/class-admin-tools.php'; | |
| 1637 | + } | |
| 1638 | + | |
| 1639 | + switch ( $export_type ) { | |
| 1640 | + case 'all': | |
| 1641 | + WeForms_Admin_Tools::export_to_json(); | |
| 1642 | + break; | |
| 1643 | + | |
| 1644 | + case 'selected': | |
| 1645 | + WeForms_Admin_Tools::export_to_json( $selected ); | |
| 1646 | + break; | |
| 1647 | + } | |
| 1648 | + } | |
| 1649 | +} | |