forked from themeum/tutor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREST_Topic.php
More file actions
87 lines (74 loc) · 1.68 KB
/
Copy pathREST_Topic.php
File metadata and controls
87 lines (74 loc) · 1.68 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
<?php
/**
* REST API for course topics.
*
* @package Tutor\RestAPI
* @author Themeum <[email protected]>
* @link https://themeum.com
* @since 1.7.1
*/
namespace TUTOR;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class REST_Topic
*
* @package Tutor
*
* @since 1.7.1
*/
class REST_Topic {
use REST_Response;
/**
* Post parent ID.
*
* @var int $post_parent The ID of the post parent.
*/
private $post_parent;
/**
* Post type.
*
* @var string $post_type The post type for topics.
*/
private $post_type = 'topics';
/**
* Retrieve topics by course ID via REST API.
*
* @param WP_REST_Request $request The REST request object.
*
* @since 1.7.1
*
* @return mixed
*/
public function course_topic( WP_REST_Request $request ) {
$this->post_parent = $request->get_param( 'course_id' );
if ( ! isset( $this->post_parent ) ) {
$response = array(
'code' => 'get_topic',
'message' => __( 'course_id is required', 'tutor' ),
'data' => array(),
);
return self::send( $response );
}
global $wpdb;
$result = $wpdb->get_results(
$wpdb->prepare( "SELECT ID, post_title, post_content, post_name FROM {$wpdb->posts} WHERE post_type = %s AND post_parent = %d", $this->post_type, $this->post_parent )
);
if ( count( $result ) > 0 ) {
$response = array(
'code' => 'get_topic',
'message' => __( 'Topic retrieved successfully', 'tutor' ),
'data' => $result,
);
return self::send( $response );
}
$response = array(
'code' => 'not_found',
'message' => __( 'Topic not found for given course ID', 'tutor' ),
'data' => array(),
);
return self::send( $response );
}
}