Skip to:
Content

BuddyPress.org

Changeset 7885


Ignore:
Timestamp:
02/14/2014 07:21:52 PM (8 years ago)
Author:
boonebgorges
Message:

Add support for prev_value param to _update_meta() functions

This parameter, inherited from update_metadata(), allows you to limit your
updates only to rows where the meta_value matches your specified prev_value.

See #5400

Location:
trunk
Files:
8 edited

Legend:

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

    r7883 r7885  
    653653 * @since BuddyPress (1.2)
    654654 *
    655  * @param int $activity_id ID of the activity item whose metadata is being updated.
     655 * @param int $activity_id ID of the activity item whose metadata is being
     656 *        updated.
    656657 * @param string $meta_key Key of the metadata being updated.
    657658 * @param mixed $meta_value Value to be set.
     659 * @param mixed $prev_value Optional. If specified, only update existing
     660 *        metadata entries with the specified value. Otherwise, update all
     661 *        entries.
    658662 * @return bool True on success, false on failure.
    659663 */
    660 function bp_activity_update_meta( $activity_id, $meta_key, $meta_value ) {
     664function bp_activity_update_meta( $activity_id, $meta_key, $meta_value, $prev_value = '' ) {
    661665
    662666    // Legacy - Make sure activity_id is valid
     
    669673
    670674    add_filter( 'query', 'bp_filter_metaid_column_name' );
    671     $retval = update_metadata( 'activity', $activity_id, $meta_key, $meta_value );
     675    $retval = update_metadata( 'activity', $activity_id, $meta_key, $meta_value, $prev_value );
    672676    remove_filter( 'query', 'bp_filter_metaid_column_name' );
    673677
  • trunk/bp-blogs/bp-blogs-functions.php

    r7883 r7885  
    922922 * @param string $meta_key Key of the metadata being updated.
    923923 * @param mixed $meta_value Value to be set.
     924 * @param mixed $prev_value Optional. If specified, only update existing
     925 *        metadata entries with the specified value. Otherwise, update all
     926 *        entries.
    924927 * @return bool True on success, false on failure.
    925928 */
    926 function bp_blogs_update_blogmeta( $blog_id, $meta_key, $meta_value ) {
     929function bp_blogs_update_blogmeta( $blog_id, $meta_key, $meta_value, $prev_value = '' ) {
    927930
    928931    // Legacy - Sanitize meta_key
     
    930933
    931934    add_filter( 'query', 'bp_filter_metaid_column_name' );
    932     $retval = update_metadata( 'blog', $blog_id, $meta_key, $meta_value );
     935    $retval = update_metadata( 'blog', $blog_id, $meta_key, $meta_value, $prev_value );
    933936    remove_filter( 'query', 'bp_filter_metaid_column_name' );
    934937
  • trunk/bp-groups/bp-groups-functions.php

    r7883 r7885  
    10411041 * @param string $meta_key Metadata key.
    10421042 * @param mixed $meta_value Value to store.
     1043 * @param mixed $prev_value Optional. If specified, only update existing
     1044 *        metadata entries with the specified value. Otherwise, update all
     1045 *        entries.
    10431046 * @return bool True on success, false on failure.
    10441047 */
    1045 function groups_update_groupmeta( $group_id, $meta_key, $meta_value ) {
     1048function groups_update_groupmeta( $group_id, $meta_key, $meta_value, $prev_value = '' ) {
    10461049
    10471050    add_filter( 'query', 'bp_filter_metaid_column_name' );
    1048     $retval = update_metadata( 'group', $group_id, $meta_key, $meta_value );
     1051    $retval = update_metadata( 'group', $group_id, $meta_key, $meta_value, $prev_value );
    10491052    remove_filter( 'query', 'bp_filter_metaid_column_name' );
    10501053
  • trunk/bp-xprofile/bp-xprofile-functions.php

    r7883 r7885  
    634634 * @param string $meta_key Key of the metadata being updated.
    635635 * @param mixed $meta_value Value of the metadata being updated.
     636 * @param mixed $prev_value Optional. If specified, only update existing
     637 *        metadata entries with the specified value. Otherwise, update all
     638 *        entries.
    636639 * @return bool True on success, false on failure.
    637640 */
    638 function bp_xprofile_update_meta( $object_id, $object_type, $meta_key, $meta_value ) {
     641function bp_xprofile_update_meta( $object_id, $object_type, $meta_key, $meta_value, $prev_value = '' ) {
    639642
    640643    // Legacy - sanitize meta_key
     
    643646    add_filter( 'query', 'bp_filter_metaid_column_name' );
    644647    add_filter( 'query', 'bp_xprofile_filter_meta_query' );
    645     $retval = update_metadata( 'xprofile_' . $object_type, $object_id, $meta_key, $meta_value );
     648    $retval = update_metadata( 'xprofile_' . $object_type, $object_id, $meta_key, $meta_value, $prev_value );
    646649    remove_filter( 'query', 'bp_xprofile_filter_meta_query' );
    647650    remove_filter( 'query', 'bp_filter_metaid_column_name' );
  • trunk/tests/testcases/activity/functions.php

    r7884 r7885  
    190190        $this->assertFalse( bp_activity_update_meta( $a, 'foo', 'bar' ) );
    191191        $this->assertSame( 'bar', bp_activity_get_meta( $a, 'foo' ) );
     192    }
     193
     194    /**
     195     * @group activitymeta
     196     * @group bp_activity_update_meta
     197     */
     198    public function test_bp_activity_update_meta_prev_value() {
     199        $a = $this->factory->activity->create();
     200        bp_activity_add_meta( $a, 'foo', 'bar' );
     201        $this->assertFalse( bp_activity_update_meta( $a, 'foo', 'bar2', 'baz' ) );
     202        $this->assertTrue( bp_activity_update_meta( $a, 'foo', 'bar2', 'bar' ) );
    192203    }
    193204
  • trunk/tests/testcases/blogs/functions.php

    r7883 r7885  
    186186    /**
    187187     * @group blogmeta
    188      * @group bp_blogs_update_groupmeta
     188     * @group bp_blogs_update_blogmeta
    189189     */
    190190    public function test_bp_blogs_update_blogmeta_new() {
     
    195195    /**
    196196     * @group blogmeta
    197      * @group bp_blogs_update_groupmeta
     197     * @group bp_blogs_update_blogmeta
    198198     */
    199199    public function test_bp_blogs_update_blogmeta_existing() {
     
    206206    /**
    207207     * @group blogmeta
    208      * @group bp_blogs_update_groupmeta
     208     * @group bp_blogs_update_blogmeta
    209209     */
    210210    public function test_bp_blogs_update_blogmeta_existing_no_change() {
     
    212212        $this->assertSame( 'bar', bp_blogs_get_blogmeta( 1, 'foo' ) );
    213213        $this->assertFalse( bp_blogs_update_blogmeta( 1, 'foo', 'bar' ) );
     214    }
     215
     216    /**
     217     * @group blogmeta
     218     * @group bp_blogs_update_blogmeta
     219     */
     220    public function test_bp_blogs_update_meta_prev_value() {
     221        bp_blogs_add_blogmeta( 1, 'foo', 'bar' );
     222        $this->assertFalse( bp_blogs_update_blogmeta( 1, 'foo', 'bar2', 'baz' ) );
     223        $this->assertTrue( bp_blogs_update_blogmeta( 1, 'foo', 'bar2', 'bar' ) );
    214224    }
    215225
  • trunk/tests/testcases/groups/functions.php

    r7883 r7885  
    337337    /**
    338338     * @group groupmeta
     339     * @group groups_update_groupmeta
     340     */
     341    public function test_groups_update_groupmeta_prev_value() {
     342        $g = $this->factory->group->create();
     343        groups_add_groupmeta( $g, 'foo', 'bar' );
     344        $this->assertFalse( groups_update_groupmeta( $g, 'foo', 'bar2', 'baz' ) );
     345        $this->assertTrue( groups_update_groupmeta( $g, 'foo', 'bar2', 'bar' ) );
     346    }
     347
     348    /**
     349     * @group groupmeta
    339350     *
    340351     * @todo Why do we do this?
  • trunk/tests/testcases/xprofile/functions.php

    r7883 r7885  
    443443    /**
    444444     * @group xprofilemeta
     445     * @group bp_xprofile_update_meta
     446     */
     447    public function test_bp_xprofile_update_meta_prev_value() {
     448        $g = $this->factory->xprofile_group->create();
     449        bp_xprofile_add_meta( $g, 'group', 'foo', 'bar' );
     450        $this->assertFalse( bp_xprofile_update_meta( $g, 'group', 'foo', 'bar2', 'baz' ) );
     451        $this->assertTrue( bp_xprofile_update_meta( $g, 'group', 'foo', 'bar2', 'bar' ) );
     452    }
     453
     454    /**
     455     * @group xprofilemeta
    445456     * @group bp_xprofile_add_meta
    446457     */
Note: See TracChangeset for help on using the changeset viewer.