true, 'fields' => 'ID', // Only get IDs to reduce memory usage 'number' => $batch_size, 'offset' => $offset ); $users = get_users( $args ); if ( empty( $users ) ) { break; } foreach ( $users as $user ) { try { // Get user meta $user_meta = get_user_meta( $user, THRIVE_SOCIAL_OPTION_NAME, true ); // Skip if no meta data if ( empty( $user_meta ) ) { continue; } // Skip if no 't' data to migrate if ( empty( $user_meta['t'] ) ) { continue; } // Migrate the value from t to x $user_meta['x'] = $user_meta['t']; unset( $user_meta['t'] ); // Update the user meta update_user_meta( $user, THRIVE_SOCIAL_OPTION_NAME, $user_meta ); } catch ( Exception $e ) { error_log( sprintf( 'Error processing user ID %d: %s', $user, $e->getMessage() ) ); } } $offset += $batch_size; // Give the server a small break between batches if ( function_exists( 'usleep' ) ) { usleep( 100000 ); // 100ms delay } } } /** * Run the migration */ public static function run_migration() { global $wpdb; // Get all posts that might contain Twitter social share elements $posts = $wpdb->get_results( " SELECT ID, post_content FROM {$wpdb->posts} WHERE post_content LIKE '%tve_s_t_share%' AND post_status != 'trash' " ); $updated = 0; foreach ( $posts as $post ) { $content = $post->post_content; // Replace Twitter elements with X elements $new_content = self::replace_twitter_elements( $content ); if ( $content !== $new_content ) { wp_update_post( array( 'ID' => $post->ID, 'post_content' => $new_content ) ); $updated++; } } // Update quiz variations self::update_quiz_variations(); // Migrate user social meta self::migrate_user_social_meta(); // Mark migration as completed update_option( 'tcb_twitter_to_x_migration_completed', true ); } /** * Update quiz variations that contain Twitter elements */ private static function update_quiz_variations() { global $wpdb; $updated = 0; // Get all quiz variations that contain Twitter elements $variations = $wpdb->get_results( $wpdb->prepare( " SELECT id, content FROM {$wpdb->prefix}tqb_variations WHERE content LIKE %s ", '%tve_s_t_share%' ) ); foreach ( $variations as $variation ) { $content = $variation->content; // Replace Twitter elements with X elements $new_content = self::replace_twitter_elements( $content ); if ( $content !== $new_content ) { $wpdb->update( $wpdb->prefix . 'tqb_variations', array( 'content' => $new_content ), array( 'id' => $variation->id ), array( '%s' ), array( '%d' ) ); $updated++; } } return $updated; } /** * Replace Twitter elements with X elements in content */ private static function replace_twitter_elements( $content ) { // Pattern to match Twitter social share elements $pattern = '/]*class="[^"]*tve_s_item[^"]*tve_s_t_share[^"]*"[^>]*data-s="t_share"[^>]*>.*?<\/div>/s'; // Replacement function $replacement = function( $matches ) { $element = $matches[0]; // Extract data attributes preg_match( '/data-href="([^"]*)"/', $element, $href_matches ); $href = isset( $href_matches[1] ) ? $href_matches[1] : '{tcb_post_url}'; preg_match( '/data-via="([^"]*)"/', $element, $via_matches ); $via = isset( $via_matches[1] ) ? $via_matches[1] : ''; preg_match( '/([^<]*)<\/span>/', $element, $label_matches ); $label = isset( $label_matches[1] ) ? $label_matches[1] : esc_html__( 'Post', 'thrive-cb' ); // Create new X element return sprintf( '', esc_attr( $href ), esc_attr__( 'I got: %result%', 'thrive-cb' ), // translators: %result% is a placeholder for quiz result esc_attr( $via ), esc_html( $label ) ); }; return preg_replace_callback( $pattern, $replacement, $content ); } } // Initialize the migration TCB_Social_Migration::init();