Skip to content

Instantly share code, notes, and snippets.

@annuman97
Created March 31, 2026 09:42
Show Gist options
  • Select an option

  • Save annuman97/0615d78608d91cc4b1d8ea3addbaebe9 to your computer and use it in GitHub Desktop.

Select an option

Save annuman97/0615d78608d91cc4b1d8ea3addbaebe9 to your computer and use it in GitHub Desktop.
Option(on User Profile) to display/hide Leaderboard publicly in Fluent Community
if (!defined('ABSPATH')) {
return;
}
if (!class_exists('FCOM_Leaderboard_Optout_Profile_Field_Snippet')) {
class FCOM_Leaderboard_Optout_Profile_Field_Snippet
{
const FIELD_SLUG = '_hide_from_leaderboard';
const GROUP_SLUG = '_privacy_settings';
public function __construct()
{
if ($this->is_fluent_community_ready()) {
$this->boot();
} else {
add_action('init', [$this, 'maybe_boot_later'], 20);
}
}
public function maybe_boot_later()
{
if ($this->is_fluent_community_ready()) {
$this->boot();
}
}
public function boot()
{
static $booted = false;
if ($booted) {
return;
}
$booted = true;
$this->maybe_register_profile_field();
add_filter('fluent_community/update_profile_data', [$this, 'capture_profile_preference'], 20, 3);
add_filter('fluent_community/leaderboard_api_response', [$this, 'filter_leaderboard_response'], 10, 3);
add_filter('fluent_community/profile_view_data', [$this, 'set_default_field_value'], 20, 2);
}
private function is_fluent_community_ready()
{
return class_exists('\FluentCommunity\App\Functions\Utility')
&& class_exists('\FluentCommunity\Framework\Support\Arr');
}
private function maybe_register_profile_field()
{
$features = \FluentCommunity\App\Functions\Utility::getFeaturesConfig();
if (($features['custom_profile_fields'] ?? 'no') !== 'yes') {
$features['custom_profile_fields'] = 'yes';
\FluentCommunity\App\Functions\Utility::updateOption('fluent_community_features', $features);
}
$config = \FluentCommunity\App\Functions\Utility::getOption('custom_profile_fields', []);
if (!is_array($config)) {
$config = [];
}
$config = wp_parse_args($config, [
'is_enabled' => 'yes',
'groups' => [],
'fields' => []
]);
if (($config['is_enabled'] ?? 'no') !== 'yes') {
$config['is_enabled'] = 'yes';
}
$groupExists = false;
foreach ((array) $config['groups'] as $group) {
if (($group['slug'] ?? '') === self::GROUP_SLUG) {
$groupExists = true;
break;
}
}
if (!$groupExists) {
$config['groups'][] = [
'slug' => self::GROUP_SLUG,
'label' => __('Privacy Settings', 'fcom-leaderboard-optout'),
'edit_description' => __('Manage your leaderboard visibility.', 'fcom-leaderboard-optout'),
'is_system' => false
];
}
$fieldExists = false;
foreach ((array) $config['fields'] as &$field) {
if (($field['slug'] ?? '') === self::FIELD_SLUG) {
$fieldExists = true;
$field['label'] = __('Hide me from leaderboard', 'fcom-leaderboard-optout');
$field['type'] = 'radio';
$field['options'] = ['No', 'Yes'];
$field['is_required'] = false;
$field['is_enabled'] = true;
$field['privacy'] = 'private';
$field['group'] = self::GROUP_SLUG;
}
}
unset($field);
if (!$fieldExists) {
$config['fields'][] = [
'slug' => self::FIELD_SLUG,
'label' => __('Hide me from leaderboard', 'fcom-leaderboard-optout'),
'type' => 'radio',
'placeholder' => '',
'options' => ['No', 'Yes'],
'is_required' => false,
'is_enabled' => true,
'privacy' => 'private',
'group' => self::GROUP_SLUG
];
}
\FluentCommunity\App\Functions\Utility::updateOption('custom_profile_fields', $config);
}
public function set_default_field_value($profileData, $xprofile)
{
if (empty($profileData['custom_field_groups']) || !is_array($profileData['custom_field_groups'])) {
return $profileData;
}
foreach ($profileData['custom_field_groups'] as $groupIndex => $group) {
if (empty($group['fields']) || !is_array($group['fields'])) {
continue;
}
foreach ($group['fields'] as $fieldIndex => $field) {
if (($field['slug'] ?? '') === self::FIELD_SLUG && empty($field['value'])) {
$profileData['custom_field_groups'][$groupIndex]['fields'][$fieldIndex]['value'] = 'No';
}
}
}
return $profileData;
}
public function capture_profile_preference($updateData, $data, $xProfile)
{
$customFields = $data['custom_fields'] ?? null;
if (!is_array($customFields) || !array_key_exists(self::FIELD_SLUG, $customFields)) {
return $updateData;
}
$value = sanitize_text_field(wp_unslash($customFields[self::FIELD_SLUG]));
$hide = strtolower($value) === 'yes';
$excluded = \FluentCommunity\App\Functions\Utility::getOption('fcom_leaderboard_excluded_user_ids', []);
if (!is_array($excluded)) {
$excluded = [];
}
$userId = (int) $xProfile->user_id;
$excluded = array_map('intval', $excluded);
if ($hide) {
$excluded[] = $userId;
} else {
$excluded = array_filter($excluded, function ($id) use ($userId) {
return (int) $id !== $userId;
});
}
$excluded = array_values(array_unique(array_map('intval', $excluded)));
\FluentCommunity\App\Functions\Utility::updateOption('fcom_leaderboard_excluded_user_ids', $excluded);
$this->clear_leaderboard_cache();
return $updateData;
}
private function clear_leaderboard_cache()
{
foreach (['leader_board_cache_7_10', 'leader_board_cache_30_10', 'leader_board_cache_0_10'] as $cacheKey) {
\FluentCommunity\App\Functions\Utility::deleteOption($cacheKey);
}
}
public function filter_leaderboard_response($response, $xProfiles, $requestData)
{
if (empty($response['leaderboard']) || !is_array($response['leaderboard'])) {
return $response;
}
foreach ($response['leaderboard'] as $groupIndex => $group) {
if (empty($group['items']) || !is_array($group['items'])) {
continue;
}
foreach ($group['items'] as $itemIndex => $item) {
$userId = 0;
if (!empty($item['user_id'])) {
$userId = (int) $item['user_id'];
} elseif (!empty($item['xprofile'])) {
if (is_object($item['xprofile']) && !empty($item['xprofile']->user_id)) {
$userId = (int) $item['xprofile']->user_id;
} elseif (is_array($item['xprofile']) && !empty($item['xprofile']['user_id'])) {
$userId = (int) $item['xprofile']['user_id'];
}
}
if (!$userId) {
continue;
}
$customFields = [];
if (!empty($item['xprofile'])) {
if (is_object($item['xprofile']) && !empty($item['xprofile']->custom_fields) && is_array($item['xprofile']->custom_fields)) {
$customFields = $item['xprofile']->custom_fields;
} elseif (is_array($item['xprofile']) && !empty($item['xprofile']['custom_fields']) && is_array($item['xprofile']['custom_fields'])) {
$customFields = $item['xprofile']['custom_fields'];
}
}
$hide = false;
if (array_key_exists(self::FIELD_SLUG, $customFields)) {
$hide = strtolower((string) $customFields[self::FIELD_SLUG]) === 'yes';
}
if (!$hide) {
$excluded = \FluentCommunity\App\Functions\Utility::getOption('fcom_leaderboard_excluded_user_ids', []);
if (is_array($excluded) && in_array($userId, array_map('intval', $excluded), true)) {
$hide = true;
}
}
if ($hide) {
unset($response['leaderboard'][$groupIndex]['items'][$itemIndex]);
}
}
$response['leaderboard'][$groupIndex]['items'] = array_values($response['leaderboard'][$groupIndex]['items']);
}
return $response;
}
}
new FCOM_Leaderboard_Optout_Profile_Field_Snippet();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment