Skip to:
Content

BuddyPress.org

Changeset 10515


Ignore:
Timestamp:
02/05/2016 03:37:27 AM (10 years ago)
Author:
boonebgorges
Message:

Sort activity actions when they're added, instead of when they're fetched.

Sorting activity actions can be an expensive process, so should not be done
any more frequently than necessary. And it's only necessary to sort when a
new action has been added. So instead of sorting in bp_activity_get_actions(),
we now do it in bp_activity_set_action().

Props boonebgorges, imath.
Fixes #6865.

Location:
trunk
Files:
2 added
2 edited

Legend:

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

    r10457 r10515  
    385385        'position'        => $position,
    386386    ), $component_id, $type, $description, $format_callback, $label, $context );
     387
     388    // Sort the actions of the affected component.
     389    $action_array = (array) $bp->activity->actions->{$component_id};
     390    $action_array = bp_sort_by_key( $action_array, 'position', 'num' );
     391
     392    // Restore keys.
     393    $bp->activity->actions->{$component_id} = new stdClass;
     394    foreach ( $action_array as $key_ordered ) {
     395        $bp->activity->actions->{$component_id}->{$key_ordered['key']} = $key_ordered;
     396    }
    387397
    388398    return true;
     
    571581                $post_type->position
    572582            );
    573         }
    574     }
    575 
    576     // Sort the actions by their position within each component.
    577     foreach ( $bp->activity->actions as $component => $actions ) {
    578         $actions = (array) $actions;
    579         $temp = bp_sort_by_key( $actions, 'position', 'num' );
    580 
    581         // Restore keys.
    582         $bp->activity->actions->{$component} = new stdClass;
    583         foreach ( $temp as $key_ordered ) {
    584             $bp->activity->actions->{$component}->{$key_ordered['key']} = $key_ordered;
    585583        }
    586584    }
  • trunk/tests/phpunit/testcases/activity/functions.php

    r10412 r10515  
    870870    }
    871871
    872     /**
    873      * @group activity_action
    874      * @group bp_activity_get_actions
    875      */
    876     public function test_bp_activity_get_actions_should_sort_by_position() {
    877         $old_actions = bp_activity_get_actions();
    878         buddypress()->activity->actions = new stdClass;
    879 
    880         register_post_type( 'foo5', array(
    881             'public'      => true,
    882             'supports'    => array( 'buddypress-activity' ),
    883             'bp_activity' => array(
    884                 'component_id' => 'foo',
    885                 'action_id' => 'foo_bar_5',
    886                 'position' => 5,
    887             ),
    888         ) );
    889 
    890         register_post_type( 'foo50', array(
    891             'public'      => true,
    892             'supports'    => array( 'buddypress-activity' ),
    893             'bp_activity' => array(
    894                 'component_id' => 'foo',
    895                 'action_id' => 'foo_bar_50',
    896                 'position' => 50,
    897             ),
    898         ) );
    899 
    900         register_post_type( 'foo25', array(
    901             'public'      => true,
    902             'supports'    => array( 'buddypress-activity' ),
    903             'bp_activity' => array(
    904                 'component_id' => 'foo',
    905                 'action_id' => 'foo_bar_25',
    906                 'position' => 25,
    907             ),
    908         ) );
    909 
    910         $actions = bp_activity_get_actions();
    911 
    912         $expected = array(
    913             'foo_bar_5',
    914             'foo_bar_25',
    915             'foo_bar_50',
    916         );
    917         $foo_actions = (array) $actions->foo;
    918         $this->assertEquals( $expected, array_values( wp_list_pluck( $foo_actions, 'key' ) ) );
    919 
    920         buddypress()->activity->actions = $old_actions;
    921     }
    922872
    923873    /**
Note: See TracChangeset for help on using the changeset viewer.