Skip to:
Content

BuddyPress.org

Changeset 7883


Ignore:
Timestamp:
02/14/2014 07:02:16 PM (11 years ago)
Author:
boonebgorges
Message:

Add delete_all support for all _delete_meta() functions

The delete_all parameter allows you to delete all meta items matching the
specified meta_key, regardless of the associated object.

Because our meta delete functions also support the deletion of all metadata
associated with an object (by leaving out the meta_key param), a decision had
to be made regarding the behavior when _delete_meta() is called with delete_all

true and meta_key = false. The most logical (and least destructive) strategy

was deemed to be: force delete_all to false when meta_key is also false. This
ensures that you don't accidentally wipe out all metadata for a component.

See #5400

Location:
trunk
Files:
8 edited

Legend:

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

    r7879 r7883  
    539539 * Delete a meta entry from the DB for an activity stream item.
    540540 *
    541  * @since BuddyPress (1.2)
     541 * @since BuddyPress (1.2.0)
    542542 *
    543543 * @global object $wpdb WordPress database access object.
    544544 * @global object $bp BuddyPress global settings.
    545  * @uses wp_cache_delete()
    546  * @uses is_wp_error()
    547545 *
    548546 * @param int $activity_id ID of the activity item whose metadata is being deleted.
    549547 * @param string $meta_key Optional. The key of the metadata being deleted. If
    550  *                         omitted, all metadata associated with the activity
    551  *                         item will be deleted.
     548 *        omitted, all metadata associated with the activity
     549 *        item will be deleted.
    552550 * @param string $meta_value Optional. If present, the metadata will only be
    553  *                           deleted if the meta_value matches this parameter.
     551 *        deleted if the meta_value matches this parameter.
     552 * @param bool $delete_all Optional. If true, delete matching metadata entries
     553 *    for all objects, ignoring the specified object_id. Otherwise,
     554 *    only delete matching metadata entries for the specified
     555 *    activity item. Default: false.
    554556 * @return bool True on success, false on failure.
    555557 */
    556 function bp_activity_delete_meta( $activity_id, $meta_key = '', $meta_value = '' ) {
     558function bp_activity_delete_meta( $activity_id, $meta_key = '', $meta_value = '', $delete_all = false ) {
    557559    global $wpdb, $bp;
    558560
     
    572574        $all_meta = bp_activity_get_meta( $activity_id );
    573575        $keys     = ! empty( $all_meta ) ? wp_list_pluck( $all_meta, 'meta_key' ) : array();
     576
     577        // With no meta_key, ignore $delete_all
     578        $delete_all = false;
    574579    } else {
    575580        $keys = array( $meta_key );
     
    578583    add_filter( 'query', 'bp_filter_metaid_column_name' );
    579584    foreach ( $keys as $key ) {
    580         $retval = delete_metadata( 'activity', $activity_id, $key, $meta_value );
     585        $retval = delete_metadata( 'activity', $activity_id, $key, $meta_value, $delete_all );
    581586    }
    582587    remove_filter( 'query', 'bp_filter_metaid_column_name' );
  • trunk/bp-blogs/bp-blogs-functions.php

    r7879 r7883  
    835835 * @param string $meta_value Optional. If present, the metadata will only be
    836836 *        deleted if the meta_value matches this parameter.
     837 * @param bool $delete_all Optional. If true, delete matching metadata entries
     838 *    for all objects, ignoring the specified blog_id. Otherwise, only
     839 *    delete matching metadata entries for the specified blog.
     840 *    Default: false.
    837841 * @return bool True on success, false on failure.
    838842 */
    839 function bp_blogs_delete_blogmeta( $blog_id, $meta_key = false, $meta_value = false ) {
     843function bp_blogs_delete_blogmeta( $blog_id, $meta_key = false, $meta_value = false, $delete_all = false ) {
    840844    global $wpdb, $bp;
    841845
     
    851855    if ( empty( $meta_key ) ) {
    852856        $keys = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM {$wpdb->blogmeta} WHERE blog_id = %d", $blog_id ) );
     857        $delete_all = false;
    853858    } else {
    854859        $keys = array( $meta_key );
     
    861866
    862867    foreach ( $keys as $key ) {
    863         $retval = delete_metadata( 'blog', $blog_id, $key, $meta_value );
     868        $retval = delete_metadata( 'blog', $blog_id, $key, $meta_value, $delete_all );
    864869    }
    865870
  • trunk/bp-groups/bp-groups-functions.php

    r7875 r7883  
    966966 * @param bool $delete_all Optional. If true, delete matching metadata entries
    967967 *        for all groups. Default: false.
     968 * @param bool $delete_all Optional. If true, delete matching metadata entries
     969 *    for all objects, ignoring the specified group_id. Otherwise, only
     970 *    delete matching metadata entries for the specified group.
     971 *    Default: false.
    968972 * @return bool True on success, false on failure.
    969973 */
    970974function groups_delete_groupmeta( $group_id, $meta_key = false, $meta_value = false, $delete_all = false ) {
     975    global $wpdb;
    971976
    972977    // Legacy - return false if non-int group ID
     
    978983    $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    979984
     985    // Legacy - if no meta_key is passed, delete all for the item
     986    if ( empty( $meta_key ) ) {
     987        $keys = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM {$wpdb->groupmeta} WHERE group_id = %d", $group_id ) );
     988
     989        // With no meta_key, ignore $delete_all
     990        $delete_all = false;
     991    } else {
     992        $keys = array( $meta_key );
     993    }
     994
    980995    add_filter( 'query', 'bp_filter_metaid_column_name' );
    981     $retval = delete_metadata( 'group', $group_id, $meta_key, $meta_value, $delete_all );
     996
     997    foreach ( $keys as $key ) {
     998        $retval = delete_metadata( 'group', $group_id, $key, $meta_value, $delete_all );
     999    }
     1000
    9821001    remove_filter( 'query', 'bp_filter_metaid_column_name' );
    9831002
  • trunk/bp-xprofile/bp-xprofile-functions.php

    r7879 r7883  
    539539 * @param mixed $meta_value Optional. If provided, only metadata that matches
    540540 *        the value will be permitted.
     541 * @param bool $delete_all Optional. If true, delete matching metadata entries
     542 *    for all objects, ignoring the specified object_id. Otherwise, only
     543 *    delete matching metadata entries for the specified object.
     544 *    Default: false.
    541545 * @return bool True on success, false on failure.
    542546 */
    543 function bp_xprofile_delete_meta( $object_id, $object_type, $meta_key = false, $meta_value = false ) {
     547function bp_xprofile_delete_meta( $object_id, $object_type, $meta_key = false, $meta_value = false, $delete_all = false ) {
    544548    global $wpdb;
    545549
     
    559563        $table_name = $wpdb->{$table_key};
    560564        $keys = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM {$table_name} WHERE object_type = %s AND object_id = %d", $object_type, $object_id ) );
     565
     566        // Force delete_all to false if deleting all for object
     567        $delete_all = false;
    561568    } else {
    562569        $keys = array( $meta_key );
     
    570577
    571578    foreach ( $keys as $key ) {
    572         $retval = delete_metadata( 'xprofile_' . $object_type, $object_id, $key, $meta_value );
     579        $retval = delete_metadata( 'xprofile_' . $object_type, $object_id, $key, $meta_value, $delete_all );
    573580    }
    574581
  • trunk/tests/testcases/activity/functions.php

    r7879 r7883  
    337337    /**
    338338     * @group activitymeta
     339     * @group bp_activity_delete_meta
     340     */
     341    public function test_bp_activity_delete_meta_with_delete_all_but_no_meta_key() {
     342        // With no meta key, don't delete for all items - just delete
     343        // all for a single item
     344        $a1 = $this->factory->activity->create();
     345        $a2 = $this->factory->activity->create();
     346        bp_activity_update_meta( $a1, 'foo', 'bar' );
     347        bp_activity_update_meta( $a1, 'foo1', 'bar1' );
     348        bp_activity_update_meta( $a2, 'foo', 'bar' );
     349        bp_activity_update_meta( $a2, 'foo1', 'bar1' );
     350
     351        $this->assertTrue( bp_activity_delete_meta( $a1, '', '', true ) );
     352        $this->assertEmpty( bp_activity_get_meta( $a1 ) );
     353        $this->assertSame( 'bar', bp_activity_get_meta( $a2, 'foo' ) );
     354        $this->assertSame( 'bar1', bp_activity_get_meta( $a2, 'foo1' ) );
     355    }
     356
     357    /**
     358     * @group activitymeta
     359     * @group bp_activity_delete_meta
     360     */
     361    public function test_bp_activity_delete_meta_with_delete_all() {
     362        // With no meta key, don't delete for all items - just delete
     363        // all for a single item
     364        $a1 = $this->factory->activity->create();
     365        $a2 = $this->factory->activity->create();
     366        bp_activity_update_meta( $a1, 'foo', 'bar' );
     367        bp_activity_update_meta( $a1, 'foo1', 'bar1' );
     368        bp_activity_update_meta( $a2, 'foo', 'bar' );
     369        bp_activity_update_meta( $a2, 'foo1', 'bar1' );
     370
     371        $this->assertTrue( bp_activity_delete_meta( $a1, 'foo', '', true ) );
     372        $this->assertEmpty( '', bp_activity_get_meta( $a1, 'foo' ) );
     373        $this->assertEmpty( '', bp_activity_get_meta( $a2, 'foo' ) );
     374        $this->assertSame( 'bar1', bp_activity_get_meta( $a1, 'foo1' ) );
     375        $this->assertSame( 'bar1', bp_activity_get_meta( $a2, 'foo1' ) );
     376    }
     377    /**
     378     * @group activitymeta
    339379     * @group bp_activity_add_meta
    340380     */
  • trunk/tests/testcases/blogs/functions.php

    r7879 r7883  
    6363    /**
    6464     * @group blogmeta
     65     * @group bp_blogs_delete_blogmeta
     66     */
     67    public function test_bp_blogs_delete_blogmeta_with_delete_all_but_no_meta_key() {
     68        // With no meta key, don't delete for all items - just delete
     69        // all for a single item
     70        bp_blogs_add_blogmeta( 1, 'foo', 'bar' );
     71        bp_blogs_add_blogmeta( 1, 'foo1', 'bar1' );
     72        bp_blogs_add_blogmeta( 2, 'foo', 'bar' );
     73        bp_blogs_add_blogmeta( 2, 'foo1', 'bar1' );
     74
     75        $this->assertTrue( bp_blogs_delete_blogmeta( 1, '', '', true ) );
     76        $this->assertEmpty( bp_blogs_get_blogmeta( 1 ) );
     77        $this->assertSame( 'bar', bp_blogs_get_blogmeta( 2, 'foo' ) );
     78        $this->assertSame( 'bar1', bp_blogs_get_blogmeta( 2, 'foo1' ) );
     79    }
     80
     81    /**
     82     * @group blogmeta
     83     * @group bp_blogs_delete_blogmeta
     84     */
     85    public function test_bp_blogs_delete_blogmeta_with_delete_all() {
     86        // With no meta key, don't delete for all items - just delete
     87        // all for a single item
     88        bp_blogs_add_blogmeta( 1, 'foo', 'bar' );
     89        bp_blogs_add_blogmeta( 1, 'foo1', 'bar1' );
     90        bp_blogs_add_blogmeta( 2, 'foo', 'bar' );
     91        bp_blogs_add_blogmeta( 2, 'foo1', 'bar1' );
     92
     93        $this->assertTrue( bp_blogs_delete_blogmeta( 1, 'foo', '', true ) );
     94        $this->assertEmpty( '', bp_blogs_get_blogmeta( 1, 'foo' ) );
     95        $this->assertEmpty( '', bp_blogs_get_blogmeta( 2, 'foo' ) );
     96        $this->assertSame( 'bar1', bp_blogs_get_blogmeta( 1, 'foo1' ) );
     97        $this->assertSame( 'bar1', bp_blogs_get_blogmeta( 2, 'foo1' ) );
     98    }
     99
     100    /**
     101     * @group blogmeta
    65102     * @group bp_blogs_get_blogmeta
    66103     */
  • trunk/tests/testcases/groups/functions.php

    r7879 r7883  
    437437    /**
    438438     * @group groupmeta
     439     * @group groups_delete_groupmeta
     440     */
     441    public function test_groups_delete_groupmeta_with_delete_all_but_no_meta_key() {
     442        // With no meta key, don't delete for all items - just delete
     443        // all for a single item
     444        $g1 = $this->factory->group->create();
     445        $g2 = $this->factory->group->create();
     446        groups_add_groupmeta( $g1, 'foo', 'bar' );
     447        groups_add_groupmeta( $g1, 'foo1', 'bar1' );
     448        groups_add_groupmeta( $g2, 'foo', 'bar' );
     449        groups_add_groupmeta( $g2, 'foo1', 'bar1' );
     450
     451        $this->assertTrue( groups_delete_groupmeta( $g1, '', '', true ) );
     452        $this->assertEmpty( groups_get_groupmeta( $g1 ) );
     453        $this->assertSame( 'bar', groups_get_groupmeta( $g2, 'foo' ) );
     454        $this->assertSame( 'bar1', groups_get_groupmeta( $g2, 'foo1' ) );
     455    }
     456
     457    /**
     458     * @group groupmeta
     459     * @group groups_delete_groupmeta
     460     */
     461    public function test_groups_delete_groupmeta_with_delete_all() {
     462        // With no meta key, don't delete for all items - just delete
     463        // all for a single item
     464        $g1 = $this->factory->group->create();
     465        $g2 = $this->factory->group->create();
     466        groups_add_groupmeta( $g1, 'foo', 'bar' );
     467        groups_add_groupmeta( $g1, 'foo1', 'bar1' );
     468        groups_add_groupmeta( $g2, 'foo', 'bar' );
     469        groups_add_groupmeta( $g2, 'foo1', 'bar1' );
     470
     471        $this->assertTrue( groups_delete_groupmeta( $g1, 'foo', '', true ) );
     472        $this->assertEmpty( '', groups_get_groupmeta( $g1, 'foo' ) );
     473        $this->assertEmpty( '', groups_get_groupmeta( $g2, 'foo' ) );
     474        $this->assertSame( 'bar1', groups_get_groupmeta( $g1, 'foo1' ) );
     475        $this->assertSame( 'bar1', groups_get_groupmeta( $g2, 'foo1' ) );
     476    }
     477
     478    /**
     479     * @group groupmeta
    439480     * @group groups_add_groupmeta
    440481     */
  • trunk/tests/testcases/xprofile/functions.php

    r7879 r7883  
    234234        $this->assertTrue( bp_xprofile_delete_meta( $g, 'group' ) );
    235235
    236         // These will fail because of a caching bug
    237236        $this->assertEquals( '', bp_xprofile_get_meta( $g, 'group', 'foo' ) );
    238237        $this->assertEquals( '', bp_xprofile_get_meta( $g, 'group', 'foo2' ) );
     238    }
     239
     240    /**
     241     * @group xprofilemeta
     242     * @group bp_xprofile_delete_meta
     243     */
     244    public function test_bp_xprofile_delete_meta_with_delete_all_but_no_meta_key() {
     245        // With no meta key, don't delete for all items - just delete
     246        // all for a single item
     247        $g1 = $this->factory->xprofile_group->create();
     248        $g2 = $this->factory->xprofile_group->create();
     249        bp_xprofile_add_meta( $g1, 'group', 'foo', 'bar' );
     250        bp_xprofile_add_meta( $g1, 'group', 'foo1', 'bar1' );
     251        bp_xprofile_add_meta( $g2, 'group', 'foo', 'bar' );
     252        bp_xprofile_add_meta( $g2, 'group', 'foo1', 'bar1' );
     253
     254        $this->assertTrue( bp_xprofile_delete_meta( $g1, 'group', '', '', true ) );
     255        $this->assertEmpty( bp_xprofile_get_meta( $g1, 'group' ) );
     256        $this->assertSame( 'bar', bp_xprofile_get_meta( $g2, 'group', 'foo' ) );
     257        $this->assertSame( 'bar1', bp_xprofile_get_meta( $g2, 'group', 'foo1' ) );
     258    }
     259
     260    /**
     261     * @group xprofilemeta
     262     * @group bp_xprofile_delete_meta
     263     */
     264    public function test_bp_xprofile_delete_meta_with_delete_all() {
     265        // With no meta key, don't delete for all items - just delete
     266        // all for a single item
     267        $g1 = $this->factory->xprofile_group->create();
     268        $g2 = $this->factory->xprofile_group->create();
     269        bp_xprofile_add_meta( $g1, 'group', 'foo', 'bar' );
     270        bp_xprofile_add_meta( $g1, 'group', 'foo1', 'bar1' );
     271        bp_xprofile_add_meta( $g2, 'group', 'foo', 'bar' );
     272        bp_xprofile_add_meta( $g2, 'group', 'foo1', 'bar1' );
     273
     274        $this->assertTrue( bp_xprofile_delete_meta( $g1, 'group', 'foo', '', true ) );
     275        $this->assertEmpty( '', bp_xprofile_get_meta( $g1, 'group', 'foo' ) );
     276        $this->assertEmpty( '', bp_xprofile_get_meta( $g2, 'group', 'foo' ) );
     277        $this->assertSame( 'bar1', bp_xprofile_get_meta( $g1, 'group', 'foo1' ) );
     278        $this->assertSame( 'bar1', bp_xprofile_get_meta( $g2, 'group', 'foo1' ) );
    239279    }
    240280
Note: See TracChangeset for help on using the changeset viewer.