class-ld-rest-courses-prerequisites-controller.php
5.61 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
<?php
/**
* LearnDash REST API V2 Courses Prerequisites Controller.
*
* This Controller class is used to GET/UPDATE/DELETE the association
* between the LearnDash Courses (sfwd-courses) and Course
* Prerequisites.
*
* This class extends the LD_REST_Posts_Controller_V2 class.
*
* @since 3.3.0
* @package LearnDash\REST\V2
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( ! class_exists( 'LD_REST_Courses_Prerequisites_Controller_V2' ) ) && ( class_exists( 'LD_REST_Posts_Controller_V2' ) ) ) {
/**
* Class LearnDash REST API V2 Courses Prerequisites Controller.
*
* @since 3.3.0
* @uses LD_REST_Posts_Controller_V2
*/
class LD_REST_Courses_Prerequisites_Controller_V2 extends LD_REST_Posts_Controller_V2 /* phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound */ {
/**
* Public constructor for class
*
* @since 3.3.0
*
* @param string $post_type Post type.
*/
public function __construct( $post_type = '' ) {
if ( empty( $post_type ) ) {
$post_type = learndash_get_post_type_slug( 'course' );
}
$this->post_type = $post_type;
parent::__construct( $this->post_type );
/**
* Set the rest_base after the parent __constructor
* as it will set these var with WP specific details.
*/
$this->rest_base = $this->get_rest_base( 'courses' );
$this->rest_sub_base = $this->get_rest_base( 'courses-prerequisites' );
}
/**
* Registers the routes for the objects of the controller.
*
* @since 3.3.0
*
* @see register_rest_route() in WordPress core.
*/
public function register_routes() {
$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',
);
}
$courses_namespace = trailingslashit( LEARNDASH_REST_API_NAMESPACE ) . $this->version;
$courses_rest_base = $this->get_rest_base( 'courses' );
register_rest_route(
$courses_namespace,
'/' . $courses_rest_base . '/(?P<id>[\d]+)/' . $this->rest_sub_base,
array(
'args' => array(
'id' => array(
'description' => sprintf(
// translators: placeholder: Course.
esc_html_x(
'%s ID',
'placeholder: Course',
'learndash'
),
LearnDash_Custom_Label::get_label( 'course' )
),
'required' => true,
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_courses_prerequisites' ),
'permission_callback' => array( $this, 'get_courses_prerequisites_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Filter Course Prerequisites Query args.
*
* @since 3.3.0
*
* @param array $query_args Key value array of query var to query value.
* @param WP_REST_Request $request The request used.
*
* @return array Key value array of query var to query value.
*/
public function rest_query_filter( $query_args, $request ) {
if ( ! $this->is_rest_request( $request ) ) {
return $query_args;
}
$query_args = parent::rest_query_filter( $query_args, $request );
$route_url = $request->get_route();
$ld_route_url = '/' . $this->namespace . '/' . $this->rest_base . '/' . absint( $request['id'] ) . '/' . $this->rest_sub_base;
if ( ( ! empty( $route_url ) ) && ( $ld_route_url === $route_url ) ) {
$course_id = (int) $request['id'];
if ( ! empty( $course_id ) ) {
$this->course_post = get_post( $course_id );
if ( ( $this->course_post ) && ( is_a( $this->course_post, 'WP_Post' ) ) && ( learndash_get_post_type_slug( 'course' ) === $this->course_post->post_type ) ) {
$course_pre = learndash_get_course_prerequisites( $this->course_post->ID, false );
if ( ! empty( $course_pre ) ) {
$query_args['post__in'] = $query_args['post__in'] ? array_intersect( array_keys( $course_pre ), $query_args['post__in'] ) : array_keys( $course_pre );
} else {
$query_args['post__in'] = array( 0 );
}
}
}
}
return $query_args;
}
/**
* Permissions check for getting course groups.
*
* @since 3.3.0
*
* @param WP_REST_Request $request Full details about the request.
*
* @return true|WP_Error True if the request has read access, otherwise WP_Error object.
*/
public function get_courses_prerequisites_permissions_check( $request ) {
if ( learndash_is_admin_user() ) {
return true;
} else {
return new WP_Error( 'ld_rest_cannot_view', esc_html__( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) );
}
}
/**
* Retrieves a course users.
*
* @since 3.3.0
*
* @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_courses_prerequisites( $request ) {
return $this->get_items( $request );
}
/**
* Gets public schema.
*
* @since 3.3.0
*
* @return array
*/
public function get_public_item_schema() {
$schema = parent::get_public_item_schema();
$schema['title'] = 'course-prerequisites';
$schema['parent'] = 'course';
return $schema;
}
// End of functions.
}
}