buddypress/BP-REST

Add links for item available actions

Closed this issue ยท 8 comments

imath commented

I'm currently working on adding a BP reNouveau template pack in BP 11.0.0. This template pack will only use the BP REST API. So I'll probably report other possible improvements.

This one is about being able to get the endpoint for action concerning a member item of a members loop (eg: the endpoints to add friend/cancel friendship)

I see the WP Posts REST Controller is adding "self" links to add available actions, see https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L1939|L1947

It would be very useful to have this kind of functionality for members actions, but also for other component items (activity, group..) actions.

Agree! This is a low-effort addition.

imath commented

Awesome, do you have time to work on it?

I do!

@imath Just to clarify something. In some of our controllers, we are already providing links to access other related endpoints. Not all controllers have it available and we can expand on that as part of this issue.

However, we are not introducing this https://github.com/WordPress/wordpress-develop/blob/ab2cd6f03b591c773ae84db338d437d6cff376a4/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L2112-L2158 which seems to be adding and it is visible together with the schema.

I'm actually not sure how those https://api.w.org links are related to the prepare_links we need to add.

imath commented

Sorry for the api.w.org thing, you're right, the best is to add new entries inside the self array making sure to identify the action eg:

		$self[] = array(
			'action' => $data['friendship_status_slug'],
			'href'   => trailingslashit( $base_url ) . $data['id'],
		);

After looking briefly at what WordPress core is doing, I think I'm starting to understand what's happening there. So the goal is:

  • Add a schema link for the new action. Explaining what the action does. Here is an example.
  • Introduce the link itself. See example below.
$self['https://api.w.org/action-activity-delete'] = array(
	'href'=> trailingslashit( $base_url ) . $data['id'],
);

They are using this approach to create the rel, https://api.w.org/action-activity-delete. We can follow this practice or create something custom or unique to our use case. The response will be converted into something like this:


"wp:action-activity-delete": [
    {
        "href": "https://site.com/wp-json/buddypress/v1/activity/12328934687234"
    }
],

I actually think we can skip the schema part since we already don't introduce for the current links.

imath commented

Instead of "wp:action", can we have "bp:action" ?