PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.3.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.3.1
3.3.1 V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 All 138 releases
bit-form / includes / Core / Messages / EmailTemplateHandler.php

EmailTemplateHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 3.3.1, at includes/Core/Messages/EmailTemplateHandler.php

159 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Core\Messages;
4
5 use BitCode\BitForm\Core\Database\EmailTemplateModel;
6
7 final class EmailTemplateHandler
8 {
9 private static $_formID;
10 private static $_emailTemplateModel;
11 private $_user_details;
12
13 public function __construct($formID, array $user_details = null)
14 {
15 static::$_formID = $formID;
16 static::$_emailTemplateModel = new EmailTemplateModel();
17 $this->_user_details = $user_details;
18 }
19
20 /**
21 * Decode a stored template `config` column into the object shape the admin editor expects.
22 *
23 * Older versions could persist an empty config as `[]`; decoded, that reaches the React editor
24 * as a JS array and setting a key on it throws inside the immutable-draft library. Anything that
25 * is not a non-empty object collapses to `{}`.
26 *
27 * @param mixed $config raw column value (JSON string, already-decoded value, or null)
28 *
29 * @return object
30 */
31 public static function normalizeConfig($config)
32 {
33 if (is_string($config)) {
34 $config = json_decode($config);
35 }
36 if (is_array($config)) {
37 $config = (object) $config;
38 }
39
40 return is_object($config) ? $config : (object) [];
41 }
42
43 public function getAllTemplate($templateID = null, $userID = null)
44 {
45 $condition = [
46 'form_id' => static::$_formID,
47 ];
48 if (!is_null($templateID)) {
49 $condition = array_merge($condition, ['id' => $templateID]);
50 }
51 if (!is_null($userID)) {
52 $condition = array_merge($condition, ['user_id' => $userID]);
53 }
54 return static::$_emailTemplateModel->get(
55 [
56 'id',
57 'title',
58 'sub',
59 'body',
60 'status',
61 'config'
62 ],
63 $condition
64 );
65 }
66
67 public function getATemplate($templateID)
68 {
69 return $this->getAllTemplate($templateID);
70 }
71
72 public function saveTemplate($templateDetail)
73 {
74 return static::$_emailTemplateModel->insert(
75 [
76 'title' => $templateDetail->title,
77 'sub' => $templateDetail->sub,
78 'body' => $templateDetail->body,
79 'status' => isset($templateDetail->status) ? (int) $templateDetail->status : 1,
80 'config' => isset($templateDetail->config) ? wp_json_encode($templateDetail->config) : null,
81 'form_id' => static::$_formID,
82 'user_id' => $this->_user_details['id'],
83 'user_ip' => $this->_user_details['ip'],
84 'created_at'=> $this->_user_details['time'],
85 'updated_at'=> $this->_user_details['time']
86 ]
87 );
88 }
89
90 public function updateTemplate($templateDetail)
91 {
92 return static::$_emailTemplateModel->update(
93 [
94 'title' => $templateDetail->title,
95 'sub' => $templateDetail->sub,
96 'body' => $templateDetail->body,
97 'status' => isset($templateDetail->status) ? (int) $templateDetail->status : 1,
98 'config' => isset($templateDetail->config) ? wp_json_encode($templateDetail->config) : null,
99 'form_id' => static::$_formID,
100 'user_id' => $this->_user_details['id'],
101 'user_ip' => $this->_user_details['ip'],
102 'updated_at'=> $this->_user_details['time']
103 ],
104 [
105 'id' => $templateDetail->id
106 ]
107 );
108 }
109
110 public function deleteTemplate($templateID)
111 {
112 return static::$_emailTemplateModel->delete(
113 [
114 'id' => $templateID,
115 'form_id' => static::$_formID,
116 ]
117 );
118 }
119
120 public function duplicateTemplate($templateID)
121 {
122 $templateDetail = static::$_emailTemplateModel->get(
123 [
124 'id' => $templateID,
125 'form_id' => static::$_formID,
126 ]
127 );
128 if (is_wp_error($templateDetail) || empty($templateDetail)) {
129 return $templateDetail;
130 }
131
132 $title = empty($templateDetail[0]->title) ? "Duplicate of Template #$templateID" :
133 "Duplicate of $templateDetail[0]->title";
134
135 $templateDetail[0]->title = $title;
136 return $this->saveTemplate($templateDetail);
137 }
138
139 public function duplicateAllTempInAForm($oldFromId)
140 {
141 $allCols = ['title', 'sub', 'body', 'form_id', 'user_id', 'user_ip', 'created_at', 'updated_at'];
142 $dupData = [
143 'title',
144 'sub',
145 'body',
146 static::$_formID,
147 $this->_user_details['id'],
148 $this->_user_details['ip'],
149 $this->_user_details['time'],
150 $this->_user_details['time']
151 ];
152 return static::$_emailTemplateModel->duplicate(
153 $allCols,
154 $dupData,
155 ['form_id' => $oldFromId]
156 );
157 }
158 }
159