PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.93
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.93
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 1.1.0 All 77 releases
fluent-community / app / Functions / Utility.php

Utility.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.0.93, at app/Functions/Utility.php

375 lines 10.7 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\Functions;
4
5 use FluentCommunity\App\Models\Meta;
6 use FluentCommunity\App\Models\Space;
7 use FluentCommunity\App\Models\SpaceGroup;
8 use FluentCommunity\App\Models\Term;
9 use FluentCommunity\App\Services\FeedsHelper;
10 use FluentCommunity\Framework\Support\Arr;
11 use FluentCommunity\Modules\Course\Model\Course;
12
13 class Utility
14 {
15
16 public static function getApp($instance = null)
17 {
18 return \FluentCommunity\App\App::getInstance($instance);
19 }
20
21 /**
22 * Get Global Fluent Community Option
23 * @param string $key The option name
24 * @param mixed $default the default value of the option if option is not available
25 * @return mixed
26 */
27 public static function getOption($key, $default = null)
28 {
29 return self::getFromCache('option_' . $key, function () use ($key, $default) {
30 $exist = \FluentCommunity\App\Models\Meta::where('object_type', 'option')
31 ->where('meta_key', $key)
32 ->first();
33
34 if ($exist) {
35 return $exist->value;
36 }
37
38 return $default;
39 });
40 }
41
42 public static function getFeaturesConfig()
43 {
44 static $features = null;
45
46 if ($features) {
47 return $features;
48 }
49
50 $features = self::getOption('fluent_community_features', []);
51
52 $defaults = [
53 'leader_board_module' => 'yes',
54 'course_module' => 'yes',
55 'giphy_module' => 'no',
56 'giphy_api_key' => '',
57 'emoji_module' => 'yes',
58 'cloud_storage' => 'no',
59 'invitation' => 'yes',
60 'user_badge' => 'yes',
61 ];
62
63
64 if (defined('FLUENT_COMMUNITY_CLOUD_STORAGE') && FLUENT_COMMUNITY_CLOUD_STORAGE) {
65 $features['cloud_storage'] = 'yes';
66 }
67
68 $features = wp_parse_args($features, $defaults);
69
70 $hasPro = defined('FLUENT_COMMUNITY_PRO') && FLUENT_COMMUNITY_PRO;
71 if (!$hasPro) {
72 $features['leader_board_module'] = 'no';
73 $features['giphy_module'] = 'no';
74 $features['emoji_module'] = 'no';
75 $features['cloud_storage'] = 'no';
76 $features['user_badge'] = 'no';
77 }
78
79 return $features;
80 }
81
82 /**
83 * Update Global Fluent Community Option
84 * @param string $key The option name
85 * @param mixed $value the value of the option
86 * @return \FluentCommunity\App\Models\Meta
87 */
88 public static function updateOption($key, $value)
89 {
90 $exist = \FluentCommunity\App\Models\Meta::where('object_type', 'option')
91 ->where('meta_key', $key)
92 ->first();
93 if ($exist) {
94 $exist->value = $value;
95 $exist->save();
96
97 } else {
98 $exist = \FluentCommunity\App\Models\Meta::create([
99 'meta_key' => $key,
100 'object_type' => 'option',
101 'value' => $value
102 ]);
103 }
104
105 self::setCache('option_' . $key, $value);
106
107 return $exist;
108 }
109
110 /**
111 * @param $key
112 * @param $callback
113 * @param $expire
114 * @return false|mixed
115 * @internal Internal Function
116 */
117 public static function getFromCache($key, $callback = false, $expire = 3600)
118 {
119 $key = 'focm_' . $key;
120
121 $value = wp_cache_get($key, 'fluent_community');
122
123 if ($value !== false) {
124 return $value;
125 }
126
127 if ($callback) {
128 $value = $callback();
129 if ($value) {
130 wp_cache_set($key, $value, 'fluent_community', $expire);
131 }
132 }
133
134 return $value;
135 }
136
137 /**
138 * @param $key
139 * @param $value
140 * @param $expire
141 * @return bool
142 * @internal Internal Function
143 */
144 public static function setCache($key, $value, $expire = 600)
145 {
146 $key = 'focm_' . $key;
147
148 return wp_cache_set($key, $value, 'fluent_community', $expire);
149 }
150
151 /**
152 * @param $key
153 * @return bool
154 * @internal Internal Function
155 */
156 public static function forgetCache($key)
157 {
158 $key = 'focm_' . $key;
159 return wp_cache_delete($key, 'fluent_community');
160 }
161
162 public static function getCustomizationSettings()
163 {
164 static $settings;
165
166 if ($settings) {
167 return $settings;
168 }
169
170 $defaults = [
171 'dark_mode' => 'yes',
172 'fixed_page_header' => 'yes',
173 'show_powered_by' => 'yes',
174 'feed_link_on_sidebar' => 'yes',
175 'fixed_sidebar' => 'no',
176 'icon_on_header_menu' => 'no',
177 'affiliate_id' => '',
178 'rich_post_layout' => 'classic',
179 'post_title_pref' => 'optional',
180 'a11y_enhancement_style' => 'no',
181 ];
182
183 $settings = self::getOption('customization_settings', $defaults);
184
185 $settings = wp_parse_args($settings, $defaults);
186
187 if (!defined('FLUENT_COMMUNITY_PRO')) {
188 $settings['show_powered_by'] = 'yes';
189 $settings['affiliate_id'] = '';
190 $settings['rich_post_layout'] = 'classic';
191 }
192
193 return $settings;
194 }
195
196 public static function getCustomizationSetting($key)
197 {
198 $settings = self::getCustomizationSettings();
199 return Arr::get($settings, $key);
200 }
201
202 public static function updateCustomizationSettings($settings)
203 {
204 $preSettings = self::getCustomizationSettings();
205 $settings = Arr::only($settings, array_keys($preSettings));
206
207 self::updateOption('customization_settings', $settings);
208
209 return $settings;
210 }
211
212 public static function isCustomizationEnabled($key, $matchingValue = 'yes')
213 {
214 $settings = self::getCustomizationSettings();
215 return isset($settings[$key]) && $settings[$key] === $matchingValue;
216 }
217
218 public static function getProductUrl($isPowered = false)
219 {
220 $url = 'https://fluentcommunity.co/';
221 if ($isPowered) {
222 $params = [
223 'utm_source' => 'power_footer',
224 'utm_medium' => 'site',
225 'utm_campaign' => 'powered_by'
226 ];
227 $settings = self::getCustomizationSettings();
228 $affId = Arr::get($settings, 'affiliate_id');
229 if ($affId) {
230 $params['ref'] = $affId;
231 }
232
233 $url = add_query_arg($params, $url);
234 }
235
236 return add_query_arg([
237 'utm_source' => 'plugin',
238 'utm_medium' => 'site',
239 'utm_campaign' => 'pligin_ui'
240 ], $url);
241 }
242
243 /**
244 * Get the email notification settings.
245 *
246 * @return array The email notification settings.
247 */
248 public static function getEmailNotificationSettings()
249 {
250 static $settings;
251 if ($settings) {
252 return $settings;
253 }
254
255 $default = [
256 'com_my_post_mail' => 'yes',
257 'reply_my_com_mail' => 'yes',
258 'mention_mail' => 'yes',
259 'digest_email_status' => 'no',
260 'digest_mail_day' => 'tue',
261 'daily_digest_time' => '09:00',
262 'send_from_email' => '',
263 'send_from_name' => '',
264 'reply_to_email' => '',
265 'reply_to_name' => '',
266 'email_footer' => 'You are getting this email because you are a member of {{site_name_with_url}}.\n\n{{manage_email_notification_url|Manage Your Email Notifications Preference}}.',
267 'disable_powered_by' => 'no',
268 ];
269
270 $settings = self::getOption('global_email_settings', $default);
271 $settings = wp_parse_args($settings, $default);
272
273 if (!defined('FLUENT_COMMUNITY_PRO')) {
274 $settings['disable_powered_by'] = 'no';
275 }
276
277 if (empty($settings['email_footer_rendered']) && !empty($settings['email_footer'])) {
278 $settings['email_footer_rendered'] = FeedsHelper::mdToHtml($settings['email_footer']);
279 }
280
281 return $settings;
282 }
283
284 public static function postTitlePref()
285 {
286 $pref = self::getCustomizationSetting('post_title_pref');
287
288 if ($pref == 'disabled') {
289 $pref = '';
290 }
291
292 return apply_filters('fluent_community/has_post_title', $pref);
293 }
294
295 public static function getSpaces($byGroups = false)
296 {
297 if ($byGroups) {
298 return SpaceGroup::orderBy('serial', 'ASC')->with('spaces', function ($q) {
299 $q->orderBy('serial', 'ASC')
300 ->where('type', 'community');
301 })->all();
302 }
303
304 return Space::where('type', 'community')->orderBy('serial', 'ASC')->get();
305 }
306
307 public static function getCourses($byGroups = false)
308 {
309 if ($byGroups) {
310 return SpaceGroup::orderBy('serial', 'ASC')->with('spaces', function ($q) {
311 $q->orderBy('serial', 'ASC')
312 ->where('type', 'course');
313 })->all();
314 }
315
316 return Course::orderBy('serial', 'ASC')->get();
317 }
318
319 public static function getTopics()
320 {
321 static $topics;
322 if ($topics) {
323 return $topics;
324 }
325
326 $key = 'fluent_community_post_topics';
327 $topics = self::getFromCache($key, function () {
328 $topics = Term::where('taxonomy_name', 'post_topic')->orderBy('title', 'ASC')->get();
329
330 /*
331 * object_id = term_id
332 * meta_key = space_id
333 */
334 $topicSpaceRelations = Meta::select(['id', 'object_id', 'meta_key'])->where('object_type', 'term_space_relation')
335 ->get();
336
337 $relations = [];
338 foreach ($topicSpaceRelations as $relation) {
339 if (!isset($relations[$relation->object_id])) {
340 $relations[$relation->object_id] = [];
341 }
342
343 $relations[$relation->object_id][] = $relation->meta_key;
344 }
345
346 $formattedTopics = [];
347 foreach ($topics as $topic) {
348 $formattedTopics[] = [
349 'id' => $topic->id,
350 'title' => $topic->title,
351 'slug' => $topic->slug,
352 'admin_only' => Arr::get($topic->settings, 'admin_only', 'no'),
353 'space_ids' => isset($relations[$topic->id]) ? $relations[$topic->id] : []
354 ];
355 }
356
357 return $formattedTopics;
358 }, MONTH_IN_SECONDS);
359
360 return $topics;
361 }
362
363 public static function getTopicsBySpaceId($spaceId)
364 {
365 $topics = self::getTopics();
366
367 $topics = array_filter($topics, function ($topic) use ($spaceId) {
368 return in_array($spaceId, $topic['space_ids']);
369 });
370
371 return $topics;
372 }
373
374 }
375