Completed
Push — update/crm-integration-fixes ( a3fc2b )
by Jeremy
14:51 queued 06:26
created

...om-json-api-get-comments-tree-v1-2-endpoint.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
new WPCOM_JSON_API_Get_Comments_Tree_v1_2_Endpoint( array(
4
	'description' => 'Get a comments tree for site.',
5
	'min_version' => '1.2',
6
	'max_version' => '1.2',
7
	'group'       => 'comments-tree',
8
	'stat'        => 'comments-tree:1',
9
10
	'method'      => 'GET',
11
	'path'        =>  '/sites/%s/comments-tree',
12
	'path_labels' => array(
13
		'$site' => '(int|string) Site ID or domain',
14
	),
15
	'query_parameters' => array(
16
		'post_id' => '(int) Filter returned comments by a post.',
17
		'status'   => '(string) Filter returned comments based on this value (allowed values: all, approved, pending, trash, spam).',
18
	),
19
	'response_format' => array(
20
		'comments_tree'    => '(array) Array of post IDs representing the comments tree for given site or post (max 50000)',
21
		'trackbacks_tree'  => '(array) Array of post IDs representing the trackbacks tree for given site or post (max 50000)',
22
		'pingbacks_tree'   => '(array) Array of post IDs representing the pingbacks tree for given site or post (max 50000)',
23
	),
24
25
	'example_request' => 'https://public-api.wordpress.com/rest/v1.2/sites/en.blog.wordpress.com/comments-tree?&status=approved&post_id=123',
26
) );
27
28
class WPCOM_JSON_API_Get_Comments_Tree_v1_2_Endpoint extends WPCOM_JSON_API_Get_Comments_Tree_v1_1_Endpoint {
29
	/**
30
	 * Retrieves a list of comment data.
31
	 *
32
	 * @param array $args {
33
	 *     Optional. Arguments to control behavior. Default empty array.
34
	 *
35
	 *     @type int    $max_comment_count Maximum number of comments returned.
36
	 *     @type int    $post_id           Filter by post.
37
	 *     @type int    $start_at          First comment to search from going back in time.
38
	 *     @type string $status            Filter by status: all, approved, pending, spam or trash.
39
	 * }
40
	 *
41
	 * @return array
42
	 */
43
	function get_site_tree_v1_2( $args = array() ) {
44
		global $wpdb;
45
		$defaults = array(
46
			'max_comment_count' => 50000,
47
			'post_id'           => NULL,
48
			'start_at'          => PHP_INT_MAX,
49
			'status'            => 'all',
50
		);
51
		$args = wp_parse_args( $args, $defaults );
52
		$db_status = $this->get_comment_db_status( $args['status'] );
53
54
		if ( ! empty( $args['post_id'] ) ) {
55
			$db_comment_rows = $wpdb->get_results(
56
				$wpdb->prepare(
57
					"SELECT comment_ID, comment_parent, comment_type " .
58
					"FROM $wpdb->comments AS comments " .
59
					"WHERE comment_ID <= %d AND comment_post_ID = %d AND ( %s = 'all' OR comment_approved = %s ) " .
60
					"ORDER BY comment_ID DESC " .
61
					"LIMIT %d",
62
					(int) $args['start_at'], (int) $args['post_id'], $db_status, $db_status, $args['max_comment_count']
63
				),
64
				ARRAY_N
65
			);
66
		} else {
67
			$db_comment_rows = $wpdb->get_results(
68
				$wpdb->prepare(
69
					"SELECT comment_ID, comment_parent, comment_type, comment_post_ID " .
70
					"FROM $wpdb->comments AS comments " .
71
					"INNER JOIN $wpdb->posts AS posts ON comments.comment_post_ID = posts.ID " .
72
					"WHERE comment_ID <= %d AND ( %s = 'all' OR comment_approved = %s ) " .
73
					"ORDER BY comment_ID DESC " .
74
					"LIMIT %d",
75
					(int) $args['start_at'], $db_status, $db_status, $args['max_comment_count']
76
				),
77
				ARRAY_N
78
			);
79
		}
80
81
		$comments = array();
82
		$trackbacks = array();
83
		$pingbacks = array();
84
		foreach ( $db_comment_rows as $row ) {
85
			$comment_id = intval( $row[0] );
86
			$comment_parent_id = intval( $row[1] );
87
			$comment_post_id = isset( $args['post_id'] ) ? intval( $args['post_id'] ) : intval( $row[3] );
88
89
			if ( ! isset( $comments[ $comment_post_id ] ) ) {
90
				$comments[ $comment_post_id ] = array( array(), array() );
91
			}
92 View Code Duplication
			switch ( $row[2] ) {
93
				case 'trackback':
94
					$trackbacks[ $comment_post_id ][] = $comment_id;
95
					break;
96
				case 'pingback':
97
					$pingbacks[ $comment_post_id ][] = $comment_id;
98
					break;
99
				default:
100
					if ( 0 === $comment_parent_id ) {
101
						$comments[ $comment_post_id ][0][] = $comment_id;
102
					} else {
103
						$comments[ $comment_post_id ][1][] = array( $comment_id, $comment_parent_id );
104
					}
105
			}
106
		}
107
108
		return array(
109
			'comments_tree' => $comments,
110
			'trackbacks_tree' => $trackbacks,
111
			'pingbacks_tree' => $pingbacks,
112
		);
113
	}
114
115
	/**
116
	 * Endpoint callback for /sites/%s/comments-tree
117
	 *
118
	 * @param string $path
119
	 * @param int    $blog_id
120
	 *
121
	 * @return array Site or post tree results by status.
122
	 */
123
	function callback( $path = '', $blog_id = 0 ) {
124
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
125
		if ( is_wp_error( $blog_id ) ) {
126
			return $blog_id;
127
		}
128
129
		$args = $this->query_args();
130
		$filters = array();
131
132
		if ( ! empty( $args['status'] ) ) {
133
			if ( ! $this->validate_status_param( $args['status'] ) ) {
134
				return new WP_Error( 'invalid_status', 'Invalid comment status value provided: ' . $args['status'] . '.', 400 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'invalid_status'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
135
			}
136
			$filters['status'] = $args['status'];
137
		}
138
139
		if ( ! empty( $args['post_id'] ) ) {
140
			if ( is_null( get_post( absint( $args['post_id'] ) ) ) ) {
141
				return new WP_Error( 'invalid_post', 'Invalid post', 400 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'invalid_post'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
142
			}
143
			$filters['post_id'] = absint( $args['post_id'] );
144
		}
145
146
		return $this->get_site_tree_v1_2( $filters );
147
	}
148
}
149