| 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' ) { |
| 27 |
// @todo remove in 3.0.0 |
| 28 |
if ( $query_vars instanceof \WP_Comment_Query ) { |
| 29 |
_doing_it_wrong( |
| 30 |
__METHOD__, |
| 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' ), |
| 32 |
'1.9.0' |
| 33 |
); |
| 34 |
$query_vars = $query_vars->query_vars; |
| 35 |
} |
| 36 |
|
| 37 |
// Initialize the class properties. |
| 38 |
parent::__construct( $query_vars, $cursor ); |
| 39 |
|
| 40 |
// Set ID Key. |
| 41 |
$this->id_key = "{$this->wpdb->comments}.comment_ID"; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* {@inheritDoc} |
| 46 |
* |
| 47 |
* @return ?\WP_Comment |
| 48 |
*/ |
| 49 |
public function get_cursor_node() { |
| 50 |
// Bail if no offset. |
| 51 |
if ( ! $this->cursor_offset ) { |
| 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 |
* @hookGroup connections |
| 63 |
* @since 0.0.5 |
| 64 |
* |
| 65 |
* @return \WP_Comment|null |
| 66 |
*/ |
| 67 |
$pre_comment = apply_filters( 'graphql_pre_comment_cursor_node', null, $this->cursor_offset, $this ); |
| 68 |
if ( null !== $pre_comment ) { |
| 69 |
return $pre_comment; |
| 70 |
} |
| 71 |
|
| 72 |
// Get cursor node. |
| 73 |
$comment = WP_Comment::get_instance( $this->cursor_offset ); |
| 74 |
|
| 75 |
return false !== $comment ? $comment : null; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* {@inheritDoc} |
| 80 |
*/ |
| 81 |
public function get_where() { |
| 82 |
// If we have a bad cursor, just skip. |
| 83 |
if ( ! $this->is_valid_offset_and_node() ) { |
| 84 |
return ''; |
| 85 |
} |
| 86 |
|
| 87 |
$orderby = $this->get_query_var( 'orderby' ); |
| 88 |
$order = $this->get_query_var( 'order' ); |
| 89 |
|
| 90 |
// if there's custom ordering, use it to determine the cursor |
| 91 |
if ( ! empty( $orderby ) ) { |
| 92 |
$this->compare_with( $orderby, $order ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* If there's no orderby specified yet, compare with the following fields. |
| 97 |
*/ |
| 98 |
if ( ! $this->builder->has_fields() ) { |
| 99 |
$this->compare_with_cursor_fields( |
| 100 |
[ |
| 101 |
[ |
| 102 |
'key' => "{$this->wpdb->comments}.comment_date", |
| 103 |
'value' => $this->cursor_node ? $this->cursor_node->comment_date : null, |
| 104 |
'type' => 'DATETIME', |
| 105 |
], |
| 106 |
] |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
$this->compare_with_id_field(); |
| 111 |
|
| 112 |
return $this->to_sql(); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get AND operator for given order by key |
| 117 |
* |
| 118 |
* @param string $by The order by key |
| 119 |
* @param string $order The order direction ASC or DESC |
| 120 |
* |
| 121 |
* @return void |
| 122 |
*/ |
| 123 |
public function compare_with( $by, $order ) { |
| 124 |
// Bail early, if "key" and "value" provided in query_vars. |
| 125 |
$key = $this->get_query_var( "graphql_cursor_compare_by_{$by}_key" ); |
| 126 |
$value = $this->get_query_var( "graphql_cursor_compare_by_{$by}_value" ); |
| 127 |
if ( ! empty( $key ) && ! empty( $value ) ) { |
| 128 |
$this->builder->add_field( $key, $value, null, $order ); |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
$key = "{$this->wpdb->comments}.{$by}"; |
| 133 |
$value = $this->cursor_node->{$by}; |
| 134 |
$type = null; |
| 135 |
|
| 136 |
if ( 'comment_date' === $by ) { |
| 137 |
$type = 'DATETIME'; |
| 138 |
} |
| 139 |
|
| 140 |
$value = $this->cursor_node->{$by} ?? null; |
| 141 |
if ( ! empty( $value ) ) { |
| 142 |
$this->builder->add_field( $key, $value, $type ); |
| 143 |
return; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* {@inheritDoc} |
| 149 |
*/ |
| 150 |
public function to_sql() { |
| 151 |
$sql = $this->builder->to_sql(); |
| 152 |
return ! empty( $sql ) ? ' AND ' . $sql : ''; |
| 153 |
} |
| 154 |
} |
| 155 |
|