Site icon Hip-Hop Website Design and Development

Customized endpoint and X-WP-TotalPages (headers)

I’ve a customized endpoint outlined like so:

    add_action( 'rest_api_init', perform () {
        register_rest_route( 'menc/v1', '/crosscat/(?P<dept>[w-]+)/(?P<cat>[w-]+)', array(
            'strategies' => 'GET',
            'callback' => 'dept_cat_api',
            'args' => array(
                'dept' => array(
                    'sanitize_callback' => perform($param, $request, $key) {
                        return sanitize_text_field( $param );
                    }
                ),
                'cat' => array(
                    'sanitize_callback' => perform($param, $request, $key) {
                        return sanitize_text_field( $param );
                    }
                ),
                'per_page' => array(
                    'default' => 10,
                    'sanitize_callback' => 'absint',
                ),
                'web page' => array(
                    'default' => 1,
                    'sanitize_callback' => 'absint',
                ),
                'orderby' => array(
                    'default' => 'date',
                    'sanitize_callback' => 'sanitize_text_field',
                ),
                'order' => array(
                    'default' => 'desc',
                    'sanitize_callback' => 'sanitize_text_field',
                ),
            ),
        ) );
    } );

    perform dept_cat_api( $request ) {
        $args = array(
            'post_type' => 'put up',
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'crosspost',
                    'discipline'    => 'slug',
                    'phrases'    => array( $request['dept'] ),
                ),
                array(
                    'taxonomy' => 'class',
                    'discipline'    => 'slug',
                    'phrases'    => array( $request['cat'] ),
                ),
            ),
            'per_page' => $request['per_page'],
            'web page' => $request['page'],
            'orderby' => $request['orderby'],
            'order' => $request['order']
        );

        $posts = get_posts( $args );

        if ( empty( $posts ) ) return new WP_Error( 'no_posts', 'Invalid time period(s)', array( 'standing' => 404 ) );

        $controller = new WP_REST_Posts_Controller('put up');

        foreach ( $posts as $put up ) {
            $response = $controller->prepare_item_for_response( $put up, $request );
            $knowledge[] = $controller->prepare_response_for_collection( $response );  
        }

// return outcomes
        return new WP_REST_Response($knowledge, 200);

    }

Remotely I am having an issue. After I name the route all the pieces appears superb with the content material, nevertheless each X-WP-TotalPages and X-WP-Whole return empty responses.

    object(Requests_Utility_CaseInsensitiveDictionary)#991 (1) {
      ["data":protected]=>
      array(20) {
        ["server"]=>
        string(5) "nginx"
        ["date"]=>
        string(29) "Wed, 14 Jun 2017 14:07:51 GMT"
        ["content-type"]=>
        string(31) "utility/json; charset=UTF-8"
        ["content-length"]=>
        string(6) "104787"
        ["expires"]=>
        string(29) "Thu, 19 Nov 1981 08:52:00 GMT"
        ["pragma"]=>
        string(8) "no-cache"
        ["x-robots-tag"]=>
        string(7) "noindex"
        ["link"]=>
        string(65) "; rel="https://api.w.org/""
        ["x-content-type-options"]=>
        string(7) "nosniff"
        ["access-control-expose-headers"]=>
        string(27) "X-WP-Whole, X-WP-TotalPages"
        ["access-control-allow-headers"]=>
        string(27) "Authorization, Content material-Sort"
        ["allow"]=>
        string(3) "GET"
        ["x-cacheable"]=>
        string(5) "SHORT"
        ["vary"]=>
        string(22) "Settle for-Encoding,Cookie"
        ["cache-control"]=>
        string(28) "max-age=600, must-revalidate"
        ["accept-ranges"]=>
        string(5) "bytes"
        ["x-cache"]=>
        string(6) "HIT: 1"
        ["x-pass-why"]=>
        string(0) ""
        ["x-cache-group"]=>
        string(6) "regular"
        ["x-type"]=>
        string(7) "default"
      }
    }

Ideally I want each populated, however I may make do with one. I assumed WordPress calculated/populated these by way of the controller. Do I’ve to populate them manually? Have I carried out one thing incorrectly? Am I experiencing a bug?