• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql / 17562196906

08 Sep 2025 07:44PM UTC coverage: 84.575% (+0.4%) from 84.17%
17562196906

push

github

web-flow
Merge pull request #3389 from wp-graphql/develop

release: next version 📦

238 of 308 new or added lines in 13 files covered. (77.27%)

6 existing lines in 6 files now uncovered.

15884 of 18781 relevant lines covered (84.57%)

261.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.64
/src/Data/Cursor/CommentObjectCursor.php
1
<?php
2

3
namespace WPGraphQL\Data\Cursor;
4

5
use WP_Comment;
6

7
/**
8
 * Comment Cursor
9
 *
10
 * This class generates the SQL and operators for cursor based pagination for comments
11
 *
12
 * @package WPGraphQL\Data\Cursor
13
 */
14
class CommentObjectCursor extends AbstractCursor {
15

16
        /**
17
         * @var ?\WP_Comment
18
         */
19
        public $cursor_node;
20

21
        /**
22
         * {@inheritDoc}
23
         *
24
         * @param array<string,mixed>|\WP_Comment_Query $query_vars The query vars to use when building the SQL statement.
25
         */
26
        public function __construct( $query_vars, $cursor = 'after' ) {
3✔
27
                // @todo remove in 3.0.0
28
                if ( $query_vars instanceof \WP_Comment_Query ) {
3✔
NEW
29
                        _doing_it_wrong(
×
NEW
30
                                __METHOD__,
×
NEW
31
                                esc_html__( 'The first argument should be an array of $query_vars, not the WP_Query object. This will throw an error in the next major release', 'wp-graphql' ),
×
NEW
32
                                '1.9.0'
×
NEW
33
                        );
×
UNCOV
34
                        $query_vars = $query_vars->query_vars;
×
35
                }
36

37
                // Initialize the class properties.
38
                parent::__construct( $query_vars, $cursor );
3✔
39

40
                // Set ID Key.
41
                $this->id_key = "{$this->wpdb->comments}.comment_ID";
3✔
42
        }
43

44
        /**
45
         * {@inheritDoc}
46
         *
47
         * @return ?\WP_Comment
48
         */
49
        public function get_cursor_node() {
3✔
50
                // Bail if no offset.
51
                if ( ! $this->cursor_offset ) {
3✔
52
                        return null;
×
53
                }
54

55
                /**
56
                 * If pre-hooked, return filtered node.
57
                 *
58
                 * @param \WP_Comment|null                           $pre_comment The pre-filtered comment node.
59
                 * @param int                                        $offset      The cursor offset.
60
                 * @param \WPGraphQL\Data\Cursor\CommentObjectCursor $node        The cursor instance.
61
                 *
62
                 * @return \WP_Comment|null
63
                 */
64
                $pre_comment = apply_filters( 'graphql_pre_comment_cursor_node', null, $this->cursor_offset, $this );
3✔
65
                if ( null !== $pre_comment ) {
3✔
66
                        return $pre_comment;
×
67
                }
68

69
                // Get cursor node.
70
                $comment = WP_Comment::get_instance( $this->cursor_offset );
3✔
71

72
                return false !== $comment ? $comment : null;
3✔
73
        }
74

75
        /**
76
         * {@inheritDoc}
77
         */
78
        public function get_where() {
3✔
79
                // If we have a bad cursor, just skip.
80
                if ( ! $this->is_valid_offset_and_node() ) {
3✔
81
                        return '';
×
82
                }
83

84
                $orderby = $this->get_query_var( 'orderby' );
3✔
85
                $order   = $this->get_query_var( 'order' );
3✔
86

87
                // if there's custom ordering, use it to determine the cursor
88
                if ( ! empty( $orderby ) ) {
3✔
89
                        $this->compare_with( $orderby, $order );
3✔
90
                }
91

92
                /**
93
                 * If there's no orderby specified yet, compare with the following fields.
94
                 */
95
                if ( ! $this->builder->has_fields() ) {
3✔
96
                        $this->compare_with_cursor_fields(
×
97
                                [
×
98
                                        [
×
99
                                                'key'  => "{$this->wpdb->comments}.comment_date",
×
100
                                                'by'   => $this->cursor_node ? $this->cursor_node->comment_date : null,
×
101
                                                'type' => 'DATETIME',
×
102
                                        ],
×
103
                                ]
×
104
                        );
×
105
                }
106

107
                $this->compare_with_id_field();
3✔
108

109
                return $this->to_sql();
3✔
110
        }
111

112
        /**
113
         * Get AND operator for given order by key
114
         *
115
         * @param string $by    The order by key
116
         * @param string $order The order direction ASC or DESC
117
         *
118
         * @return void
119
         */
120
        public function compare_with( $by, $order ) {
3✔
121
                // Bail early, if "key" and "value" provided in query_vars.
122
                $key   = $this->get_query_var( "graphql_cursor_compare_by_{$by}_key" );
3✔
123
                $value = $this->get_query_var( "graphql_cursor_compare_by_{$by}_value" );
3✔
124
                if ( ! empty( $key ) && ! empty( $value ) ) {
3✔
125
                        $this->builder->add_field( $key, $value, null, $order );
×
126
                        return;
×
127
                }
128

129
                $key   = "{$this->wpdb->comments}.{$by}";
3✔
130
                $value = $this->cursor_node->{$by};
3✔
131
                $type  = null;
3✔
132

133
                if ( 'comment_date' === $by ) {
3✔
134
                        $type = 'DATETIME';
3✔
135
                }
136

137
                $value = $this->cursor_node->{$by} ?? null;
3✔
138
                if ( ! empty( $value ) ) {
3✔
139
                        $this->builder->add_field( $key, $value, $type );
3✔
140
                        return;
3✔
141
                }
142
        }
143

144
        /**
145
         * {@inheritDoc}
146
         */
147
        public function to_sql() {
3✔
148
                $sql = $this->builder->to_sql();
3✔
149
                return ! empty( $sql ) ? ' AND ' . $sql : '';
3✔
150
        }
151
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc