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