Skip to content

Instantly share code, notes, and snippets.

@Qubadi
Last active September 5, 2024 12:01
Show Gist options
  • Save Qubadi/ecc4e8db265ef2f2e642120bac2a8ef2 to your computer and use it in GitHub Desktop.
Save Qubadi/ecc4e8db265ef2f2e642120bac2a8ef2 to your computer and use it in GitHub Desktop.
JetEngine profilebuilder, profile view counter
Copy the following PHP code and create a PHP snippet using your snippet plugins.
Paste the code into the plugin and save it.
Add the [user_profile_view_count] shortcode to your profile single page
Custom JetEngine code that shows the view count of every profile in ProfileBuilder.
______________________________________
// Function to handle AJAX request for updating user profile views
function ajax_update_user_profile_views() {
// Check the nonce for security
check_ajax_referer('update_profile_view_nonce', 'nonce');
// Get the user identifier from the AJAX request
$user_identifier = isset($_POST['user_identifier']) ? sanitize_text_field($_POST['user_identifier']) : '';
$identifier_type = isset($_POST['identifier_type']) ? sanitize_text_field($_POST['identifier_type']) : '';
// Strict input validation for identifier_type
if (!in_array($identifier_type, ['id', 'nicename', 'username'], true)) {
wp_send_json_error('Invalid identifier type');
return;
}
if ($user_identifier && $identifier_type) {
// Get the user ID based on the identifier type
$user_id = 0;
switch ($identifier_type) {
case 'id':
$user_id = intval($user_identifier);
break;
case 'nicename':
$user = get_user_by('slug', $user_identifier);
if ($user) {
$user_id = $user->ID;
}
break;
case 'username':
$user = get_user_by('login', $user_identifier);
if ($user) {
$user_id = $user->ID;
}
break;
}
// Validate user existence
if ($user_id && get_userdata($user_id)) {
// Define the meta keys for storing view counts and last view time
$meta_key_count = 'jet_profile_views_count_engine';
$meta_key_last_view = 'jet_profile_last_view_time_engine';
// Get the current view count and last view time
$view_count = get_user_meta($user_id, $meta_key_count, true);
$last_view_time = get_user_meta($user_id, $meta_key_last_view, true);
// Get the current time
$current_time = time();
// If no view count exists, initialize it to 0
if ($view_count === '') {
$view_count = 0;
}
// Check if this is a new view (more than 24 hours since last view)
$time_threshold = 24 * 60 * 60; // 24 hours in seconds
if (empty($last_view_time) || ($current_time - intval($last_view_time)) > $time_threshold) {
// Increment the view count
$view_count++;
// Update the user meta with the new view count and current time
update_user_meta($user_id, $meta_key_count, $view_count);
update_user_meta($user_id, $meta_key_last_view, $current_time);
}
// Return the current view count
wp_send_json_success($view_count);
} else {
wp_send_json_error('User not found');
}
} else {
wp_send_json_error('Invalid user identifier or identifier type');
}
}
// Hook the AJAX function for logged-in users and guests
add_action('wp_ajax_update_user_profile_views', 'ajax_update_user_profile_views');
add_action('wp_ajax_nopriv_update_user_profile_views', 'ajax_update_user_profile_views');
// Shortcode to display user profile view count
function user_profile_view_count_shortcode() {
// Get the current user identifier from the URL
$user_identifier = get_query_var('jet_pb_user');
if (!$user_identifier) {
return ''; // Return nothing if no user identifier is found
}
// Determine the identifier type
$identifier_type = 'id'; // Default to ID
if (!is_numeric($user_identifier)) {
$identifier_type = username_exists($user_identifier) ? 'username' : 'nicename';
}
// Get the user ID
$user_id = 0;
switch ($identifier_type) {
case 'id':
$user_id = intval($user_identifier);
break;
case 'nicename':
$user = get_user_by('slug', $user_identifier);
if ($user) {
$user_id = $user->ID;
}
break;
case 'username':
$user = get_user_by('login', $user_identifier);
if ($user) {
$user_id = $user->ID;
}
break;
}
if (!$user_id) {
return ''; // Return nothing if no valid user is found
}
// Generate a nonce for security
$nonce = wp_create_nonce('update_profile_view_nonce');
// Inline JavaScript to trigger AJAX on page load and update display every 30 seconds
ob_start(); ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
function updateViewCount() {
$.post('<?php echo esc_url(admin_url('admin-ajax.php')); ?>', {
action: 'update_user_profile_views',
user_identifier: '<?php echo esc_js($user_identifier); ?>',
identifier_type: '<?php echo esc_js($identifier_type); ?>',
nonce: '<?php echo esc_js($nonce); ?>'
}, function(response) {
if (response.success) {
$('#profile-view-count').text('This profile has been viewed ' + response.data + ' times.');
} else {
console.error('Error updating view count:', response.data); // Error handling
}
}).fail(function(jqXHR, textStatus, errorThrown) {
console.error('AJAX request failed:', textStatus, errorThrown); // Error handling
});
}
// Initial update
updateViewCount();
// Update display every 30 seconds
setInterval(updateViewCount, 30000);
});
</script>
<?php
// Get the initial view count to display
$view_count = get_user_meta($user_id, 'jet_profile_views_count_engine', true);
$view_count = $view_count ? $view_count : 0;
// Return the view count with a placeholder for dynamic updates
return '<p id="profile-view-count">This profile has been viewed ' . esc_html($view_count) . ' times.</p>';
}
// Register the shortcode
add_shortcode('user_profile_view_count', 'user_profile_view_count_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment