Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
WP_REST_Content_Research_Summarize
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 3
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 register_rest_route
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
2
 get_summary
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * WP_REST_Content_Research_Summarize file.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8namespace A8C\FSE;
9
10use Automattic\Jetpack\Connection\Client;
11
12/**
13 * Class WP_REST_Content_Research_Summarize.
14 *
15 * Proxies summarize requests to the WPcom platform endpoint.
16 */
17class WP_REST_Content_Research_Summarize extends \WP_REST_Controller {
18
19    /**
20     * WP_REST_Content_Research_Summarize constructor.
21     */
22    public function __construct() {
23        $this->namespace = 'content-research';
24        $this->rest_base = '/summarize';
25    }
26
27    /**
28     * Register available routes.
29     */
30    public function register_rest_route() {
31        register_rest_route(
32            $this->namespace,
33            $this->rest_base,
34            array(
35                'methods'             => \WP_REST_Server::CREATABLE,
36                'callback'            => array( $this, 'get_summary' ),
37                'permission_callback' => 'is_user_logged_in',
38                'args'                => array(
39                    'topic'   => array(
40                        'type'              => 'string',
41                        'required'          => true,
42                        'sanitize_callback' => 'sanitize_text_field',
43                    ),
44                    'results' => array(
45                        'type'     => 'array',
46                        'required' => true,
47                        'items'    => array(
48                            'type' => 'object',
49                        ),
50                    ),
51                ),
52            )
53        );
54    }
55
56    /**
57     * Proxy the summarize request to the WPcom platform endpoint.
58     *
59     * @param \WP_REST_Request $request The incoming request.
60     * @return \WP_REST_Response|\WP_Error
61     */
62    public function get_summary( \WP_REST_Request $request ) {
63        $body = Client::wpcom_json_api_request_as_user(
64            '/content-research/summarize',
65            '2',
66            array(
67                'method' => 'POST',
68            ),
69            array(
70                'topic'   => $request['topic'],
71                'results' => $request['results'],
72            )
73        );
74
75        if ( is_wp_error( $body ) ) {
76            return $body;
77        }
78
79        $response = json_decode( wp_remote_retrieve_body( $body ) );
80
81        return rest_ensure_response( $response );
82    }
83}