💦 FULL SET: Changeset/ - Full Archive

Skip to:

Changeset 14171


Ignore:
Timestamp:
12/26/2025 07:02:18 PM (3 days ago)
Author:
espellcaste
Message:

Avoid an error attempting to convert a WP_Error object to a string when
checking for profile data existence.

While checking if a profile data exist, let us improve our error handling to avoid a PHP error trying to convert a WP_Error object to a string.

The reason why we might get a WP_Error object is varied, but it implies the data does not exist, and we should fail gracefully rather than throw an error.

Props rollybueno and potcus.

Closes https://github.com/buddypress/buddypress/pull/412/
Fixes #9290 (trunk)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-profiledata.php

    r13890 r14171  
    107107
    108108            // If the value is serializable, do not stripslashes.
    109             $this->value        = is_serialized( $profiledata->value ) ? $profiledata->value : stripslashes( $profiledata->value );
     109            $this->value = is_serialized( $profiledata->value ) ? $profiledata->value : stripslashes( $profiledata->value );
    110110        } else {
    111111            // When no row is found, we'll need to set these properties manually.
    112             $this->field_id    = (int) $field_id;
    113             $this->user_id      = (int) $user_id;
     112            $this->field_id = (int) $field_id;
     113            $this->user_id  = (int) $user_id;
    114114        }
    115115    }
     
    130130        $cache_key = "{$this->user_id}:{$this->field_id}";
    131131        $cached    = wp_cache_get( $cache_key, 'bp_xprofile_data' );
     132        $retval    = false;
    132133
    133134        if ( $cached && ! empty( $cached->id ) ) {
    134135            $retval = true;
    135136        } else {
    136             $bp     = buddypress();
    137             $retval = $wpdb->get_row( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_data} WHERE user_id = %d AND field_id = %d", $this->user_id, $this->field_id ) );
     137            $bp    = buddypress();
     138            $dbval = $wpdb->get_row( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_data} WHERE user_id = %d AND field_id = %d", $this->user_id, $this->field_id ) );
     139
     140            if ( ! is_wp_error( $dbval ) && ! empty( $dbval ) ) {
     141                $retval = true;
     142            }
    138143        }
    139144
     
    146151         * @param BP_XProfile_ProfileData $profile_data Instance of the current BP_XProfile_ProfileData class.
    147152         */
    148         return apply_filters_ref_array( 'xprofile_data_exists', array( (bool)$retval, $this ) );
     153        return (bool) apply_filters_ref_array( 'xprofile_data_exists', array( $retval, $this ) );
    149154    }
    150155
     
    163168        $bp     = buddypress();
    164169        $retval = $wpdb->get_row( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE id = %d", $this->field_id ) );
     170
     171        if ( is_wp_error( $retval ) || empty( $retval ) ) {
     172            $retval = false;
     173        }
    165174
    166175        /**
     
    172181         * @param BP_XProfile_ProfileData $profile_data Instance of the current BP_XProfile_ProfileData class.
    173182         */
    174         return apply_filters_ref_array( 'xprofile_data_is_valid_field', array( (bool)$retval, $this ) );
     183        return (bool) apply_filters_ref_array( 'xprofile_data_is_valid_field', array( (bool) $retval, $this ) );
    175184    }
    176185
Note: See TracChangeset for help on using the changeset viewer.