Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/fluentform/app/Modules/Form/Transfer.php

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