Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/fluentform/app/Models/FormMeta.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
3
+
namespace FluentForm\App\Models;
4
+
5
+
use FluentForm\Framework\Support\Arr;
6
+
7
+
// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- This class uses custom meta table (fluentform_form_meta), not WordPress postmeta
8
+
class FormMeta extends Model
9
+
{
10
+
/**
11
+
* The table associated with the model.
12
+
*
13
+
* @var string
14
+
*/
15
+
protected $table = 'fluentform_form_meta';
16
+
17
+
/**
18
+
* Indicates if the model should be timestamped.
19
+
*
20
+
* @var bool
21
+
*/
22
+
public $timestamps = false;
23
+
24
+
/**
25
+
* A formMeta is owned by a form.
26
+
*
27
+
* @return \FluentForm\Framework\Database\Orm\Relations\BelongsTo
28
+
*/
29
+
public function form()
30
+
{
31
+
return $this->belongsTo(Form::class, 'form_id', 'id');
32
+
}
33
+
34
+
public static function prepare($attributes, $predefinedForm)
35
+
{
36
+
$formMeta = [];
37
+
38
+
$formMeta[] = [
39
+
'meta_key' => 'formSettings',
40
+
'value' => json_encode(Form::getFormsDefaultSettings()),
41
+
];
42
+
43
+
$formMeta[] = [
44
+
'meta_key' => 'template_name',
45
+
'value' => Arr::get($attributes, 'predefined'),
46
+
];
47
+
48
+
if (isset($predefinedForm['notifications'])) {
49
+
$formMeta[] = [
50
+
'meta_key' => 'notifications',
51
+
'value' => json_encode($predefinedForm['notifications']),
52
+
];
53
+
}
54
+
55
+
if (Arr::get($attributes, 'predefined') == 'conversational') {
56
+
$formMeta[] = [
57
+
'meta_key' => 'is_conversion_form',
58
+
'value' => 'yes',
59
+
];
60
+
}
61
+
62
+
return $formMeta;
63
+
}
64
+
65
+
public static function retrieve($key, $formId = null, $default = null)
66
+
{
67
+
$meta = static::when($formId, function ($q) use ($formId) {
68
+
return $q->where('form_id', $formId);
69
+
})
70
+
->where('meta_key', $key)
71
+
->first();
72
+
73
+
if ($meta && isset($meta->value)) {
74
+
$value = json_decode($meta->value, true);
75
+
if (JSON_ERROR_NONE == json_last_error()) {
76
+
return $value;
77
+
}
78
+
return $meta->value;
79
+
}
80
+
81
+
return $default;
82
+
}
83
+
84
+
public static function store(Form $form, $formMeta)
85
+
{
86
+
foreach ($formMeta as $meta) {
87
+
$meta['value'] = trim(preg_replace('/\s+/', ' ', $meta['value']));
88
+
89
+
$form->formMeta()->create([
90
+
'meta_key' => $meta['meta_key'],
91
+
'value' => $meta['value'],
92
+
]);
93
+
}
94
+
}
95
+
96
+
public static function persist($formId, $metaKey, $metaValue)
97
+
{
98
+
if (is_array($metaValue) || is_object($metaValue)) {
99
+
$metaValue = json_encode($metaValue);
100
+
}
101
+
102
+
return static::updateOrCreate(
103
+
['form_id' => $formId, 'meta_key' => $metaKey],
104
+
['value' => $metaValue]
105
+
);
106
+
}
107
+
108
+
public static function remove($formId, $metaKey)
109
+
{
110
+
static::where('form_id', $formId)
111
+
->where('meta_key', $metaKey)
112
+
->delete();
113
+
}
114
+
}
115
+