Skip to:
Content

BuddyPress.org

Changeset 9324


Ignore:
Timestamp:
01/09/2015 05:48:38 AM (10 years ago)
Author:
r-a-y
Message:

XProfile: Make sure xprofile_get_field_id_from_name() returns a field ID for a field name.

Previously, when using xprofile_get_field_id_from_name(), it was possible
to get the option ID instead of the desired field ID because a field name
and an option value (for a checkbox or radio field) can potentially be the
the same in the wp_bp_xprofile_fields DB table.

This commit ensures that field name look ups will return a field ID instead
of an option ID. Commit also includes a unit test.

Props dontdream.

Fixes #6082.

Location:
trunk
Files:
2 edited

Legend:

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

    r9315 r9324  
    938938            return false;
    939939
    940         return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s", $field_name ) );
     940        return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s AND parent_id = 0", $field_name ) );
    941941    }
    942942
  • trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-field.php

    r8958 r9324  
    2727        $this->assertEquals( '0', $f2->can_delete );
    2828    }
     29
     30    /**
     31     * @group xprofile_get_field_id_from_name
     32     */
     33    public function test_get_id_from_name_field_name_option_value_conflict() {
     34        $group = $this->factory->xprofile_group->create();
     35
     36        // force some checkbox options for our profile field
     37        $_POST['checkbox_option'] = array(
     38            1 => 'BuddyPress',
     39            2 => 'WordPress'
     40        );
     41
     42        // checkbox field
     43        $f1 = $this->factory->xprofile_field->create( array(
     44            'field_group_id' => $group,
     45            'type' => 'checkbox',
     46            'name' => 'Interests'
     47        ) );
     48
     49        // textbox field with the same name as our checkbox value
     50        $f2 = $this->factory->xprofile_field->create( array(
     51            'field_group_id' => $group,
     52            'type' => 'textbox',
     53            'name' => 'BuddyPress'
     54        ) );
     55
     56        $this->assertEquals( $f2, xprofile_get_field_id_from_name( 'BuddyPress' ) );
     57
     58        // cleanup!
     59        unset( $_POST['checkbox_option'] );
     60    }
    2961}
Note: See TracChangeset for help on using the changeset viewer.