Created
January 15, 2026 17:06
-
-
Save goranefbl/fd6406385749b33a2c757fdcc32933cb to your computer and use it in GitHub Desktop.
sync date on imported points
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 | |
| /** | |
| * Sync import activity dates via admin URL | |
| * Usage: /wp-admin/admin.php?wpgens_sync_import_dates=1&_wpnonce=NONCE | |
| * | |
| * Generate the link by visiting any admin page and copying the nonce from: | |
| * /wp-admin/admin.php?wpgens_sync_import_dates=1&_wpnonce=<?php echo wp_create_nonce('wpgens_sync_import_dates'); ?> | |
| */ | |
| add_action('admin_init', function() { | |
| // Check if our parameter is set | |
| if (!isset($_GET['wpgens_sync_import_dates'])) { | |
| return; | |
| } | |
| // Security checks | |
| if (!current_user_can('manage_options')) { | |
| wp_die('Unauthorized access'); | |
| } | |
| if (!isset($_GET['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'])), 'wpgens_sync_import_dates')) { | |
| wp_die('Invalid nonce'); | |
| } | |
| global $wpdb; | |
| // Get all users with imported points - use their import timestamp as activity date | |
| $results = $wpdb->get_results( | |
| "SELECT user_id, MAX(timestamp) as last_activity | |
| FROM {$wpdb->prefix}wpgens_loyalty_points_log | |
| WHERE source = 'IMPORT' | |
| GROUP BY user_id" | |
| ); | |
| $updated = 0; | |
| foreach ($results as $row) { | |
| update_user_meta($row->user_id, '_wpgens_loyalty_last_activity_date', $row->last_activity); | |
| $updated++; | |
| } | |
| // Show result and stop | |
| wp_die( | |
| sprintf( | |
| '<h2>Sync Complete</h2><p>Updated activity dates for <strong>%d</strong> users based on their import timestamps.</p><p><a href="%s">← Back to Dashboard</a></p>', | |
| $updated, | |
| esc_url(admin_url()) | |
| ), | |
| 'Import Dates Synced', | |
| ['response' => 200] | |
| ); | |
| }); | |
| /** | |
| * Add a helper to show the sync link in admin | |
| * Adds a notice on the Loyalty Program pages with the sync link | |
| */ | |
| add_action('admin_notices', function() { | |
| $screen = get_current_screen(); | |
| // Only show on loyalty-related pages or plugins page | |
| if (!$screen || strpos($screen->id, 'wpgens-loyalty') === false) { | |
| return; | |
| } | |
| // Only for admins | |
| if (!current_user_can('manage_options')) { | |
| return; | |
| } | |
| // Check if there are any imports that might need syncing | |
| global $wpdb; | |
| $import_count = $wpdb->get_var( | |
| "SELECT COUNT(DISTINCT user_id) FROM {$wpdb->prefix}wpgens_loyalty_points_log WHERE source = 'IMPORT'" | |
| ); | |
| if ($import_count > 0) { | |
| $sync_url = wp_nonce_url( | |
| admin_url('admin.php?wpgens_sync_import_dates=1'), | |
| 'wpgens_sync_import_dates' | |
| ); | |
| printf( | |
| '<div class="notice notice-info is-dismissible"><p><strong>Loyalty Import Tool:</strong> You have %d users with imported points. <a href="%s">Click here to sync their activity dates</a> with their import timestamps (for expiration calculation).</p></div>', | |
| intval($import_count), | |
| esc_url($sync_url) | |
| ); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment