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/Services/Placeholder/PlaceholderService.php +82 -28 0.6 → 1.5 View file →
@@ -1,20 +1,17 @@
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 COPYING.md for license details.
6 6 */
7 7
8 8 namespace IvyForms\Services\Placeholder;
9 9
10 -// phpcs:disable PSR1.Files.SideEffects
11 10 use IvyForms\Common\Helpers\EntryHelper;
11 +use IvyForms\Services\Security\Common\IpDetectionService;
12 +use IvyForms\Entity\Notification\Notification;
12 13
13 -if (!defined('ABSPATH')) {
14 - exit; // Exit if accessed directly
15 -}
16 -
17 14 class PlaceholderService
18 15 {
19 16 /**
20 17 * Replace placeholders in a template string with actual values.
@@ -22,8 +19,9 @@
22 19 * @param string $template
23 20 * @param array<string, string|array<string, string>> $fieldData
24 21 * @param array<string, string|int> $generalData
25 22 * @param array<string, string> $fieldLabels
23 + * @param Notification|null $notificationContext Notification context for empty field filtering
26 24 * @return string
27 25 */
28 26 public static function replacePlaceholders(
29 27 string $template,
@@ -28,45 +26,100 @@
28 26 public static function replacePlaceholders(
29 27 string $template,
30 28 array $fieldData = [],
31 29 array $generalData = [],
32 - array $fieldLabels = []
30 + array $fieldLabels = [],
31 + ?Notification $notificationContext = null
33 32 ): string {
33 + $placeholders = self::buildFieldPlaceholders(
34 + $fieldData,
35 + $fieldLabels,
36 + $notificationContext !== null ? $notificationContext->isShowEmptyFields() : false
37 + );
38 + $placeholders = self::buildGeneralPlaceholders($placeholders, $generalData);
39 +
40 + // Replace all placeholders
41 + $result = strtr($template, $placeholders);
42 +
43 + // Remove any unreplaced placeholders
44 + $result = preg_replace('/{{[^}]+}}/', '', $result);
45 +
46 + return $result;
47 + }
48 +
49 + /**
50 + * Build placeholders for fields and {{all_data}}.
51 + *
52 + * @param array<string, string|array<string, string>> $fieldData
53 + * @param array<string, string> $fieldLabels
54 + * @param bool $showEmptyFields
55 + * @return array<string, string>
56 + */
57 + private static function buildFieldPlaceholders(
58 + array $fieldData,
59 + array $fieldLabels,
60 + bool $showEmptyFields
61 + ): array {
34 62 $placeholders = [];
35 -
36 - // Field placeholders and all_data
37 63 $allData = [];
38 64 $addedLabels = [];
65 +
39 66 foreach ($fieldData as $key => $value) {
40 - // If value is an array (parent-child fields), convert to string
41 - $valueString = is_array($value)
42 - ? implode(' ', array_filter(array_map(function ($val) {
43 - return $val;
44 - }, $value)))
45 - : $value;
67 + $valueString = self::normalizeFieldValue($value);
46 68 $placeholders['{{' . $key . '}}'] = $valueString;
47 - if (isset($fieldLabels[$key]) && !in_array($fieldLabels[$key], $addedLabels, true)) {
48 - $label = $fieldLabels[$key];
49 - $allData[] = $label . ': ' . $valueString;
69 +
70 + if (!isset($fieldLabels[$key]) || in_array($fieldLabels[$key], $addedLabels, true)) {
71 + continue;
72 + }
73 +
74 + $label = $fieldLabels[$key];
75 + if (!$showEmptyFields && trim((string) $valueString) === '') {
50 76 $addedLabels[] = $label;
77 + continue;
51 78 }
79 +
80 + $allData[] = $label . ': ' . $valueString;
81 + $addedLabels[] = $label;
52 82 }
83 +
53 84 if (!empty($fieldData)) {
54 - $placeholders['{{all_data}}'] = implode("\n", $allData);
85 + $placeholders['{{all_data}}'] = implode('<br>', $allData);
55 86 }
56 87
57 - // General placeholders
88 + return $placeholders;
89 + }
90 +
91 + /**
92 + * Build placeholders for general data.
93 + *
94 + * @param array<string, string> $placeholders
95 + * @param array<string, string|int> $generalData
96 + * @return array<string, string>
97 + */
98 + private static function buildGeneralPlaceholders(array $placeholders, array $generalData): array
99 + {
58 100 foreach ($generalData as $key => $value) {
59 - $placeholders['{{wp.' . $key . '}}'] = $value;
101 + $placeholders['{{wp.' . $key . '}}'] = (string) $value;
60 102 }
61 103
62 - // Replace all placeholders
63 - $result = strtr($template, $placeholders);
104 + return $placeholders;
105 + }
64 106
65 - // Remove any unreplaced placeholders
66 - $result = preg_replace('/{{[^}]+}}/', '', $result);
107 + /**
108 + * Normalize field value for placeholders.
109 + *
110 + * @param string|array<string, string> $value
111 + * @return string
112 + */
113 + private static function normalizeFieldValue($value): string
114 + {
115 + if (is_array($value)) {
116 + return implode(' ', array_filter(array_map(function ($val) {
117 + return $val;
118 + }, $value)));
119 + }
67 120
68 - return $result;
121 + return (string) $value;
69 122 }
70 123
71 124 /**
72 125 * Build field data for placeholders from form fields and submission data.
@@ -92,11 +145,12 @@
92 145 * @since 1.0.0
93 146 *
94 147 * @param string $value The field value
95 148 * @param object $field The field object
149 + * @param array $formFields All form fields (for parent-child lookups)
96 150 * @return string The converted value
97 151 */
98 - $value = apply_filters('ivyforms/placeholder/filter_field_value', $value, $field);
152 + $value = apply_filters('ivyforms/placeholder/filter_field_value', $value, $field, $formFields);
99 153
100 154 $fieldData[$typeIndex] = $value;
101 155 $fieldData[$id] = $value;
102 156 }
@@ -117,9 +171,9 @@
117 171 'site_url' => get_bloginfo('url'),
118 172 'site_title' => get_bloginfo('name'),
119 173 'admin_email' => get_bloginfo('admin_email'),
120 174 'date' => date(get_option('date_format')),
121 - 'user_ip' => EntryHelper::getClientIpAddress() ?? '',
175 + 'user_ip' => IpDetectionService::getUserIpAddress(),
122 176 'user_agent' => EntryHelper::getUserAgent(),
123 177 'referer_url' => $submissionData['referer'] ?? '',
124 178 ];
125 179