Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 76
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
REST_Settings_Controller
0.00% covered (danger)
0.00%
0 / 76
0.00% covered (danger)
0.00%
0 / 7
380
0.00% covered (danger)
0.00%
0 / 1
 register_rest_routes
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
6
 require_admin_privilege_callback
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 require_publish_posts_permission_callback
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 get_item
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 update_item
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
30
 prepare_item_for_response
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
 get_item_schema
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * The Social Rest Controller class.
4 * Registers the REST routes for Social.
5 *
6 * @package automattic/jetpack-social-plugin
7 */
8
9namespace Automattic\Jetpack\Social;
10
11use Automattic\Jetpack\Modules;
12use Automattic\Jetpack\Publicize\Publicize_Utils;
13use Jetpack_Social;
14use WP_Error;
15use WP_REST_Controller;
16use WP_REST_Request;
17use WP_REST_Server;
18
19/**
20 * Registers the REST routes for Social.
21 */
22class REST_Settings_Controller extends WP_REST_Controller {
23    /**
24     * Registers the REST routes for Social.
25     *
26     * @access public
27     * @static
28     */
29    public function register_rest_routes() {
30
31        if ( Publicize_Utils::should_use_jetpack_module_endpoint() ) {
32            return;
33        }
34
35        // If the site has an older version of Jetpack we still need to register the route.
36        register_rest_route(
37            'jetpack/v4',
38            '/social/settings',
39            array(
40                array(
41                    'methods'             => WP_REST_Server::READABLE,
42                    'callback'            => array( $this, 'get_item' ),
43                    'permission_callback' => array( $this, 'require_admin_privilege_callback' ),
44                    'args'                => $this->get_endpoint_args_for_item_schema(),
45                ),
46                array(
47                    'methods'             => WP_REST_Server::EDITABLE,
48                    'callback'            => array( $this, 'update_item' ),
49                    'permission_callback' => array( $this, 'require_admin_privilege_callback' ),
50                    'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
51                ),
52            )
53        );
54    }
55
56    /**
57     * Only administrators can access the API.
58     *
59     * @return bool|WP_Error True if a blog token was used to sign the request, WP_Error otherwise.
60     */
61    public function require_admin_privilege_callback() {
62        if ( current_user_can( 'manage_options' ) ) {
63            return true;
64        }
65
66        return new WP_Error(
67            'rest_forbidden',
68            esc_html__( 'You are not allowed to perform this action.', 'jetpack-social' ),
69            array( 'status' => rest_authorization_required_code() )
70        );
71    }
72
73    /**
74     * Check to see if a user is able to publish posts
75     *
76     * @return bool|WP_Error
77     */
78    public function require_publish_posts_permission_callback() {
79        if ( current_user_can( 'publish_posts' ) ) {
80            return true;
81        }
82
83        return new WP_Error(
84            'rest_forbidden',
85            esc_html__( 'You are not allowed to perform this action.', 'jetpack-social' ),
86            array( 'status' => rest_authorization_required_code() )
87        );
88    }
89
90    /**
91     * Updates the settings.
92     *
93     * @param WP_REST_Request $request Full details about the request.
94     * @return array|WP_Error Array on success, or error object on failure.
95     */
96    public function get_item( $request ) {
97        $fields = $this->get_fields_for_response( $request );
98        $data   = array();
99
100        if ( rest_is_field_included( 'publicize', $fields ) ) {
101            $data['publicize'] = Jetpack_Social::is_publicize_active();
102        }
103
104        return $this->prepare_item_for_response( $data, $request );
105    }
106
107    /**
108     * POST `jetpack/v4/social/settings`
109     *
110     * @param WP_REST_Request $request - REST request.
111     */
112    public function update_item( $request ) {
113        $params   = $request->get_params();
114        $settings = $this->get_endpoint_args_for_item_schema( $request->get_method() );
115
116        foreach ( array_keys( $settings ) as $name ) {
117            if ( ! array_key_exists( $name, $params ) ) {
118                continue;
119            }
120
121            switch ( $name ) {
122                case 'publicize':
123                    $updated = ( new Modules() )->update_status( \Jetpack_Social::JETPACK_PUBLICIZE_MODULE_SLUG, (bool) $params[ $name ], false, false );
124                    if ( is_wp_error( $updated ) ) {
125                        return $updated;
126                    }
127                    break;
128            }
129        }
130
131        return $this->get_item( $request );
132    }
133
134    /**
135     * Prepares the settings data to return from the endpoint.
136     * Includes checking the values against the schema.
137     *
138     * @param array           $settings  The settings data to prepare.
139     * @param WP_REST_Request $request   REST request.
140     * @return array|WP_Error The prepared settings or a WP_Error on failure.
141     */
142    public function prepare_item_for_response( $settings, $request ) {
143        $args   = $this->get_endpoint_args_for_item_schema( $request->get_method() );
144        $return = array();
145        foreach ( $settings as $name => $value ) {
146            if ( empty( $args[ $name ] ) ) {
147                // This setting shouldn't be returned.
148                continue;
149            }
150            $is_valid = rest_validate_value_from_schema( $value, $args[ $name ], $name );
151            if ( is_wp_error( $is_valid ) ) {
152                return $is_valid;
153            }
154            $sanitized = rest_sanitize_value_from_schema( $value, $args[ $name ] );
155            if ( is_wp_error( $sanitized ) ) {
156                return $sanitized;
157            }
158            $return[ $name ] = $sanitized;
159        }
160        return rest_ensure_response( $return );
161    }
162
163    /**
164     * Get the settings schema, conforming to JSON Schema.
165     *
166     * @return array
167     */
168    public function get_item_schema() {
169        $schema = array(
170            '$schema'    => 'http://json-schema.org/draft-04/schema#',
171            'title'      => 'system_status',
172            'type'       => 'object',
173            'properties' => array(
174                'publicize' => array(
175                    'description' => __( 'Is the publicize module enabled?', 'jetpack-social' ),
176                    'type'        => 'boolean',
177                    'context'     => array( 'view', 'edit' ),
178                ),
179            ),
180        );
181        return $this->add_additional_fields_schema( $schema );
182    }
183}