Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserInteractionController
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 register_routes
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
2
 post_feedback
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 check_permission
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace NewfoldLabs\WP\Module\HelpCenter;
4
5use NewfoldLabs\WP\Module\HelpCenter\Util;
6
7/**
8 * APIs for getting the result from the AI service
9 */
10class UserInteractionController extends \WP_REST_Controller {
11    /**
12     * The namespace of this controller's route.
13     *
14     * @var string
15     */
16    protected $namespace = 'nfd-help-center/v1';
17
18    /**
19     * The base of this controller's route
20     *
21     * @var string
22     */
23    protected $rest_base = 'feedback';
24
25    /**
26     * Register the routes for this objects of the controller
27     */
28    public function register_routes() {
29        register_rest_route(
30            $this->namespace,
31            '/' . $this->rest_base,
32            array(
33                array(
34                    'methods'             => \WP_REST_Server::CREATABLE,
35                    'callback'            => array( $this, 'post_feedback' ),
36                    'args'                => array(
37                        'post_id' => array(
38                            'required' => true,
39                            'type'     => 'integer',
40                        ),
41                        'status'  => array(
42                            'required' => true,
43                            'type'     => 'string',
44                        ),
45                    ),
46                    'permission_callback' => array( $this, 'check_permission' ),
47                ),
48            )
49        );
50    }
51
52    /**
53     * Proxy to the AI service to get the responses.
54     *
55     * @param \WP_REST_Request $request Request object
56     *
57     * @return \WP_REST_Response|\WP_Error
58     */
59    public function post_feedback( \WP_REST_Request $request ) {
60        $post_id = $request['post_id'];
61        $status  = $request['status'];
62
63        $response = Util::post_feedback( $post_id, $status );
64
65        if ( ! $response ) {
66            return new \WP_Error( 'ServerError', $response['error'] );
67        }
68
69        return new \WP_REST_Response( true, 200 );
70    }
71
72    /**
73     * Check permissions for routes.
74     *
75     * @return \WP_Error|boolean
76     */
77    public function check_permission() {
78        if ( ! current_user_can( 'read' ) ) {
79            return new \WP_Error(
80                'rest_forbidden',
81                __( 'You must be authenticated to make this call', 'wp-module-help-center' ),
82                array( 'status' => 401 )
83            );
84        }
85        return true;
86    }
87}