Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Message_Templates_Placeholders_Controller
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 4
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 register_routes
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
2
 permissions_check
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_placeholders
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Publicize: Message Templates Placeholders Controller
4 *
5 * @package automattic/jetpack-publicize
6 */
7
8namespace Automattic\Jetpack\Publicize\REST_API;
9
10use Automattic\Jetpack\Publicize\Message_Templates_Placeholders;
11use WP_REST_Server;
12
13if ( ! defined( 'ABSPATH' ) ) {
14    exit( 0 );
15}
16
17/**
18 * Publicize: Message Templates Placeholders Controller class.
19 *
20 * @phan-constructor-used-for-side-effects
21 */
22class Message_Templates_Placeholders_Controller extends Base_Controller {
23
24    /**
25     * The constructor sets the route namespace, rest_base, and registers our API route and endpoint.
26     */
27    public function __construct() {
28        parent::__construct();
29
30        $this->namespace = 'wpcom/v2';
31        $this->rest_base = 'publicize/message-templates/placeholders';
32
33        $this->allow_requests_as_blog = true;
34
35        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
36    }
37
38    /**
39     * Register the routes.
40     */
41    public function register_routes() {
42        register_rest_route(
43            $this->namespace,
44            '/' . $this->rest_base,
45            array(
46                'methods'                        => WP_REST_Server::READABLE,
47                'callback'                       => array( $this, 'get_placeholders' ),
48                'permission_callback'            => array( $this, 'permissions_check' ),
49                'private_site_security_settings' => array(
50                    'allow_blog_token_access' => true,
51                ),
52                'schema'                         => array(
53                    '$schema' => 'http://json-schema.org/draft-04/schema#',
54                    'title'   => 'publicize-message-templates-placeholders',
55                    'type'    => 'array',
56                    'items'   => array(
57                        'type'       => 'object',
58                        'properties' => array(
59                            'id'    => array(
60                                'type'        => 'string',
61                                'description' => __( 'The placeholder token (e.g. {title}).', 'jetpack-publicize-pkg' ),
62                            ),
63                            'label' => array(
64                                'type'        => 'string',
65                                'description' => __( 'Human-readable description of the placeholder.', 'jetpack-publicize-pkg' ),
66                            ),
67                        ),
68                    ),
69                ),
70            )
71        );
72    }
73
74    /**
75     * Permission check.
76     *
77     * @return true|\WP_Error
78     */
79    public function permissions_check() {
80        return $this->publicize_permissions_check();
81    }
82
83    /**
84     * Return the placeholder catalogue.
85     *
86     * @return \WP_REST_Response
87     */
88    public function get_placeholders() {
89        return rest_ensure_response( Message_Templates_Placeholders::get_all() );
90    }
91}