BuddyDev

Search

Replies

  • Participant
    Level: Guru
    Posts: 902
    Tosin on in reply to: [Resolved] Attribute post on account deletion in buddypress #50911

    You are awesome Ravi, thanks a lot

  • Participant
    Level: Guru
    Posts: 902
    Tosin on in reply to: [Resolved] Attribute post on account deletion in buddypress #50889

    it might also be advisable to also use this code to disable comments on posts attributed to the non active user

     function disable_comments_on_single_post_by_user() {
        if (is_singular('post')) {
            $post_id = get_queried_object_id(); // Get the current post ID
    
            // Get the post's author ID and comment status
            $post_author_id = get_post_field('post_author', $post_id);
            $comment_status = get_post_field('comment_status', $post_id);
    
            // Check if the post author's ID matches the specified user ID and comments are open
            if ($post_author_id == 466526 && $comment_status === 'open') {
                // Update post's comment status to 'closed'
                wp_update_post(array(
                    'ID'             => $post_id,
                    'comment_status' => 'closed',
                ));
            }
        }
    }
    add_action('template_redirect', 'disable_comments_on_single_post_by_user');
     
  • Participant
    Level: Guru
    Posts: 902
    Tosin on in reply to: [Resolved] Attribute post on account deletion in buddypress #50888

    Hi Brajesh

    I finally got it to work with this code, kindly review

    I really think this would help with buddyblog when multiple authors delete their account, lots of 404 errors would be prevented

    Also note that the ghostwriter account is just a non active account used to preserve useful content from deleted accounts.

     function reassign_posts_on_user_deletion_to_ghostwriter($user_id) {
        // Check if the deleted user is not the target user (466526)
        if ($user_id !== 466526) {
            $args = array(
                'post_type'      => 'post',
                'author'         => $user_id, // Posts authored by the deleted user
                'posts_per_page' => -1,       // Get all posts
                'fields'         => 'ids',    // Retrieve only post IDs
                'post_status'    => 'publish',// Only published posts
            );
            $posts = get_posts($args);
            // Reassign published posts to user with ID 466526
            foreach ($posts as $post_id) {
                $post_status = get_post_status($post_id);
                if ($post_status === 'publish') {
                    wp_update_post(array(
                        'ID'          => $post_id,
                        'post_author' => 466526,
                    ));
                }
            }
        }
    }
    add_action('bp_core_pre_delete_account', 'reassign_posts_on_user_deletion_to_ghostwriter', 10, 1);
     
  • Participant
    Level: Guru
    Posts: 902

    I don’t want to give up on this so im trying another approach below

     // change the role of inactive members over 1 year to ghost member
    function update_site_ghost_members() {
        // Check if it's the activity directory page and the user is an admin
        if (bp_is_activity_directory() && current_user_can('administrator')) {
            $last_run_timestamp = get_user_meta(0, 'last_ghost_member_role_update', true);
    
            // Check if a month (2592000 seconds = 30 days) has passed since the last run
            if (!$last_run_timestamp || (time() - $last_run_timestamp) >= 2592000) {
                // Get users with specific roles (e.g., 'contributor' and 'subscriber')
                $users = get_users(array(
                    'fields' => 'ID',
                    'role__in' => array('contributor', 'subscriber'),
                ));
    
                foreach ($users as $user_id) {
                    // Check user's last activity using BuddyPress function
                    $last_activity = bp_get_user_last_activity($user_id);
    
                    // Calculate time difference
                    $inactive_time = strtotime('now') - strtotime($last_activity);
    
                    // If inactive for more than one year (31536000 seconds = 1 year)
                    if ($inactive_time > 31536000) {
                        // Assign 'Ghost Member' role to the user
                        $user = new WP_User($user_id);
                        $user->add_role('ghost_member');
                    }
                }
    
                // Update the timestamp to indicate the code ran
                update_user_meta(0, 'last_ghost_member_role_update', time());
            }
        }
    }
    add_action('template_redirect', 'update_site_ghost_members'); 

    running get_users would be awesome in batches

  • Participant
    Level: Guru
    Posts: 902
    Tosin on in reply to: [Resolved] auto follow featured members #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');
     
  • Participant
    Level: Guru
    Posts: 902

    I found another solution below

     function disable_admin_new_user_registration_email( $wp_new_user_notification_email_admin, $user, $blogname )
    {
    	//Stop wp_mail() from working
    	add_filter( 'pre_wp_mail', '__return_false' );
    
    	//Return an unchanged value from this filter
    	return $wp_new_user_notification_email_admin;
    }
    add_filter( 'wp_new_user_notification_email_admin', 'disable_admin_new_user_registration_email', 10, 3 ); 
  • Participant
    Level: Guru
    Posts: 902
    Tosin on in reply to: Plugin question in relation to buddypress #50863

    Hello Brajesh

    Have you been able to test this plugin and see how if affects buddypress performance

    Thanks

  • Participant
    Level: Guru
    Posts: 902

    After all the stress there is something so simple

     add_filter('wp_new_user_notification_email', '__return_false');
     
  • Participant
    Level: Guru
    Posts: 902

    This code seemed to work

     // Disable new user registration email in WordPress for admin users only
    function disable_send_new_user_notifications($user_id, $notify = 'user') {
        // Check if user role is administrator
        $user_role = get_user_meta($user_id, 'wp_capabilities', true);
        if (isset($user_role['administrator'])) {
            // User is an administrator, skip sending notification
            return;
        }
    
        if (empty($notify) || 'admin' === $notify) {
            return;
        } elseif ('both' === $notify) {
            // Send new users the email but not the admin.
            $notify = 'user';
        }
        wp_send_new_user_notifications($user_id, $notify);
    }
    
    // Disable default email notifications
    add_action('init', function () {
        remove_action('register_new_user', 'wp_send_new_user_notifications');
        remove_action('edit_user_created_user', 'wp_send_new_user_notifications');
    
        // For BuddyPress
        remove_action('bp_core_activated_user', 'wp_send_new_user_notifications');
    
        // For WooCommerce
        remove_action('woocommerce_created_customer', 'wp_send_new_user_notifications');
    });
    
    // Replace default notifications with custom function
    add_action('register_new_user', 'disable_send_new_user_notifications');
    add_action('edit_user_created_user', 'disable_send_new_user_notifications', 10, 2);
    
    // For BuddyPress
    add_action('bp_core_activated_user', 'disable_send_new_user_notifications');
    
    // For WooCommerce
    add_action('woocommerce_created_customer', 'disable_send_new_user_notifications'); 
  • Participant
    Level: Guru
    Posts: 902
    Tosin on in reply to: [Resolved] auto follow featured members #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