hide_errors(); require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $charset_collate = $wpdb->get_charset_collate(); $subscription_table = $wpdb->prefix . 'wc_admail_subscriptions'; if ($wpdb->get_var("SHOW TABLES LIKE '$subscription_table'") !== $subscription_table) { $sql = "CREATE TABLE `$subscription_table` ( email VARCHAR(255) NOT NULL, sessionID VARCHAR(100) NOT NULL, product_id BIGINT(20) UNSIGNED NOT NULL, status VARCHAR(20) NOT NULL DEFAULT 'enabled', lang VARCHAR(10) DEFAULT NULL, subscription_date DATETIME DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (email, product_id) ) ENGINE=InnoDB $charset_collate;"; dbDelta($sql); } } ambisn_create_wc_admail_subscriptions_table(); $plugin_stored_data = get_option('ambisn_plugin_data', []); $plugin_stored_data = is_array($plugin_stored_data) ? $plugin_stored_data : []; $plugin_stored_data['actions'] = $plugin_stored_data['actions'] ?? []; $ambisn_subscriptions_migration_process = $plugin_stored_data['actions']['subscriptions_migration_process'] ?? []; if (!empty($plugin_stored_data['actions']['merge_subscriptions_to_admail_table'])) { return; } if (!wp_next_scheduled('ambisn_subscription_migration_event')) { wp_schedule_single_event(time() + 1, 'ambisn_subscription_migration_event'); } if (empty($ambisn_subscriptions_migration_process['s']) || $ambisn_subscriptions_migration_process['s'] !== 'started') { ambisn_start_subscription_migration(); } elseif (empty($ambisn_subscriptions_migration_process['gids']) || $ambisn_subscriptions_migration_process['gids'] !== 'finished') { add_action('ambisn_subscription_migration_event', 'ambisn_migration_get_product_ids_task'); } elseif (empty($ambisn_subscriptions_migration_process['ms']) || $ambisn_subscriptions_migration_process['ms'] !== 'finished') { add_action('ambisn_subscription_migration_event', 'ambisn_migrate_subscriptions_in_background'); } // Start the subscription migration process function ambisn_start_subscription_migration() { global $wpdb; $table_name = $wpdb->prefix . 'wc_admail'; $count = (int) $wpdb->get_var("SELECT COUNT(*) FROM $table_name"); $plugin_stored_data = get_option('ambisn_plugin_data', []); if (!is_array($plugin_stored_data)) { $plugin_stored_data = []; } if ($count === 0) { wp_schedule_single_event(time() + 1, 'ambisn_migrate_subscriptions_end_event'); $plugin_stored_data['actions']['merge_subscriptions_to_admail_table'] = true; update_option('ambisn_plugin_data', $plugin_stored_data); return; } set_transient( 'ambisn_migration_notice', 'AdMail: Subscription Migration Started – We’re improving subscription performance.', 12 * HOUR_IN_SECONDS ); $plugin_stored_data['actions']['subscriptions_migration_process'] = ['s' => 'started']; update_option('ambisn_plugin_data', $plugin_stored_data); } // Collect product IDs for migration function ambisn_migration_get_variation_ids($parent_product_id) { $variation_ids = get_posts([ 'post_parent' => $parent_product_id, 'post_type' => 'product_variation', 'fields' => 'ids', 'posts_per_page' => -1 ]); $valid_variation_ids = []; foreach ($variation_ids as $variation_id) { $variationSubscribers = get_post_meta($variation_id, 'wc_admail_data', true) ?: []; if (!empty($variationSubscribers) && is_array($variationSubscribers)) { $valid_variation_ids[] = $variation_id; } } return $valid_variation_ids; } function ambisn_migration_get_product_ids_task() { global $wpdb; $batch_size = 1000; $plugin_stored_data = get_option('ambisn_plugin_data', []); if (!is_array($plugin_stored_data)) { $plugin_stored_data = []; } $ambisn_subscriptions_migration_process = $plugin_stored_data['actions']['subscriptions_migration_process'] ?? []; $offset = 0; if (isset($ambisn_subscriptions_migration_process['gids']) && intval($ambisn_subscriptions_migration_process['gids'])) { $offset = intval($ambisn_subscriptions_migration_process['gids']); } $saved_product_ids = get_option('ambisn_migration_product_ids', []); $query = $wpdb->prepare( "SELECT p.ID FROM {$wpdb->prefix}posts p WHERE p.post_type = 'product' ORDER BY p.ID ASC LIMIT %d, %d", $offset, $batch_size ); $product_ids = $wpdb->get_col($query); if (!empty($product_ids)) { $placeholders = implode(',', array_fill(0, count($product_ids), '%d')); $query = $wpdb->prepare( "SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = 'wc_admail' AND post_id IN ($placeholders)", ...$product_ids ); $filtered_product_ids = $wpdb->get_col($query); $filtered_global_ids = []; foreach ($filtered_product_ids as $product_id) { $productSubscribers = get_post_meta($product_id, 'wc_admail_data', true) ?: []; if (!empty($productSubscribers) && is_array($productSubscribers)) { $filtered_global_ids[] = $product_id; } $product_types = get_the_terms($product_id, 'product_type'); if ($product_types && !is_wp_error($product_types)) { $product_type = $product_types[0]->slug; $ambisn_enabled = get_post_meta($product_id, 'wc_admail', true); if ($product_type === 'variable') { $variation_ids = ambisn_migration_get_variation_ids($product_id); $filtered_global_ids = array_merge($filtered_global_ids, $variation_ids); } } } update_option('ambisn_migration_product_ids', array_merge($saved_product_ids, $filtered_global_ids)); $saved_product_ids_count = count($saved_product_ids) + count($filtered_global_ids); $plugin_stored_data['actions']['subscriptions_migration_process'] = array('s' => 'started', 'gids' => ($offset + $batch_size)); update_option('ambisn_plugin_data', $plugin_stored_data); wp_schedule_single_event(time() + 1, 'ambisn_migration_get_product_ids_task'); $ambisn_migration_notice = "AdMail: Scanning for Subscriptions – We are identifying products with active subscriptions to improve performance. This process will be completed shortly..."; set_transient('ambisn_migration_notice', $ambisn_migration_notice, 12 * HOUR_IN_SECONDS); } else { $plugin_stored_data['actions']['subscriptions_migration_process'] = array('s' => 'started', 'gids' => 'finished'); update_option('ambisn_plugin_data', $plugin_stored_data); delete_option('ambisn_migration_offset'); $ambisn_migration_notice = 'AdMail: We\'re improving subscription performance: The update is in progress, and we\'re working to make it faster. 0% completed.'; set_transient('ambisn_migration_notice', $ambisn_migration_notice, 12 * HOUR_IN_SECONDS); } } // Migrate subscriptions in background function ambisn_migrate_subscriptions_in_background() { global $wpdb; $plugin_stored_data = get_option('ambisn_plugin_data', []); if (!is_array($plugin_stored_data)) { $plugin_stored_data = []; } update_option('wc_admail_subscriptions_migration_status', 'in_progress'); $processed_count = get_transient('wc_admail_processed_count') ?: 0; $product_ids = get_option('ambisn_migration_product_ids'); $total_count = count($product_ids); if (!is_array($product_ids) || $total_count === 0) { wp_schedule_single_event(time() + 1, 'ambisn_migrate_subscriptions_end_event'); $plugin_stored_data['actions']['merge_subscriptions_to_admail_table'] = true; update_option('ambisn_plugin_data', $plugin_stored_data); return; } $sliced_product_ids = array_slice($product_ids, $processed_count, AMBISN_BATCH_SIZE); foreach ($sliced_product_ids as $product_id) { $product_subscribers = get_post_meta($product_id, 'wc_admail_data', true) ?: []; if (count($product_subscribers) > 0) { foreach ($product_subscribers as $email => $subscription_data) { $existing_subscription = $wpdb->get_var( $wpdb->prepare( "SELECT email FROM {$wpdb->prefix}wc_admail_subscriptions WHERE email = %s AND product_id = %d", $email, $product_id ) ); if (!$existing_subscription) { $wpdb->insert( $wpdb->prefix . 'wc_admail_subscriptions', array( 'email' => $email, 'product_id' => $product_id, 'status' => isset($subscription_data['status']) ? $subscription_data['status'] : 'enabled', 'sessionID' => isset($subscription_data['sessionID']) ? $subscription_data['sessionID'] : '', 'lang' => isset($subscription_data['lang']) ? $subscription_data['lang'] : '', 'subscription_date' => isset($subscription_data['date']) ? $subscription_data['date'] : current_time('mysql'), ) ); } } } $processed_count++; } $progress = intval(($processed_count / $total_count) * 100); set_transient('wc_admail_subscriptions_progress', $progress, 12 * HOUR_IN_SECONDS); set_transient('wc_admail_processed_count', $processed_count , 12 * HOUR_IN_SECONDS); if ($processed_count == $total_count) { $ambisn_migration_notice = 'AdMail: Subscription Performance Enhanced – The update has been successfully completed.'; set_transient('ambisn_migration_notice', $ambisn_migration_notice , 12 * HOUR_IN_SECONDS); wp_schedule_single_event(time() + 1, 'ambisn_migrate_subscriptions_end_event'); }else{ $ambisn_migration_notice = 'AdMail: We\'re improving subscription performance: The update is in progress, and we\'re working to make it faster. ' . $progress . '% completed.'; set_transient('ambisn_migration_notice', $ambisn_migration_notice , 12 * HOUR_IN_SECONDS); wp_schedule_single_event(time() + 1, 'ambisn_migrate_subscriptions_task'); } } function ambisn_finalize_migration_process(){ delete_transient('wc_admail_subscriptions_progress'); delete_transient('wc_admail_processed_count'); delete_option('ambisn_migration_product_ids'); delete_transient('ambisn_migration_notice'); $plugin_stored_data = get_option('ambisn_plugin_data', []); unset($plugin_stored_data['actions']['subscriptions_migration_process']); $plugin_stored_data['actions']['merge_subscriptions_to_admail_table'] = true; update_option('ambisn_plugin_data', $plugin_stored_data); } add_action('ambisn_migrate_subscriptions_end_event','ambisn_finalize_migration_process'); // Get migration progress function ambisn_get_migration_progress() { $progress = get_transient('wc_admail_subscriptions_progress') ?: 0; $processed_count = get_transient('wc_admail_processed_count') ?: 0; wp_send_json_success([ 'notice' => get_transient('ambisn_migration_notice'), 'progress' => $progress, 'processed_count' => $processed_count, 'status' => get_option('wc_admail_subscriptions_migration_status', 'not_done') ]); } add_action('wp_ajax_ambisn_get_migration_progress', 'ambisn_get_migration_progress'); // Display migration progress notice function ambisn_migration_progress_notice() { $migration_status = get_option('wc_admail_subscriptions_migration_status', 'not_done'); $progress = get_transient('wc_admail_subscriptions_progress') ?: 0; $processed_count = get_transient('wc_admail_processed_count') ?: 0; $migration_notice = get_transient('ambisn_migration_notice'); if (!empty($migration_notice)) { ?>
AdMail: Subscription performance has been improved! The update is now complete, and the system will perform faster.
'; } } add_action('admin_notices', 'ambisn_migration_progress_notice');