Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/fluentformpro/src/Components/Post/MetaboxHelper.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
3
+
namespace FluentFormPro\Components\Post;
4
+
5
+
if (!defined('ABSPATH')) {
6
+
exit; // Exit if accessed directly.
7
+
}
8
+
9
+
use FluentForm\App\Helpers\Helper;
10
+
use FluentForm\Framework\Helpers\ArrayHelper as Arr;
11
+
12
+
class MetaboxHelper
13
+
{
14
+
use Getter;
15
+
16
+
public static function hasMetabox()
17
+
{
18
+
return defined('RWMB_VER');
19
+
}
20
+
21
+
public static function getUserFields()
22
+
{
23
+
if (!self::hasMetabox()) {
24
+
return [
25
+
'general' => [],
26
+
'advanced' => []
27
+
];
28
+
}
29
+
$meta_box_registry = rwmb_get_registry('meta_box');
30
+
$args = [
31
+
'object_type' => 'user'
32
+
];
33
+
$meta_boxes = $meta_box_registry->get_by($args);
34
+
$validBoxes = [];
35
+
foreach ($meta_boxes as $key => $meta_box) {
36
+
if (empty($meta_box->meta_box)) {
37
+
continue;
38
+
}
39
+
$validBoxes[$key] = $meta_box;
40
+
}
41
+
return self::classifyFields($validBoxes);
42
+
}
43
+
44
+
public static function getPostFields($postType, $withRaw = false)
45
+
{
46
+
if(!self::hasMetabox()) {
47
+
return [
48
+
'general' => [],
49
+
'advanced' => []
50
+
];
51
+
}
52
+
53
+
$meta_box_registry = rwmb_get_registry( 'meta_box' );
54
+
$args = [
55
+
'object_type' => 'post'
56
+
];
57
+
$meta_boxes = $meta_box_registry->get_by( $args );
58
+
59
+
$validBoxes = [];
60
+
61
+
foreach ($meta_boxes as $key => $meta_box) {
62
+
if(empty($meta_box->meta_box)) {
63
+
continue;
64
+
}
65
+
$postTypes = Arr::get($meta_box->meta_box, 'post_types', []);
66
+
if($postTypes && in_array($postType, $postTypes)) {
67
+
$validBoxes[$key] = $meta_box;
68
+
}
69
+
}
70
+
return self::classifyFields($validBoxes, $withRaw);
71
+
}
72
+
73
+
public static function classifyFields($metaBoxes, $withRaw = false)
74
+
{
75
+
$generalAcfFields = self::getGeneralFields();
76
+
$advancedAcfFields = self::getAdvancedFields();
77
+
78
+
$generalFields = [];
79
+
$advancedFields = [];
80
+
81
+
foreach ($metaBoxes as $field_group) {
82
+
$fields = $field_group->meta_box['fields'];
83
+
foreach ($fields as $field) {
84
+
if (in_array($field['type'], $generalAcfFields)) {
85
+
$generalFields[$field['field_name']] = [
86
+
'type' => $field['type'],
87
+
'label' => $field['name'],
88
+
'name' => $field['field_name'],
89
+
'key' => $field['id']
90
+
];
91
+
if($withRaw) {
92
+
$generalFields[$field['field_name']]['raw'] = $field;
93
+
}
94
+
} else if (isset($advancedAcfFields[$field['type']])) {
95
+
$settings = $advancedAcfFields[$field['type']];
96
+
$advancedFields[$field['field_name']] = [
97
+
'type' => $field['type'],
98
+
'label' => $field['name'],
99
+
'name' => $field['field_name'],
100
+
'key' => $field['id'],
101
+
'acceptable_fields' => $settings['acceptable_fields'],
102
+
'help_message' => $settings['help']
103
+
];
104
+
105
+
if ($withRaw) {
106
+
$advancedFields[$field['field_name']]['raw'] = $field;
107
+
}
108
+
}
109
+
}
110
+
}
111
+
112
+
return [
113
+
'general' => $generalFields,
114
+
'advanced' => $advancedFields
115
+
];
116
+
}
117
+
118
+
private static function getGeneralFields()
119
+
{
120
+
$acceptedFields = [
121
+
'hidden',
122
+
'password',
123
+
'text',
124
+
'textarea',
125
+
'url',
126
+
'wysiwyg',
127
+
'time',
128
+
'slider',
129
+
'color',
130
+
'email',
131
+
'number',
132
+
'range',
133
+
'tel'
134
+
];
135
+
$acceptedFields = apply_filters_deprecated(
136
+
'fluent_post_metabox_accepted_general_fields',
137
+
[
138
+
$acceptedFields
139
+
],
140
+
FLUENTFORM_FRAMEWORK_UPGRADE,
141
+
'fluentform/post_metabox_accepted_general_fields',
142
+
'Use fluentform/post_metabox_accepted_general_fields instead of fluent_post_metabox_accepted_general_fields.'
143
+
);
144
+
return apply_filters('fluentform/post_metabox_accepted_general_fields', $acceptedFields);
145
+
}
146
+
147
+
public static function getAdvancedFields()
148
+
{
149
+
$acceptedFields = [
150
+
'select' => [
151
+
'acceptable_fields' => ['select'],
152
+
'help' => __('Select select field for this mapping', 'fluentformpro')
153
+
],
154
+
'select_advanced' => [
155
+
'acceptable_fields' => ['select'],
156
+
'help' => __('Select appropriate field for this mapping', 'fluentformpro')
157
+
],
158
+
'checkbox' => [
159
+
'acceptable_fields' => ['gdpr_agreement', 'terms_and_condition'],
160
+
'help' => __('Select checkbox field for this mapping', 'fluentformpro')
161
+
],
162
+
'checkbox_list' => [
163
+
'acceptable_fields' => ['input_checkbox'],
164
+
'help' => __('Select checkbox field for this mapping', 'fluentformpro')
165
+
],
166
+
'radio' => [
167
+
'acceptable_fields' => ['input_radio'],
168
+
'help' => __('Select radio field for this mapping', 'fluentformpro')
169
+
],
170
+
'button_group' => [
171
+
'acceptable_fields' => ['input_radio'],
172
+
'help' => __('Select radio field for this mapping', 'fluentformpro')
173
+
],
174
+
'image_select' => [
175
+
'acceptable_fields' => ['input_radio', 'input_checkbox', 'select'],
176
+
'help' => __('Select appropriate field for this mapping', 'fluentformpro')
177
+
],
178
+
'datetime-local' => [
179
+
'acceptable_fields' => ['input_date'],
180
+
'help' => __('Select Date field for this mapping', 'fluentformpro')
181
+
],
182
+
'date' => [
183
+
'acceptable_fields' => ['input_date'],
184
+
'help' => __('Select Date field for this mapping', 'fluentformpro')
185
+
],
186
+
'datetime' => [
187
+
'acceptable_fields' => ['input_date'],
188
+
'help' => __('Select Date field for this mapping', 'fluentformpro')
189
+
],
190
+
'switch' => [
191
+
'acceptable_fields' => ['gdpr_agreement', 'terms_and_condition'],
192
+
'help' => __('Select Date field for this mapping', 'fluentformpro')
193
+
],
194
+
'file' => [
195
+
'acceptable_fields' => ['input_file'],
196
+
'help' => __('Select File Upload field for this mapping', 'fluentformpro')
197
+
],
198
+
'file_advanced' => [
199
+
'acceptable_fields' => ['input_file'],
200
+
'help' => __('Select Date field for this mapping', 'fluentformpro')
201
+
],
202
+
'file_upload' => [
203
+
'acceptable_fields' => ['input_file'],
204
+
'help' => __('Select Date field for this mapping', 'fluentformpro')
205
+
],
206
+
'file_input' => [
207
+
'acceptable_fields' => ['input_file', 'input_image'],
208
+
'help' => __('Select Date field for this mapping', 'fluentformpro')
209
+
],
210
+
'image' => [
211
+
'acceptable_fields' => ['input_image'],
212
+
'help' => __('Select Date field for this mapping', 'fluentformpro')
213
+
],
214
+
'image_advanced' => [
215
+
'acceptable_fields' => ['input_image'],
216
+
'help' => __('Select Date field for this mapping', 'fluentformpro')
217
+
],
218
+
'image_upload' => [
219
+
'acceptable_fields' => ['input_image'],
220
+
'help' => __('Select Date field for this mapping', 'fluentformpro')
221
+
],
222
+
'single_image' => [
223
+
'acceptable_fields' => ['input_image'],
224
+
'help' => __('Select Date field for this mapping', 'fluentformpro')
225
+
],
226
+
];
227
+
$acceptedFields = apply_filters_deprecated(
228
+
'fluent_post_metabox_accepted_advanced_fields',
229
+
[
230
+
$acceptedFields
231
+
],
232
+
FLUENTFORM_FRAMEWORK_UPGRADE,
233
+
'fluentform/post_metabox_accepted_advanced_fields',
234
+
'Use fluentform/post_metabox_accepted_advanced_fields instead of fluent_post_metabox_accepted_advanced_fields.'
235
+
);
236
+
return apply_filters('fluentform/post_metabox_accepted_advanced_fields', $acceptedFields);
237
+
}
238
+
239
+
public static function prepareFieldsData($fields, $postType, $formData, $isUpdate)
240
+
{
241
+
if ('user' == $postType) {
242
+
$fieldGroups = self::getUserFields();
243
+
} else {
244
+
$fieldGroups = self::getPostFields($postType, true);
245
+
}
246
+
$metaValues = [];
247
+
$generalFields = $fieldGroups['general'];
248
+
foreach ($fields['general'] as $field) {
249
+
$fieldValue = Arr::get($field, 'field_value');
250
+
$fieldKey = Arr::get($field, 'field_key');
251
+
252
+
if (!$fieldKey || !$fieldValue || !isset($generalFields[$fieldKey])) {
253
+
continue;
254
+
}
255
+
256
+
$fieldConfig = $generalFields[$fieldKey];
257
+
$mataName = $fieldConfig['name'];
258
+
$metaValues[$mataName] = $fieldValue;
259
+
}
260
+
261
+
$advancedFields = $fieldGroups['advanced'];
262
+
263
+
foreach ($fields['advanced'] as $field) {
264
+
$fieldValue = Arr::get($formData, $field['field_value']);
265
+
$fieldKey = Arr::get($field, 'field_key');
266
+
267
+
if (!$fieldKey || !isset($advancedFields[$fieldKey])) {
268
+
continue;
269
+
}
270
+
$fieldConfig = $advancedFields[$fieldKey];
271
+
$type = $fieldConfig['type'];
272
+
273
+
if ('checkbox_list' === $type && !$fieldValue && $isUpdate) {
274
+
$fieldValue = [0 => "0"]; //make metabox field empty
275
+
}
276
+
if (!$fieldValue && !$isUpdate) {
277
+
continue;
278
+
}
279
+
280
+
$fieldKey = str_replace('[]', '', $fieldKey);
281
+
$rawTypes = ['checkbox_list', 'button_group', 'select', 'select_advanced', 'radio', 'datetime-local', 'image_select', 'date', 'datetime'];
282
+
if(in_array($type, $rawTypes)) {
283
+
$metaValues[$fieldKey] = $fieldValue;
284
+
continue;
285
+
}
286
+
287
+
$fileIdsTypes = ['file', 'file_advanced', 'file_upload', 'image', 'image_advanced', 'image_upload', 'single_image'];
288
+
289
+
if (in_array($type, $fileIdsTypes)) {
290
+
$existingAttachmentIds = static::maybeDeleteAndGetExistingAttachmentIds($field['field_value'], $formData);
291
+
$fileIds = self::getFileIdsFromUrls($fieldValue);
292
+
if($fileIds) {
293
+
if ($type == 'single_image') {
294
+
$metaValues[$fieldKey] = $fileIds[0];
295
+
} else {
296
+
if ($existingAttachmentIds) {
297
+
$fileIds = array_merge($existingAttachmentIds, $fileIds);
298
+
}
299
+
$metaValues[$fieldKey] = $fileIds;
300
+
}
301
+
}
302
+
continue;
303
+
}
304
+
if ($type == 'file_input') {
305
+
if(is_array($fieldValue)) {
306
+
$metaValues[$fieldKey] = $fieldValue[0];
307
+
}
308
+
continue;
309
+
}
310
+
311
+
$booleanTypes = ['switch', 'checkbox'];
312
+
if (in_array($type, $booleanTypes)) {
313
+
$metaValues[$fieldKey] = 1;
314
+
}
315
+
}
316
+
317
+
318
+
return $metaValues;
319
+
320
+
}
321
+
322
+
// Post update mapping
323
+
public static function maybePopulatePostUpdateMetaFields(&$metaFields, $feed, $postId, $formId)
324
+
{
325
+
$mbFields = self::getPostFields(self::getPostType($formId));
326
+
$args = [
327
+
'object_type' => 'post'
328
+
];
329
+
if ($generalMetas = self::getMetBoxFieldsValue($mbFields['general'], Arr::get($feed->value, 'metabox_mappings', []), $args, $postId)) {
330
+
$metaFields['mb_general_metas'] = $generalMetas;
331
+
}
332
+
if ($advanceMetas = self::getMetBoxFieldsValue($mbFields['advanced'], Arr::get($feed->value, 'advanced_metabox_mappings', []), $args, $postId, 'advanced')) {
333
+
$metaFields['mb_advanced_metas'] = $advanceMetas;
334
+
}
335
+
}
336
+
337
+
// User update mapping
338
+
public static function getUserMappingValue($mappingFields)
339
+
{
340
+
$metas = [];
341
+
$mbFields = self::getUserFields();
342
+
$args = [
343
+
'object_type' => 'user'
344
+
];
345
+
346
+
$userId = get_current_user_id();
347
+
if ($generalMetas = self::getMetBoxFieldsValue($mbFields['general'], Arr::get($mappingFields, 'general', []), $args, $userId)) {
348
+
$metas = $generalMetas;
349
+
}
350
+
if ($advanceMetas = self::getMetBoxFieldsValue($mbFields['advanced'], Arr::get($mappingFields, 'advanced', []), $args, $userId, 'advanced')) {
351
+
$metas = array_merge($metas, $advanceMetas);
352
+
}
353
+
354
+
return $metas;
355
+
}
356
+
357
+
358
+
private static function getMetBoxFieldsValue($mbFields, $mappingFields, $args, $objectId, $from = 'general')
359
+
{
360
+
$metaFields = [];
361
+
$mbFieldsKeys = array_keys($mbFields);
362
+
foreach ($mappingFields as $field) {
363
+
if (!in_array($field['field_key'], $mbFieldsKeys) || !function_exists('rwmb_get_value')) {
364
+
continue;
365
+
}
366
+
367
+
$fieldName = Arr::get($field, 'field_value', '');
368
+
$field = Arr::get($mbFields, $field['field_key'], '');
369
+
370
+
if ($from === 'advanced') {
371
+
$name = $fieldName;
372
+
} else {
373
+
$name = Helper::getInputNameFromShortCode($fieldName);
374
+
}
375
+
376
+
if (!$name && !$field) {
377
+
continue;
378
+
}
379
+
$value = rwmb_get_value($field['key'], $args, $objectId);
380
+
381
+
if (in_array($field['type'], ['file_upload', 'image_upload', 'image', 'file_advanced', 'file'])) {
382
+
if (count($value) > 0) {
383
+
$value = array_values($value);
384
+
}
385
+
}
386
+
$metaFields[] = [
387
+
"name" => $name,
388
+
"type" => $field['type'],
389
+
"value" => $value
390
+
];
391
+
}
392
+
return $metaFields;
393
+
}
394
+
395
+
public static function maybeUpdateUserMetas($userId, $formData, $form, $feed)
396
+
{
397
+
if (self::hasMetabox() && $metaboxFields = Arr::get($feed, 'settings.metabox_mappings')) {
398
+
$isUserUpdate = 'user_update' == Arr::get($feed, 'settings.list_id');
399
+
if (Arr::get($metaboxFields, 'general')) {
400
+
$metaboxFields['general'] = Arr::get($feed, 'processedValues.metabox_mappings.general');
401
+
}
402
+
$metaboxMeta = self::prepareFieldsData($metaboxFields, 'user', $formData, $isUserUpdate);
403
+
if ($metaboxMeta && function_exists('rwmb_set_meta')) {
404
+
$args = [
405
+
'object_type' => 'user'
406
+
];
407
+
foreach ($metaboxMeta as $fieldKey => $value) {
408
+
rwmb_set_meta($userId, $fieldKey, $value, $args);
409
+
}
410
+
}
411
+
}
412
+
}
413
+
414
+
415
+
private static function getFileIdsFromUrls($fieldValue)
416
+
{
417
+
if (!is_array($fieldValue)) {
418
+
return [];
419
+
}
420
+
421
+
$attachmentIds = [];
422
+
foreach ($fieldValue as $item) {
423
+
$attachmentId = (new PostFormHandler())->getAttachmentToImageUrl($item);
424
+
if ($attachmentId) {
425
+
$attachmentIds[] = $attachmentId;
426
+
}
427
+
}
428
+
429
+
return $attachmentIds;
430
+
}
431
+
432
+
}
433
+