class-ld-rest-courses-controller.php
6.37 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* LearnDash REST API V1 Courses Post Controller.
*
* @since 2.5.8
* @package LearnDash\REST\V1
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( ! class_exists( 'LD_REST_Courses_Controller_V1' ) ) && ( class_exists( 'LD_REST_Posts_Controller_V1' ) ) ) {
/**
* Class LearnDash REST API V1 Courses Post Controller.
*
* @since 2.5.8
*/
class LD_REST_Courses_Controller_V1 extends LD_REST_Posts_Controller_V1 /* //phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound */ {
/**
* Public constructor for class
*
* @since 2.5.8
*
* @param string $post_type Post type.
*/
public function __construct( $post_type = '' ) {
$this->post_type = 'sfwd-courses';
$this->taxonomies = array();
parent::__construct( $this->post_type );
$this->namespace = LEARNDASH_REST_API_NAMESPACE . '/' . $this->version;
$this->rest_base = LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_General_REST_API', 'sfwd-courses' );
}
/**
* Registers the routes for the objects of the controller.
*
* @since 2.5.8
*
* @see register_rest_route() in WordPress core.
*/
public function register_routes() {
$this->register_fields();
parent::register_routes_wpv2();
$collection_params = $this->get_collection_params();
$schema = $this->get_item_schema();
$get_item_args = array(
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
);
if ( isset( $schema['properties']['password'] ) ) {
$get_item_args['password'] = array(
'description' => esc_html__( 'The password for the post if it is password protected.', 'learndash' ),
'type' => 'string',
);
}
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'create_item' ),
'permission_callback' => array( $this, 'create_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
),
'schema' => array( $this, 'get_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)',
array(
'args' => array(
'id' => array(
'description' => esc_html__( 'Unique identifier for the object.', 'learndash' ),
'required' => true,
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => $get_item_args,
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_item' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
'args' => array(
'force' => array(
'type' => 'boolean',
'default' => false,
'description' => esc_html__( 'Whether to bypass trash and force deletion.', 'learndash' ),
),
),
),
'schema' => array( $this, 'get_schema' ),
)
);
include LEARNDASH_REST_API_DIR . '/' . $this->version . '/class-ld-rest-courses-steps-controller.php';
$this->sub_controllers['class-ld-rest-courses-steps-controller'] = new LD_REST_Courses_Steps_Controller_V1();
$this->sub_controllers['class-ld-rest-courses-steps-controller']->register_routes();
include LEARNDASH_REST_API_DIR . '/' . $this->version . '/class-ld-rest-courses-users-controller.php';
$this->sub_controllers['class-ld-rest-courses-users-controller'] = new LD_REST_Courses_Users_Controller_V1();
$this->sub_controllers['class-ld-rest-courses-users-controller']->register_routes();
include LEARNDASH_REST_API_DIR . '/' . $this->version . '/class-ld-rest-courses-groups-controller.php';
$this->sub_controllers['class-ld-rest-courses-groups-controller'] = new LD_REST_Courses_Groups_Controller_V1();
$this->sub_controllers['class-ld-rest-courses-groups-controller']->register_routes();
}
/**
* Gets Course schema.
*
* @since 2.5.8
*
* @return array
*/
public function get_schema() {
$schema = $this->get_public_item_schema();
$schema['title'] = 'course';
return $schema;
}
/**
* Override the REST response links.
*
* @since 2.5.8
*
* @param WP_REST_Response $response WP_REST_Response instance.
* @param WP_Post $post WP_Post instance.
* @param WP_REST_Request $request WP_REST_Request instance.
*/
public function rest_prepare_response_filter( WP_REST_Response $response, WP_Post $post, WP_REST_Request $request ) {
if ( $this->post_type === $post->post_type ) {
$base = sprintf( '/%s/%s', $this->namespace, $this->rest_base );
$request_route = $request->get_route();
if ( ( ! empty( $request_route ) ) && ( strpos( $request_route, $base ) !== false ) ) {
$links = array();
if ( ! isset( $response->links['steps'] ) ) {
$links['steps'] = array(
'href' => rest_url( trailingslashit( $base ) . $post->ID ) . '/steps',
'embeddable' => true,
);
}
if ( ! isset( $response->links['users'] ) ) {
$links['users'] = array(
'href' => rest_url( trailingslashit( $base ) . $post->ID ) . '/users',
'embeddable' => true,
);
}
if ( ! isset( $response->links['groups'] ) ) {
$links['groups'] = array(
'href' => rest_url( trailingslashit( $base ) . $post->ID ) . '/groups',
'embeddable' => true,
);
}
if ( ! empty( $links ) ) {
$response->add_links( $links );
}
}
}
return $response;
}
// End of functions.
}
}