Submissions.php
31.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
<?php if ( ! defined( 'ABSPATH' ) ) exit;
use NinjaForms\Includes\Admin\Processes\DeleteBatchFile;
use NinjaForms\Includes\Contracts\SubmissionHandler;
use NinjaForms\Includes\Entities\SingleSubmission;
use NinjaForms\Includes\Entities\SubmissionExtraHandlerResponse;
use NinjaForms\Includes\Factories\SubmissionAggregateFactory;
use NinjaForms\Includes\Factories\SubmissionFilterFactory;
/**
* Class NF_Routes_SubmissionsActions
*/
final class NF_Routes_Submissions extends NF_Abstracts_Routes
{
/**
* Register REST API routes related to submissions actions
*
* @since 3.4.33
*
* @route "ninja-forms-submissions/export"
* @route 'ninja-forms-submissions/email-action"
*/
function register_routes() {
register_rest_route('ninja-forms-submissions', 'submissions/get', array(
'methods' => 'GET',
'args' => [
'form_ids' => [
'required' => true,
'description' => esc_attr__('Form IDs', 'ninja-forms'),
'type' => 'array',
'validate_callback' => 'rest_validate_request_arg',
]
],
'callback' => [ $this, 'get_submissions' ],
'permission_callback' => [ $this, 'get_submissions_permission_callback' ]
));
register_rest_route('ninja-forms-submissions', 'submissions/delete', array(
'methods' => 'POST',
'args' => [
'submissions' => [
'required' => true,
'description' => esc_attr__('Submissions', 'ninja-forms'),
'type' => 'array',
'validate_callback' => 'rest_validate_request_arg',
]
],
'callback' => [ $this, 'delete_submissions' ],
'permission_callback' => [ $this, 'delete_submissions_permission_callback' ]
));
register_rest_route('ninja-forms-submissions', 'submissions/update', array(
'methods' => 'POST',
'args' => [
'singleSubmission' => [
'required' => true,
'description' => esc_attr__('Update single Submission', 'ninja-forms'),
'type' => 'JSON encoded array',
'validate_callback' => 'rest_validate_request_arg',
]
],
'callback' => [ $this, 'update_submission' ],
'permission_callback' => [ $this, 'update_submission_permission_callback' ]
));
register_rest_route('ninja-forms-submissions', 'submissions/restore', array(
'methods' => 'POST',
'args' => [
'restore' => [
'required' => true,
'description' => esc_attr__('Update Submission', 'ninja-forms'),
'type' => 'boolean',
'validate_callback' => 'rest_validate_request_arg',
],
'submissions' => [
'required' => true,
'description' => esc_attr__('Array of Submissions', 'ninja-forms'),
'type' => 'array',
'validate_callback' => 'rest_validate_request_arg',
]
],
'callback' => [ $this, 'restore_submissions' ],
'permission_callback' => [ $this, 'update_submission_permission_callback' ]
));
register_rest_route('ninja-forms-submissions', 'submissions/handle-extra', array(
'methods' => 'POST',
'args' => [
'singleSubmission' => [
'required' => true,
'description' => esc_attr__('Update Submission', 'ninja-forms'),
'type' => 'JSON encoded array',
'validate_callback' => 'rest_validate_request_arg',
],
'handleExtra' => [
'required' => true,
'description' => esc_attr__('Extra Handler of Submission', 'ninja-forms'),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
]
],
'callback' => [ $this, 'handle_extra_submission' ],
'permission_callback' => [ $this, 'handle_extra_submission_permission_callback' ]
));
register_rest_route('ninja-forms-submissions', 'export', array(
'methods' => 'POST',
'args' => [
'form_ids' => [
'required' => false,
'description' => esc_attr__('Array of Form IDs we want to get the submissions from.', 'ninja-forms'),
'type' => 'array',
'validate_callback' => 'rest_validate_request_arg',
],
'start_date' => [
'required' => false,
'description' => esc_attr__('strtotime($date) that represents the start date we will retrieve submssions at.', 'ninja-forms'),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
],
'end_date' => [
'required' => false,
'description' => esc_attr__('strtotime($date) that represents the end date we will retrieve submssions at.', 'ninja-forms'),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
],
'singleSubmission' => [
'required' => false,
'description' => esc_attr__('Export single submission if a JSON encoded submission is passed', 'ninja-forms'),
'type' => 'JSON encoded array',
'validate_callback' => 'rest_validate_request_arg',
],
'submissions' => [
'required' => false,
'description' => esc_attr__('Export submissions based on array of submissions IDs', 'ninja-forms'),
'type' => 'array',
'validate_callback' => 'rest_validate_request_arg',
]
],
'callback' => [ $this, 'bulk_export_submissions' ],
'permission_callback' => [ $this, 'get_submissions_permission_callback' ],
));
register_rest_route('ninja-forms-submissions', 'download-all', array(
'methods' => 'POST',
'args' => [
'form_ids' => [
'required' => false,
'description' => esc_attr__('Array of Form IDs we want to get the submissions from.', 'ninja-forms'),
'type' => 'array',
'validate_callback' => 'rest_validate_request_arg',
],
],
'callback' => [ $this, 'download_all_submissions' ],
'permission_callback' => [ $this, 'get_submissions_permission_callback' ],
));
/**
* Delete the temp file created from the `download-all` request
*/
register_rest_route('ninja-forms-submissions', 'delete-download-file', array(
'methods' => 'POST',
'args' => [
'file_path' => [
'required' => true,
'description' => esc_attr__('File path of the file to delete', 'ninja-forms'),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
],
],
'callback' => [ $this, 'delete_download_file' ],
// Uses the same permissions as the `download-all` request
'permission_callback' => [ $this, 'get_submissions_permission_callback' ],
));
register_rest_route('ninja-forms-submissions', 'set-submissions-settings', array(
'methods' => 'POST',
'args' => [
'settingName' => [
'required' => true,
'description' => esc_attr__('Setting name in the submissionsSettings array', 'ninja-forms'),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
],
'data' => [
'required' => false,
'description' => esc_attr__('Settings data', 'ninja-forms'),
'type' => 'object',
'validate_callback' => 'rest_validate_request_arg',
],
'formID' => [
'required' => false,
'description' => esc_attr__('Form ID of the setting saved', 'ninja-forms'),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
]
],
'callback' => [ $this, 'set_submissions_settings' ],
'permission_callback' => [ $this, 'get_submissions_permission_callback' ],
));
register_rest_route('ninja-forms-submissions', 'get-submissions-settings', array(
'methods' => 'GET',
'callback' => [ $this, 'get_submissions_settings' ],
'permission_callback' => [ $this, 'get_submissions_permission_callback' ],
));
register_rest_route('ninja-forms-submissions', 'email-action', array(
'methods' => 'POST',
'args' => [
'submission' => [
'required' => true,
'description' => esc_attr__('Submission ID', 'ninja-forms'),
'type' => 'int',
'validate_callback' => 'rest_validate_request_arg',
],
'action_settings' => [
'required' => true,
'description' => esc_attr__('Email Action Settings', 'ninja-forms'),
'type' => 'object',
'validate_callback' => 'rest_validate_request_arg',
],
],
'callback' => [ $this, 'trigger_email_action' ],
'permission_callback' => [ $this, 'permission_callback' ],
));
}
/**
* Secure endpoint to allow users to read submissions
*
* @since 3.5.8
*
* Already passed Nonce validation via wp_rest and x_wp_nonce header checked against rest_cookie_check_errors()
*/
public function get_submissions_permission_callback(WP_REST_Request $request) {
//Set default to false
$allowed = false;
// Allow only admin to export personally identifiable data
$permissionLevel = 'manage_options';
$allowed= \current_user_can($permissionLevel);
/**
* Filter permissions for Reading Submissions
*
* @param bool $allowed Is request authorized?
* @param WP_REST_Request $request The current request
*/
return apply_filters( 'ninja_forms_api_allow_get_submissions', $allowed, $request );
}
/**
* Secure endpoint to allow users to delete submissions
*
* @since 3.5.8
*
* Already passed Nonce validation via wp_rest and x_wp_nonce header checked against rest_cookie_check_errors()
*/
public function delete_submissions_permission_callback(WP_REST_Request $request) {
//Set default to false
$allowed = false;
// Allow only admin to export personally identifiable data
$permissionLevel = 'manage_options';
$allowed= \current_user_can($permissionLevel);
/**
* Filter permissions for Reading Submissions
*
* @param bool $allowed Is request authorized?
* @param WP_REST_Request $request The current request
*/
return apply_filters( 'ninja_forms_api_allow_delete_submissions', $allowed, $request );
}
/**
* Secure endpoint to allow users to update a submission
*
* @since 3.5.8
*
* Already passed Nonce validation via wp_rest and x_wp_nonce header checked against rest_cookie_check_errors()
*/
public function update_submission_permission_callback(WP_REST_Request $request) {
//Set default to false
$allowed = false;
// Allow only admin to export personally identifiable data
$permissionLevel = 'manage_options';
$allowed= \current_user_can($permissionLevel);
/**
* Filter permissions for updating a submission
*
* @param bool $allowed Is request authorized?
* @param WP_REST_Request $request The current request
*/
return apply_filters( 'ninja_forms_api_allow_update_submission', $allowed, $request );
}
/**
* Secure endpoint to allow users to perform extra handling
*
* @since 3.5.8
*
* Already passed Nonce validation via wp_rest and x_wp_nonce header checked against rest_cookie_check_errors()
*/
public function handle_extra_submission_permission_callback(WP_REST_Request $request) {
//Set default to false
$allowed = false;
// Allow only admin to export personally identifiable data
$permissionLevel = 'manage_options';
$allowed= \current_user_can($permissionLevel);
/**
* Filter permissions for updating a submission
*
* @param bool $allowed Is request authorized?
* @param WP_REST_Request $request The current request
*/
return apply_filters( 'ninja_forms_api_allow_handle_extra_submission', $allowed, $request );
}
/**
* Secure endpoint to allowed users
*
* Security disclosure regarding <=3.5.7 showed that any logged in user
* could export form data, possibly exposing personally identifiable
* information. Permissions changed such that only admin can export
* submission data; a filter enables one to override that permission if
* desired.
*
* @since 3.4.33
*
* Already passed Nonce validation via wp_rest and x_wp_nonce header checked
* against rest_cookie_check_errors()
*/
public function permission_callback(WP_REST_Request $request) {
//Set default to false
$allowed = false;
// Allow only admin to export personally identifiable data
$permissionLevel = 'manage_options';
$allowed= \current_user_can($permissionLevel);
/**
* Filter permissions for Triggering Email Actions
*
* @param bool $allowed Is request authorized?
* @param WP_REST_Request $request The current request
*/
return apply_filters( 'ninja_forms_api_allow_email_action', $allowed, $request );
}
/**
* Bulk_export_submissions
*
* @since 3.4.33
*
* @return array of CSVs by form
*/
public function bulk_export_submissions(WP_REST_Request $request)
{
//Gather data from the request
$data = json_decode($request->get_body());
//TODO organize queries to work with a single method instead of defining a method depending on the data received
if (!empty($data->form_ids) && !empty($data->start_date) && !empty($data->end_date)) { // export by date from multiple forms
$form_ids = explode(",", $data->form_ids);
$start_date = $data->start_date;
$end_date = $data->end_date;
$requestType = 'filterByDates';
} elseif (!empty($data->singleSubmission)) { // export a single submission
$singleSubmissionJson = $data->singleSubmission;
$requestType = 'getSingleSubmission';
} else if (!empty($data->submissions) && !empty($data->form_ids)) { // export multiple submissions from a single form
$form_ids = explode(",", $data->form_ids);
$submissionsIDs = $data->submissions;
$requestType = 'getSubmissions';
} else {
return new WP_Error('malformed_request', __('This request is missing data', 'ninja-forms'));
}
if ('filterByDates' === $requestType) {
foreach ($form_ids as $formId) {
$params = (new SubmissionFilterFactory())->maybeLimitByLoggedInUser()
->setStartDate($start_date)
->setEndDate($end_date)
->setNfFormIds([$formId])
->setStatus(['active', 'publish']);
// construct aggregate within CSV adapter, applying filter to aggregate
$submissionAggregateCsvExportAdapter = (new SubmissionAggregateFactory())->SubmissionAggregateCsvExportAdapter();
$submissionAggregateCsvExportAdapter->submissionAggregate->filterSubmissions($params);
$csvObject = (new NF_Exports_SubmissionCsvExport())->setUseAdminLabels(true)
->setSubmissionAggregateCsvExportAdapter($submissionAggregateCsvExportAdapter);
$csv[$formId] = $csvObject->handle();
}
} elseif ('getSingleSubmission' === $requestType) {
$singleSubmission = SingleSubmission::fromArray(json_decode($singleSubmissionJson, true));
$formId = $singleSubmission->getFormId();
// construct aggregate within CSV adapter, applying filter to aggregate
$submissionAggregateCsvExportAdapter = (new SubmissionAggregateFactory())->SubmissionAggregateCsvExportAdapter();
$submissionAggregateCsvExportAdapter->submissionAggregate->requestSingleSubmission($singleSubmission);
$csvObject = (new NF_Exports_SubmissionCsvExport())->setUseAdminLabels(true)
->setSubmissionAggregateCsvExportAdapter($submissionAggregateCsvExportAdapter);
$csv[$formId] = $csvObject->handle();
} elseif ('getSubmissions' === $requestType) {
$params = (new SubmissionFilterFactory())->maybeLimitByLoggedInUser()
->setNfFormIds($form_ids)
->setStartDate(0)
->setEndDate(time())
->setSubmissionsIDs($submissionsIDs);
$submissionAggregateCsvExportAdapter = (new SubmissionAggregateFactory())->SubmissionAggregateCsvExportAdapter();
$submissionAggregateCsvExportAdapter->submissionAggregate->filterSubmissions($params);
$csvObject = (new NF_Exports_SubmissionCsvExport())->setUseAdminLabels(true)->setSubmissionAggregateCsvExportAdapter($submissionAggregateCsvExportAdapter);
$csv[$form_ids[0]] = $csvObject->handle();
}
// Return CSV objects
return $csv;
}
/**
* Download all submissions
*
*
* @return array
*/
public function download_all_submissions(WP_REST_Request $request)
{
//Gather data from the request
$data = json_decode($request->get_body());
if (!empty($data->form_ids)) { // download all submissions
$form_ids = explode(",", $data->form_ids);
$formId = $form_ids[0];
} else {
return new WP_Error('malformed_request', __('This request is missing data', 'ninja-forms'));
}
$export = (new NF_Admin_Processes_ExportSubmissions($formId));
$downloadUrl = $export->getFileUrl();
$filePath = $export->getFilePath();
$response =[
'formId'=>$formId,
'downloadUrl'=>$downloadUrl,
'filePath'=>$filePath
];
return $response;
}
/**
* Delete a file when provided valid file path
*
* @param WP_REST_Request $request Object with `file_path` property
* @return void
*/
public function delete_download_file(WP_REST_Request $request)
{
//Gather data from the request
$data = json_decode($request->get_body());
if(!empty($data->file_path)){
$filePath = (string)$data->file_path;
$deleteFile = (new DeleteBatchFile())->delete($filePath);
$response=[
'result'=>$deleteFile
];
return $response;
}else{
return new WP_Error('malformed_request', __('This request is missing data for method delete_download_file', 'ninja-forms'));
}
}
/**
* Trigger Email Action endpoint callback
*
* @since 3.4.33
*
* @return bool|int depending on the value returned by wp_mail
*/
public function trigger_email_action(WP_REST_Request $request) {
//Extract required data
$data = json_decode($request->get_body());
$form = Ninja_Forms()->form( $data->formID );
$sub = $form->get_sub( $data->submission );
$field_values = $sub->get_field_values();
//Throw error if we're missing data
if( !isset($data) || empty($form) || empty($sub) ) {
return new WP_Error( 'malformed_request', __('This request is missing data', 'ninja-forms') );
}
//Process Merge tags
$action_settings = $this->process_merge_tags( $data->action_settings, $data->formID, $sub );
//Process Email Action
$email_action = new NF_Actions_Email();
$result = $email_action->process( (array) $action_settings, $data->formID, (array) $field_values );
//Return true if wp_mail returned true or the submission ID if it failed.
$return = !empty($result['actions']['email']['sent']) && true === $result['actions']['email']['sent'] ? $result['actions']['email']['sent'] : $sub->get_seq_num();
return $return;
}
/**
* Process Merge tags for a given Value
*
* @since 3.4.33
*
* @return object of Email Action Model with merge tags settingsprocessed
*
*/
public function process_merge_tags( $data, $form_id, $sub) {
// Init Field Merge Tags.
$fields_merge_tag_object = Ninja_Forms()->merge_tags[ 'fields' ];
$fields_merge_tag_object->set_form_id($form_id);
//Process Fields Merge Tags
$fields = Ninja_Forms()->form( $form_id )->get_fields();
$fields = new NF_Adapters_SubmissionsSubmission( $fields, $form_id, $sub );
foreach( $fields as $field_id => $field){
$fields_merge_tag_object->add_field( $field );
}
//Add All Fields merge tags
$fields_merge_tag_object->include_all_fields_merge_tags();
//include fields to the {all_fields_table} and {fields_table} mrerge tags
foreach( $fields as $field_id => $field){
$fields_merge_tag_object->add_field( $field );
}
//Loop through Action settings and apply merge tags
$array_data = (array) $data;
foreach( $array_data as $ind => $value ){
if( !empty($value) && is_string($value) ){
//Merge tag
$data->$ind = apply_filters( 'ninja_forms_merge_tags', $value );
}
}
return $data;
}
/**
* Get Submissions
*
* @since 3.5.8
*
* @return array of submissions for a Form
*/
public function get_submissions(WP_REST_Request $request) {
//Gather data from the request
if( empty( $_GET["type"] ) || empty( $_GET["form_ids"] )){
return new WP_Error( 'malformed_request', __('This request is missing data', 'ninja-forms') );
} else {
//Set Usefull data
$type = $_GET["type"];
$form_ids = $_GET["form_ids"];
$page_size = !empty( $_GET["page_size"] ) ? $_GET["page_size"] : "10";
$current_page = !empty( $_GET["current_page"] ) ? $_GET["current_page"] : "1";
$start_date = $_GET["start_date"] != 0 ? $_GET["start_date"] : 378687600;
// If no end date is specified, then use current time plus one day
// to ensure it goes beyond all time zones
$end_date = $_GET["end_date"] != 0 ? $_GET["end_date"] : time()+(60*60*24);
$search_term = !empty($_GET["search_term"]) ? $_GET["search_term"] : null;
$status = !empty($_GET["status"]) && $_GET["status"] === "trash" ? ["trash"] : ['active', 'publish'];
}
//Get aggregated submissions
$params = (new SubmissionFilterFactory())->maybeLimitByLoggedInUser()->setNfFormIds([$form_ids]);
if(!empty($start_date) && !empty($end_date)){
$params->setStartDate($start_date);
$params->setEndDate($end_date);
}
if($search_term){
$params->setSearchString( $search_term );
}
$params->setStatus( $status );
$submissionAggregate = (new SubmissionAggregateFactory())->submissionAggregate();
$filteredSubmissions = $submissionAggregate->filterSubmissions( $params );
//Get values of needed submissions
if( $type === "columns" ){
$response = $submissionAggregate->getFieldDefinitionCollection();
} else if( $type === "data" ){
$response = [];
$aggregatedSubmissions = $submissionAggregate->getAggregatedSubmissions();
$offset = ($current_page - 1) * $page_size;
$submissions_needed = array_slice( $aggregatedSubmissions, $offset, $page_size );
foreach( $submissions_needed as $key => $params ){
$response['data'][$key] = $submissionAggregate->getSubmissionValuesByAggregatedKey( $key );
}
$response['count'] = $submissionAggregate->getSubmissionCount();
}
// Return submissions data from request
return rest_ensure_response( $response );
}
/**
* Save submissions interface settings
*
* Data passes a json string
*
* @param WP_REST_Request $request
* @return void
*/
public function set_submissions_settings(WP_REST_Request $request){
//Extract required data
$data = $request->get_json_params();
$setting = $data['settingName'];
$new_data = $data['data'];
$form_id = $data['formID'];
//Get data stored and create the new value for the correct setting
$option = get_option( 'ninja_forms_submissions_settings' );
$current_setting_value = $option[$form_id][$setting];
$updated_option = $option;
$updated_option[$form_id][$setting] = $new_data;
$response = (object)[];
if ( false !== $option ) {
// option exist
if ( $current_setting_value === $new_data ) {
$response->message = "unchanged";
$response->status = false;
} else {
$response->message = "update_option";
$response->status = update_option( 'ninja_forms_submissions_settings', $updated_option );
}
} else {
// option don't exist
$response->message = "add_option";
$response->status = add_option( 'ninja_forms_submissions_settings', $updated_option );
}
return rest_ensure_response( json_encode( $response ) );
}
/**
* Get submissions interface settings
*
* @param WP_REST_Request $request
* @return array of settings
*/
public function get_submissions_settings(WP_REST_Request $request){
$settings = get_option( 'ninja_forms_submissions_settings' );
return rest_ensure_response( json_encode( $settings ) );
}
/**
* Request deletion of a collection of submissions
*
* Data passes as a collection of single submission entities keyed
* under submissions
*
* {"submissions": SingleSubmission[]}
*
* @param WP_REST_Request $request
* @return void
*/
public function delete_submissions(WP_REST_Request $request){
//Extract required data
$data = json_decode($request->get_body());
$submissions = $data->submissions;
$submissionAggregate = (new SubmissionAggregateFactory())->submissionAggregate();
foreach($submissions as $obj){
$singleSubmission = SingleSubmission::fromArray((array)$obj);
$submissionAggregate->deleteSingleSubmission($singleSubmission);
}
return 'ok';
}
/**
* Request restoration of a collection of submissions
*
* Data passes as a collection of single submission entities keyed
* under submissions
*
* {"submissions": SingleSubmission[]}
*
* @param WP_REST_Request $request
* @return void
*/
public function restore_submissions(WP_REST_Request $request){
//Extract required data
$data = json_decode($request->get_body());
$submissions = $data->submissions;
$submissionAggregate = (new SubmissionAggregateFactory())->submissionAggregate();
foreach($submissions as $obj){
$singleSubmission = SingleSubmission::fromArray((array)$obj);
$submissionAggregate->restoreSingleSubmission($singleSubmission);
}
return 'ok';
}
/**
* Request update of a single submission
*
* Data passes as a single submission entity keyed under submission
*
* {"submission": SingleSubmission}
*
* @param WP_REST_Request $request
* @return void
*/
public function update_submission(WP_REST_Request $request){
//Extract required data
$data = json_decode($request->get_body(),true);
$singleSubmissionArray = $data['singleSubmission'];
$singleSubmission = SingleSubmission::fromArray($singleSubmissionArray);
$submissionAggregate = (new SubmissionAggregateFactory())->submissionAggregate();
$submissionAggregate->updateSingleSubmission($singleSubmission);
return 'ok';
}
/**
* Handle extra request for submissions
*
* This is a starting proof of concept that triggers a download for the PDF submissions add-on
*
* Data passes as a single submission entity keyed under submission and a Class to call the handler under handleExtra key
*
* {
* "submission": SingleSubmission,
* "handleExtra": HandleExtraClassName
* }
*
* @param WP_REST_Request $request
* @return object with string of responseType, blob of PDF download and string of blobType
*/
public function handle_extra_submission(WP_REST_Request $request){
// set default response
$response = [ ];
//Extract required data
$data = json_decode($request->get_body(),true);
$singleSubmissionArray = $data['singleSubmission'];
$singleSubmission = SingleSubmission::fromArray($singleSubmissionArray);
$submissionAggregate = (new SubmissionAggregateFactory())->submissionAggregate();
$populatedSubmission = $submissionAggregate->requestSingleSubmission($singleSubmission);
$extraHandler = $data['handleExtra'];
/** @var SubmissionHandler $object */
if(class_exists($extraHandler)){
$object = new $extraHandler;
$response = $object->handle($populatedSubmission);
}
// Handlers using NinjaForms\Includes\Abstracts\SubmissionHandler
// already pass through entity, but it is not guaranteed that all
// handlers will use the abstract
$arrayFromEntity = (SubmissionExtraHandlerResponse::fromArray($response))->toArray();
return $arrayFromEntity;
}
}