PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 11.0.5
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v11.0.5
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
acymailing / back / Core / AcymClass.php

AcymClass.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 11.0.5, at back/Core/AcymClass.php

197 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AcyMailing\Core;
4
5 abstract class AcymClass extends AcymObject
6 {
7 // Handle errors
8 public array $errors = [];
9 // Information messages, mainly for the cron report
10 public array $messages = [];
11
12 protected string $table;
13 protected string $pkey = '';
14 protected bool $forceInsert = false;
15 protected array $intColumns = [];
16 protected array $jsonColumns = [];
17
18 /**
19 * Returns an array containing "elements[]" which are all the requested elements and "total" which is the number of elements.
20 * Can also contain "status[]", containing the status of elements. Optional.
21 */
22 public function getMatchingElements(array $settings = []): array
23 {
24 if (!empty($this->table) && !empty($this->pkey)) {
25 $query = 'SELECT * FROM #__acym_'.acym_secureDBColumn($this->table);
26 $queryCount = 'SELECT COUNT(*) AS total FROM #__acym_'.acym_secureDBColumn($this->table);
27
28 if (empty($settings['ordering'])) $settings['ordering'] = $this->pkey;
29 $query .= ' ORDER BY `'.acym_secureDBColumn($settings['ordering']).'`';
30 if (!empty($settings['ordering_sort_order'])) $query .= ' '.acym_secureDBColumn(strtoupper($settings['ordering_sort_order']));
31
32 $elements = acym_loadObjectList($query);
33 $total = acym_loadObject($queryCount);
34 } else {
35 $elements = [];
36 $total = new \stdClass();
37 $total->total = 0;
38 }
39
40 array_map([$this, 'fixTypes'], $elements);
41
42 return [
43 'elements' => $elements,
44 'total' => $total,
45 ];
46 }
47
48 public function getOneById(int $id): ?object
49 {
50 $element = acym_loadObject('SELECT * FROM #__acym_'.acym_secureDBColumn($this->table).' WHERE `'.acym_secureDBColumn($this->pkey).'` = '.intval($id));
51
52 if (empty($element)) {
53 return null;
54 }
55
56 $this->fixTypes($element);
57
58 return $element;
59 }
60
61 public function getByIds(array $ids): array
62 {
63 if (empty($ids)) {
64 return [];
65 }
66
67 acym_arrayToInteger($ids);
68
69 $elements = acym_loadObjectList('SELECT * FROM #__acym_'.acym_secureDBColumn($this->table).' WHERE `'.acym_secureDBColumn($this->pkey).'` IN ("'.implode('","', $ids).'")');
70 array_map([$this, 'fixTypes'], $elements);
71
72 return $elements;
73 }
74
75 public function getAll(?string $key = null): array
76 {
77 if (empty($key)) {
78 $key = $this->pkey;
79 }
80
81 $elements = acym_loadObjectList('SELECT * FROM #__acym_'.acym_secureDBColumn($this->table), $key);
82 array_map([$this, 'fixTypes'], $elements);
83
84 return $elements;
85 }
86
87 public function save(object $element): ?int
88 {
89 $tableColumns = acym_getColumns($this->table);
90 // We clone the element because we don't want to modify it for later in the code
91 $cloneElement = clone $element;
92 foreach ($cloneElement as $column => $value) {
93 if (!in_array($column, $tableColumns)) {
94 // Unset variables that don't exist in the table
95 unset($cloneElement->$column);
96 continue;
97 }
98 acym_secureDBColumn($column);
99 }
100
101 $pkey = $this->pkey;
102
103 try {
104 if (empty($cloneElement->$pkey) || $this->forceInsert) {
105 $status = acym_insertObject('#__acym_'.$this->table, $cloneElement);
106 } else {
107 $status = acym_updateObject('#__acym_'.$this->table, $cloneElement, [$pkey]);
108 }
109 } catch (\Exception $e) {
110 $status = false;
111 }
112
113 if (empty($status)) {
114 $dbError = acym_stripTags(isset($e) ? $e->getMessage() : acym_getDBError());
115 if (!empty($dbError)) {
116 if (strlen($dbError) > 203) $dbError = substr($dbError, 0, 200).'...';
117 $this->errors[] = $dbError;
118 }
119
120 return false;
121 }
122
123 return (int)(empty($cloneElement->$pkey) ? $status : $cloneElement->$pkey);
124 }
125
126 public function delete(array $elements): int
127 {
128 if (empty($elements)) {
129 return 0;
130 }
131
132 $escapedElements = array_map('acym_escapeDB', $elements);
133
134 if (empty($this->pkey) || empty($this->table) || empty($escapedElements)) {
135 return 0;
136 }
137
138 acym_trigger('onAcymBefore'.ucfirst($this->table).'Delete', [&$elements]);
139
140 $query = 'DELETE FROM #__acym_'.acym_secureDBColumn($this->table).' WHERE '.acym_secureDBColumn($this->pkey).' IN ('.implode(',', $escapedElements).')';
141 $result = acym_query($query);
142
143 if (!$result) {
144 return 0;
145 }
146
147 acym_trigger('onAcymAfter'.ucfirst($this->table).'Delete', [&$elements]);
148
149 return (int)$result;
150 }
151
152 public function setActive(array $elements): void
153 {
154 if (empty($elements)) {
155 return;
156 }
157
158 acym_arrayToInteger($elements);
159 acym_query('UPDATE '.acym_secureDBColumn('#__acym_'.$this->table).' SET active = 1 WHERE `'.acym_secureDBColumn($this->pkey).'` IN ('.implode(',', $elements).')');
160 }
161
162 public function setInactive(array $elements): void
163 {
164 if (empty($elements)) {
165 return;
166 }
167
168 acym_arrayToInteger($elements);
169 acym_query('UPDATE '.acym_secureDBColumn('#__acym_'.$this->table).' SET active = 0 WHERE `'.acym_secureDBColumn($this->pkey).'` IN ('.implode(',', $elements).')');
170 }
171
172 /**
173 * Joomla/WordPress can return ints as strings or ints depending on their versions
174 */
175 protected function fixTypes(object $element): void
176 {
177 foreach ($this->intColumns as $intColumn) {
178 if (isset($element->$intColumn)) {
179 $element->$intColumn = (int)$element->$intColumn;
180 }
181 }
182
183 foreach ($this->jsonColumns as $oneColumn) {
184 if (!property_exists($element, $oneColumn)) {
185 continue;
186 }
187
188 if (empty($element->$oneColumn)) {
189 $element->$oneColumn = [];
190 } elseif (is_string($element->$oneColumn)) {
191 $decodedColumn = json_decode($element->$oneColumn, true);
192 $element->$oneColumn = is_array($decodedColumn) ? $decodedColumn : [];
193 }
194 }
195 }
196 }
197