1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\Hooks; |
6
|
|
|
|
7
|
|
|
use PoP\Hooks\AbstractHookSet; |
8
|
|
|
use GraphQLAPI\GraphQLAPI\Admin\FieldResolvers\CPTFieldResolver; |
9
|
|
|
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLCacheControlListPostType; |
10
|
|
|
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLAccessControlListPostType; |
11
|
|
|
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLSchemaConfigurationPostType; |
12
|
|
|
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLFieldDeprecationListPostType; |
13
|
|
|
use PoPSchema\SchemaCommons\DataLoading\ReturnTypes; |
14
|
|
|
|
15
|
|
|
class QueryHooks extends AbstractHookSet |
16
|
|
|
{ |
17
|
|
|
public const NON_EXISTING_ID = "non-existing"; |
18
|
|
|
|
19
|
|
|
protected function init() |
20
|
|
|
{ |
21
|
|
|
$this->hooksAPI->addAction( |
22
|
|
|
'CMSAPI:customposts:query', |
23
|
|
|
[$this, 'convertCustomPostsQuery'], |
24
|
|
|
10, |
25
|
|
|
2 |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Remove querying private CPTs |
31
|
|
|
* |
32
|
|
|
* @param array<string, mixed> $query |
33
|
|
|
* @param array<string, mixed> $options |
34
|
|
|
* @return array<string, mixed> |
35
|
|
|
*/ |
36
|
|
|
public function convertCustomPostsQuery(array $query, array $options): array |
37
|
|
|
{ |
38
|
|
|
// Hooks must be active only when resolving the query into IDs, |
39
|
|
|
// and not when resolving IDs into object, since there we don't have `$options` |
40
|
|
|
if (isset($query['post_type']) |
41
|
|
|
&& (!isset($options[CPTFieldResolver::QUERY_OPTION_ALLOW_QUERYING_PRIVATE_CPTS]) || !$options[CPTFieldResolver::QUERY_OPTION_ALLOW_QUERYING_PRIVATE_CPTS]) |
42
|
|
|
&& isset($options['return-type']) && $options['return-type'] == ReturnTypes::IDS |
43
|
|
|
) { |
44
|
|
|
// These CPTs must not be queried from outside, since they contain private configuration data |
45
|
|
|
$query['post_type'] = array_diff( |
46
|
|
|
$query['post_type'], |
47
|
|
|
[ |
48
|
|
|
GraphQLAccessControlListPostType::POST_TYPE, |
49
|
|
|
GraphQLCacheControlListPostType::POST_TYPE, |
50
|
|
|
GraphQLFieldDeprecationListPostType::POST_TYPE, |
51
|
|
|
GraphQLSchemaConfigurationPostType::POST_TYPE, |
52
|
|
|
] |
53
|
|
|
); |
54
|
|
|
// If there are no valid postTypes, then return no results |
55
|
|
|
// By not adding the post type, WordPress will fetch a "post" |
56
|
|
|
// Then, include a non-existing ID |
57
|
|
|
if (!$query['post_type']) { |
58
|
|
|
$query['include'] = self::NON_EXISTING_ID; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
return $query; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|