PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.4.01
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.4.01
2.11.0 2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 All 78 releases
fluent-community / app / Services / SmartCodeParser.php

SmartCodeParser.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.4.01, at app/Services/SmartCodeParser.php

327 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\App\Services;
4
5 use FluentCommunity\Modules\Course\Model\CourseTopic;
6 use FluentCommunity\Modules\Course\Model\Course;
7 use FluentCommunity\App\Services\ProfileHelper;
8 use FluentCommunity\App\Services\Helper;
9 use FluentCommunity\Framework\Support\Arr;
10
11 class SmartCodeParser
12 {
13 protected static $isHtml = true;
14
15 protected static $store = [
16 'user' => null,
17 'feed' => null,
18 'course' => null,
19 'community' => null
20 ];
21
22 public function parse($templateString, $user, $feed = null, $isHtml = true)
23 {
24 static::$isHtml = $isHtml;
25 static::setData($user, $feed);
26
27 $result = [];
28 $isSingle = false;
29
30 if (!is_array($templateString)) {
31 $isSingle = true;
32 }
33
34 foreach ((array)$templateString as $key => $string) {
35 $result[$key] = $this->parseShortcode($string);
36 }
37
38 if ($isSingle) {
39 return reset($result);
40 }
41
42 return $result;
43 }
44
45 protected static function setData($user, $feed = null)
46 {
47 static::$store['user'] = $user;
48 static::$store['feed'] = $feed;
49 static::$store['course'] = ($feed && $feed->course) ? $feed->course : null;
50 static::$store['community'] = Helper::generalSettings();
51 }
52
53 public function parseShortcode($string)
54 {
55 // check if the string contains any smartcode
56 if (strpos($string, '{{') === false && strpos($string, '##') === false) {
57 return $string;
58 }
59
60 return preg_replace_callback('/({{|##)+(.*?)(}}|##)/', function ($matches) {
61 return $this->replace($matches);
62 }, $string);
63 }
64
65 protected function replace($matches)
66 {
67 if (empty($matches[2])) {
68 return apply_filters('fluent_community/smartcode_fallback', $matches[0], $this->store['user']);
69 }
70
71 $matches[2] = trim($matches[2]);
72
73 $matched = explode('.', $matches[2]);
74
75 if (count($matched) <= 1) {
76 return apply_filters('fluent_community/smartcode_fallback', $matches[0], $this->store['user']);
77 }
78
79 $dataKey = trim(array_shift($matched));
80
81 $valueKey = trim(implode('.', $matched));
82
83 if (!$valueKey) {
84 return apply_filters('fluent_community/smartcode_fallback', $matches[0], $this->store['user']);
85 }
86
87 $valueKeys = explode('|', $valueKey);
88
89 $valueKey = $valueKeys[0];
90 $defaultValue = '';
91 $transformer = '';
92
93 $valueCounts = count($valueKeys);
94
95 if ($valueCounts >= 3) {
96 $defaultValue = trim($valueKeys[1]);
97 $transformer = trim($valueKeys[2]);
98 } else if ($valueCounts === 2) {
99 $defaultValue = trim($valueKeys[1]);
100 }
101
102 $value = '';
103 switch ($dataKey) {
104 case 'site':
105 $value = $this->getWpValue($valueKey, $defaultValue);
106 break;
107 case 'user':
108 $value = static::$store['user'] ? $this->getUserValue($valueKey, $defaultValue) : $defaultValue;
109 break;
110 case 'community':
111 $value = $this->getCommunityValue($valueKey, $defaultValue);
112 break;
113 case 'section':
114 $value = $this->getSectionValue($valueKey, $defaultValue);
115 break;
116 case 'course':
117 $value = $this->getCourseValue($valueKey, $defaultValue);
118 break;
119 default:
120 $value = apply_filters('fluent_community/smartcode_group_callback_' . $dataKey, $matches[0], $valueKey, $defaultValue, static::$store['user']);
121 }
122
123 if ($transformer && is_string($transformer) && $value) {
124 switch ($transformer) {
125 case 'trim':
126 return trim($value);
127 case 'ucfirst':
128 return ucfirst($value);
129 case 'strtolower':
130 return strtolower($value);
131 case 'strtoupper':
132 return strtoupper($value);
133 case 'ucwords':
134 return ucwords($value);
135 case 'concat_first': // usage: {{contact.first_name||concat_first|Hi
136 if (isset($valueKeys[3])) {
137 $value = trim($valueKeys[3] . ' ' . $value);
138 }
139 return $value;
140 case 'concat_last': // usage: {{contact.first_name||concat_last|, => FIRST_NAME,
141 if (isset($valueKeys[3])) {
142 $value = trim($value . '' . $valueKeys[3]);
143 }
144 return $value;
145 case 'show_if': // usage {{contact.first_name||show_if|First name exist
146 if (isset($valueKeys[3])) {
147 $value = $valueKeys[3];
148 }
149 return $value;
150 default:
151 return $value;
152 }
153 }
154
155 return $value;
156
157 }
158
159 protected function getWpValue($valueKey, $defaultValue)
160 {
161 if ($valueKey == 'login_url') {
162 return network_site_url('wp-login.php', 'login');
163 }
164
165 if ($valueKey == 'name') {
166 return wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
167 }
168
169 $value = get_bloginfo($valueKey);
170 if (!$value) {
171 return $defaultValue;
172 }
173 return $value;
174 }
175
176
177 protected function getUserValue($valueKey, $defaultValue)
178 {
179 $userModel = static::$store['user'];
180 if (!$userModel || !$userModel instanceof \FluentCommunity\App\Models\User) {
181 return $defaultValue;
182 }
183
184 $xProfile = $userModel->xprofile;
185
186 if ($valueKey == 'profile_link') {
187 if ($xProfile) {
188 return $xProfile->getPermalink();
189 }
190 return $defaultValue;
191 }
192
193 if ($xProfile) {
194 if ($valueKey == 'display_name') {
195 return $xProfile->display_name;
196 }
197 }
198
199 if ($valueKey == 'photo_html') {
200 if ($xProfile) {
201 return '<img src="' . esc_url($xProfile->avatar) . '" alt="' . esc_attr($xProfile->display_name) . '" class="fcom_user_dynamic_photo" />';
202 }
203 return '<img src="' . esc_url($userModel->photo) . '" alt="' . esc_attr($userModel->display_name) . '" class="fcom_user_dynamic_photo" />';
204 }
205
206 $wpUser = $userModel->getWpUser();
207 $valueKeys = explode('.', $valueKey);
208 if (count($valueKeys) == 1) {
209 $value = $wpUser->get($valueKey);
210 if (!$value) {
211 return $defaultValue;
212 }
213
214 if (!is_array($value) || !is_object($value)) {
215 return $value;
216 }
217
218 return $defaultValue;
219 }
220
221 $customKey = $valueKeys[0];
222 $customProperty = $valueKeys[1];
223
224 if ($customKey === 'photo_html') {
225 $width = (string)esc_attr($customProperty);
226 $style = 'style="width: ' . $width . '; height: ' . $width . ';"';
227 if ($xProfile) {
228 return '<img ' . $style . ' src="' . esc_url($xProfile->avatar) . '" alt="' . esc_attr($xProfile->display_name) . '" class="fcom_user_dynamic_photo" />';
229 }
230 return '<img ' . $style . ' src="' . esc_url($userModel->photo) . '" alt="' . esc_attr($userModel->display_name) . '" class="fcom_user_dynamic_photo" />';
231 }
232
233 if ($customKey == 'meta') {
234 $metaValue = get_user_meta($wpUser->ID, $customProperty, true);
235 if (!$metaValue) {
236 return $defaultValue;
237 }
238
239 if (!is_array($metaValue) || !is_object($metaValue)) {
240 return $metaValue;
241 }
242
243 return $defaultValue;
244 }
245
246 return $defaultValue;
247 }
248
249 protected function getCommunityValue($valueKey, $defaultValue)
250 {
251 $communitySettings = static::$store['community'];
252
253 if ($valueKey == 'name') {
254 return Arr::get($communitySettings, 'site_title');
255 }
256
257 if ($valueKey == 'name_with_url') {
258 $siteTitle = Arr::get($communitySettings, 'site_title');
259 return static::$isHtml ? '<a target="_blank" href="' . Helper::baseUrl('/') . '">' . $siteTitle . '</a>' : $siteTitle;
260 }
261
262 if (isset($communitySettings[$valueKey])) {
263 return $communitySettings[$valueKey];
264 }
265
266 return $defaultValue;
267 }
268
269 protected function getSectionValue($valueKey, $defaultValue)
270 {
271 $sectionModel = static::$store['feed'];
272 if (!$sectionModel || !$sectionModel instanceof CourseTopic) {
273 return $defaultValue;
274 }
275
276 if ($valueKey === 'url') {
277 $user = static::$store['user'] ?? null;
278 $course = static::$store['course'] ?? null;
279
280 if (!$course instanceof Course) {
281 return $defaultValue;
282 }
283
284 $courseUrl = $course->getPermalink();
285
286 $userId = $user->ID ?? null;
287 $signedUrl = $userId ? ProfileHelper::signUserUrlWithAuthHash($courseUrl, $userId) : $courseUrl;
288
289 if (static::$isHtml) {
290 return sprintf('<a href="%s">%s</a>', esc_url($signedUrl), esc_html($courseUrl));
291 }
292
293 return $signedUrl;
294 }
295
296 $fillables = array_merge(
297 (new CourseTopic())->getFillable(),
298 ['id', 'created_at', 'updated_at']
299 );
300
301 if (in_array($valueKey, $fillables)) {
302 return $sectionModel->{$valueKey};
303 }
304
305 return $defaultValue;
306 }
307
308 protected function getCourseValue($valueKey, $defaultValue)
309 {
310 $courseModel = static::$store['course'];
311 if (!$courseModel || !$courseModel instanceof Course) {
312 return $defaultValue;
313 }
314
315 $fillables = array_merge(
316 (new Course())->getFillable(),
317 ['id', 'created_at', 'updated_at']
318 );
319
320 if (in_array($valueKey, $fillables)) {
321 return $courseModel->{$valueKey};
322 }
323
324 return $defaultValue;
325 }
326 }
327