PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.8
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.8
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Modules / Form / Transfer.php

Transfer.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.8, at app/Modules/Form/Transfer.php

183 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Modules\Form;
4
5 use FluentForm\App\Helpers\Helper;
6 use FluentForm\App\Services\Transfer\TransferService;
7 use FluentForm\Framework\Foundation\Application;
8 use FluentForm\Framework\Helpers\ArrayHelper;
9 use FluentForm\Framework\Http\Request\File;
10
11 /* @deprecated Current File FluentForm\App\Http\Controllers\TransferController */
12
13 class Transfer
14 {
15 /**
16 * Request object
17 *
18 * @var \FluentForm\Framework\Request\Request $request
19 */
20 protected $request;
21
22 /**
23 * Transfer constructor.
24 *
25 * @param \FluentForm\Framework\Foundation\Application $application
26 */
27 public function __construct(Application $application)
28 {
29 $this->request = $application->make('request');
30 }
31
32 /**
33 * Export forms as JSON.
34 */
35 public function export()
36 {
37 // Get the IDs of the forms the user wants to be exported.
38 $formIds = $this->request->get('forms');
39
40 // Load the forms for a given form IDs and the settings from the DB.
41 $result = wpFluent()
42 ->table('fluentform_forms')
43 ->whereIn('id', $formIds)
44 ->get();
45
46 // Prepare the loaded query results to form and it's settings objects.
47 $forms = [];
48 foreach ($result as $item) {
49 $form = $item;
50 $form->form_fields = json_decode($form->form_fields);
51 $form->metas = $this->getFormMetas($item->id);
52 $forms[] = $form;
53 }
54
55 $fileName = 'fluentform-export-forms-' . count($forms) . '-' . date('d-m-Y') . '.json';
56
57 header('Content-disposition: attachment; filename=' . $fileName);
58
59 header('Content-type: application/json');
60
61 echo json_encode(array_values($forms)); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $forms is escaped before being passed in.
62
63 die();
64 }
65
66 /**
67 * Import forms from a previously exported JSON file.
68 */
69 public function import()
70 {
71 $file = $this->request->file('file');
72
73 if ($file instanceof File) {
74 $forms = \json_decode($file->getContents(), true);
75 $insertedForms = [];
76 if ($forms && is_array($forms)) {
77 foreach ($forms as $formItem) {
78 // First of all make the form object.
79 $formFields = json_encode([]);
80 if ($fields = ArrayHelper::get($formItem, 'form', '')) {
81 $formFields = json_encode($fields);
82 } elseif ($fields = ArrayHelper::get($formItem, 'form_fields', '')) {
83 $formFields = json_encode($fields);
84 } else {
85 wp_send_json([
86 'message' => __('You have a faulty JSON file, please export the Fluent Forms again.', 'fluentform'),
87 ], 422);
88 }
89
90 $form = [
91 'title' => ArrayHelper::get($formItem, 'title'),
92 'form_fields' => $formFields,
93 'status' => ArrayHelper::get($formItem, 'status', 'published'),
94 'has_payment' => ArrayHelper::get($formItem, 'has_payment', 0),
95 'type' => ArrayHelper::get($formItem, 'type', 'form'),
96 'created_by' => get_current_user_id(),
97 ];
98
99 if (ArrayHelper::get($formItem, 'conditions')) {
100 $form['conditions'] = ArrayHelper::get($formItem, 'conditions');
101 }
102
103 if (isset($formItem['appearance_settings'])) {
104 $form['appearance_settings'] = $formItem['appearance_settings'];
105 }
106
107 // Insert the form to the DB.
108 $formId = wpFluent()->table('fluentform_forms')->insertGetId($form);
109
110 $insertedForms[$formId] = [
111 'title' => $form['title'],
112 'edit_url' => admin_url('admin.php?page=fluent_forms&route=editor&form_id=' . $formId),
113 ];
114
115 if (isset($formItem['metas'])) {
116 foreach ($formItem['metas'] as $metaData) {
117 $metaKey = sanitize_text_field(ArrayHelper::get($metaData, 'meta_key'));
118 $settings = [
119 'form_id' => $formId,
120 'meta_key' => $metaKey,
121 'value' => TransferService::sanitizeImportedMetaValue(
122 $metaKey,
123 ArrayHelper::get($metaData, 'value')
124 ),
125 ];
126 wpFluent()->table('fluentform_form_meta')->insert($settings);
127 }
128 } else {
129 $oldKeys = [
130 'formSettings',
131 'notifications',
132 'mailchimp_feeds',
133 'slack',
134 ];
135 foreach ($oldKeys as $key) {
136 if (isset($formItem[$key])) {
137 $settings = [
138 'form_id' => $formId,
139 'meta_key' => $key,
140 'value' => json_encode($formItem[$key]),
141 ];
142 wpFluent()->table('fluentform_form_meta')->insert($settings);
143 }
144 }
145 }
146
147 do_action_deprecated(
148 'fluentform_form_imported',
149 [
150 $formId
151 ],
152 FLUENTFORM_FRAMEWORK_UPGRADE,
153 'fluentform/form_imported',
154 'Use fluentform/form_imported instead of fluentform_form_imported.'
155 );
156
157 do_action('fluentform/form_imported', $formId);
158 }
159
160 wp_send_json([
161 'message' => __('You form has been successfully imported.', 'fluentform'),
162 'inserted_forms' => $insertedForms,
163 ], 200);
164 }
165 }
166
167 wp_send_json([
168 'message' => __('You have a faulty JSON file, please export the Fluent Forms again.', 'fluentform'),
169 ], 422);
170 }
171
172 public function getFormMetas($formId)
173 {
174 return wpFluent()
175 ->table('fluentform_form_meta')
176 ->select(['meta_key', 'value'])
177 ->where('form_id', $formId)
178 ->whereNotIn('meta_key', ['_total_views', '_ff_form_styler_css'])
179 ->get()
180 ->toArray();
181 }
182 }
183