← All changes
|
backend/src/Repository/Notification/NotificationRepository.php
+65
-30
1.4
→
1.5.1
View file →
| @@ -29,26 +29,14 @@ | ||
| 29 | 29 | */ |
| 30 | 30 | public function add($entity): int |
| 31 | 31 | { |
| 32 | 32 | $data = $entity->toArray(); |
| 33 | + $row = $this->buildPersistRow($data); | |
| 33 | 34 | |
| 34 | 35 | $result = $this->wpdb->insert( |
| 35 | 36 | $this->table, |
| 36 | - [ | |
| 37 | - 'formId' => (int) $data['formId'], | |
| 38 | - 'name' => $data['name'], | |
| 39 | - 'sender' => $data['sender'], | |
| 40 | - 'replyTo' => $data['replyTo'], | |
| 41 | - 'receiver' => $data['receiver'], | |
| 42 | - 'cc' => $data['cc'], | |
| 43 | - 'bcc' => $data['bcc'], | |
| 44 | - 'enabled' => (int) $data['enabled'], | |
| 45 | - 'subject' => $data['subject'], | |
| 46 | - 'message' => $data['message'], | |
| 47 | - 'showEmptyFields' => (int) $data['showEmptyFields'], | |
| 48 | - 'smartLogic' => wp_json_encode($data['smartLogic']), | |
| 49 | - ], | |
| 50 | - ['%d', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s', '%d', '%s'] | |
| 37 | + $row['values'], | |
| 38 | + $row['formats'] | |
| 51 | 39 | ); |
| 52 | 40 | |
| 53 | 41 | if ($result === false) { |
| 54 | 42 | throw new QueryExecutionException( |
| @@ -71,27 +59,15 @@ | ||
| 71 | 59 | */ |
| 72 | 60 | public function update(int $id, $entity): bool |
| 73 | 61 | { |
| 74 | 62 | $data = $entity->toArray(); |
| 63 | + $row = $this->buildPersistRow($data); | |
| 75 | 64 | |
| 76 | 65 | $result = $this->wpdb->update( |
| 77 | 66 | $this->table, |
| 78 | - [ | |
| 79 | - 'formId' => (int) $data['formId'], | |
| 80 | - 'name' => $data['name'], | |
| 81 | - 'sender' => $data['sender'], | |
| 82 | - 'replyTo' => $data['replyTo'], | |
| 83 | - 'receiver' => $data['receiver'], | |
| 84 | - 'cc' => $data['cc'], | |
| 85 | - 'bcc' => $data['bcc'], | |
| 86 | - 'enabled' => (int) $data['enabled'], | |
| 87 | - 'subject' => $data['subject'], | |
| 88 | - 'message' => $data['message'], | |
| 89 | - 'showEmptyFields' => (int) $data['showEmptyFields'], | |
| 90 | - 'smartLogic' => wp_json_encode($data['smartLogic']), | |
| 91 | - ], | |
| 67 | + $row['values'], | |
| 92 | 68 | ['id' => $id], |
| 93 | - ['%d', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s', '%d', '%s'], | |
| 69 | + $row['formats'], | |
| 94 | 70 | ['%d'] |
| 95 | 71 | ); |
| 96 | 72 | |
| 97 | 73 | if ($result === false) { |
| @@ -98,8 +74,67 @@ | ||
| 98 | 74 | throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__); |
| 99 | 75 | } |
| 100 | 76 | |
| 101 | 77 | return true; |
| 78 | + } | |
| 79 | + | |
| 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 | + ]; | |
| 102 | 137 | } |
| 103 | 138 | |
| 104 | 139 | /** |
| 105 | 140 | * Get all notifications by form ID. |