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
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
new WPCOM_JSON_API_Get_Comments_Tree_v1_2_Endpoint(
array(
'description' => 'Get a comments tree for site.',
'min_version' => '1.2',
'max_version' => '1.2',
'group' => 'comments-tree',
'stat' => 'comments-tree:1',
'method' => 'GET',
'path' => '/sites/%s/comments-tree',
'path_labels' => array(
'$site' => '(int|string) Site ID or domain',
),
'query_parameters' => array(
'post_id' => '(int) Filter returned comments by a post.',
'status' => '(string) Filter returned comments based on this value (allowed values: all, approved, pending, trash, spam).',
),
'response_format' => array(
'comments_tree' => '(array) Array of post IDs representing the comments tree for given site or post (max 50000)',
'trackbacks_tree' => '(array) Array of post IDs representing the trackbacks tree for given site or post (max 50000)',
'pingbacks_tree' => '(array) Array of post IDs representing the pingbacks tree for given site or post (max 50000)',
),
'example_request' => 'https://public-api.wordpress.com/rest/v1.2/sites/en.blog.wordpress.com/comments-tree?&status=approved&post_id=123',
)
);
/**
* Get comments tree v1_2 endpoint class.
*/
class WPCOM_JSON_API_Get_Comments_Tree_v1_2_Endpoint extends WPCOM_JSON_API_Get_Comments_Tree_v1_1_Endpoint { // phpcs:ignore
/**
* Retrieves a list of comment data.
*
* @param array $args {
* Optional. Arguments to control behavior. Default empty array.
*
* @type int $max_comment_count Maximum number of comments returned.
* @type int $post_id Filter by post.
* @type int $start_at First comment to search from going back in time.
* @type string $status Filter by status: all, approved, pending, spam or trash.
* }
*
* @return array
*/
public function get_site_tree_v1_2( $args = array() ) {
global $wpdb;
$defaults = array(
'max_comment_count' => 50000,
'post_id' => null,
'start_at' => PHP_INT_MAX,
'status' => 'all',
);
$args = wp_parse_args( $args, $defaults );
$db_status = $this->get_comment_db_status( $args['status'] );
if ( ! empty( $args['post_id'] ) ) {
$db_comment_rows = $wpdb->get_results(
$wpdb->prepare(
'SELECT comment_ID, comment_parent, comment_type ' .
"FROM $wpdb->comments AS comments " .
"WHERE comment_ID <= %d AND comment_post_ID = %d AND ( %s = 'all' OR comment_approved = %s ) " .
'ORDER BY comment_ID DESC ' .
'LIMIT %d',
(int) $args['start_at'],
(int) $args['post_id'],
$db_status,
$db_status,
$args['max_comment_count']
),
ARRAY_N
);
} else {
$db_comment_rows = $wpdb->get_results(
$wpdb->prepare(
'SELECT comment_ID, comment_parent, comment_type, comment_post_ID ' .
"FROM $wpdb->comments AS comments " .
"INNER JOIN $wpdb->posts AS posts ON comments.comment_post_ID = posts.ID " .
"WHERE comment_ID <= %d AND ( %s = 'all' OR comment_approved = %s ) " .
'ORDER BY comment_ID DESC ' .
'LIMIT %d',
(int) $args['start_at'],
$db_status,
$db_status,
$args['max_comment_count']
),
ARRAY_N
);
}
$comments = array();
$trackbacks = array();
$pingbacks = array();
foreach ( $db_comment_rows as $row ) {
$comment_id = (int) $row[0];
$comment_parent_id = (int) $row[1];
$comment_post_id = isset( $args['post_id'] ) ? (int) $args['post_id'] : (int) $row[3];
if ( ! isset( $comments[ $comment_post_id ] ) ) {
$comments[ $comment_post_id ] = array( array(), array() );
}
switch ( $row[2] ) {
case 'trackback':
$trackbacks[ $comment_post_id ][] = $comment_id;
break;
case 'pingback':
$pingbacks[ $comment_post_id ][] = $comment_id;
break;
default:
if ( 0 === $comment_parent_id ) {
$comments[ $comment_post_id ][0][] = $comment_id;
} else {
$comments[ $comment_post_id ][1][] = array( $comment_id, $comment_parent_id );
}
}
}
return array(
'comments_tree' => $comments,
'trackbacks_tree' => $trackbacks,
'pingbacks_tree' => $pingbacks,
);
}
/**
* Endpoint callback for /sites/%s/comments-tree
*
* @param string $path - the path.
* @param int $blog_id - the blog ID.
*
* @return array Site or post tree results by status.
*/
public function callback( $path = '', $blog_id = 0 ) {
$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
if ( is_wp_error( $blog_id ) ) {
return $blog_id;
}
$args = $this->query_args();
$filters = array();
if ( ! empty( $args['status'] ) ) {
if ( ! $this->validate_status_param( $args['status'] ) ) {
return new WP_Error( 'invalid_status', 'Invalid comment status value provided: ' . $args['status'] . '.', 400 );
}
$filters['status'] = $args['status'];
}
if ( ! empty( $args['post_id'] ) ) {
if ( get_post( absint( $args['post_id'] ) ) === null ) {
return new WP_Error( 'invalid_post', 'Invalid post', 400 );
}
$filters['post_id'] = absint( $args['post_id'] );
}
return $this->get_site_tree_v1_2( $filters );
}
}
|