Skip to:
Content

BuddyPress.org

Opened 3 years ago

Closed 3 years ago

#8499 closed defect (bug) (fixed)

Allow BP_Buttons_Group::update() method to add new buttons

Reported by: sbrajesh's profile sbrajesh Owned by: imath's profile imath
Milestone: 9.0.0 Priority: normal
Severity: normal Version:
Component: Templates Keywords: has-patch commit
Cc:

Description

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?

Attachments (1)

8499.diff (4.3 KB) - added by sbrajesh 3 years ago.

Download all attachments as: .zip

Change History (5)

@sbrajesh
3 years ago

#1 @imath
3 years ago

  • Component changed from Core to Templates
  • Milestone changed from Awaiting Review to 9.0.0

Interesting. Thanks for your suggestion and patch. I'll look at it more deeply asap.

#2 @sbrajesh
3 years ago

Thank you.

#3 @imath
3 years ago

  • Keywords commit added

Just tested it! Good catch! Thanks a lot for your patch. I'll commit it asap.

#4 @imath
3 years ago

  • Owner set to imath
  • Resolution set to fixed
  • Status changed from new to closed

In 12983:

BP Nouveau: improve the update() method of the BP_Buttons_Group

  • Create a new method to add buttons.
  • Use it into the update() method to allow it to add new buttons to pre-existing ones.

Props sbrajesh

Fixes #8499

Note: See TracTickets for help on using tickets.