Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 94
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
FeaturesAPI
0.00% covered (danger)
0.00%
0 / 94
0.00% covered (danger)
0.00%
0 / 8
110
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_routes
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 1
2
 validateFeatureParam
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 checkPermission
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 features
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 featureEnable
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 featureDisable
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 featureIsEnabled
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace NewfoldLabs\WP\Module\Features;
4
5use WP_REST_Controller;
6use WP_REST_Request;
7use WP_REST_Response;
8use WP_REST_Server;
9use WP_Error;
10use function NewfoldLabs\WP\Module\Features\enable;
11use function NewfoldLabs\WP\Module\Features\disable;
12use function NewfoldLabs\WP\Module\Features\isEnabled;
13
14/**
15 * Class FeaturesAPI
16 */
17class FeaturesAPI extends WP_REST_Controller {
18
19    /**
20     * The namespace of this controller's route.
21     *
22     * @var string
23     */
24    protected $namespace = 'newfold-features/v1';
25
26    /**
27     * An instance of the Features class.
28     *
29     * @var Features
30     */
31    protected $features;
32
33    /**
34     * FeaturesApi Controller constructor.
35     */
36    public function __construct() {
37        $this->features = Features::getInstance();
38        $this->register_routes();
39    }
40
41    /**
42     * Register API Routes
43     */
44    public function register_routes() {
45
46        // Get all features endpoint
47        register_rest_route(
48            $this->namespace,
49            '/features',
50            array(
51                'methods'             => WP_REST_Server::READABLE,
52                'callback'            => array( $this, 'features' ),
53                'permission_callback' => array( $this, 'checkPermission' ),
54            )
55        );
56
57        // Register feature enable endpoint
58        register_rest_route(
59            $this->namespace,
60            '/feature/enable',
61            array(
62                'methods'             => WP_REST_Server::EDITABLE,
63                'callback'            => array( $this, 'featureEnable' ),
64                'permission_callback' => array( $this, 'checkPermission' ),
65                'args'                => array(
66                    'feature' => array(
67                        'required'          => true,
68                        'validate_callback' => array( $this, 'validateFeatureParam' ),
69                    ),
70                ),
71            )
72        );
73
74        // Register feature disable endpoint
75        register_rest_route(
76            $this->namespace,
77            '/feature/disable',
78            array(
79                'methods'             => WP_REST_Server::EDITABLE,
80                'callback'            => array( $this, 'featureDisable' ),
81                'permission_callback' => array( $this, 'checkPermission' ),
82                'args'                => array(
83                    'feature' => array(
84                        'required'          => true,
85                        'validate_callback' => array( $this, 'validateFeatureParam' ),
86                    ),
87                ),
88            )
89        );
90
91        // Register feature is enabled check endpoint
92        register_rest_route(
93            $this->namespace,
94            '/feature/isEnabled',
95            array(
96                'methods'             => WP_REST_Server::READABLE,
97                'callback'            => array( $this, 'featureIsEnabled' ),
98                'permission_callback' => array( $this, 'checkPermission' ),
99                'args'                => array(
100                    'feature' => array(
101                        'required'          => true,
102                        'validate_callback' => array( $this, 'validateFeatureParam' ),
103                    ),
104                ),
105            )
106        );
107    }
108
109    /**
110     * Callback to validate feature exists
111     *
112     * @param string          $param the parameter
113     * @param WP_REST_Request $request the request
114     * @param string          $key the key
115     * @return bool
116     */
117    public function validateFeatureParam( $param, $request, $key ) {
118        return $this->features->hasFeature( $param );
119    }
120
121    /**
122     * Check permissions for routes.
123     * Always returns true since permissions are managed in the specific Feature classes
124     *
125     * @return bool
126     */
127    public function checkPermission() {
128        return true;
129    }
130
131    /**
132     * Get features via REST API.
133     *
134     * @param WP_REST_Request $request The request object.
135     * @return WP_REST_Response The response object.
136     */
137    public function features( WP_REST_Request $request ) {
138        return new WP_REST_Response(
139            array(
140                'features' => $this->features->getFeatures(),
141            ),
142            200
143        );
144    }
145
146    /**
147     * Callback to enable a feature via REST API.
148     *
149     * @param WP_REST_Request $request The request object.
150     * @return WP_REST_Response|WP_Error The response object or WP_Error on failure.
151     */
152    public function featureEnable( WP_REST_Request $request ) {
153        $name   = $request->get_param( 'feature' );
154        $result = enable( $name );
155
156        // success
157        if ( $result ) {
158            return new WP_REST_Response(
159                isEnabled( $name ), // verifying enable was successful since actions could override
160                200
161            );
162        }
163
164        // else other error, typically permissions
165        return new WP_Error(
166            'nfd_features_error',
167            __( 'Cannot modify this feature.', 'wp-module-features' ),
168            array( 'status' => 403 )
169        );
170    }
171
172    /**
173     * Callback to disable a feature via REST API.
174     *
175     * @param WP_REST_Request $request The request object.
176     * @return WP_REST_Response|WP_Error The response object or WP_Error on failure.
177     */
178    public function featureDisable( WP_REST_Request $request ) {
179        $name   = $request->get_param( 'feature' );
180        $result = disable( $name );
181
182        // success
183        if ( $result ) {
184            return new WP_REST_Response(
185                ! isEnabled( $name ), // verifying enable was successful since actions could override
186                200
187            );
188        }
189
190        // else other error, typically permissions
191        return new WP_Error(
192            'nfd_features_error',
193            __( 'Cannot modify this feature.', 'wp-module-features' ),
194            array( 'status' => 403 )
195        );
196    }
197
198    /**
199     * Callback to check if a feature is enabled via REST API.
200     *
201     * @param WP_REST_Request $request The request object.
202     * @return WP_REST_Response The response object.
203     */
204    public function featureIsEnabled( WP_REST_Request $request ) {
205        $name   = $request['feature'];
206        $result = isEnabled( $name );
207
208        return new WP_REST_Response(
209            $result,
210            200
211        );
212    }
213}