Skip to:
Content

BuddyPress.org

Changeset 14025


Ignore:
Timestamp:
09/23/2024 02:35:01 PM (19 months ago)
Author:
espellcaste
Message:

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/

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-taxonomy.php

    r13968 r14025  
    142142 *
    143143 * @since 2.2.0
    144  *
    145  * @see wp_get_object_terms() for a full description of function and parameters.
     144 * @since 15.0.0 Added a `_doing_it_wrong` check to prevent errors when using the function too early.
     145 *
     146 * @see wp_get_object_terms() for a full description of function and parameters
    146147 *
    147148 * @param int|array    $object_ids ID or IDs of objects.
    148149 * @param string|array $taxonomies Name or names of taxonomies to match.
    149150 * @param array        $args       See {@see wp_get_object_terms()}.
    150  * @return array
     151 *
     152 * @return WP_Term[]|int[]|string[]
    151153 */
    152154function bp_get_object_terms( $object_ids, $taxonomies, $args = array() ) {
     155
     156    // Check if the bp_register_taxonomies hook is set.
     157    if ( ! did_action( 'bp_register_taxonomies' ) ) {
     158        _doing_it_wrong(
     159            __FUNCTION__,
     160            sprintf(
     161                /* translators: 1: the name of the function. 2: the name of the hook. */
     162                esc_html__( 'The %1$s function requires the %2$s hook to be fired before is used.', 'buddypress' ),
     163                '<code>bp_get_object_terms</code>',
     164                '<code>bp_register_taxonomies</code>'
     165            ),
     166            '15.0.0'
     167        );
     168
     169        // Return empty array since we don't have any taxonomies registered yet.
     170        return array();
     171    }
     172
    153173    // Different taxonomies must be stored on different sites.
    154174    $taxonomy_site_map = array();
     
    161181    foreach ( $taxonomy_site_map as $taxonomy_site_id => $site_taxonomies ) {
    162182        $switched = false;
     183
    163184        if ( $taxonomy_site_id !== get_current_blog_id() ) {
    164185            switch_to_blog( $taxonomy_site_id );
     
    168189
    169190        $site_terms = wp_get_object_terms( $object_ids, $site_taxonomies, $args );
    170         $retval     = array_merge( $retval, $site_terms );
     191
     192        // Handle the case where wp_get_object_terms() returns an error.
     193        if ( is_wp_error( $site_terms ) || empty( $site_terms ) ) {
     194            $site_terms = array();
     195        }
     196
     197        $retval = array_merge( $retval, $site_terms );
    171198
    172199        if ( $switched ) {
Note: See TracChangeset for help on using the changeset viewer.