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