PluginProbe
The Innovative Form Builder – IvyForms / 0.4
The Innovative Form Builder – IvyForms v0.4
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
ivyforms / backend / src / Services / Placeholder / PlaceholderService.php

PlaceholderService.php in The Innovative Form Builder – IvyForms 0.4, at backend/src/Services/Placeholder/PlaceholderService.php

153 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © TMS-Plugins. All rights reserved.
5 * @licence See COPYING.md for license details.
6 */
7
8 namespace IvyForms\Services\Placeholder;
9
10 // phpcs:disable PSR1.Files.SideEffects
11 use IvyForms\Repository\Entry\EntryHelper;
12
13 if (!defined('ABSPATH')) {
14 exit; // Exit if accessed directly
15 }
16
17 class PlaceholderService
18 {
19 /**
20 * Replace placeholders in a template string with actual values.
21 *
22 * @param string $template
23 * @param array<string, string|array<string, string>> $fieldData
24 * @param array<string, string|int> $generalData
25 * @param array<string, string> $fieldLabels
26 * @return string
27 */
28 public static function replacePlaceholders(
29 string $template,
30 array $fieldData = [],
31 array $generalData = [],
32 array $fieldLabels = []
33 ): string {
34 $placeholders = [];
35
36 // Field placeholders and all_data
37 $allData = [];
38 $addedLabels = [];
39 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;
46 $placeholders['{{' . $key . '}}'] = $valueString;
47 if (isset($fieldLabels[$key]) && !in_array($fieldLabels[$key], $addedLabels, true)) {
48 $label = $fieldLabels[$key];
49 $allData[] = $label . ': ' . $valueString;
50 $addedLabels[] = $label;
51 }
52 }
53 if (!empty($fieldData)) {
54 $placeholders['{{all_data}}'] = implode("\n", $allData);
55 }
56
57 // General placeholders
58 foreach ($generalData as $key => $value) {
59 $placeholders['{{wp.' . $key . '}}'] = $value;
60 }
61
62 // Replace all placeholders
63 $result = strtr($template, $placeholders);
64
65 // Remove any unreplaced placeholders
66 $result = preg_replace('/{{[^}]+}}/', '', $result);
67
68 return $result;
69 }
70
71 /**
72 * Build field data for placeholders from form fields and submission data.
73 *
74 * @param array<int, object> $formFields
75 * @param array<string, string> $submissionData
76 * @return array<string, string>
77 */
78 public static function buildFieldData(
79 array $formFields,
80 array $submissionData
81 ): array {
82 $fieldData = [];
83 foreach ($formFields as $field) {
84 $typeIndex = $field->getType() . '_' . $field->getIndex();
85 $id = $field->getId();
86 if (isset($submissionData[$id])) {
87 $fieldData[$typeIndex] = $submissionData[$id];
88 $fieldData[$id] = $submissionData[$id];
89 }
90 }
91 return $fieldData;
92 }
93
94 /**
95 * Build general data for placeholders from environment and submission data.
96 *
97 * @param int $entryId
98 * @param array<string, mixed> $submissionData
99 * @return array<string, string|int>
100 */
101 public static function buildGeneralData(int $entryId, array $submissionData = []): array
102 {
103 $generalData = [
104 'site_url' => get_bloginfo('url'),
105 'site_title' => get_bloginfo('name'),
106 'admin_email' => get_bloginfo('admin_email'),
107 'date' => date(get_option('date_format')),
108 'user_ip' => EntryHelper::getClientIpAddress() ?? '',
109 'user_agent' => EntryHelper::getUserAgent(),
110 'referer_url' => $submissionData['referer'] ?? '',
111 ];
112
113 if (!empty($submissionData['postId'])) {
114 $post = get_post($submissionData['postId']);
115 if ($post) {
116 $generalData['post_id'] = $post->ID;
117 $generalData['post_title'] = $post->post_title;
118 $generalData['post_permalink'] = get_permalink($post->ID);
119 }
120 }
121
122 if (is_user_logged_in()) {
123 $currentUser = wp_get_current_user();
124 $generalData['user_id'] = $currentUser->ID;
125 $generalData['user_name'] = $currentUser->user_login;
126 $generalData['user_email'] = $currentUser->user_email;
127 $generalData['user_first_name'] = $currentUser->user_firstname;
128 $generalData['user_last_name'] = $currentUser->user_lastname;
129 }
130 if ($entryId) {
131 $generalData['entry_id'] = $entryId;
132 }
133 return $generalData;
134 }
135
136 /**
137 * Build field labels mapping for placeholders from form fields.
138 *
139 * @param array<int, object> $formFields
140 * @return array<string, string>
141 */
142 public static function buildFieldLabels(array $formFields): array
143 {
144 $fieldLabels = [];
145 foreach ($formFields as $field) {
146 $typeIndex = $field->getType() . '_' . $field->getIndex();
147 $fieldLabels[$typeIndex] = $field->getFieldGeneralSettings()->getLabel();
148 $fieldLabels[$field->getId()] = $field->getFieldGeneralSettings()->getLabel();
149 }
150 return $fieldLabels;
151 }
152 }
153