PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.18
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.18
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 3.6.66 All 195 releases
fluentform / app / Modules / Form / Transfer.php

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

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