class-ld-rest-echo-controller.php
3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* LearnDash REST API V1 Echo Controller.
*
* @since 3.0.7
* @package LearnDash\REST\V1
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( ! class_exists( 'LD_REST_Echo_Controller_V1' ) ) && ( class_exists( 'WP_REST_Controller' ) ) ) {
/**
* Class LearnDash REST API V1 Echo Controller.
*
* @since 3.0.7
*/
class LD_REST_Echo_Controller_V1 extends WP_REST_Controller /* phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound */ {
/**
* Version
*
* @var string
*/
protected $version = 'v1';
/**
* Constructor.
*
* @since 3.0.7
*/
public function __construct() {
$this->namespace = LEARNDASH_REST_API_NAMESPACE . '/' . $this->version;
$this->rest_base = 'echo';
}
/**
* Registers the routes for the objects of the controller.
*
* @since 3.0.7
*
* @see register_rest_route()
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_response' ),
'permission_callback' => array( $this, 'get_response_permissions_check' ),
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'get_response' ),
'permission_callback' => array( $this, 'get_response_permissions_check' ),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'get_response' ),
'permission_callback' => array( $this, 'get_response_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
}
/**
* Checks if a given request has access to read the theme.
*
* @since 3.0.7
*
* @param WP_REST_Request $request Full details about the request.
*
* @return true|WP_Error True if the request has read access for the item, otherwise WP_Error object.
*/
public function get_response_permissions_check( $request ) {
return true;
}
/**
* Retrieves a response.
*
* @since 3.0.7
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_response( $request ) {
$response_array = array();
$response_array['method'] = $request->get_method();
$response_array['route'] = $request->get_route();
$response_array['authenticated'] = is_user_logged_in() ? 1 : 0;
$response_array['query_params'] = $request->get_query_params();
$request_body = $request->get_body();
if ( ! empty( $request_body ) ) {
$request_body = json_decode( $request_body, true );
$response_array['content-type'] = $request->get_header( 'content-type' );
$response_array['body'] = $request_body;
} else {
$response_array['body'] = '';
}
$response = rest_ensure_response( $response_array );
$response->header( 'X-WP-Total', count( $response_array ) );
$response->header( 'X-WP-TotalPages', count( $response_array ) );
return $response;
}
/**
* Retrieves the schema, conforming to JSON Schema.
*
* @since 3.0.7
*
* @return array Item schema data.
*/
public function get_item_schema() {
$schema = array();
return $this->add_additional_fields_schema( $schema );
}
}
}