PluginProbe
WPGraphQL / trunk
WPGraphQL vtrunk
2.22.3 2.22.2 2.22.1 2.22.0 2.21.1 2.21.0 2.20.0 2.19.0 2.18.0 2.17.0 2.16.0 2.15.1 2.15.0 2.14.1 2.14.0 2.13.0 2.2.0 2.3.0 2.3.3 2.3.6 2.3.8 2.5.0 2.5.1 2.5.2 2.5.3 All 177 releases
wp-graphql / src / Data / Loader / CommentLoader.php

CommentLoader.php in WPGraphQL trunk, at src/Data/Loader/CommentLoader.php

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