Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save annuman97/4daa3c9888ccddebc4212f7eba62b694 to your computer and use it in GitHub Desktop.

Select an option

Save annuman97/4daa3c9888ccddebc4212f7eba62b694 to your computer and use it in GitHub Desktop.
Upon clicking a Gutenberg button from custom lock screen Description section, user will be added to a CRM tag.
<?php
/**
* Add the user to a FluentCRM tag when a private-space request is submitted.
*/
add_action('fluent_community/space/join_requested', function ($space, $userId, $by) {
if (!defined('FLUENTCRM')) {
return;
}
// Space slug => FluentCRM Tag ID
$spaceTagMap = [
'start-here' => 1,
'jurassicninja' => 2,
];
$spaceSlug = $space->slug;
if (!isset($spaceTagMap[$spaceSlug])) {
return;
}
$tagId = $spaceTagMap[$spaceSlug];
$contact = FluentCrmApi('contacts')->getContactByUserRef($userId);
if (!$contact) {
$contactData = \FluentCrm\App\Services\Funnel\FunnelHelper::prepareUserData($userId);
$contactData['status'] = 'pending';
$contactData['source'] = 'fluent-community';
$contact = \FluentCrm\App\Services\Funnel\FunnelHelper::createOrUpdateContact($contactData);
}
if ($contact) {
$contact->attachTags([$tagId]);
}
}, 10, 3);
/**
* Make the custom Gutenberg "Join Waitlist" button submit the join request.
*/
add_action('fluent_community/portal_footer', function () {
?>
<style>
.fcom-waitlist-message {
display: block;
width: 100%;
max-width: 720px;
margin: 16px auto;
padding: 20px 24px;
border: 1px solid #d8e3ef;
border-radius: 12px;
background: #f7fbff;
color: #26364d;
font-size: 17px;
line-height: 1.6;
text-align: center;
box-shadow: 0 5px 18px rgba(31, 41, 55, 0.08);
}
.fcom-waitlist-message strong {
color: #142b4a;
font-weight: 700;
}
.fcom-waitlist-error {
margin-top: 12px;
color: #b42318;
text-align: center;
}
</style>
<script>
(function () {
function getSpaceSlug() {
if (
window.fcom_current_community &&
window.fcom_current_community.slug
) {
return window.fcom_current_community.slug;
}
const match = window.location.pathname.match(/\/space\/([^/]+)/);
return match ? match[1] : '';
}
function getCommunityName() {
if (
window.fcom_current_community &&
window.fcom_current_community.title
) {
return window.fcom_current_community.title;
}
return document.querySelector(
'.fcom_space_title, .fcom_space_header h1, h1'
)?.textContent.trim() || 'this community';
}
document.addEventListener('click', function (event) {
const button = event.target.closest(
'button, a, .wp-block-button__link'
);
if (!button || !/join\s+waitlist/i.test(button.textContent.trim())) {
return;
}
event.preventDefault();
event.stopPropagation();
if (button.dataset.waitlistSubmitting === 'true') {
return;
}
const spaceSlug = getSpaceSlug();
if (!spaceSlug || !window.FluentCommunityUtil?.Rest) {
return;
}
button.dataset.waitlistSubmitting = 'true';
button.disabled = true;
button.setAttribute('aria-busy', 'true');
button.textContent = 'Submitting...';
window.FluentCommunityUtil.Rest.post(
'spaces/' + encodeURIComponent(spaceSlug) + '/join'
)
.then(function () {
const communityName = getCommunityName();
const message = document.createElement('div');
message.className = 'fcom-waitlist-message';
message.setAttribute('role', 'status');
message.setAttribute('aria-live', 'polite');
message.innerHTML =
"You've been added to the <strong>" +
communityName +
"</strong> waitlist. We'll notify you as soon as this community unlocks.";
const buttonWrapper =
button.closest('.wp-block-button') ||
button.parentElement ||
button;
button.style.display = 'none';
buttonWrapper.appendChild(message);
})
.catch(function (error) {
console.error('Waitlist request failed:', error);
button.dataset.waitlistSubmitting = 'false';
button.disabled = false;
button.removeAttribute('aria-busy');
button.textContent = 'Join Waitlist';
const errorMessage = document.createElement('div');
errorMessage.className = 'fcom-waitlist-error';
errorMessage.textContent =
'Unable to submit your request. Please try again.';
button.parentElement.appendChild(errorMessage);
});
});
}());
</script>
<?php
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment