Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Controller
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 permission_check
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * What every Sharing & Likes REST controller shares.
4 *
5 * @package automattic/jetpack-sharing-likes
6 */
7
8declare( strict_types = 1 );
9
10namespace Automattic\Jetpack\Sharing_Likes\REST;
11
12use Automattic\Jetpack\Sharing_Likes\Settings\Environment;
13use WP_Error;
14use WP_REST_Controller;
15
16/**
17 * Namespace and authorization for the routes behind Settings > Sharing.
18 */
19abstract class Controller extends WP_REST_Controller {
20
21    /**
22     * Set the namespace every route shares.
23     */
24    public function __construct() {
25        $this->namespace = Endpoints::REST_NAMESPACE;
26    }
27
28    /**
29     * Site administrators only, and only where the screen exists.
30     *
31     * @return true|WP_Error
32     */
33    public function permission_check() {
34        if ( ! current_user_can( 'manage_options' ) ) {
35            return new WP_Error(
36                'rest_forbidden',
37                __( 'Sorry, you are not allowed to manage sharing settings on this site.', 'jetpack-sharing-likes' ),
38                array( 'status' => rest_authorization_required_code() )
39            );
40        }
41
42        // Per request, not at registration: see `Endpoints::init()`.
43        if ( ! Environment::settings_screen_supported() ) {
44            return new WP_Error(
45                'rest_sharing_likes_unavailable',
46                __( 'Sharing settings are not available on this site until it is connected to WordPress.com.', 'jetpack-sharing-likes' ),
47                array( 'status' => 409 )
48            );
49        }
50
51        return true;
52    }
53}