PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / json-endpoints / class.wpcom-json-api-get-comments-tree-v1-1-endpoint.php

class.wpcom-json-api-get-comments-tree-v1-1-endpoint.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.5, at json-endpoints/class.wpcom-json-api-get-comments-tree-v1-1-endpoint.php

107 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_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 */
41 class 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 }
107