1: <?php
 2: 
 3:  4:  5: 
 6: class Quform_Form_Exporter
 7: {
 8:      9: 10: 
11:     protected $repository;
12: 
13:     14: 15: 
16:     public function __construct(Quform_Repository $repository)
17:     {
18:         $this->repository = $repository;
19:     }
20: 
21:     22: 23: 24: 25: 
26:     public function export()
27:     {
28:         $this->validateExportRequest();
29: 
30:         $config = $this->repository->getConfig((int) $_POST['id']);
31: 
32:         if ( ! is_array($config)) {
33:             wp_send_json(array(
34:                 'type' => 'error',
35:                 'errors' => array(
36:                     'qfb-export-form' => __('Form not found', 'quform')
37:                 )
38:             ));
39:         }
40: 
41:         wp_send_json(array(
42:             'type' => 'success',
43:             'data' => base64_encode(serialize($config))
44:         ));
45:     }
46: 
47:     48: 49: 50: 51: 
52:     protected function validateExportRequest()
53:     {
54:         if ( ! Quform::isPostRequest() || ! isset($_POST['id']) || ! is_numeric($_POST['id'])) {
55:             wp_send_json(array(
56:                 'type' => 'error',
57:                 'message' => __('Bad request', 'quform')
58:             ));
59:         }
60: 
61:         if ( ! current_user_can('quform_export_forms')) {
62:             wp_send_json(array(
63:                 'type' => 'error',
64:                 'message' => __('Insufficient permissions', 'quform')
65:             ));
66:         }
67: 
68:         if ( ! check_ajax_referer('quform_export_form', false, false)) {
69:             wp_send_json(array(
70:                 'type'    => 'error',
71:                 'message' => __('Nonce check failed', 'quform')
72:             ));
73:         }
74:     }
75: }
76: