Skip to:
Content

BuddyPress.org

Opened 2 months ago

Closed 3 weeks ago

#9227 closed enhancement (fixed)

Fatal error due to bp_get_object_terms function

Reported by: raviousprime's profile raviousprime Owned by: espellcaste's profile espellcaste
Milestone: 15.0.0 Priority: normal
Severity: normal Version:
Component: Members Keywords: has-patch has-testing-info
Cc:

Description

the function with name 'bp_get_object_terms' doing fatal due to array_merge call in it. In my case 'wp_get_object_terms' returning WP_Error instance.

Change History (13)

#1 @espellcaste
2 months ago

  • Milestone changed from 15.0.0 to 14.1.0

Thanks! I think we can handle this better. Could you share the WP_Error message?

Last edited 2 months ago by espellcaste (previous) (diff)

#2 @raviousprime
2 months ago

Thank you,

PHP Fatal error: Uncaught TypeError: array_merge(): Argument #2 must be of type array, WP_Error given in /var/www/bp/support/wp-content/plugins/buddypress/bp-core/bp-core-taxonomy.php:173

WP Error message: 'Invalid taxonomy'.

#3 @espellcaste
2 months ago

@raviousprime Do you have access to the invalid taxonomy too?

#4 @raviousprime
2 months ago

Hello

Complete error code

[19-Aug-2024 16:33:42 UTC] PHP Fatal error:  Uncaught TypeError: array_merge(): Argument #2 must be of type array, WP_Error given in /var/www/bp/support/wp-content/plugins/buddypress/bp-core/bp-core-taxonomy.php:171
Stack trace:
#0 /var/www/bp/support/wp-content/plugins/buddypress/bp-core/bp-core-taxonomy.php(171): array_merge()
#1 /var/www/bp/support/wp-content/plugins/buddypress/bp-members/bp-members-functions.php(3197): bp_get_object_terms()
#2 /var/www/bp/support/wp-content/plugins/bp-custom.php(22): bp_get_member_type()
#3 /var/www/bp/support/wp-includes/class-wp-hook.php(324): BuddyPress->{closure}()
#4 /var/www/bp/support/wp-includes/plugin.php(205): WP_Hook->apply_filters()
#5 /var/www/bp/support/wp-content/mu-plugins/user-switching.php(374): apply_filters()
#6 /var/www/bp/support/wp-content/mu-plugins/user-switching.php(194): user_switching::get_redirect()
#7 /var/www/bp/support/wp-includes/class-wp-hook.php(324): user_switching->action_init()
#8 /var/www/bp/support/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
#9 /var/www/bp/support/wp-includes/plugin.php(517): WP_Hook->do_action()
#10 /var/www/bp/support/wp-settings.php(700): do_action()
#11 /var/www/bp/support/wp-config.php(99): require_once('...')
#12 /var/www/bp/support/wp-load.php(50): require_once('...')
#13 /var/www/bp/support/wp-login.php(12): require('...')
#14 {main}
  thrown in /var/www/bp/support/wp-content/plugins/buddypress/bp-core/bp-core-taxonomy.php on line 171

PHP Code

<?php

add_filter( 'login_redirect', function ( $redirect_to_calculated, $redirect_url_specified, $user ) {

        if ( ! $user || is_wp_error( $user ) ) {
                return $redirect_to_calculated;
        }

        $member_type = bp_get_member_type( $user->ID, true );

        if ( ! $member_type ) {
                return '';
        }

        return $redirect_to_calculated;
}, 111, 3 );

#5 @imath
2 months ago

  • Component changed from Core to Members
  • Priority changed from high to normal
  • Severity changed from critical to normal

@raviousprime If you get an Invalid taxonomy WP_Error, it means you're trying to access to the Member Type taxonomy (bp_member_type) too early. So your code won't work after we've fixed the fatal unfortunately.

The best way to be sure the Member Type taxonomy is registered is to wait until the action hook bp_register_taxonomies is fired using priority 21 or later.

@espellcaste to me, it's not a regression introduced in 14.0.0 and the root cause is a custom code that is wrongly trying to get something that doesn't exist yet.

We need to fix it for sure, but I would do it during the 15.0.0 milestone as I don't think it's an emergency justifying we bypass the main role of 14.x maintenance releases (fixing regressions introduced in 14.0).

#6 @espellcaste
2 months ago

  • Keywords needs-patch added
  • Milestone changed from 14.1.0 to 15.0.0
  • Type changed from defect (bug) to enhancement

@imath Awesome! Thanks for pointing the root of the issue.

#7 @raviousprime
8 weeks ago

@imath @espellcaste The issue is caused by the user-switching plugin. On the init action, it switches the user. However, I am using this plugin as a mu-plugin, which causes the problem. Additionally, there is still an issue with the function bp_get_object_terms, specifically in the array_merge call. This function merges the result of wp_get_object_terms without checking whether the result is an array.

#8 @imath
8 weeks ago

@raviousprime Sure, we’ll work on a fix.

My point is the plugin you use is uselessly checking for a member type as the corresponding taxonomy has no chance to be registered that early. So my advise to the plugin author would be to use another strategy to perform the redirect to a different page if a user has a member type.

#9 @raviousprime
8 weeks ago

Got it, thanks for the feedback!

This ticket was mentioned in PR #370 on buddypress/buddypress by renatonascalves.


6 weeks ago
#10

  • Keywords has-patch added; needs-patch removed

#11 @espellcaste
6 weeks ago

I recreated the same environment from @raviousprime locally and was able to replicate.

I added a pull request where we perform a few tasks:

  • We add a doing it wrong notice. So that we highlight to the site that the function is being used too early.
  • If the function is used too early, we add this notice and return an empty array. Since there is no registered taxonomy yet, it is a safer option than returning another error.
  • We handle cases where wp_get_object_terms returns an empty value or a WP_Error.

@raviousprime If you could test this, it'd be great. You should see a new PHP notice:

PHP Notice: Function bp_get_object_terms was called <strong>incorrectly</strong>. The <code>bp_get_object_terms</code> function requires the <code>bp_register_taxonomies</code> hook to be fired before is used. Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 15.0.0.) in /var/www/bp-single/wp-includes/functions.php on line 6085


#12 @narenin
6 weeks ago

  • Keywords has-testing-info added

Hi,

Test Report

Patch tested: https://github.com/buddypress/buddypress/pull/370

Environment:

OS: Windows
PHP: 8.2.12
WordPress: 6.6.1
Browser: Chrome
Theme: Twenty Twenty-One (Version: 2.3)
Plugins: BuddyPress

Actual Results:

✅ Patch working well as desired solution.

#13 @espellcaste
3 weeks ago

  • Owner set to espellcaste
  • Resolution set to fixed
  • Status changed from new to closed

In 14025:

Fix: bp_get_object_terms returning a WP_Error object when used too early.

Added a _doing_it_wrong check to prevent errors when using the bp_get_object_terms function too early (before taxonomies are registered). Also, handle the case where wp_get_object_terms() returns an error.

Props raviousprime, paulgibbs, and imath.

Fixes #9227
Closes https://github.com/buddypress/buddypress/pull/370/

Note: See TracTickets for help on using tickets.