Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
79.25% covered (warning)
79.25%
42 / 53
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
84.00% covered (warning)
84.00%
42 / 50
50.00% covered (danger)
50.00%
2 / 4
6.15
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%
33 / 33
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        register_rest_route(
54            $this->namespace,
55            $this->rest_base . '/post-previews',
56            array(
57                array(
58                    'methods'             => WP_REST_Server::READABLE,
59                    'callback'            => array( $this, 'get_abilities' ),
60                    'permission_callback' => array( $this, 'get_item_permissions_check' ),
61                ),
62            )
63        );
64    }
65
66    /**
67     * Checks if a given request has access to application password extras.
68     *
69     * @param WP_REST_Request $request Full details about the request.
70     * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
71     */
72    public function get_item_permissions_check( $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
73        if ( ! is_user_logged_in() ) {
74            return new WP_Error(
75                'rest_forbidden',
76                __( 'Sorry, you must be logged in to access this endpoint.', 'jetpack' ),
77                array( 'status' => rest_authorization_required_code() )
78            );
79        }
80
81        return true;
82    }
83
84    /**
85     * Retrieves the application password extras abilities.
86     *
87     * @param WP_REST_Request $request Full details about the request.
88     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
89     */
90    public function get_abilities( $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
91        if ( ! class_exists( 'Jetpack_Application_Password_Extras' ) ) {
92            return new WP_Error(
93                'rest_application_password_extras_unavailable',
94                __( 'Application password extras functionality is not available.', 'jetpack' ),
95                array( 'status' => 503 )
96            );
97        }
98
99        return rest_ensure_response( Jetpack_Application_Password_Extras::get_abilities() );
100    }
101}
102
103wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Application_Password_Extras' );