Last active
September 4, 2025 12:21
-
-
Save vapvarun/9d6c441e3f7853b022f96afb47b44587 to your computer and use it in GitHub Desktop.
WP Stories - Smart Grouping Based on Cover Images (Club stories without covers, separate stories with covers)
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 | |
| /** | |
| * WP Stories - Fix PUBLIC Story Separation | |
| * | |
| * This ensures PUBLIC stories are NEVER combined, even from the same user | |
| * Each PUBLIC story should appear as its own separate icon | |
| * | |
| * Add this code to your theme's functions.php or as a custom plugin | |
| * | |
| * @version 3.0.0 | |
| */ | |
| // Prevent direct access | |
| if ( ! defined( 'ABSPATH' ) ) { | |
| exit; | |
| } | |
| /** | |
| * Override the get_activity_stories function to properly separate PUBLIC stories | |
| */ | |
| add_filter( 'pre_option_wp_stories_temp_activity_stories', 'override_activity_stories_display', 10, 2 ); | |
| function override_activity_stories_display( $pre_value, $option ) { | |
| // Only run on activity pages | |
| $is_activity = false; | |
| if ( function_exists( 'bp_is_activity_directory' ) && bp_is_activity_directory() ) { | |
| $is_activity = true; | |
| } | |
| if ( class_exists( 'PeepSo' ) && isset( $_GET['section'] ) && 'activity' === $_GET['section'] ) { | |
| $is_activity = true; | |
| } | |
| if ( ! $is_activity ) { | |
| return $pre_value; | |
| } | |
| // This will trigger our custom logic | |
| return 'use_custom_grouping'; | |
| } | |
| /** | |
| * Replace the activity stories shortcode output | |
| */ | |
| add_filter( 'do_shortcode_tag', 'fix_public_stories_separation', 5, 4 ); | |
| function fix_public_stories_separation( $output, $tag, $attr, $m ) { | |
| // Only modify wb-story-activities shortcode on activity pages | |
| if ( 'wb-story-activities' !== $tag ) { | |
| return $output; | |
| } | |
| // Check if we're on activity pages | |
| $is_activity = false; | |
| if ( function_exists( 'bp_is_activity_directory' ) && bp_is_activity_directory() ) { | |
| $is_activity = true; | |
| } | |
| if ( class_exists( 'PeepSo' ) && isset( $_GET['section'] ) && 'activity' === $_GET['section'] ) { | |
| $is_activity = true; | |
| } | |
| if ( ! $is_activity ) { | |
| return $output; | |
| } | |
| // Get all stories properly separated | |
| $stories = get_properly_separated_stories(); | |
| if ( empty( $stories ) ) { | |
| return $output; | |
| } | |
| // Generate custom output with proper separation | |
| $unique = 'fixed_' . wp_rand( 99, 999999 ); | |
| ob_start(); | |
| ?> | |
| <script> | |
| // Override the stories data to ensure proper separation | |
| var wbstories<?php echo esc_js( $unique ); ?> = <?php echo json_encode( $stories ); ?>; | |
| </script> | |
| <?php | |
| // Get display settings | |
| $general_settings = get_option( 'wp_stories_general_settings' ); | |
| $data_option = function_exists( 'get_wp_user_story_shortcode_attr' ) | |
| ? get_wp_user_story_shortcode_attr( '', true ) | |
| : ''; | |
| $story_html = function_exists( 'get_wp_user_story_shortcode_css' ) | |
| ? get_wp_user_story_shortcode_css( '', $unique ) | |
| : ''; | |
| $classes = array( 'wb-story-shortcode', 'wb-story-style-class-' . $unique ); | |
| echo $story_html; | |
| ?> | |
| <div id="wb-story-shortcode-<?php echo esc_attr( $unique ); ?>" | |
| data-unique="<?php echo esc_attr( $unique ); ?>" | |
| class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>" | |
| <?php echo $data_option; ?> | |
| data-shortcode-type="activities"> | |
| <?php if ( apply_filters( 'wb_stories_loaders', true ) ) : ?> | |
| <?php | |
| $loader_count = apply_filters( 'wb_stories_loaders_count', 10 ); | |
| echo str_repeat( '<span class="wb-stories-loader"><span></span><span></span><span></span><span></span></span>', $loader_count ); | |
| ?> | |
| <?php endif; ?> | |
| </div> | |
| <?php | |
| return ob_get_clean(); | |
| } | |
| /** | |
| * Get stories with proper separation of PUBLIC stories | |
| */ | |
| function get_properly_separated_stories() { | |
| global $wpdb; | |
| // Query all stories | |
| $args = array( | |
| 'post_type' => 'wb-user-story', | |
| 'post_status' => 'publish', | |
| 'posts_per_page' => apply_filters( 'wp_story_activity_count', 50 ), | |
| 'orderby' => 'date', | |
| 'order' => 'DESC', | |
| ); | |
| $query = new WP_Query( $args ); | |
| if ( ! $query->have_posts() ) { | |
| wp_reset_postdata(); | |
| return array(); | |
| } | |
| $stories_data = array(); | |
| $single_stories_by_user = array(); | |
| $public_stories = array(); | |
| $current_user_id = get_current_user_id(); | |
| // First pass: categorize stories | |
| while ( $query->have_posts() ) { | |
| $query->the_post(); | |
| $story_id = get_the_ID(); | |
| $author_id = get_the_author_meta( 'ID' ); | |
| $story_type = get_post_meta( $story_id, 'user_story_type', true ); | |
| if ( $story_type === 'public' ) { | |
| // Each PUBLIC story gets its own entry | |
| $public_stories[] = array( | |
| 'id' => $story_id, | |
| 'author_id' => $author_id, | |
| 'timestamp' => get_post_timestamp( $story_id ), | |
| 'title' => get_the_title(), | |
| 'has_thumbnail' => has_post_thumbnail( $story_id ) | |
| ); | |
| } else { | |
| // SINGLE stories will be clubbed | |
| if ( ! isset( $single_stories_by_user[ $author_id ] ) ) { | |
| $single_stories_by_user[ $author_id ] = array(); | |
| } | |
| $single_stories_by_user[ $author_id ][] = $story_id; | |
| } | |
| } | |
| wp_reset_postdata(); | |
| $final_stories = array(); | |
| // Process SINGLE stories (clubbed by user) | |
| foreach ( $single_stories_by_user as $author_id => $story_ids ) { | |
| $items = array(); | |
| foreach ( $story_ids as $story_id ) { | |
| $story_meta = get_post_meta( $story_id, 'wb_story_items', true ); | |
| if ( function_exists( 'get_wp_activity_story_items' ) ) { | |
| $story_items = get_wp_activity_story_items( $story_meta, $story_id ); | |
| if ( ! empty( $story_items ) ) { | |
| $items = array_merge( $items, $story_items ); | |
| } | |
| } | |
| } | |
| if ( empty( $items ) ) continue; | |
| $last_story_id = end( $story_ids ); | |
| // Get profile URLs | |
| $profile_url = '#'; | |
| $profile_link = ''; | |
| if ( class_exists( 'PeepSo' ) ) { | |
| $profile_url = PeepSoUser::get_instance( $author_id )->get_profileurl(); | |
| } elseif ( function_exists( 'bp_members_get_user_url' ) ) { | |
| $profile_url = bp_members_get_user_url( $author_id ); | |
| $profile_link = bp_members_get_user_url( $author_id ) . 'wp-story-view'; | |
| } | |
| $final_stories[] = array( | |
| 'id' => 'author-story-' . $author_id, | |
| 'photo' => function_exists( 'get_wp_stories_user_avatar' ) | |
| ? get_wp_stories_user_avatar( $author_id, 180 ) | |
| : get_avatar_url( $author_id, array( 'size' => 180 ) ), | |
| 'name' => function_exists( 'get_wp_stories_user_name' ) | |
| ? get_wp_stories_user_name( $author_id ) | |
| : get_the_author_meta( 'display_name', $author_id ), | |
| 'author_name' => function_exists( 'get_wp_stories_user_name' ) | |
| ? get_wp_stories_user_name( $author_id ) | |
| : get_the_author_meta( 'display_name', $author_id ), | |
| 'link' => $profile_url, | |
| 'profile_link'=> $profile_link, | |
| 'login_user' => ( $current_user_id == $author_id ), | |
| 'lastUpdated' => get_post_timestamp( $last_story_id ), | |
| 'seen' => false, | |
| 'items' => $items | |
| ); | |
| } | |
| // Process PUBLIC stories (each separate) | |
| foreach ( $public_stories as $public_story ) { | |
| $story_id = $public_story['id']; | |
| $author_id = $public_story['author_id']; | |
| // Get story meta | |
| $story_meta = get_post_meta( $story_id, 'wb_story_items', true ); | |
| $items = array(); | |
| if ( function_exists( 'get_wp_activity_story_items' ) ) { | |
| $items = get_wp_activity_story_items( $story_meta, $story_id ); | |
| } | |
| if ( empty( $items ) ) continue; | |
| // Get cover photo | |
| $photo = ''; | |
| if ( $public_story['has_thumbnail'] ) { | |
| $photo = get_the_post_thumbnail_url( $story_id, 'full' ); | |
| } | |
| if ( empty( $photo ) ) { | |
| $photo = function_exists( 'get_wp_stories_user_avatar' ) | |
| ? get_wp_stories_user_avatar( $author_id, 180 ) | |
| : get_avatar_url( $author_id, array( 'size' => 180 ) ); | |
| } | |
| // Get title | |
| $title = $public_story['title']; | |
| if ( empty( $title ) ) { | |
| $title = function_exists( 'get_wp_stories_user_name' ) | |
| ? get_wp_stories_user_name( $author_id ) . ' - Story ' . $story_id | |
| : get_the_author_meta( 'display_name', $author_id ) . ' - Story ' . $story_id; | |
| } | |
| // Get profile URLs | |
| $profile_url = '#'; | |
| $profile_link = ''; | |
| if ( class_exists( 'PeepSo' ) ) { | |
| $profile_url = PeepSoUser::get_instance( $author_id )->get_profileurl(); | |
| } elseif ( function_exists( 'bp_members_get_user_url' ) ) { | |
| $profile_url = bp_members_get_user_url( $author_id ); | |
| $profile_link = bp_members_get_user_url( $author_id ) . 'wp-story-view'; | |
| } | |
| // Each PUBLIC story gets its own complete entry | |
| $final_stories[] = array( | |
| 'id' => 'public-story-' . $story_id, // Unique ID for each PUBLIC story | |
| 'post_id' => $story_id, | |
| 'photo' => $photo, | |
| 'name' => $title, | |
| 'author_name' => function_exists( 'get_wp_stories_user_name' ) | |
| ? get_wp_stories_user_name( $author_id ) | |
| : get_the_author_meta( 'display_name', $author_id ), | |
| 'link' => $profile_url, | |
| 'profile_link'=> $profile_link, | |
| 'login_user' => ( $current_user_id == $author_id ), | |
| 'lastUpdated' => $public_story['timestamp'], | |
| 'seen' => false, | |
| 'items' => $items | |
| ); | |
| } | |
| // Sort by timestamp | |
| usort( $final_stories, function( $a, $b ) { | |
| return $b['lastUpdated'] - $a['lastUpdated']; | |
| }); | |
| return $final_stories; | |
| } | |
| /** | |
| * Add debug info for admins | |
| */ | |
| add_action( 'wp_footer', function() { | |
| if ( ! current_user_can( 'manage_options' ) ) { | |
| return; | |
| } | |
| $is_activity = false; | |
| if ( function_exists( 'bp_is_activity_directory' ) && bp_is_activity_directory() ) { | |
| $is_activity = true; | |
| } | |
| if ( class_exists( 'PeepSo' ) && isset( $_GET['section'] ) && 'activity' === $_GET['section'] ) { | |
| $is_activity = true; | |
| } | |
| if ( ! $is_activity ) { | |
| return; | |
| } | |
| ?> | |
| <script> | |
| console.log('=== WP Stories Debug ==='); | |
| console.log('Story separation fix is active'); | |
| if ( typeof wbcomZeusStories !== 'undefined' ) { | |
| console.log('Stories data:', wbcomZeusStories); | |
| console.log('Total stories:', wbcomZeusStories.length); | |
| wbcomZeusStories.forEach(function(story) { | |
| console.log('Story ID:', story.id, 'Type:', story.id.includes('public') ? 'PUBLIC' : 'SINGLE'); | |
| }); | |
| } | |
| </script> | |
| <?php | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment