Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buddypress follow plugin and buddypress 12.0.0 #125

Open
teboy opened this issue Sep 11, 2023 · 12 comments
Open

Buddypress follow plugin and buddypress 12.0.0 #125

teboy opened this issue Sep 11, 2023 · 12 comments

Comments

@teboy
Copy link

teboy commented Sep 11, 2023

Hello @ray,

I decided to test buddypress 12.0.0 beta 1 & 2 and noticed that clicking the user navigation menu for following and followers tab triggers an error.
The plugin is not compatible with the upcoming buddypress 12.0.0

A resolution would be really appreciated

Thanks

@r-a-y
Copy link
Owner

r-a-y commented Sep 16, 2023

I've merged #126 into master branch. Please give latest master branch a try to see if that fixes things.

@teboy
Copy link
Author

teboy commented Sep 18, 2023

Hello @r-a-y

I have updated the plugin but I am still getting errors this is message from my debug log file

2023-09-18T09:57:56+00:00 CRITICAL Cannot redeclare bp_follow_screen_followers() (previously declared in /var/www/wptbox/wp-content/plugins/buddypress-followers-master/_inc/users/screens.php:19) in /var/www/wptbox/wp-content/plugins/buddypress-followers-master/_inc/users/screens.php on line 17

2023-09-18T09:58:25+00:00 CRITICAL Cannot redeclare bp_follow_action_start() (previously declared in /var/www/wptbox/wp-content/plugins/buddypress-followers-master/_inc/users/actions.php:22) in /var/www/wptbox/wp-content/plugins/buddypress-followers-master/_inc/users/actions.php on line 21

Thanks

@r-a-y
Copy link
Owner

r-a-y commented Sep 19, 2023

I cannot duplicate this problem. Can you try either renaming or deleting the buddypress-followers-master folder and downloading the latest version from here -- https://github.com/r-a-y/buddypress-followers/archive/refs/heads/master.zip ?

Or if you are able to start on a fresh, test install of BP 12 beta with the latest version of buddypress-followers, that would help as well.

@teboy
Copy link
Author

teboy commented Sep 21, 2023

Hi

For this error 2023-09-18T09:58:25+00:00 CRITICAL Cannot redeclare bp_follow_action_start() (previously declared in /var/www/wptbox/wp-content/plugins/buddypress-followers-master/_inc/users/actions.php:22) in /var/www/wptbox/wp-content/plugins/buddypress-followers-master/_inc/users/actions.php on line 21

I tried using gpt and it provided me with an updated code and I no longer receive this error again.
To fix the error related to the actions.php file in the BuddyPress Follow plugin, you should check for the existence of functions before declaring them to avoid redeclaration issues. Here's the corrected code for the actions.php file:

`<?php
/**

  • BP Follow Actions
  • @Package BP-Follow
  • @subpackage Actions
    */

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

if ( ! function_exists( 'bp_follow_action_start' ) ) {
/**
* Catches clicks on a "Follow" button and tries to make that happen.
*
* @uses check_admin_referer() Checks to make sure the WP security nonce matches.
* @uses bp_follow_is_following() Checks to see if a user is following another user already.
* @uses bp_follow_start_following() Starts a user following another user.
* @uses bp_core_add_message() Adds an error/success message to be displayed after redirect.
* @uses bp_core_redirect() Safe redirects the user to a particular URL.
*/
function bp_follow_action_start() {
$bp = $GLOBALS['bp'];

    if ( ! bp_is_current_component( $bp->follow->followers->slug ) || ! bp_is_current_action( 'start' ) ) {
        return;
    }

    if ( bp_displayed_user_id() === bp_loggedin_user_id() ) {
        return;
    }

    check_admin_referer( 'start_following' );

    if ( bp_follow_is_following( array( 'leader_id' => bp_displayed_user_id(), 'follower_id' => bp_loggedin_user_id() ) ) ) {
        bp_core_add_message( sprintf( __( 'You are already following %s.', 'buddypress-followers' ), bp_get_displayed_user_fullname() ), 'error' );

    } else {
        if ( ! bp_follow_start_following( array( 'leader_id' => bp_displayed_user_id(), 'follower_id' => bp_loggedin_user_id() ) ) ) {
            bp_core_add_message( sprintf( __( 'There was a problem when trying to follow %s, please try again.', 'buddypress-followers' ), bp_get_displayed_user_fullname() ), 'error' );
        } else {
            bp_core_add_message( sprintf( __( 'You are now following %s.', 'buddypress-followers' ), bp_get_displayed_user_fullname() ) );
        }
    }

    // it's possible that wp_get_referer() returns false, so let's fallback to the displayed user's page.
    $redirect = wp_get_referer() ? wp_get_referer() : bp_displayed_user_domain();
    bp_core_redirect( $redirect );
}
add_action( 'bp_actions', 'bp_follow_action_start' );

}

if ( ! function_exists( 'bp_follow_action_stop' ) ) {
/**
* Catches clicks on a "Unfollow" button and tries to make that happen.
*
* @uses check_admin_referer() Checks to make sure the WP security nonce matches.
* @uses bp_follow_is_following() Checks to see if a user is following another user already.
* @uses bp_follow_stop_following() Stops a user following another user.
* @uses bp_core_add_message() Adds an error/success message to be displayed after redirect.
* @uses bp_core_redirect() Safe redirects the user to a particular URL.
*/
function bp_follow_action_stop() {
$bp = $GLOBALS['bp'];

    if ( ! bp_is_current_component( $bp->follow->followers->slug ) || ! bp_is_current_action( 'stop' ) ) {
        return;
    }

    if ( bp_displayed_user_id() === bp_loggedin_user_id() ) {
        return;
    }

    check_admin_referer( 'stop_following' );

    if ( ! bp_follow_is_following( array( 'leader_id' => bp_displayed_user_id(), 'follower_id' => bp_loggedin_user_id() ) ) ) {
        bp_core_add_message( sprintf( __( 'You are not following %s.', 'buddypress-followers' ), bp_get_displayed_user_fullname() ), 'error' );

    } else {
        if ( ! bp_follow_stop_following( array( 'leader_id' => bp_displayed_user_id(), 'follower_id' => bp_loggedin_user_id() ) ) ) {
            bp_core_add_message( sprintf( __( 'There was a problem when trying to stop following %s, please try again.', 'buddypress-followers' ), bp_get_displayed_user_fullname() ), 'error' );
        } else {
            bp_core_add_message( sprintf( __( 'You are no longer following %s.', 'buddypress-followers' ), bp_get_displayed_user_fullname() ) );
        }
    }

    // it's possible that wp_get_referer() returns false, so let's fallback to the displayed user's page.
    $redirect = wp_get_referer() ? wp_get_referer() : bp_displayed_user_domain();
    bp_core_redirect( $redirect );
}
add_action( 'bp_actions', 'bp_follow_action_stop' );

}

if ( ! function_exists( 'bp_follow_my_following_feed' ) ) {
/**
* Add RSS feed support for a user's following activity.
*
* Ex.: example.com/members/USERNAME/activity/following/feed/
*
* Only available in BuddyPress 1.8+.
*
* @SInCE 1.2.1
* @author r-a-y
*/
function bp_follow_my_following_feed() {
// only available in BP 1.8+.
if ( ! class_exists( 'BP_Activity_Feed' ) ) {
return;
}

    if ( ! bp_is_user_activity() || ! bp_is_current_action( constant( 'BP_FOLLOWING_SLUG' ) ) || ! bp_is_action_variable( 'feed', 0 ) ) {
        return false;
    }

    $bp = $GLOBALS['bp'];

    // setup the feed.
    $bp->activity->feed = new BP_Activity_Feed( array(
        'id'            => 'myfollowing',

        /* translators: User's following activity RSS title - "[Site Name] | [User Display Name] | Following Activity" */
        'title'         => sprintf( __( '%1$s | %2$s | Following Activity', 'buddypress-followers' ), bp_get_site_name(), bp_get_displayed_user_fullname() ),

        'link'          => bp_follow_get_user_url( bp_displayed_user_id(), array( bp_get_activity_slug(), constant( 'BP_FOLLOWING_SLUG' ) ) ),
        'description'   => sprintf( __( "Activity feed for people that %s is following.", 'buddypress' ), bp_get_displayed_user_fullname() ),
        'activity_args' => array(
            'user_id'  => bp_get_following_ids(),
            'display_comments' => 'threaded'
        )
    ) );
}
add_action( 'bp_actions', 'bp_follow_my_following_feed' );

}
`

In this corrected code, I've added conditional checks using `if ( ! function_exists(

@teboy
Copy link
Author

teboy commented Sep 21, 2023

The only error left now in my log is

2023-09-21T12:58:02+00:00 CRITICAL Cannot redeclare bp_follow_screen_followers() (previously declared in /var/www/wptbox/wp-content/plugins/buddypress-followers-master/_inc/users/screens.php:19) in /var/www/wptbox/wp-content/plugins/buddypress-followers-master/_inc/users/screens.php on line 17

@mrjarbenne
Copy link

Are you sure you don't have two different copies of the plugin active? The plugin from the WP repo is in a directory named buddypress-followers, while the one from Github has "-master" at the end, so manually FTP'ing the plugin wouldn't replace the older version. I would check the wp-content/plugins folder for duplicates.

@teboy
Copy link
Author

teboy commented Sep 21, 2023

@mrjarbenne im 100% sure I only have one copy. Im only using the copy from github

@mrjarbenne
Copy link

And the error still exists if you deactivate all your other plugins and activated a default theme? All these re-declarations make it seem like perhaps another plugin or theme has tried to integrate bp-follow functions that are getting triggered first.

@teboy
Copy link
Author

teboy commented Sep 21, 2023

Hello @r-a-y @mrjarbenne

This issue is now resolved. The problem was caused by a plugin conflict with https://wordpress.org/plugins/mark-new-posts/
Its quite strange but immediately I deactivated Mark New Post plugin there was no more error

Thanks so much for your support

r-a-y added a commit that referenced this issue Sep 21, 2023
…ng plugins.

In BuddyPress 12, the 'bp_parse_query' hook can run twice if a plugin
does weird things like fetching posts on the 'parse_query' again.

See #125.
@r-a-y
Copy link
Owner

r-a-y commented Sep 21, 2023

I looked into the "Mark New Posts" plugin and the problem is with this line: https://plugins.trac.wordpress.org/browser/mark-new-posts/trunk/mark-new-posts.php#L160 .

The plugin is fetching posts on the 'pre_get_posts' hook, so this means that any plugin hooked to any main post query would run twice. I've added a clause so my code is only included once, but even with this line added, the "Mark New Posts" plugin will still break any BuddyPress page. I've replied to teboy's post on wordpress.org with some recommendations: https://wordpress.org/support/topic/buddypress-follow-conflict/#post-17069687 .

@epgb101
Copy link

epgb101 commented Dec 22, 2023

Thank you all for doing this - I downloaded the zip file and it worked great on BP :)

@faithsly
Copy link

faithsly commented Feb 10, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants