class-ld-rest-sections-controller.php
2.52 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
<?php
/**
* LearnDash REST API V1 Sections Controller.
*
* @since 3.0.0
* @package LearnDash\REST\V1
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( ! class_exists( 'LD_REST_Sections_Controller_V1' ) ) && ( class_exists( 'WP_REST_Controller' ) ) ) {
/**
* Class LearnDash REST API V1 Sections Controller.
*
* @since 3.0.0
*/
class LD_REST_Sections_Controller_V1 extends WP_REST_Controller /* phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound */ {
/**
* Registers the routes for the objects of the controller.
*
* @since 3.0.0
*
* @see register_rest_route() in WordPress core.
*/
public function register_routes() {
$version = '1';
$namespace = LEARNDASH_REST_API_NAMESPACE . '/v' . $version;
$base = 'sections';
register_rest_route(
$namespace,
'/' . $base . '/(?P<id>[\d]+)',
array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_item' ),
'permission_callback' => array( $this, 'permissions_check' ),
'args' => array(
'id' => array(
'required' => true,
'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
},
'sanitize_callback' => 'absint',
),
),
),
)
);
}
/**
* Check if a given request has access manage the item.
*
* @since 3.0.0
*
* @param WP_REST_Request $request Full data about the request.
*
* @return WP_Error|bool
*/
public function permissions_check( $request ) {
$params = $request->get_params();
$course_id = $params['id'];
return current_user_can( 'edit_post', $course_id );
}
/**
* Update sections data.
*
* @since 3.0.0
*
* @param WP_REST_Request $request Full data about the request.
*
* @return WP_Error|WP_REST_Request
*/
public function update_item( $request ) {
$params = $request->get_params();
$course_id = $params['id'];
$sections = isset( $params['sections'] ) ? wp_slash( $params['sections'] ) : '';
update_post_meta( $course_id, 'course_sections', $sections );
return new WP_REST_Response( $this->get_sections_data( $course_id ), 200 );
}
/**
* Get sections data.
*
* @since 3.0.0
*
* @param int $course_id The course ID.
*
* @return object
*/
public function get_sections_data( $course_id ) {
$sections = get_post_meta( $course_id, 'course_sections', true );
return $sections;
}
}
}