1: <?php
2:
3: 4: 5:
6: class Quform_Form_Importer
7: {
8: 9: 10:
11: protected $repository;
12:
13: 14: 15:
16: protected $scriptLoader;
17:
18: 19: 20:
21: protected $builder;
22:
23: 24: 25: 26: 27:
28: public function __construct(Quform_Repository $repository, Quform_Builder $builder, Quform_ScriptLoader $scriptLoader)
29: {
30: $this->repository = $repository;
31: $this->builder = $builder;
32: $this->scriptLoader = $scriptLoader;
33: }
34:
35: 36: 37: 38: 39:
40: public function import()
41: {
42: $this->validateImportRequest();
43:
44: @set_time_limit(3600);
45:
46: $config = base64_decode(trim(stripslashes($_POST['config'])));
47: $config = maybe_unserialize($config);
48:
49: if ( ! is_array($config)) {
50: wp_send_json(array(
51: 'type' => 'error',
52: 'errors' => array(
53: 'qfb-import-form-data' => __('The import data is invalid', 'quform')
54: )
55: ));
56: }
57:
58: if ( ! isset($config['notifications'])) {
59: wp_send_json(array(
60: 'type' => 'error',
61: 'errors' => array(
62: 'qfb-import-form-data' => __('The import data does not appear to be a valid form. If the form is from Quform 1.x, please import the form at Forms → Tools → Migrate → Import a single Quform 1.x form.', 'quform')
63: )
64: ));
65: }
66:
67: $config = $this->builder->sanitizeForm($config);
68: $config = $this->repository->add($config);
69:
70: if ( ! is_array($config)) {
71: wp_send_json(array(
72: 'type' => 'error',
73: 'message' => wp_kses(sprintf(
74:
75: __('Failed to insert into database, check the %1$serror log%2$s for more information', 'quform'),
76: '<a href="https://support.themecatcher.net/quform-wordpress-v2/guides/advanced/enabling-debug-logging">',
77: '</a>'
78: ), array('a' => array('href' => array())))
79: ));
80: }
81:
82: $this->scriptLoader->rebuildScriptCache();
83:
84: wp_send_json(array(
85: 'type' => 'success',
86: 'message' => wp_kses(sprintf(
87:
88: __('Form imported successfully, %1$sedit the form%2$s', 'quform'),
89: '<a href="' . esc_url(admin_url('admin.php?page=quform.forms&sp=edit&id=' . $config['id'])) . '">',
90: '</a>'
91: ), array('a' => array('href' => array())))
92: ));
93: }
94:
95: 96: 97: 98: 99:
100: protected function validateImportRequest()
101: {
102: if ( ! isset($_POST['config']) || ! Quform::isNonEmptyString($_POST['config'])) {
103: wp_send_json(array(
104: 'type' => 'error',
105: 'message' => __('Bad request', 'quform')
106: ));
107: }
108:
109: if ( ! current_user_can('quform_import_forms')) {
110: wp_send_json(array(
111: 'type' => 'error',
112: 'message' => __('Insufficient permissions', 'quform')
113: ));
114: }
115:
116: if ( ! check_ajax_referer('quform_import_form', false, false)) {
117: wp_send_json(array(
118: 'type' => 'error',
119: 'message' => __('Nonce check failed', 'quform')
120: ));
121: }
122: }
123: }
124: