Skip to:
Content

BuddyPress.org

Changeset 7878


Ignore:
Timestamp:
02/14/2014 01:41:22 PM (8 years ago)
Author:
boonebgorges
Message:

Introduce bp_xprofile_add_meta().

See #5400

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-xprofile/bp-xprofile-functions.php

    r7862 r7878  
    643643}
    644644
     645/**
     646 * Add a piece of xprofile metadata.
     647 *
     648 * @since BuddyPress (2.0.0)
     649 *
     650 * @param int $object_id ID of the object the metadata belongs to.
     651 * @param string $object_type Type of object. 'group', 'field', or 'data'.
     652 * @param string $meta_key Metadata key.
     653 * @param mixed $meta_value Metadata value.
     654 * @param bool $unique. Optional. Whether to enforce a single metadata value
     655 *        for the given key. If true, and the object already has a value for
     656 *        the key, no change will be made. Default: false.
     657 * @return int|bool The meta ID on successful update, false on failure.
     658 */
     659function bp_xprofile_add_meta( $object_id, $object_type, $meta_key, $meta_value, $unique = false ) {
     660    add_filter( 'query', 'bp_filter_metaid_column_name' );
     661    add_filter( 'query', 'bp_xprofile_filter_meta_query' );
     662    $retval = add_metadata( 'xprofile_' . $object_type , $object_id, $meta_key, $meta_value, $unique );
     663    remove_filter( 'query', 'bp_filter_metaid_column_name' );
     664    remove_filter( 'query', 'bp_xprofile_filter_meta_query' );
     665
     666    return $retval;
     667}
     668
    645669function bp_xprofile_update_fieldgroup_meta( $field_group_id, $meta_key, $meta_value ) {
    646670    return bp_xprofile_update_meta( $field_group_id, 'group', $meta_key, $meta_value );
  • trunk/tests/testcases/xprofile/functions.php

    r7873 r7878  
    377377        $this->assertFalse( bp_xprofile_update_meta( $g, 'group', 'foo', 'bar' ) );
    378378    }
     379
     380    /**
     381     * @group xprofilemeta
     382     * @group bp_xprofile_add_meta
     383     */
     384    public function test_bp_xprofile_add_meta_no_meta_key() {
     385        $this->assertFalse( bp_xprofile_add_meta( 1, 'group', '', 'bar' ) );
     386    }
     387
     388    /**
     389     * @group xprofilemeta
     390     * @group bp_xprofile_add_meta
     391     */
     392    public function test_bp_xprofile_add_meta_empty_object_id() {
     393        $this->assertFalse( bp_xprofile_add_meta( 0, 'group', 'foo', 'bar' ) );
     394    }
     395
     396    /**
     397     * @group xprofilemeta
     398     * @group bp_xprofile_add_meta
     399     */
     400    public function test_bp_xprofile_add_meta_existing_unique() {
     401        $g = $this->factory->xprofile_group->create();
     402        bp_xprofile_add_meta( $g, 'group', 'foo', 'bar' );
     403        $this->assertFalse( bp_xprofile_add_meta( $g, 'group', 'foo', 'baz', true ) );
     404    }
     405
     406    /**
     407     * @group xprofilemeta
     408     * @group bp_xprofile_add_meta
     409     */
     410    public function test_bp_xprofile_add_meta_existing_not_unique() {
     411        $g = $this->factory->xprofile_group->create();
     412        bp_xprofile_add_meta( $g, 'group', 'foo', 'bar' );
     413        $this->assertNotEmpty( bp_xprofile_add_meta( $g, 'group', 'foo', 'baz' ) );
     414    }
    379415}
Note: See TracChangeset for help on using the changeset viewer.