| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPGraphQL\Data\Loader; |
| 4 |
|
| 5 |
use WPGraphQL\Model\MenuItem; |
| 6 |
use WPGraphQL\Model\Post; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class PostObjectLoader |
| 10 |
* |
| 11 |
* @package WPGraphQL\Data\Loader |
| 12 |
*/ |
| 13 |
class PostObjectLoader extends AbstractDataLoader { |
| 14 |
|
| 15 |
/** |
| 16 |
* {@inheritDoc} |
| 17 |
* |
| 18 |
* @param mixed|\WP_Post $entry The Post Object |
| 19 |
* |
| 20 |
* @return \WPGraphQL\Model\Post|\WPGraphQL\Model\MenuItem|null |
| 21 |
*/ |
| 22 |
protected function get_model( $entry, $key ) { |
| 23 |
if ( ! $entry instanceof \WP_Post ) { |
| 24 |
return null; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* If there's a Post Author connected to the post, we need to resolve the |
| 29 |
* user as it gets set in the globals via `setup_post_data()` and doing it this way |
| 30 |
* will batch the loading so when `setup_post_data()` is called the user |
| 31 |
* is already in the cache. |
| 32 |
*/ |
| 33 |
$context = $this->context; |
| 34 |
|
| 35 |
if ( ! empty( $entry->post_author ) && absint( $entry->post_author ) ) { |
| 36 |
$user_id = $entry->post_author; |
| 37 |
$context->get_loader( 'user' )->load_deferred( $user_id ); |
| 38 |
} |
| 39 |
|
| 40 |
if ( 'revision' === $entry->post_type && ! empty( $entry->post_parent ) && absint( $entry->post_parent ) ) { |
| 41 |
$post_parent = $entry->post_parent; |
| 42 |
$context->get_loader( 'post' )->load_deferred( $post_parent ); |
| 43 |
} |
| 44 |
|
| 45 |
if ( 'nav_menu_item' === $entry->post_type ) { |
| 46 |
return new MenuItem( $entry ); |
| 47 |
} |
| 48 |
|
| 49 |
$post = new Post( $entry ); |
| 50 |
if ( empty( $post->fields ) ) { |
| 51 |
return null; |
| 52 |
} |
| 53 |
|
| 54 |
return $post; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* {@inheritDoc} |
| 59 |
* |
| 60 |
* @return array<string|int,\WP_Post|null> |
| 61 |
*/ |
| 62 |
public function loadKeys( array $keys ) { |
| 63 |
if ( empty( $keys ) ) { |
| 64 |
return $keys; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Prepare the args for the query. We're provided a specific |
| 69 |
* set of IDs, so we want to query as efficiently as possible with |
| 70 |
* as little overhead as possible. We don't want to return post counts, |
| 71 |
* we don't want to include sticky posts, and we want to limit the query |
| 72 |
* to the count of the keys provided. The query must also return results |
| 73 |
* in the same order the keys were provided in. |
| 74 |
*/ |
| 75 |
$post_types = \WPGraphQL::get_allowed_post_types(); |
| 76 |
$post_types = array_merge( $post_types, [ 'revision', 'nav_menu_item' ] ); |
| 77 |
$args = [ |
| 78 |
'post_type' => $post_types, |
| 79 |
'post_status' => 'any', |
| 80 |
'posts_per_page' => count( $keys ), |
| 81 |
'post__in' => $keys, |
| 82 |
'orderby' => 'post__in', |
| 83 |
'no_found_rows' => true, |
| 84 |
'split_the_query' => false, |
| 85 |
'ignore_sticky_posts' => true, |
| 86 |
]; |
| 87 |
|
| 88 |
/** |
| 89 |
* Ensure that WP_Query doesn't first ask for IDs since we already have them. |
| 90 |
*/ |
| 91 |
add_filter( |
| 92 |
'split_the_query', |
| 93 |
static function ( $split, \WP_Query $query ) { |
| 94 |
if ( false === $query->get( 'split_the_query' ) ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
return $split; |
| 99 |
}, |
| 100 |
10, |
| 101 |
2 |
| 102 |
); |
| 103 |
new \WP_Query( $args ); |
| 104 |
$loaded_posts = []; |
| 105 |
foreach ( $keys as $key ) { |
| 106 |
/** |
| 107 |
* The query above has added our objects to the cache |
| 108 |
* so now we can pluck them from the cache to return here |
| 109 |
* and if they don't exist we can throw an error, otherwise |
| 110 |
* we can proceed to resolve the object via the Model layer. |
| 111 |
*/ |
| 112 |
$post_object = get_post( (int) $key ); |
| 113 |
|
| 114 |
if ( ! $post_object instanceof \WP_Post ) { |
| 115 |
$loaded_posts[ $key ] = null; |
| 116 |
} else { |
| 117 |
|
| 118 |
/** |
| 119 |
* Once dependencies are loaded, return the Post Object |
| 120 |
*/ |
| 121 |
$loaded_posts[ $key ] = $post_object; |
| 122 |
} |
| 123 |
} |
| 124 |
return $loaded_posts; |
| 125 |
} |
| 126 |
} |
| 127 |
|