BuddyDev

Search

[Resolved] auto follow featured members

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #50837

    Hello Brajesh

    Please can you help review this code below against security and performance issues

     /** BuddyPress Auto Friendship Pro + BuddyPress Featured Members autofollow. */
    function auto_follow_on_welcome_page_load() {
        // Check if the current page slug matches 'welcome'
        if (is_page('welcome')) {
            if (!function_exists('bp_follow_start_following')) {
                return;
            }
    
            $user = wp_get_current_user();
    
            // Check if the user has no last activity and hasn't already performed the follow action
            if (empty(bp_get_user_last_activity($user->ID)) && !get_user_meta($user->ID, 'has_followed_featured_users', true)) {
    
                global $wpdb;
    
                $featured_user_ids = $wpdb->get_col(
                    $wpdb->prepare(
                        "
                        SELECT user_id
                        FROM {$wpdb->usermeta}
                        INNER JOIN {$wpdb->users} ON {$wpdb->usermeta}.user_id = {$wpdb->users}.ID
                        WHERE {$wpdb->usermeta}.meta_key = %s
                        AND {$wpdb->usermeta}.meta_value = %s
                        AND {$wpdb->users}.user_status = %d
                        ",
                        '_is_featured',
                        '1',
                        0
                    )
                );
    
                $users_more_than_2_posts = array();
                foreach ($featured_user_ids as $featured_user_id) {
                    $post_count = count_user_posts($featured_user_id);
                    if ($post_count > 2) {
                        $users_more_than_2_posts[] = $featured_user_id;
                    }
                }
    
                $total_featured_users = count($users_more_than_2_posts);
                $selected_user_ids = $users_more_than_2_posts;
                if ($total_featured_users > 10) {
                    shuffle($users_more_than_2_posts);
                    $selected_user_ids = array_slice($users_more_than_2_posts, 0, 9);
                } else if ($total_featured_users > 5) {
                    shuffle($users_more_than_2_posts);
                    $selected_user_ids = array_slice($users_more_than_2_posts, 0, 6);
                } else if ($total_featured_users < 5) {
                    shuffle($users_more_than_2_posts);
                    $selected_user_ids = array_slice($users_more_than_2_posts, 0, 3);
                }
    
                foreach ($selected_user_ids as $featured_user_id) {
                    bp_follow_start_following(array(
                        'leader_id' => $featured_user_id,
                        'follower_id' => $user->ID,
                    ));
                }
    
                // Set a user meta flag to indicate that the follow action has been performed
                update_user_meta($user->ID, 'has_followed_featured_users', true);
            }
        }
    }
    add_action('template_redirect', 'auto_follow_on_welcome_page_load'); 

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24611
    Brajesh Singh on #50840

    Hi Tosin,
    Thank you for the question.

    I will suggest adding a check for is_user_loggdin() or when using wp_get_current_user() test if the relevant object was returned before proceeding further.

    It should work fine. The count_user_posts is a bit slow and being called for all featured users is a performance penalty.

    Overall, the code can be simplified a lot.

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #50846

    I have done as you instructed with is_user_loggdin() but now how can I resolve the performance issue with count_user_posts and simplify the code

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24611
    Brajesh Singh on #50873

    Hi Tosin,
    I will suggest adding a wrapper to ‘count_user_posts’ and caching the value in transient. That way, you will only need to flush the cache for author on new posts and there will be almost 1 total count calculation per author irrespective of number of queries(and you will only need to count if the author publishes a new post).

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #50878

    Will this code work

     /** 
     * Custom function to count user posts and cache the result using transients
     */
    function custom_count_user_posts($user_id) {
        $post_count = get_transient('custom_user_post_count_' . $user_id);
    
        if (false === $post_count) {
            $post_count = count_user_posts($user_id);
            set_transient('custom_user_post_count_' . $user_id, $post_count);
        }
    
        return $post_count;
    }
    
    /** BuddyPress Auto Friendship Pro + BuddyPress Featured Members autofollow. */
    function auto_follow_on_welcome_page_load() {
        // Check if the current page slug matches 'welcome'
        if (is_page('welcome')) {
            if (!function_exists('bp_follow_start_following')) {
                return;
            }
    
            $user = wp_get_current_user();
    
            // Check if the user has no last activity and hasn't already performed the follow action
            if (empty(bp_get_user_last_activity($user->ID)) && !get_user_meta($user->ID, 'has_followed_featured_users', true)) {
    
                global $wpdb;
    
                $featured_user_ids = $wpdb->get_col(
                    $wpdb->prepare(
                        "
                        SELECT user_id
                        FROM {$wpdb->usermeta}
                        INNER JOIN {$wpdb->users} ON {$wpdb->usermeta}.user_id = {$wpdb->users}.ID
                        WHERE {$wpdb->usermeta}.meta_key = %s
                        AND {$wpdb->usermeta}.meta_value = %s
                        AND {$wpdb->users}.user_status = %d
                        ",
                        '_is_featured',
                        '1',
                        0
                    )
                );
    
                $users_more_than_2_posts = array();
                foreach ($featured_user_ids as $featured_user_id) {
                    $post_count = custom_count_user_posts($featured_user_id);
                    if ($post_count > 2) {
                        $users_more_than_2_posts[] = $featured_user_id;
                    }
                }
    
                $total_featured_users = count($users_more_than_2_posts);
                $selected_user_ids = $users_more_than_2_posts;
                if ($total_featured_users > 10) {
                    shuffle($users_more_than_2_posts);
                    $selected_user_ids = array_slice($users_more_than_2_posts, 0, 9);
                } else if ($total_featured_users > 5) {
                    shuffle($users_more_than_2_posts);
                    $selected_user_ids = array_slice($users_more_than_2_posts, 0, 6);
                } else if ($total_featured_users < 5) {
                    shuffle($users_more_than_2_posts);
                    $selected_user_ids = array_slice($users_more_than_2_posts, 0, 3);
                }
    
                foreach ($selected_user_ids as $featured_user_id) {
                    bp_follow_start_following(array(
                        'leader_id' => $featured_user_id,
                        'follower_id' => $user->ID,
                    ));
                }
    
                // Set a user meta flag to indicate that the follow action has been performed
                update_user_meta($user->ID, 'has_followed_featured_users', true);
            }
        }
    }
    add_action('template_redirect', 'auto_follow_on_welcome_page_load');
     
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #50909

    Hello Tosin,

    Use the following code to reset post count

    
    // Reset post count.
    function custom_delete_user_posts_count( $post_id ) {
    	delete_transient( 'custom_user_post_count_' . get_post_field( 'post_author', $post_id ) );
    }
    
    add_action( 'bblpro_post_submitted', 'custom_delete_user_posts_count' );
    add_action( 'bblpro_post_updated', 'custom_delete_user_posts_count' );
    
    

    Use the following code to fetch featured user ids:

    
    $featured_user_ids = get_users(
    		array(
    			'fields'   => 'ID',
    			'number'   => - 1,
    			'meta_key' => '_is_featured',
    		)
    	);
    

    Please give it a try

    Regards
    Ravi

You must be logged in to reply to this topic.

This topic is: resolved