| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See COPYING.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Services\Placeholder; |
| 9 |
|
| 10 |
use IvyForms\Common\Helpers\EntryHelper; |
| 11 |
use IvyForms\Services\Security\Common\IpDetectionService; |
| 12 |
use IvyForms\Entity\Notification\Notification; |
| 13 |
|
| 14 |
class PlaceholderService |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Replace placeholders in a template string with actual values. |
| 18 |
* |
| 19 |
* @param string $template |
| 20 |
* @param array<string, string|array<string, string>> $fieldData |
| 21 |
* @param array<string, string|int> $generalData |
| 22 |
* @param array<string, string> $fieldLabels |
| 23 |
* @param Notification|null $notificationContext Notification context for empty field filtering |
| 24 |
* @return string |
| 25 |
*/ |
| 26 |
public static function replacePlaceholders( |
| 27 |
string $template, |
| 28 |
array $fieldData = [], |
| 29 |
array $generalData = [], |
| 30 |
array $fieldLabels = [], |
| 31 |
?Notification $notificationContext = null |
| 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 { |
| 62 |
$placeholders = []; |
| 63 |
$allData = []; |
| 64 |
$addedLabels = []; |
| 65 |
|
| 66 |
foreach ($fieldData as $key => $value) { |
| 67 |
$valueString = self::normalizeFieldValue($value); |
| 68 |
$placeholders['{{' . $key . '}}'] = $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) === '') { |
| 76 |
$addedLabels[] = $label; |
| 77 |
continue; |
| 78 |
} |
| 79 |
|
| 80 |
$allData[] = $label . ': ' . $valueString; |
| 81 |
$addedLabels[] = $label; |
| 82 |
} |
| 83 |
|
| 84 |
if (!empty($fieldData)) { |
| 85 |
$placeholders['{{all_data}}'] = implode('<br>', $allData); |
| 86 |
} |
| 87 |
|
| 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 |
{ |
| 100 |
foreach ($generalData as $key => $value) { |
| 101 |
$placeholders['{{wp.' . $key . '}}'] = (string) $value; |
| 102 |
} |
| 103 |
|
| 104 |
return $placeholders; |
| 105 |
} |
| 106 |
|
| 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 |
} |
| 120 |
|
| 121 |
return (string) $value; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Build field data for placeholders from form fields and submission data. |
| 126 |
* |
| 127 |
* @param array<int, object> $formFields |
| 128 |
* @param array<string, string> $submissionData |
| 129 |
* @return array<string, string> |
| 130 |
*/ |
| 131 |
public static function buildFieldData( |
| 132 |
array $formFields, |
| 133 |
array $submissionData |
| 134 |
): array { |
| 135 |
$fieldData = []; |
| 136 |
foreach ($formFields as $field) { |
| 137 |
$typeIndex = $field->getType() . '_' . $field->getIndex(); |
| 138 |
$id = $field->getId(); |
| 139 |
if (isset($submissionData[$id])) { |
| 140 |
$value = $submissionData[$id]; |
| 141 |
|
| 142 |
/** |
| 143 |
* Filter to convert fields values to HTML representation |
| 144 |
* |
| 145 |
* @since 1.0.0 |
| 146 |
* |
| 147 |
* @param string $value The field value |
| 148 |
* @param object $field The field object |
| 149 |
* @param array $formFields All form fields (for parent-child lookups) |
| 150 |
* @return string The converted value |
| 151 |
*/ |
| 152 |
$value = apply_filters('ivyforms/placeholder/filter_field_value', $value, $field, $formFields); |
| 153 |
|
| 154 |
$fieldData[$typeIndex] = $value; |
| 155 |
$fieldData[$id] = $value; |
| 156 |
} |
| 157 |
} |
| 158 |
return $fieldData; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Build general data for placeholders from environment and submission data. |
| 163 |
* |
| 164 |
* @param int $entryId |
| 165 |
* @param array<string, mixed> $submissionData |
| 166 |
* @return array<string, string|int> |
| 167 |
*/ |
| 168 |
public static function buildGeneralData(int $entryId, array $submissionData = []): array |
| 169 |
{ |
| 170 |
$generalData = [ |
| 171 |
'site_url' => get_bloginfo('url'), |
| 172 |
'site_title' => get_bloginfo('name'), |
| 173 |
'admin_email' => get_bloginfo('admin_email'), |
| 174 |
'date' => date(get_option('date_format')), |
| 175 |
'user_ip' => IpDetectionService::getUserIpAddress(), |
| 176 |
'user_agent' => EntryHelper::getUserAgent(), |
| 177 |
'referer_url' => $submissionData['referer'] ?? '', |
| 178 |
]; |
| 179 |
|
| 180 |
if (!empty($submissionData['postId'])) { |
| 181 |
$post = get_post($submissionData['postId']); |
| 182 |
if ($post) { |
| 183 |
$generalData['post_id'] = $post->ID; |
| 184 |
$generalData['post_title'] = $post->post_title; |
| 185 |
$generalData['post_permalink'] = get_permalink($post->ID); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
if (is_user_logged_in()) { |
| 190 |
$currentUser = wp_get_current_user(); |
| 191 |
$generalData['user_id'] = $currentUser->ID; |
| 192 |
$generalData['user_name'] = $currentUser->user_login; |
| 193 |
$generalData['user_email'] = $currentUser->user_email; |
| 194 |
$generalData['user_first_name'] = $currentUser->user_firstname; |
| 195 |
$generalData['user_last_name'] = $currentUser->user_lastname; |
| 196 |
} |
| 197 |
if ($entryId) { |
| 198 |
$generalData['entry_id'] = $entryId; |
| 199 |
} |
| 200 |
return $generalData; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Build field labels mapping for placeholders from form fields. |
| 205 |
* |
| 206 |
* @param array<int, object> $formFields |
| 207 |
* @return array<string, string> |
| 208 |
*/ |
| 209 |
public static function buildFieldLabels(array $formFields): array |
| 210 |
{ |
| 211 |
$fieldLabels = []; |
| 212 |
foreach ($formFields as $field) { |
| 213 |
$typeIndex = $field->getType() . '_' . $field->getIndex(); |
| 214 |
$fieldLabels[$typeIndex] = $field->getFieldGeneralSettings()->getLabel(); |
| 215 |
$fieldLabels[$field->getId()] = $field->getFieldGeneralSettings()->getLabel(); |
| 216 |
} |
| 217 |
return $fieldLabels; |
| 218 |
} |
| 219 |
} |
| 220 |
|