Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
17.50% covered (danger)
17.50%
7 / 40
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
CapabilityController
17.50% covered (danger)
17.50%
7 / 40
25.00% covered (danger)
25.00%
1 / 4
26.21
0.00% covered (danger)
0.00%
0 / 1
 register_routes
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
2
 get_capability
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 get_brand
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 check_permission
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace NewfoldLabs\WP\Module\HelpCenter;
4
5use NewfoldLabs\WP\Module\Data\SiteCapabilities;
6
7/**
8 * APIs for cehcking if we have the capability to render Help Center
9 */
10class CapabilityController 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 = 'capability';
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::READABLE,
35                    'callback'            => array( $this, 'get_capability' ),
36                    'permission_callback' => array( $this, 'check_permission' ),
37                ),
38            )
39        );
40
41        register_rest_route(
42            $this->namespace,
43            '/' . $this->rest_base . '/brand',
44            array(
45                array(
46                    'methods'             => \WP_REST_Server::READABLE,
47                    'callback'            => array( $this, 'get_brand' ),
48                    'permission_callback' => array( $this, 'check_permission' ),
49                ),
50            )
51        );
52    }
53
54    /**
55     * Get the capability from module data
56     *
57     * @return \WP_REST_Response|\WP_Error
58     */
59    public function get_capability() {
60        $capability   = new SiteCapabilities();
61        $help_enabled = $capability->get( 'canAccessHelpCenter' );
62
63        return new \WP_REST_Response( $help_enabled, 200 );
64    }
65
66    /**
67     * Get the current brand from module data
68     *
69     * @return \WP_REST_Response|\WP_Error
70     */
71    public function get_brand() {
72        $brand = NFD_HELPCENTER_PLUGIN_BRAND;
73        if ( ! $brand ) {
74            return new \WP_Error(
75                'rest_not_found',
76                __( 'We could not find the brand', 'wp-module-help-center' ),
77                array( 'status' => 401 )
78            );
79        }
80
81        return new \WP_REST_Response( $brand, 200 );
82    }
83
84    /**
85     * Check permissions for routes.
86     *
87     * @return \WP_Error|boolean
88     */
89    public function check_permission() {
90        if ( ! current_user_can( 'read' ) ) {
91            return new \WP_Error(
92                'rest_forbidden',
93                __( 'You must be authenticated to make this call', 'wp-module-help-center' ),
94                array( 'status' => 401 )
95            );
96        }
97        return true;
98    }
99}