Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 73
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_JSON_API_Get_Comments_Tree_v1_1_Endpoint
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 1
56
0.00% covered (danger)
0.00%
0 / 1
 get_site_tree
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 1
56
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3if ( ! defined( 'ABSPATH' ) ) {
4    exit( 0 );
5}
6
7new WPCOM_JSON_API_Get_Comments_Tree_v1_1_Endpoint(
8    array(
9        'description'      => 'Get a comments tree for site.',
10        'min_version'      => '1.1',
11        'max_version'      => '1.1',
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            'status' => '(string) Filter returned comments based on this value (allowed values: all, approved, pending, trash, spam).',
22        ),
23        'response_format'  => array(
24            'comments_count'   => '(int) Total number of comments on the site',
25            'comments_tree'    => '(array) Array of post IDs representing the comments tree for given site (max 50000)',
26            'trackbacks_count' => '(int) Total number of trackbacks on the site',
27            'trackbacks_tree'  => '(array) Array of post IDs representing the trackbacks tree for given site (max 50000)',
28            'pingbacks_count'  => '(int) Total number of pingbacks on the site',
29            'pingbacks_tree'   => '(array) Array of post IDs representing the pingbacks tree for given site (max 50000)',
30        ),
31
32        'example_request'  => 'https://public-api.wordpress.com/rest/v1.1/sites/en.blog.wordpress.com/comments-tree?status=approved',
33    )
34);
35
36/**
37 * GET comments tree v1_1 endpoint.
38 *
39 * @phan-constructor-used-for-side-effects
40 */
41class WPCOM_JSON_API_Get_Comments_Tree_v1_1_Endpoint extends WPCOM_JSON_API_Get_Comments_Tree_Endpoint { // phpcs:ignore
42    /**
43     * Retrieves a list of comment data for a given site.
44     *
45     * @param string $status Filter by status: all, approved, pending, spam or trash.
46     * @param int    $start_at first comment to search from going back in time.
47     *
48     * @return array
49     */
50    public function get_site_tree( $status, $start_at = PHP_INT_MAX ) {
51        global $wpdb;
52        $max_comment_count = 50000;
53        $db_status         = $this->get_comment_db_status( $status );
54
55        $db_comment_rows = $wpdb->get_results(
56            $wpdb->prepare(
57                'SELECT comment_ID, comment_post_ID, comment_parent, comment_type ' .
58                "FROM $wpdb->comments AS comments " .
59                "INNER JOIN $wpdb->posts AS posts ON comments.comment_post_ID = posts.ID " .
60                "WHERE comment_ID <= %d AND ( %s = 'all' OR comment_approved = %s ) " .
61                'ORDER BY comment_ID DESC ' .
62                'LIMIT %d',
63                (int) $start_at,
64                $db_status,
65                $db_status,
66                $max_comment_count
67            ),
68            ARRAY_N
69        );
70
71        $comments   = array();
72        $trackbacks = array();
73        $pingbacks  = array();
74        foreach ( $db_comment_rows as $row ) {
75            $comment_id        = (int) $row[0];
76            $comment_post_id   = (int) $row[1];
77            $comment_parent_id = (int) $row[2];
78            if ( ! isset( $comments[ $comment_post_id ] ) ) {
79                $comments[ $comment_post_id ] = array( array(), array() );
80            }
81            switch ( $row[3] ) {
82                case 'trackback':
83                    $trackbacks[ $comment_post_id ][] = $comment_id;
84                    break;
85                case 'pingback':
86                    $pingbacks[ $comment_post_id ][] = $comment_id;
87                    break;
88                default:
89                    if ( 0 === $comment_parent_id ) {
90                        $comments[ $comment_post_id ][0][] = $comment_id;
91                    } else {
92                        $comments[ $comment_post_id ][1][] = array( $comment_id, $comment_parent_id );
93                    }
94            }
95        }
96
97        return array(
98            'comments_count'   => $this->get_site_tree_total_count( $status, 'comment' ),
99            'comments_tree'    => $comments,
100            'trackbacks_count' => $this->get_site_tree_total_count( $status, 'trackback' ),
101            'trackbacks_tree'  => $trackbacks,
102            'pingbacks_count'  => $this->get_site_tree_total_count( $status, 'pingback' ),
103            'pingbacks_tree'   => $pingbacks,
104        );
105    }
106}