Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.67% covered (warning)
66.67%
44 / 66
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
WP_REST_Help_Center_Forum
66.67% covered (warning)
66.67%
44 / 66
66.67% covered (warning)
66.67%
2 / 3
5.93
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 register_rest_route
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
1 / 1
1
 submit_new_topic
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * WP_REST_Help_Center_Forum file.
4 *
5 * @package automattic/jetpack-help-center
6 */
7
8namespace Automattic\Jetpack\Help_Center;
9
10/**
11 * Class WP_REST_Help_Center_Forum.
12 */
13class WP_REST_Help_Center_Forum extends WP_REST_Help_Center_Controller {
14    /**
15     * WP_REST_Help_Center_Forum constructor.
16     *
17     * @param Wpcom_Request_Client|null $wpcom_request_client WP.com request client.
18     */
19    public function __construct( ?Wpcom_Request_Client $wpcom_request_client = null ) {
20        parent::__construct( $wpcom_request_client );
21        $this->namespace = 'help-center';
22        $this->rest_base = '/forum';
23    }
24
25    /**
26     * Register available routes.
27     */
28    public function register_rest_route() {
29        register_rest_route(
30            $this->namespace,
31            $this->rest_base . '/new',
32            array(
33                'methods'             => \WP_REST_Server::CREATABLE,
34                'callback'            => array( $this, 'submit_new_topic' ),
35                'permission_callback' => '__return_true',
36                'args'                => array(
37                    'subject'                => array(
38                        'type'     => 'string',
39                        'required' => true,
40                    ),
41                    'message'                => array(
42                        'type'     => 'string',
43                        'required' => true,
44                    ),
45                    'blog_url'               => array(
46                        'type'     => 'string',
47                        'required' => true,
48                    ),
49                    'blog_id'                => array(
50                        'type'    => 'integer',
51                        'default' => null,
52                    ),
53                    'tags'                   => array(
54                        'type'    => 'array',
55                        'default' => array(),
56                    ),
57                    'locale'                 => array(
58                        'type' => 'string',
59                    ),
60                    'hide_blog_info'         => array(
61                        'type'    => 'boolean',
62                        'default' => false,
63                    ),
64                    'should_use_test_forums' => array(
65                        'type' => 'boolean',
66                    ),
67                ),
68            )
69        );
70    }
71
72    /**
73     * Should create a new forum topic
74     *
75     * @param \WP_REST_Request $request    The request sent to the API.
76     */
77    public function submit_new_topic( \WP_REST_Request $request ) {
78        $topic_args = array(
79            'subject'                => $request['subject'],
80            'message'                => $request['message'],
81            'blog_url'               => $request['blog_url'],
82            'blog_id'                => $request['blog_id'],
83            'tags'                   => $request['tags'],
84            'locale'                 => $request['locale'],
85            'hide_blog_info'         => $request['hide_blog_info'],
86            'should_use_test_forums' => isset( $request['should_use_test_forums'] ) && $request['should_use_test_forums'],
87        );
88
89        $body = $this->wpcom_request_client->request(
90            '/help/forum/new',
91            '2',
92            array(
93                'method' => 'POST',
94            ),
95            $topic_args
96        );
97
98        if ( is_wp_error( $body ) ) {
99            return $body;
100        }
101
102        $response = json_decode( wp_remote_retrieve_body( $body ) );
103
104        return rest_ensure_response( $response );
105    }
106}