get_results( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s AND post_status IN (%s, %s, %s)", 'ajsqtesl_slider', 'publish', 'draft', 'pending' ) ); if ( empty( $legacy_posts ) ) { return; } $new_testimonial_ids = array(); foreach ( $legacy_posts as $legacy_post ) { $wpdb->update( $wpdb->posts, array( 'post_type' => 'bhub_testimonial' ), array( 'ID' => $legacy_post->ID ), array( '%s' ), array( '%d' ) ); $old_subtitle = get_post_meta( $legacy_post->ID, 'ajsqtesl_slider_subtitle', true ); if ( '' !== $old_subtitle ) { update_post_meta( $legacy_post->ID, 'bridgehub_testimonial_designation', $old_subtitle ); } $new_testimonial_ids[] = $legacy_post->ID; } self::create_default_slider( $new_testimonial_ids ); } private static function fix_broken_2_0_0_migration() { global $wpdb; $orphan_posts = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s AND post_status IN (%s, %s, %s)", 'bridgehub_testimonial', 'publish', 'draft', 'pending' ) ); if ( empty( $orphan_posts ) ) { return; } $orphan_ids = array(); foreach ( $orphan_posts as $orphan ) { $wpdb->update( $wpdb->posts, array( 'post_type' => 'bhub_testimonial' ), array( 'ID' => $orphan->ID ), array( '%s' ), array( '%d' ) ); $orphan_ids[] = $orphan->ID; } $existing_slider = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s AND post_status = %s LIMIT 1", 'bridgehub_slider', 'publish' ) ); if ( $existing_slider ) { $existing_ids = get_post_meta( $existing_slider, 'bridgehub_testimonial_ids', true ); if ( ! is_array( $existing_ids ) ) { $existing_ids = array(); } $merged_ids = array_unique( array_merge( $existing_ids, $orphan_ids ) ); update_post_meta( $existing_slider, 'bridgehub_testimonial_ids', $merged_ids ); } else { self::create_default_slider( $orphan_ids ); } } /** * Set bridgehub_testimonial_parent_slider on all testimonials that belong to a slider. */ private static function migrate_parent_relationships() { global $wpdb; $sliders = get_posts( array( 'post_type' => 'bridgehub_slider', 'posts_per_page' => -1, 'post_status' => 'any', ) ); foreach ( $sliders as $slider ) { $testimonial_ids = get_post_meta( $slider->ID, 'bridgehub_testimonial_ids', true ); if ( ! is_array( $testimonial_ids ) ) { continue; } foreach ( $testimonial_ids as $tid ) { $tid = absint( $tid ); if ( $tid > 0 ) { update_post_meta( $tid, 'bridgehub_testimonial_parent_slider', $slider->ID ); } } } // Also fix any bhub_testimonial posts that have no parent slider set // but exist as children in a slider's testimonial_ids. $unlinked = $wpdb->get_results( $wpdb->prepare( "SELECT p.ID FROM {$wpdb->posts} p LEFT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = %s WHERE p.post_type = %s AND p.post_status IN (%s, %s, %s) AND pm.meta_id IS NULL", 'bridgehub_testimonial_parent_slider', 'bhub_testimonial', 'publish', 'draft', 'pending' ) ); if ( ! empty( $unlinked ) ) { $all_slider_ids = wp_list_pluck( $sliders, 'ID' ); foreach ( $unlinked as $row ) { $found_slider = self::find_parent_slider( $row->ID, $all_slider_ids ); if ( $found_slider ) { update_post_meta( $row->ID, 'bridgehub_testimonial_parent_slider', $found_slider ); } } } } /** * Find which slider a testimonial belongs to by checking all sliders' testimonial_ids. */ private static function find_parent_slider( $testimonial_id, $slider_ids ) { foreach ( $slider_ids as $sid ) { $ids = get_post_meta( $sid, 'bridgehub_testimonial_ids', true ); if ( is_array( $ids ) && in_array( $testimonial_id, $ids, true ) ) { return $sid; } } return 0; } private static function create_default_slider( $testimonial_ids ) { $default_slider_id = wp_insert_post( array( 'post_title' => __( 'Default Slider', 'aj-square-testimonial-slider' ), 'post_type' => 'bridgehub_slider', 'post_status' => 'publish', 'post_content' => '', ) ); if ( ! $default_slider_id || is_wp_error( $default_slider_id ) ) { return; } update_post_meta( $default_slider_id, 'bridgehub_testimonial_ids', $testimonial_ids ); foreach ( $testimonial_ids as $tid ) { update_post_meta( $tid, 'bridgehub_testimonial_parent_slider', $default_slider_id ); } $bg_color = get_theme_mod( 'slider_bg_color', '#f0f0f0' ); $text_color = get_theme_mod( 'slider_text_color', '#262525' ); $image_height = get_theme_mod( 'thumbnail_height', '100' ); $border_radius = get_theme_mod( 'ajsqtesl_image_border_radius', '0' ); update_post_meta( $default_slider_id, 'bridgehub_slider_bg_color', sanitize_hex_color( $bg_color ) ); update_post_meta( $default_slider_id, 'bridgehub_slider_text_color', sanitize_hex_color( $text_color ) ); update_post_meta( $default_slider_id, 'bridgehub_slider_image_height', absint( $image_height ) ); update_post_meta( $default_slider_id, 'bridgehub_slider_image_border_radius', absint( $border_radius ) ); update_post_meta( $default_slider_id, 'bridgehub_slider_auto_slide', 'yes' ); update_post_meta( $default_slider_id, 'bridgehub_slider_interval', '5000' ); update_post_meta( $default_slider_id, 'bridgehub_slider_navigation', 'yes' ); update_post_meta( $default_slider_id, 'bridgehub_slider_indicators', 'yes' ); } public static function run_migration_if_needed() { $installed_version = get_option( 'bridgehub_db_version', '' ); if ( version_compare( $installed_version, BRIDGEHUB_DB_VERSION, '<' ) ) { self::activate(); flush_rewrite_rules(); update_option( 'bridgehub_db_version', BRIDGEHUB_DB_VERSION ); } } }