Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.68% covered (success)
98.68%
75 / 76
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
Knowledge_Scopes_REST_Controller
100.00% covered (success)
100.00%
74 / 74
100.00% covered (success)
100.00%
6 / 6
14
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 register_routes
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 get_items_permissions_check
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 get_items
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 prepare_item_for_response
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
6
 get_item_schema
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Guideline Scopes REST API Controller.
4 *
5 * @package automattic/jetpack-knowledge
6 */
7
8namespace Automattic\Jetpack\Knowledge;
9
10use WP_Error;
11use WP_REST_Controller;
12use WP_REST_Request;
13use WP_REST_Response;
14use WP_REST_Server;
15
16if ( ! defined( 'ABSPATH' ) ) {
17    exit;
18}
19
20/**
21 * REST controller for the guideline scopes registry.
22 */
23class Knowledge_Scopes_REST_Controller extends WP_REST_Controller {
24
25    /**
26     * Constructor.
27     */
28    public function __construct() {
29        $this->namespace = 'wp/v2';
30        $this->rest_base = 'knowledge/guideline-scopes';
31    }
32
33    /**
34     * Registers the routes for the controller.
35     */
36    public function register_routes() {
37        register_rest_route(
38            $this->namespace,
39            '/' . $this->rest_base,
40            array(
41                0        => array(
42                    'methods'             => WP_REST_Server::READABLE,
43                    'callback'            => array( $this, 'get_items' ),
44                    'permission_callback' => array( $this, 'get_items_permissions_check' ),
45                ),
46                'schema' => array( $this, 'get_public_item_schema' ),
47            )
48        );
49    }
50
51    /**
52     * Checks whether the current user can read guideline scopes.
53     *
54     * Gated on the knowledge read capability, matching the data routes.
55     *
56     * @param WP_REST_Request $request Full details about the request.
57     * @return true|WP_Error True if the request has read access, WP_Error otherwise.
58     */
59    public function get_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
60        if ( ! current_user_can( 'read_knowledge_items' ) ) {
61            return new WP_Error(
62                'rest_cannot_read',
63                __( 'Sorry, you are not allowed to view guideline scopes.', 'jetpack-knowledge' ),
64                array( 'status' => rest_authorization_required_code() )
65            );
66        }
67
68        return true;
69    }
70
71    /**
72     * Retrieves all registered guideline scopes.
73     *
74     * Labels are resolved at request time (in the request locale).
75     *
76     * @param WP_REST_Request $request Full details about the request.
77     * @return WP_REST_Response Response object.
78     */
79    public function get_items( $request ) {
80        $data = array();
81
82        foreach ( wp_guideline_scopes() as $slug => $scope ) {
83            $item   = $this->prepare_item_for_response( array_merge( array( 'slug' => $slug ), $scope ), $request );
84            $data[] = $this->prepare_response_for_collection( $item );
85        }
86
87        return rest_ensure_response( $data );
88    }
89
90    /**
91     * Prepares a single scope for response.
92     *
93     * @param array           $item    Scope data with a `slug` key.
94     * @param WP_REST_Request $request Request object.
95     * @return WP_REST_Response Response object.
96     */
97    public function prepare_item_for_response( $item, $request ) {
98        $fields = $this->get_fields_for_response( $request );
99        $data   = array();
100
101        if ( rest_is_field_included( 'slug', $fields ) ) {
102            $data['slug'] = $item['slug'];
103        }
104        if ( rest_is_field_included( 'title', $fields ) ) {
105            $data['title'] = $item['title'] ?? '';
106        }
107        if ( rest_is_field_included( 'description', $fields ) ) {
108            $data['description'] = $item['description'] ?? '';
109        }
110        if ( rest_is_field_included( 'order', $fields ) ) {
111            $data['order'] = (int) ( $item['order'] ?? 0 );
112        }
113
114        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
115        $data    = $this->add_additional_fields_to_object( $data, $request );
116        $data    = $this->filter_response_by_context( $data, $context );
117
118        return rest_ensure_response( $data );
119    }
120
121    /**
122     * Retrieves the scope schema, conforming to JSON Schema.
123     *
124     * @return array Item schema data.
125     */
126    public function get_item_schema() {
127        if ( $this->schema ) {
128            return $this->add_additional_fields_schema( $this->schema );
129        }
130
131        $this->schema = array(
132            '$schema'    => 'http://json-schema.org/draft-04/schema#',
133            'title'      => 'guideline-scope',
134            'type'       => 'object',
135            'properties' => array(
136                'slug'        => array(
137                    'description' => __( 'A unique identifier for the scope.', 'jetpack-knowledge' ),
138                    'type'        => 'string',
139                    'context'     => array( 'view', 'edit', 'embed' ),
140                    'readonly'    => true,
141                ),
142                'title'       => array(
143                    'description' => __( 'The title for the scope.', 'jetpack-knowledge' ),
144                    'type'        => 'string',
145                    'context'     => array( 'view', 'edit', 'embed' ),
146                    'readonly'    => true,
147                ),
148                'description' => array(
149                    'description' => __( 'A human-readable description of the scope.', 'jetpack-knowledge' ),
150                    'type'        => 'string',
151                    'context'     => array( 'view', 'edit', 'embed' ),
152                    'readonly'    => true,
153                ),
154                'order'       => array(
155                    'description' => __( 'The sort order of the scope on the Settings page.', 'jetpack-knowledge' ),
156                    'type'        => 'integer',
157                    'context'     => array( 'view', 'edit', 'embed' ),
158                    'readonly'    => true,
159                ),
160            ),
161        );
162
163        return $this->add_additional_fields_schema( $this->schema );
164    }
165}