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