PluginProbe
The Innovative Form Builder – IvyForms / 0.8.1
The Innovative Form Builder – IvyForms v0.8.1
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.8.1, at backend/src/Services/Placeholder/PlaceholderService.php

166 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // phpcs:disable PSR1.Files.SideEffects
11 use IvyForms\Common\Helpers\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 $value = $submissionData[$id];
88
89 /**
90 * Filter to convert fields values to HTML representation
91 *
92 * @since 1.0.0
93 *
94 * @param string $value The field value
95 * @param object $field The field object
96 * @return string The converted value
97 */
98 $value = apply_filters('ivyforms/placeholder/filter_field_value', $value, $field);
99
100 $fieldData[$typeIndex] = $value;
101 $fieldData[$id] = $value;
102 }
103 }
104 return $fieldData;
105 }
106
107 /**
108 * Build general data for placeholders from environment and submission data.
109 *
110 * @param int $entryId
111 * @param array<string, mixed> $submissionData
112 * @return array<string, string|int>
113 */
114 public static function buildGeneralData(int $entryId, array $submissionData = []): array
115 {
116 $generalData = [
117 'site_url' => get_bloginfo('url'),
118 'site_title' => get_bloginfo('name'),
119 'admin_email' => get_bloginfo('admin_email'),
120 'date' => date(get_option('date_format')),
121 'user_ip' => EntryHelper::getClientIpAddress() ?? '',
122 'user_agent' => EntryHelper::getUserAgent(),
123 'referer_url' => $submissionData['referer'] ?? '',
124 ];
125
126 if (!empty($submissionData['postId'])) {
127 $post = get_post($submissionData['postId']);
128 if ($post) {
129 $generalData['post_id'] = $post->ID;
130 $generalData['post_title'] = $post->post_title;
131 $generalData['post_permalink'] = get_permalink($post->ID);
132 }
133 }
134
135 if (is_user_logged_in()) {
136 $currentUser = wp_get_current_user();
137 $generalData['user_id'] = $currentUser->ID;
138 $generalData['user_name'] = $currentUser->user_login;
139 $generalData['user_email'] = $currentUser->user_email;
140 $generalData['user_first_name'] = $currentUser->user_firstname;
141 $generalData['user_last_name'] = $currentUser->user_lastname;
142 }
143 if ($entryId) {
144 $generalData['entry_id'] = $entryId;
145 }
146 return $generalData;
147 }
148
149 /**
150 * Build field labels mapping for placeholders from form fields.
151 *
152 * @param array<int, object> $formFields
153 * @return array<string, string>
154 */
155 public static function buildFieldLabels(array $formFields): array
156 {
157 $fieldLabels = [];
158 foreach ($formFields as $field) {
159 $typeIndex = $field->getType() . '_' . $field->getIndex();
160 $fieldLabels[$typeIndex] = $field->getFieldGeneralSettings()->getLabel();
161 $fieldLabels[$field->getId()] = $field->getFieldGeneralSettings()->getLabel();
162 }
163 return $fieldLabels;
164 }
165 }
166