[buddypress-trac] [BuddyPress Trac] #8499: Allow BP_Buttons_Group::update() method to add new buttons

buddypress-trac noreply at wordpress.org
Tue Jun 29 22:24:36 UTC 2021


#8499: Allow BP_Buttons_Group::update() method to add new buttons
--------------------------+-----------------------------
 Reporter:  sbrajesh      |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Core          |    Version:
 Severity:  normal        |   Keywords:  has-patch
--------------------------+-----------------------------
 Hi,
 Currently, The BP_Buttons_Group::update() method only updates the settings
 for the  buttons which were previously added to it (The buttons with which
 the button group was initialized).  If a new button is passed, since the
 id does not exist in groups array, it is not added.

 This causes problem with buttons added via
 "bp_nouveau_get_members_buttons" and "bp_nouveau_get_groups_buttons"
 hooks.

 I will explain with an example of "bp_nouveau_get_members_buttons".

 I am adding a "View Profile" button using the following code.

 {{{

 function bp_example_group_button_error_trigger( $buttons, $user_id, $type
 ) {

         $user_url = bp_core_get_user_domain( $user_id );
         // add profile button
         $buttons['view_profile'] = array(
                 'id'                => 'view_profile',
                 'position'          => 0,
                 'component'         => 'members',
                 'must_be_logged_in' => false,
                 'parent_element'    => 'li', // this is bad, we have
 another patch for it to allow passing $args.
                 'link_text'         => _x( 'View Profile', 'button',
 'buddypress' ),
                 'parent_attr'       => array(
                         'id'    => '',
                         'class' => 'generic-button',
                 ),
                 'button_element'    => 'a',
                 'button_attr'       => array(
                         'class'         => 'button view-profile',
                         'rel'           => '',
                         'href'          => $user_url,
                 ),
         );

         return $buttons;
 }

 add_filter( 'bp_nouveau_get_members_buttons',
 'bp_example_group_button_error_trigger', 0, 3 );
 }}}

 If you use this, the Friendship button will not appear in the
 directory(assuming that you are the latest active user in the directory).

 Here is the code that runs after the above hook:-

 {{{
                 if ( ! isset( bp_nouveau()->members->member_buttons ) || !
 is_a( bp_nouveau()->members->member_buttons, 'BP_Buttons_Group' ) ) {
                         $sort = true;
                         bp_nouveau()->members->member_buttons = new
 BP_Buttons_Group( $buttons_group );

                 // It's not the first entry, the order is set, we simply
 need to update the Buttons Group
                 } else {
                         $sort = false;

                         bp_nouveau()->members->member_buttons->update(
 $buttons_group );
                 }
 }}}


 The above code initializes BP_Buttons_Group for the first time. If you are
 the the first user, Only the view_profile button gets added.
 Now, when looping for the second user, even if the friendship button is
 passed, the update code does not take it in.


 {{{
         public function update( $args = array() ) {
                 foreach ( $args as $id => $params ) {
                         if ( isset( $this->group[ $id ] ) ) {
                                 $this->group[ $id ] = bp_parse_args(
                                         $params,
                                         $this->group[ $id ],
                                         'buttons_group_update'
                                 );
                         }
                 }
         }
 }}}

 The solution is to allow adding new buttons to the group in the update
 method.

 I have refactored the BP_Buttons_Group class to allow code reuse for
 adding the button to group.

 Please feel free to update the code if the current patch does not meet the
 standards. Is it possible to have a fix for it in the next release?

-- 
Ticket URL: <https://buddypress.trac.wordpress.org/ticket/8499>
BuddyPress Trac <http://buddypress.org/>
BuddyPress Trac


More information about the buddypress-trac mailing list