| 1 |
<?php |
| 2 |
namespace WPGraphQL\Data\Loader; |
| 3 |
|
| 4 |
use WPGraphQL\Model\CommentAuthor; |
| 5 |
|
| 6 |
/** |
| 7 |
* Class CommentAuthorLoader |
| 8 |
* |
| 9 |
* @package WPGraphQL\Data\Loader |
| 10 |
*/ |
| 11 |
class CommentAuthorLoader extends AbstractDataLoader { |
| 12 |
|
| 13 |
/** |
| 14 |
* {@inheritDoc} |
| 15 |
* |
| 16 |
* @return ?\WPGraphQL\Model\CommentAuthor |
| 17 |
*/ |
| 18 |
protected function get_model( $entry, $key ) { |
| 19 |
if ( ! $entry instanceof \WP_Comment ) { |
| 20 |
return null; |
| 21 |
} |
| 22 |
|
| 23 |
return new CommentAuthor( $entry ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* {@inheritDoc} |
| 28 |
* |
| 29 |
* @param int[] $keys Array of IDs to load |
| 30 |
*/ |
| 31 |
public function loadKeys( array $keys ) { |
| 32 |
/** |
| 33 |
* Prepare the args for the query. We're provided a specific set of IDs of comments |
| 34 |
* so we want to query as efficiently as possible with as little overhead to get the comment |
| 35 |
* objects. No need to count the rows, etc. |
| 36 |
*/ |
| 37 |
$args = [ |
| 38 |
'comment__in' => $keys, |
| 39 |
'orderby' => 'comment__in', |
| 40 |
'number' => count( $keys ), |
| 41 |
'no_found_rows' => true, |
| 42 |
'count' => false, |
| 43 |
]; |
| 44 |
|
| 45 |
/** |
| 46 |
* Execute the query. Call get_comments() to add them to the cache. |
| 47 |
*/ |
| 48 |
$query = new \WP_Comment_Query( $args ); |
| 49 |
$query->get_comments(); |
| 50 |
$loaded = []; |
| 51 |
foreach ( $keys as $key ) { |
| 52 |
$loaded[ $key ] = \WP_Comment::get_instance( $key ); |
| 53 |
} |
| 54 |
return $loaded; |
| 55 |
} |
| 56 |
} |
| 57 |
|