# fluent-community/1.0.90/app/Functions/Utility.php

FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS &amp; Online Courses, version 1.0.90. 375 lines.

- Page: https://pluginprobe.com/plugins/fluent-community/1.0.90/code/app/Functions/Utility.php
- Raw: https://pluginprobe.com/plugins/fluent-community/1.0.90/raw/app/Functions/Utility.php
- Modified: 2024-11-07T12:38:44+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/fluent-community/1.0.90/code/app/Functions/Utility.php#L10-L20`.

```php
<?php

namespace FluentCommunity\App\Functions;

use FluentCommunity\App\Models\Meta;
use FluentCommunity\App\Models\Space;
use FluentCommunity\App\Models\SpaceGroup;
use FluentCommunity\App\Models\Term;
use FluentCommunity\App\Services\FeedsHelper;
use FluentCommunity\Framework\Support\Arr;
use FluentCommunity\Modules\Course\Model\Course;

class Utility
{

    public static function getApp($instance = null)
    {
        return \FluentCommunity\App\App::getInstance($instance);
    }

    /**
     * Get Global Fluent Community Option
     * @param string $key The option name
     * @param mixed $default the default value of the option if option is not available
     * @return mixed
     */
    public static function getOption($key, $default = null)
    {
        return self::getFromCache('option_' . $key, function () use ($key, $default) {
            $exist = \FluentCommunity\App\Models\Meta::where('object_type', 'option')
                ->where('meta_key', $key)
                ->first();

            if ($exist) {
                return $exist->value;
            }

            return $default;
        });
    }

    public static function getFeaturesConfig()
    {
        static $features = null;

        if ($features) {
            return $features;
        }

        $features = self::getOption('fluent_community_features', []);

        $defaults = [
            'leader_board_module' => 'yes',
            'course_module'       => 'yes',
            'giphy_module'        => 'no',
            'giphy_api_key'       => '',
            'emoji_module'        => 'yes',
            'cloud_storage'       => 'no',
            'invitation'          => 'yes',
            'user_badge'          => 'yes',
        ];


        if (defined('FLUENT_COMMUNITY_CLOUD_STORAGE') && FLUENT_COMMUNITY_CLOUD_STORAGE) {
            $features['cloud_storage'] = 'yes';
        }

        $features = wp_parse_args($features, $defaults);

        $hasPro = defined('FLUENT_COMMUNITY_PRO') && FLUENT_COMMUNITY_PRO;
        if (!$hasPro) {
            $features['leader_board_module'] = 'no';
            $features['giphy_module'] = 'no';
            $features['emoji_module'] = 'no';
            $features['cloud_storage'] = 'no';
            $features['user_badge'] = 'no';
        }

        return $features;
    }

    /**
     * Update Global Fluent Community Option
     * @param string $key The option name
     * @param mixed $value the value of the option
     * @return \FluentCommunity\App\Models\Meta
     */
    public static function updateOption($key, $value)
    {
        $exist = \FluentCommunity\App\Models\Meta::where('object_type', 'option')
            ->where('meta_key', $key)
            ->first();
        if ($exist) {
            $exist->value = $value;
            $exist->save();

        } else {
            $exist = \FluentCommunity\App\Models\Meta::create([
                'meta_key'    => $key,
                'object_type' => 'option',
                'value'       => $value
            ]);
        }

        self::setCache('option_' . $key, $value);

        return $exist;
    }

    /**
     * @param $key
     * @param $callback
     * @param $expire
     * @return false|mixed
     * @internal Internal Function
     */
    public static function getFromCache($key, $callback = false, $expire = 3600)
    {
        $key = 'focm_' . $key;

        $value = wp_cache_get($key, 'fluent_community');

        if ($value !== false) {
            return $value;
        }

        if ($callback) {
            $value = $callback();
            if ($value) {
                wp_cache_set($key, $value, 'fluent_community', $expire);
            }
        }

        return $value;
    }

    /**
     * @param $key
     * @param $value
     * @param $expire
     * @return bool
     * @internal Internal Function
     */
    public static function setCache($key, $value, $expire = 600)
    {
        $key = 'focm_' . $key;

        return wp_cache_set($key, $value, 'fluent_community', $expire);
    }

    /**
     * @param $key
     * @return bool
     * @internal Internal Function
     */
    public static function forgetCache($key)
    {
        $key = 'focm_' . $key;
        return wp_cache_delete($key, 'fluent_community');
    }

    public static function getCustomizationSettings()
    {
        static $settings;

        if ($settings) {
            return $settings;
        }

        $defaults = [
            'dark_mode'              => 'yes',
            'fixed_page_header'      => 'yes',
            'show_powered_by'        => 'yes',
            'feed_link_on_sidebar'   => 'yes',
            'fixed_sidebar'          => 'no',
            'icon_on_header_menu'    => 'no',
            'affiliate_id'           => '',
            'rich_post_layout'       => 'classic',
            'post_title_pref'        => 'optional',
            'a11y_enhancement_style' => 'no',
        ];

        $settings = self::getOption('customization_settings', $defaults);

        $settings = wp_parse_args($settings, $defaults);

        if (!defined('FLUENT_COMMUNITY_PRO')) {
            $settings['show_powered_by'] = 'yes';
            $settings['affiliate_id'] = '';
            $settings['rich_post_layout'] = 'classic';
        }

        return $settings;
    }

    public static function getCustomizationSetting($key)
    {
        $settings = self::getCustomizationSettings();
        return Arr::get($settings, $key);
    }

    public static function updateCustomizationSettings($settings)
    {
        $preSettings = self::getCustomizationSettings();
        $settings = Arr::only($settings, array_keys($preSettings));

        self::updateOption('customization_settings', $settings);

        return $settings;
    }

    public static function isCustomizationEnabled($key, $matchingValue = 'yes')
    {
        $settings = self::getCustomizationSettings();
        return isset($settings[$key]) && $settings[$key] === $matchingValue;
    }

    public static function getProductUrl($isPowered = false)
    {
        $url = 'https://fluentcommunity.co/';
        if ($isPowered) {
            $params = [
                'utm_source'   => 'power_footer',
                'utm_medium'   => 'site',
                'utm_campaign' => 'powered_by'
            ];
            $settings = self::getCustomizationSettings();
            $affId = Arr::get($settings, 'affiliate_id');
            if ($affId) {
                $params['ref'] = $affId;
            }

            $url = add_query_arg($params, $url);
        }

        return add_query_arg([
            'utm_source'   => 'plugin',
            'utm_medium'   => 'site',
            'utm_campaign' => 'pligin_ui'
        ], $url);
    }

    /**
     * Get the email notification settings.
     *
     * @return array The email notification settings.
     */
    public static function getEmailNotificationSettings()
    {
        static $settings;
        if ($settings) {
            return $settings;
        }

        $default = [
            'com_my_post_mail'    => 'yes',
            'reply_my_com_mail'   => 'yes',
            'mention_mail'        => 'yes',
            'digest_email_status' => 'no',
            'digest_mail_day'     => 'tue',
            'daily_digest_time'   => '09:00',
            'send_from_email'     => '',
            'send_from_name'      => '',
            'reply_to_email'      => '',
            'reply_to_name'       => '',
            '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}}.',
            'disable_powered_by'  => 'no',
        ];

        $settings = self::getOption('global_email_settings', $default);
        $settings = wp_parse_args($settings, $default);

        if (!defined('FLUENT_COMMUNITY_PRO')) {
            $settings['disable_powered_by'] = 'no';
        }

        if (empty($settings['email_footer_rendered']) && !empty($settings['email_footer'])) {
            $settings['email_footer_rendered'] = FeedsHelper::mdToHtml($settings['email_footer']);
        }

        return $settings;
    }

    public static function postTitlePref()
    {
        $pref = self::getCustomizationSetting('post_title_pref');

        if ($pref == 'disabled') {
            $pref = '';
        }

        return apply_filters('fluent_community/has_post_title', $pref);
    }

    public static function getSpaces($byGroups = false)
    {
        if ($byGroups) {
            return SpaceGroup::orderBy('serial', 'ASC')->with('spaces', function ($q) {
                $q->orderBy('serial', 'ASC')
                    ->where('type', 'community');
            })->all();
        }

        return Space::where('type', 'community')->orderBy('serial', 'ASC')->get();
    }

    public static function getCourses($byGroups = false)
    {
        if ($byGroups) {
            return SpaceGroup::orderBy('serial', 'ASC')->with('spaces', function ($q) {
                $q->orderBy('serial', 'ASC')
                    ->where('type', 'course');
            })->all();
        }

        return Course::orderBy('serial', 'ASC')->get();
    }

    public static function getTopics()
    {
        static $topics;
        if ($topics) {
            return $topics;
        }

        $key = 'fluent_community_post_topics';
        $topics = self::getFromCache($key, function () {
            $topics = Term::where('taxonomy_name', 'post_topic')->orderBy('title', 'ASC')->get();

            /*
             *  object_id = term_id
             *  meta_key = space_id
             */
            $topicSpaceRelations = Meta::select(['id', 'object_id', 'meta_key'])->where('object_type', 'term_space_relation')
                ->get();

            $relations = [];
            foreach ($topicSpaceRelations as $relation) {
                if (!isset($relations[$relation->object_id])) {
                    $relations[$relation->object_id] = [];
                }

                $relations[$relation->object_id][] = $relation->meta_key;
            }

            $formattedTopics = [];
            foreach ($topics as $topic) {
                $formattedTopics[] = [
                    'id'         => $topic->id,
                    'title'      => $topic->title,
                    'slug'       => $topic->slug,
                    'admin_only' => Arr::get($topic->settings, 'admin_only', 'no'),
                    'space_ids'  => isset($relations[$topic->id]) ? $relations[$topic->id] : []
                ];
            }

            return $formattedTopics;
        }, MONTH_IN_SECONDS);

        return $topics;
    }

    public static function getTopicsBySpaceId($spaceId)
    {
        $topics = self::getTopics();

        $topics = array_filter($topics, function ($topic) use ($spaceId) {
            return in_array($spaceId, $topic['space_ids']);
        });

        return $topics;
    }

}

```
