Last active
September 11, 2025 11:13
-
-
Save goranefbl/954858bbd6c49166064ed7b2d355c8fb to your computer and use it in GitHub Desktop.
Newsletter override
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Earn Points Template - Custom Override with Newsletter Signup | |
*/ | |
if (!defined('ABSPATH')) { | |
exit; | |
} | |
$user_id = get_current_user_id(); | |
$settings = WPGL_Points_Core::get_settings(); | |
$earning_actions = WPGL_Points_Core::get_earning_actions(); | |
// Sort earning actions by points (descending) but keep PLACE_ORDER first | |
usort($earning_actions, function ($a, $b) { | |
if ($a['type'] === WPGL_Points_Source_Type::PLACE_ORDER) return -1; | |
if ($b['type'] === WPGL_Points_Source_Type::PLACE_ORDER) return 1; | |
return $b['points'] - $a['points']; | |
}); | |
// Get completed social actions | |
$completed_actions = get_user_meta($user_id, '_wpgens_loyalty_completed_actions', true) ?: array(); | |
// Check if this is being included from guest template | |
$is_guest = isset($is_guest_template) && $is_guest_template; | |
// Custom Newsletter Settings | |
$newsletter_enabled = get_option('wpgens_newsletter_enabled', false); | |
$newsletter_url = get_option('wpgens_newsletter_url', ''); | |
$newsletter_points = get_option('wpgens_newsletter_points', 50); | |
$newsletter_title = get_option('wpgens_newsletter_title', 'Newsletter Signup'); | |
$newsletter_completed = in_array('NEWSLETTER_SIGNUP', $completed_actions); | |
?> | |
<div class="wpgens-loyalty-listing"> | |
<ul class="wpgens-loyalty-listing-earn" data-tab-content="earn"> | |
<?php if (isset($earning_actions)): ?> | |
<?php foreach ($earning_actions as $action): ?> | |
<?php if ($action['enabled']): ?> | |
<li class="wpgens-loyalty-listing-item <?php echo esc_attr($action['type']); ?>"> | |
<?php | |
$is_completed = in_array($action['type'], $completed_actions); | |
$icon = WPGL_Points_Core::get_action_icon($action['type']); | |
// Determine if title should be clickable and get URL | |
$clickable_url = ''; | |
$is_refer_friend = ($action['type'] === WPGL_Points_Source_Type::REFER_FRIEND); | |
$is_social_action = false; | |
if (!$is_guest && !$is_completed) { | |
switch ($action['type']) { | |
case WPGL_Points_Source_Type::FACEBOOK_LIKE: | |
$clickable_url = $settings['facebookPageUrl'] ?? ''; | |
$is_social_action = true; | |
break; | |
case WPGL_Points_Source_Type::TWITTER_FOLLOW: | |
$clickable_url = $settings['twitterPageUrl'] ?? ''; | |
$is_social_action = true; | |
break; | |
case WPGL_Points_Source_Type::INSTAGRAM_LIKE: | |
$clickable_url = $settings['instagramPageUrl'] ?? ''; | |
$is_social_action = true; | |
break; | |
case WPGL_Points_Source_Type::TIKTOK_LIKE: | |
$clickable_url = $settings['tiktokPageUrl'] ?? ''; | |
$is_social_action = true; | |
break; | |
case WPGL_Points_Source_Type::PLACE_ORDER: | |
$clickable_url = $settings['placeOrderPageUrl'] ?? ''; | |
break; | |
case WPGL_Points_Source_Type::BIRTHDAY: | |
// Check if birthday is set and points not yet awarded | |
$birthday = get_user_meta($user_id, '_wpgens_loyalty_birthday', true); | |
if (empty($birthday) && !$is_completed) { | |
// Use the my-account page URL with edit-account endpoint | |
$myaccount_url = get_permalink(get_option('woocommerce_myaccount_page_id')); | |
if ($myaccount_url) { | |
$clickable_url = trailingslashit($myaccount_url) . 'edit-account'; | |
} | |
} | |
break; | |
case WPGL_Points_Source_Type::REFER_FRIEND: | |
// Get referral link for refer friend action | |
$referral_id = get_user_meta($user_id, 'gens_referral_id', true); | |
if ($referral_id) { | |
$clickable_url = add_query_arg('raf', $referral_id, home_url()); | |
} | |
break; | |
} | |
} | |
?> | |
<?php if ($icon): ?> | |
<?php echo wp_kses_post(WPGL_HTML_Helpers::kses_svg($icon)); ?> | |
<?php endif; ?> | |
<div class="wpgens-loyalty-listing-cost"> | |
<?php | |
if ($action['type'] === WPGL_Points_Source_Type::PLACE_ORDER && function_exists('get_woocommerce_currency_symbol')) { | |
echo esc_html(sprintf('1 %s = %s', get_woocommerce_currency_symbol(), WPGL_Points_Core::format_points($action['points']))); | |
} else { | |
echo esc_html(WPGL_Points_Core::format_points($action['points'])); | |
} | |
?> | |
</div> | |
<div class="wpgens-loyalty-listing-title"> | |
<?php if (!empty($clickable_url)): ?> | |
<?php if ($is_refer_friend): ?> | |
<a href="#" | |
class="wpgens-loyalty-copy-link" | |
data-link="<?php echo esc_url($clickable_url); ?>"> | |
<?php echo esc_html($action['title']); ?> | |
</a> | |
<?php else: ?> | |
<a href="<?php echo esc_url($clickable_url); ?>" | |
class="wpgens-loyalty-action-link<?php echo $is_social_action ? ' social-action' : ''; ?>" | |
data-action="<?php echo esc_attr($action['type']); ?>" | |
data-url="<?php echo esc_url($clickable_url); ?>" | |
target="_blank" | |
rel="noopener noreferrer"> | |
<?php echo esc_html($action['title']); ?> | |
</a> | |
<?php endif; ?> | |
<?php else: ?> | |
<span><?php echo esc_html($action['title']); ?></span> | |
<?php endif; ?> | |
<?php if ($is_completed && !$is_refer_friend): ?> | |
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="green" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-circle-check-big-icon lucide-circle-check-big"><path d="M21.801 10A10 10 0 1 1 17 3.335"/><path d="m9 11 3 3L22 4"/></svg> | |
<?php endif; ?> | |
</div> | |
</li> | |
<?php endif; ?> | |
<?php endforeach; ?> | |
<?php endif; ?> | |
<?php | |
// Custom Newsletter Signup Action | |
if (!$is_guest && $newsletter_enabled && !empty($newsletter_url)): ?> | |
<li class="wpgens-loyalty-listing-item NEWSLETTER_SIGNUP"> | |
<!-- Newsletter Icon --> | |
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round"> | |
<path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"></path> | |
<polyline points="22,6 12,13 2,6"></polyline> | |
</svg> | |
<div class="wpgens-loyalty-listing-cost"> | |
<?php echo esc_html(WPGL_Points_Core::format_points($newsletter_points)); ?> | |
</div> | |
<div class="wpgens-loyalty-listing-title"> | |
<?php if (!$newsletter_completed): ?> | |
<a href="<?php echo esc_url($newsletter_url); ?>" | |
class="wpgens-loyalty-action-link social-action" | |
data-action="NEWSLETTER_SIGNUP" | |
data-url="<?php echo esc_url($newsletter_url); ?>" | |
target="_blank" | |
rel="noopener noreferrer"> | |
<?php echo esc_html($newsletter_title); ?> | |
</a> | |
<?php else: ?> | |
<span><?php echo esc_html($newsletter_title); ?></span> | |
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="green" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-circle-check-big-icon lucide-circle-check-big"> | |
<path d="M21.801 10A10 10 0 1 1 17 3.335"/><path d="m9 11 3 3L22 4"/> | |
</svg> | |
<?php endif; ?> | |
</div> | |
</li> | |
<?php endif; ?> | |
</ul> | |
</div> | |
<?php if (!$is_guest && $newsletter_enabled): ?> | |
<script> | |
jQuery(document).ready(function($) { | |
// Handle newsletter signup action | |
$(document).on('click', '[data-action="NEWSLETTER_SIGNUP"]', function(e) { | |
e.preventDefault(); | |
const button = $(this); | |
const url = button.data('url'); | |
// Open newsletter URL in new tab | |
window.open(url, '_blank'); | |
// Disable button | |
button.prop('disabled', true); | |
// Award points after 10 seconds (same as other social actions) | |
setTimeout(function() { | |
$.ajax({ | |
url: '<?php echo admin_url('admin-ajax.php'); ?>', | |
type: 'POST', | |
data: { | |
action: 'wpgens_newsletter_signup', | |
nonce: '<?php echo wp_create_nonce('wpgens_newsletter_nonce'); ?>' | |
}, | |
success: function(response) { | |
if (response.success) { | |
button.replaceWith('<span class="wpgens-loyalty-completed-action"><?php echo esc_js(__('Completed', 'wpgens-loyalty-program')); ?></span>'); | |
location.reload(); | |
} else { | |
button.prop('disabled', false); | |
alert(response.data || '<?php echo esc_js(__('Error occurred', 'wpgens-loyalty-program')); ?>'); | |
} | |
}, | |
error: function() { | |
button.prop('disabled', false); | |
alert('<?php echo esc_js(__('Error occurred', 'wpgens-loyalty-program')); ?>'); | |
} | |
}); | |
}, 10000); // 10 second delay like other social actions | |
}); | |
}); | |
</script> | |
<?php endif; ?> | |
<?php | |
// THIS IS FOR FUNCTIONS.PHP | |
// Newsletter Loyalty Program Integration | |
add_action('init', function() { | |
// Handle AJAX request for newsletter signup | |
add_action('wp_ajax_wpgens_newsletter_signup', function() { | |
// Verify nonce | |
if (!wp_verify_nonce($_POST['nonce'], 'wpgens_newsletter_nonce')) { | |
wp_send_json_error('Security check failed.'); | |
} | |
$user_id = get_current_user_id(); | |
if (!$user_id) { | |
wp_send_json_error('You must be logged in to perform this action.'); | |
} | |
// Check if newsletter signup is enabled | |
if (!get_option('wpgens_newsletter_enabled', false)) { | |
wp_send_json_error('Newsletter signup is not enabled.'); | |
} | |
// Get completed actions | |
$completed_actions = get_user_meta($user_id, '_wpgens_loyalty_completed_actions', true) ?: array(); | |
// Check if already completed | |
if (in_array('NEWSLETTER_SIGNUP', $completed_actions)) { | |
wp_send_json_error('You have already completed this action.'); | |
} | |
// Get points to award | |
$points = get_option('wpgens_newsletter_points', 50); | |
if ($points <= 0) { | |
wp_send_json_error('No points available for this action.'); | |
} | |
// Award points using the loyalty plugin's action | |
do_action( | |
'wpgens_loyalty_update_points', | |
$user_id, | |
$points, | |
'EARN', // WPGL_Points_Activity_Type::EARN | |
'NEWSLETTER_SIGNUP' | |
); | |
// Mark action as completed | |
$completed_actions[] = 'NEWSLETTER_SIGNUP'; | |
update_user_meta($user_id, '_wpgens_loyalty_completed_actions', $completed_actions); | |
wp_send_json_success('Points awarded successfully!'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment