1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class WPCOM_JSON_API_Get_Comments_Tree_Endpoint extends WPCOM_JSON_API_Endpoint { |
4
|
|
|
/** |
5
|
|
|
* Retrieves a list of comment data for a given site. |
6
|
|
|
* |
7
|
|
|
* @param {string} $status filter by status: 'all' | 'approved' | 'pending' | 'spam' | 'trash' |
|
|
|
|
8
|
|
|
* @param {int} $start_at first comment to search from going back in time |
|
|
|
|
9
|
|
|
* @returns {array} list of comment information matching query |
10
|
|
|
*/ |
11
|
|
|
public function get_site_tree( $status, $start_at = PHP_INT_MAX ) { |
12
|
|
|
global $wpdb; |
13
|
|
|
$max_comment_count = 10000; |
14
|
|
|
$total_count = $this->get_site_tree_total_count( $status ); |
15
|
|
|
$db_status = $this->get_comment_db_status( $status ); |
16
|
|
|
|
17
|
|
|
$db_comments = $wpdb->get_results( |
18
|
|
|
$wpdb->prepare( |
19
|
|
|
"SELECT comment_ID, comment_post_ID, comment_parent " . |
20
|
|
|
"FROM $wpdb->comments AS comments " . |
21
|
|
|
"INNER JOIN $wpdb->posts AS posts ON comments.comment_post_ID = posts.ID " . |
22
|
|
|
"WHERE comment_type = '' AND comment_ID <= %d AND ( %s = 'all' OR comment_approved = %s ) " . |
23
|
|
|
"ORDER BY comment_ID DESC " . |
24
|
|
|
"LIMIT %d", |
25
|
|
|
(int) $start_at, $db_status, $db_status, $max_comment_count |
26
|
|
|
), |
27
|
|
|
ARRAY_N |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
// Avoid using anonymous function bellow in order to preserve PHP 5.2 compatibility. |
31
|
|
|
function intval_array_map( $comments ) { |
32
|
|
|
return array_map( 'intval', $comments ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return array( $total_count, intval_array_map( $db_comments ) ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Retrieves a total count of comments for the given site. |
40
|
|
|
* |
41
|
|
|
* @param {string} $status filter by status: 'all' | 'approved' | 'pending' | 'spam' | 'trash' |
|
|
|
|
42
|
|
|
* @returns {int} total count of comments for a site |
43
|
|
|
*/ |
44
|
|
|
public function get_site_tree_total_count( $status ) { |
45
|
|
|
global $wpdb; |
46
|
|
|
$db_status = $this->get_comment_db_status( $status ); |
47
|
|
|
|
48
|
|
|
return $wpdb->get_var( |
49
|
|
|
$wpdb->prepare( |
50
|
|
|
"SELECT COUNT(1) " . |
51
|
|
|
"FROM $wpdb->comments AS comments " . |
52
|
|
|
"INNER JOIN $wpdb->posts AS posts ON comments.comment_post_ID = posts.ID " . |
53
|
|
|
"WHERE comment_type = '' AND ( %s = 'all' OR comment_approved = %s )", |
54
|
|
|
$db_status, $db_status |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Ensure a valid status is converted to a database-supported value if necessary. |
61
|
|
|
* |
62
|
|
|
* @param {string} $status 'all' | 'approved' | 'pending' | 'spam' | 'trash' |
|
|
|
|
63
|
|
|
* @returns {string} value that exists in database |
64
|
|
|
*/ |
65
|
|
|
public function get_comment_db_status( $status ) { |
66
|
|
|
if ( 'approved' === $status ) { |
67
|
|
|
$status = '1'; |
68
|
|
|
} |
69
|
|
|
if ( 'pending' === $status ) { |
70
|
|
|
$status = '0'; |
71
|
|
|
} |
72
|
|
|
return $status; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function validate_status_param( $status ) { |
76
|
|
|
return in_array( $status, array( 'all', 'approved', 'pending', 'spam', 'trash' ) ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// /sites/%s/comments-tree |
80
|
|
|
function callback( $path = '', $blog_id = 0 ) { |
81
|
|
|
$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); |
82
|
|
|
if ( is_wp_error( $blog_id ) ) { |
83
|
|
|
return $blog_id; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$args = $this->query_args(); |
87
|
|
|
$comment_status = $args['status']; |
88
|
|
|
|
89
|
|
|
if ( ! $this->validate_status_param( $comment_status ) ) { |
90
|
|
|
return new WP_Error( 'invalid_status', 'Invalid comment status value provided ', 400 ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->get_site_tree( $comment_status ); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.