0

I created a custom endpoint for specific data from a custom table in my Wordpress plugin. It get's all the data from the table with the getHelpers() function. After that it will be merged by some user data. I would like to add the profile_image as a link to the response so we can get it with the embed parameter.

What is the best way to add the link to the response? I know the $response->add_link() function but this would add it to the response and not to each contributor.

I tried to add the link as an array but this won't react on the _embed parameter.

This is my code for the custom endpoint:

class VEMS_Rest_Contributors extends WP_REST_Controller {

    protected $namespace = 'vems/v2';

    protected $rest_base = 'contributors';


    /**
     * Register the routes for coupons.
     */
    public function register_routes() {

         register_rest_route( $this->namespace, '/' . $this->rest_base, array(
            'methods'   => WP_REST_Server::READABLE,
            'callback'  => array( $this, 'get_items' ),
            'args'      => $this->get_collection_params(),
        ) );
    }

    public function get_items( WP_REST_Request $request ) {

        $project_id = $request->get_param( 'project_id' );

        $contributors = array();

        if( !empty($project_id) ) {

            $project = new VEMS_Project( $request['project_id'] );
            $helpers = $project->getHelpers();

            foreach($helpers as $helper) {

                $contributor = array();

                if( !empty($helper->contributor_id) ) {
                    $user = get_user_by( 'ID', $helper->contributor_id );
                    $user_meta = get_user_meta( $helper->contributor_id );

                    $contributor['ID'] = $helper->contributor_id;
                    $contributor['user_nicename'] = $user->data->display_name;
                    $contributor['user_profile_image'] = $user_meta['contributor_profile_image'][0];
                } else {
                    $contributor['user_nicename'] = $helper->name;
                    $contributor['user_profile_image'] = $helper->image_id;
                }

                $contributor['item_total'] = $helper->item_total;
                $contributor['checked'] = $helper->checked;
                $contributor['helper_date'] = $helper->helper_date;

                /*
                $contributor['_links']['profile_image'] = array(
                        'href' => rest_url( '/wp/v2/media/' . $contributor['user_profile_image'] ), 
                        'embeddable' => true
                    ); 
                */

                $contributors[] = $contributor;
            }

        }   

        $response = rest_ensure_response( $contributors );

        return $response;
    }

    public function get_collection_params() {

        $params['project_id'] = array(
            'description'       => __( 'Limit result set to contributors assigned a specific project.', 'vems' ),
            'type'              => 'integer',
            'sanitize_callback' => 'absint',
            'validate_callback' => 'rest_validate_request_arg',
        );

        return $params;
    }

}
Jainil
  • 1,488
  • 1
  • 21
  • 26
Emke
  • 91
  • 1
  • 1
  • 4
  • of which _embed parameter do you speak ? i don't see it in your code. – Kaperto Dec 03 '19 at 11:23
  • The default Wordpress rest api embed parameter which embeds the links to the json output. Like you can do with posts: /wp-json/wp/v2/posts?_embed There is an function $response->add_link( ); but i can't figure it out how it would work with multiple responses. – Emke Dec 03 '19 at 13:38

1 Answers1

0

to handle links on route vems/v2/contributors?_embed, the element profile_image must be an array of links and then you can do that

            $contributor['_links']['profile_image'] = [
                [
                    'href' => rest_url( '/wp/v2/media/' . $contributor['ID'] ), 
                    'embeddable' => true,
                ],
            ];
Kaperto
  • 281
  • 2
  • 8
  • I already tried to add this as you described. This will add it to the json but when i add the ?_embed=true parameter it won't be added in the _embedded key. Response when i go to wp-json/vems/v2/contributors/?_embed=true is still: `{ ID: "1", user_nicename: "Test", user_profile_image: "938", _links: { profile_image: { href: "http://dev.nl/vems/wp-json/wp/v2/media/938", embeddable: true } } }` – Emke Dec 03 '19 at 14:23
  • Found the solution: the ['_links']['profile_image'] needs to be a multiple array. So `$contributor['_links']['profile_image'] = array( 'href' => rest_url( '/wp/v2/media/' . $contributor['user_profile_image'] ), 'embeddable' => true ); ` wont work but `$contributor['_links']['profile_image'][] = array( 'href' => rest_url( '/wp/v2/media/' . $contributor['user_profile_image'] ), 'embeddable' => true ); ` will work! – Emke Dec 03 '19 at 14:28