Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Route | |
0.00% |
0 / 21 |
|
0.00% |
0 / 3 |
110 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| register_rest_route | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| verify_permissions | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
72 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack_Inspect\REST_API; |
| 4 | |
| 5 | class Route { |
| 6 | |
| 7 | /** |
| 8 | * @var \Automattic\Jetpack_Inspect\REST_API\Contracts\Endpoint |
| 9 | */ |
| 10 | protected $endpoint; |
| 11 | |
| 12 | protected $permissions; |
| 13 | |
| 14 | public function __construct( $endpoint ) { |
| 15 | $this->endpoint = new $endpoint(); |
| 16 | $this->permissions = $this->endpoint->permissions(); |
| 17 | } |
| 18 | |
| 19 | public function register_rest_route() { |
| 20 | register_rest_route( |
| 21 | 'jetpack-inspect', |
| 22 | $this->endpoint->name(), |
| 23 | array( |
| 24 | 'methods' => $this->endpoint->request_methods(), |
| 25 | 'callback' => array( $this->endpoint, 'response' ), |
| 26 | 'permission_callback' => array( $this, 'verify_permissions' ), |
| 27 | ) |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * This method is going to run and try to verify that |
| 33 | * all the permission callbacks are successful. |
| 34 | * |
| 35 | * If any of them fail - return false immediately. |
| 36 | * |
| 37 | * @param \WP_REST_Request $request |
| 38 | * |
| 39 | * @return bool |
| 40 | */ |
| 41 | public function verify_permissions( $request ) { |
| 42 | |
| 43 | if ( |
| 44 | defined( 'WP_ENVIRONMENT_TYPE' ) |
| 45 | && 'development' === WP_ENVIRONMENT_TYPE |
| 46 | && defined( 'JETPACK_INSPECT_DEBUG' ) |
| 47 | && JETPACK_INSPECT_DEBUG |
| 48 | ) { |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | if ( is_bool( $this->permissions ) ) { |
| 53 | return $this->permissions; |
| 54 | } |
| 55 | |
| 56 | foreach ( $this->permissions as $permission ) { |
| 57 | |
| 58 | if ( true !== $permission->verify( $request ) ) { |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | } |