Skip to:
Content

BuddyPress.org

Changeset 13792


Ignore:
Timestamp:
04/12/2024 01:12:04 PM (15 months ago)
Author:
espellcaste
Message:

A BP_XProfile_Field update does not disassociate from its XProfile parent field.

This patch makes sure that when updating an existing XProfile field option using the save() method from the BP_XProfile_Field class,
the updated option has not the parent_id set to 0, thus becoming disassociated from its XProfile parent field.

Closes https://github.com/buddypress/buddypress/pull/267
Fixes #9130

Location:
trunk
Files:
2 edited

Legend:

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

    r13494 r13792  
    449449
    450450        if ( ! $is_new_field ) {
    451             $sql = $wpdb->prepare( "UPDATE {$bp->profile->table_name_fields} SET group_id = %d, parent_id = 0, type = %s, name = %s, description = %s, is_required = %d, order_by = %s, field_order = %d, option_order = %d, can_delete = %d, is_default_option = %d WHERE id = %d", $this->group_id, $this->type, $this->name, $this->description, $this->is_required, $this->order_by, $this->field_order, $this->option_order, $this->can_delete, $this->is_default_option, $this->id );
     451            $sql = $wpdb->prepare(
     452                "UPDATE {$bp->profile->table_name_fields} SET group_id = %d, parent_id = %d, type = %s, name = %s, description = %s, is_required = %d, order_by = %s, field_order = %d, option_order = %d, can_delete = %d, is_default_option = %d WHERE id = %d",
     453                $this->group_id,
     454                $this->parent_id,
     455                $this->type,
     456                $this->name,
     457                $this->description,
     458                $this->is_required,
     459                $this->order_by,
     460                $this->field_order,
     461                $this->option_order,
     462                $this->can_delete,
     463                $this->is_default_option,
     464                $this->id
     465            );
    452466        } else {
    453             $sql = $wpdb->prepare( "INSERT INTO {$bp->profile->table_name_fields} (group_id, parent_id, type, name, description, is_required, order_by, field_order, option_order, can_delete, is_default_option ) VALUES ( %d, %d, %s, %s, %s, %d, %s, %d, %d, %d, %d )", $this->group_id, $this->parent_id, $this->type, $this->name, $this->description, $this->is_required, $this->order_by, $this->field_order, $this->option_order, $this->can_delete, $this->is_default_option );
     467            $sql = $wpdb->prepare(
     468                "INSERT INTO {$bp->profile->table_name_fields} (group_id, parent_id, type, name, description, is_required, order_by, field_order, option_order, can_delete, is_default_option ) VALUES ( %d, %d, %s, %s, %s, %d, %s, %d, %d, %d, %d )",
     469                $this->group_id,
     470                $this->parent_id,
     471                $this->type,
     472                $this->name,
     473                $this->description,
     474                $this->is_required,
     475                $this->order_by,
     476                $this->field_order,
     477                $this->option_order,
     478                $this->can_delete,
     479                $this->is_default_option
     480            );
    454481        }
    455482
  • trunk/tests/phpunit/testcases/xprofile/functions.php

    r13314 r13792  
    699699
    700700    /**
     701     * @ticket BP9130
     702     */
     703    public function test_xprofile_update_keep_parent_id() {
     704        $g      = self::factory()->xprofile_group->create();
     705        $parent = self::factory()->xprofile_field->create( array(
     706            'field_group_id' => $g,
     707            'type'           => 'selectbox',
     708            'name'           => 'Parent',
     709        ) );
     710
     711        $f = xprofile_insert_field(
     712            array(
     713                'field_group_id' => $g,
     714                'parent_id'      => $parent,
     715                'type'           => 'option',
     716                'name'           => 'Option 1',
     717                'option_order'   => 5,
     718            )
     719        );
     720
     721        $field = new BP_XProfile_Field( $f );
     722
     723        $this->assertEquals( $parent, $field->parent_id );
     724        $this->assertNotEquals( 0, $field->parent_id );
     725
     726        $field->name = 'Option 2';
     727        $field->save(); // Perform the `UPDATE` query. The reason for the bug.
     728
     729        // Fetch the new DB value.
     730        $field = new BP_XProfile_Field( $f );
     731
     732        $this->assertNotEquals( 0, $field->parent_id );
     733        $this->assertEquals( $parent, $field->parent_id );
     734    }
     735
     736    /**
    701737     * @group xprofile_insert_field
    702738     */
Note: See TracChangeset for help on using the changeset viewer.