PluginProbe
The Innovative Form Builder – IvyForms / 1.5
The Innovative Form Builder – IvyForms v1.5
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
← All changes | backend/src/Repository/Notification/NotificationRepository.php +84 -34 0.2.1 → 1.5 View file →
@@ -1,18 +1,13 @@
1 1 <?php
2 2
3 3 /**
4 - * @copyright © TMS-Plugins. All rights reserved.
4 + * @copyright © Melograno Venture Studio. All rights reserved.
5 5 * @licence See LICENCE.md for license details.
6 6 */
7 7
8 8 namespace IvyForms\Repository\Notification;
9 9
10 -// phpcs:disable PSR1.Files.SideEffects
11 -if (!defined('ABSPATH')) {
12 - exit; // Exit if accessed directly
13 -}
14 -
15 10 use IvyForms\Common\Exceptions\QueryExecutionException;
16 11 use IvyForms\Entity\Notification\Notification;
17 12 use IvyForms\Factory\Notification\NotificationFactory;
18 13 use IvyForms\Repository\AbstractRepository;
@@ -34,26 +29,20 @@
34 29 */
35 30 public function add($entity): int
36 31 {
37 32 $data = $entity->toArray();
33 + $row = $this->buildPersistRow($data);
38 34
39 35 $result = $this->wpdb->insert(
40 36 $this->table,
41 - [
42 - 'formId' => (int) $data['formId'],
43 - 'name' => $data['name'],
44 - 'sender' => $data['sender'],
45 - 'receiver' => $data['receiver'],
46 - 'enabled' => (int) $data['enabled'],
47 - 'subject' => $data['subject'],
48 - 'message' => $data['message'],
49 - 'smartLogic' => (int) $data['smartLogic'],
50 - ],
51 - ['%d', '%s', '%s', '%s', '%d', '%s', '%s', '%d']
37 + $row['values'],
38 + $row['formats']
52 39 );
53 40
54 41 if ($result === false) {
55 - throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_add_data'] . __CLASS__);
42 + throw new QueryExecutionException(
43 + BackendStrings::getExceptionStrings()['unable_to_add_data'] . __CLASS__
44 + );
56 45 }
57 46
58 47 return $this->wpdb->insert_id;
59 48 }
@@ -70,23 +59,15 @@
70 59 */
71 60 public function update(int $id, $entity): bool
72 61 {
73 62 $data = $entity->toArray();
63 + $row = $this->buildPersistRow($data);
74 64
75 65 $result = $this->wpdb->update(
76 66 $this->table,
77 - [
78 - 'formId' => (int) $data['formId'],
79 - 'name' => $data['name'],
80 - 'sender' => $data['sender'],
81 - 'receiver' => $data['receiver'],
82 - 'enabled' => (int) $data['enabled'],
83 - 'subject' => $data['subject'],
84 - 'message' => $data['message'],
85 - 'smartLogic' => (int) $data['smartLogic'],
86 - ],
67 + $row['values'],
87 68 ['id' => $id],
88 - ['%d', '%s', '%s', '%s', '%d', '%s', '%s', '%d'],
69 + $row['formats'],
89 70 ['%d']
90 71 );
91 72
92 73 if ($result === false) {
@@ -92,16 +73,77 @@
92 73 if ($result === false) {
93 74 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__);
94 75 }
95 76
96 - return $result;
77 + return true;
97 78 }
79 +
98 80 /**
81 + * Build insert/update row values and formats, allowing Pro to append columns.
82 + *
83 + * @param array<string, mixed> $data
84 + * @return array{values: array<string, mixed>, formats: string[]}
85 + */
86 + private function buildPersistRow(array $data): array
87 + {
88 + $values = [
89 + 'formId' => (int) $data['formId'],
90 + 'name' => $data['name'],
91 + 'sender' => $data['sender'],
92 + 'replyTo' => $data['replyTo'],
93 + 'receiver' => $data['receiver'],
94 + 'cc' => $data['cc'],
95 + 'bcc' => $data['bcc'],
96 + 'enabled' => (int) $data['enabled'],
97 + 'subject' => $data['subject'],
98 + 'message' => $data['message'],
99 + 'showEmptyFields' => (int) $data['showEmptyFields'],
100 + 'smartLogic' => wp_json_encode($data['smartLogic']),
101 + ];
102 + $formats = ['%d', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s', '%d', '%s'];
103 +
104 + /**
105 + * Extend notification DB persist row (values + formats) for Pro columns.
106 + *
107 + * @since 1.2.0
108 + *
109 + * @param array<string, mixed> $row
110 + * @param array<string, mixed> $data
111 + */
112 + $filtered = apply_filters(
113 + 'ivyforms/notification/repository_row',
114 + [
115 + 'values' => $values,
116 + 'formats' => $formats,
117 + ],
118 + $data
119 + );
120 +
121 + if (
122 + !is_array($filtered)
123 + || !isset($filtered['values'], $filtered['formats'])
124 + || !is_array($filtered['values'])
125 + || !is_array($filtered['formats'])
126 + ) {
127 + return [
128 + 'values' => $values,
129 + 'formats' => $formats,
130 + ];
131 + }
132 +
133 + return [
134 + 'values' => $filtered['values'],
135 + 'formats' => $filtered['formats'],
136 + ];
137 + }
138 +
139 + /**
99 140 * Get all notifications by form ID.
100 141 *
101 142 * @param int $id
102 143 *
103 144 * @return array<Notification>
145 + * @throws QueryExecutionException
104 146 */
105 147 public function getAllById(int $id): array
106 148 {
107 149 $rows = $this->wpdb->get_results(
@@ -110,17 +152,21 @@
110 152 [$id]
111 153 ),
112 154 ARRAY_A
113 155 );
114 -
156 + if ($rows === false) {
157 + throw new QueryExecutionException(
158 + BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__
159 + );
160 + }
115 161 if (empty($rows)) {
116 162 return [];
117 163 }
118 -
119 164 return array_map(function ($row) {
120 165 return call_user_func([static::FACTORY, 'create'], $row);
121 166 }, $rows);
122 167 }
168 +
123 169 /**
124 170 * Get all active notifications by form ID.
125 171 *
126 172 * @param int $id
@@ -125,8 +171,9 @@
125 171 *
126 172 * @param int $id
127 173 *
128 174 * @return array<Notification>
175 + * @throws QueryExecutionException
129 176 */
130 177 public function getAllActiveById(int $id): array
131 178 {
132 179 $rows = $this->wpdb->get_results(
@@ -135,13 +182,16 @@
135 182 $id
136 183 ),
137 184 ARRAY_A
138 185 );
139 -
186 + if ($rows === false) {
187 + throw new QueryExecutionException(
188 + BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__
189 + );
190 + }
140 191 if (empty($rows)) {
141 192 return [];
142 193 }
143 -
144 194 return array_map(function ($row) {
145 195 return call_user_func([static::FACTORY, 'create'], $row);
146 196 }, $rows);
147 197 }