Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
73.81% covered (warning)
73.81%
31 / 42
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_REST_API_V2_Endpoint_Application_Password_Extras
79.49% covered (warning)
79.49%
31 / 39
50.00% covered (danger)
50.00%
2 / 4
6.31
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 register_routes
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
1
 get_item_permissions_check
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 get_abilities
28.57% covered (danger)
28.57%
2 / 7
0.00% covered (danger)
0.00%
0 / 1
3.46
1<?php
2/**
3 * REST API endpoint for application password extras abilities.
4 *
5 * @package automattic/jetpack
6 */
7
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12/**
13 * Class WPCOM_REST_API_V2_Endpoint_Application_Password_Extras
14 */
15class WPCOM_REST_API_V2_Endpoint_Application_Password_Extras extends WP_REST_Controller {
16    /**
17     * Constructor.
18     */
19    public function __construct() {
20        $this->namespace = 'wpcom/v2';
21        $this->rest_base = 'application-password-extras';
22        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
23    }
24
25    /**
26     * Register routes.
27     */
28    public function register_routes() {
29        register_rest_route(
30            $this->namespace,
31            $this->rest_base . '/abilities',
32            array(
33                array(
34                    'methods'             => WP_REST_Server::READABLE,
35                    'callback'            => array( $this, 'get_abilities' ),
36                    'permission_callback' => array( $this, 'get_item_permissions_check' ),
37                ),
38            )
39        );
40
41        register_rest_route(
42            $this->namespace,
43            $this->rest_base . '/admin-ajax',
44            array(
45                array(
46                    'methods'             => WP_REST_Server::READABLE,
47                    'callback'            => array( $this, 'get_abilities' ),
48                    'permission_callback' => array( $this, 'get_item_permissions_check' ),
49                ),
50            )
51        );
52    }
53
54    /**
55     * Checks if a given request has access to application password extras.
56     *
57     * @param WP_REST_Request $request Full details about the request.
58     * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
59     */
60    public function get_item_permissions_check( $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
61        if ( ! is_user_logged_in() ) {
62            return new WP_Error(
63                'rest_forbidden',
64                __( 'Sorry, you must be logged in to access this endpoint.', 'jetpack' ),
65                array( 'status' => rest_authorization_required_code() )
66            );
67        }
68
69        return true;
70    }
71
72    /**
73     * Retrieves the application password extras abilities.
74     *
75     * @param WP_REST_Request $request Full details about the request.
76     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
77     */
78    public function get_abilities( $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
79        if ( ! class_exists( 'Jetpack_Application_Password_Extras' ) ) {
80            return new WP_Error(
81                'rest_application_password_extras_unavailable',
82                __( 'Application password extras functionality is not available.', 'jetpack' ),
83                array( 'status' => 503 )
84            );
85        }
86
87        return rest_ensure_response( Jetpack_Application_Password_Extras::get_abilities() );
88    }
89}
90
91wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Application_Password_Extras' );