1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\ConditionalOnEnvironment\Admin\Services\EndpointResolvers; |
6
|
|
|
|
7
|
|
|
use PoP\EngineWP\Templates\TemplateHelpers; |
8
|
|
|
use GraphQLAPI\GraphQLAPI\General\EndpointHelpers; |
9
|
|
|
use GraphQLByPoP\GraphQLRequest\Execution\QueryExecutionHelpers; |
10
|
|
|
use GraphQLAPI\GraphQLAPI\Security\UserAuthorization; |
11
|
|
|
use GraphQLAPI\GraphQLAPI\EndpointResolvers\EndpointResolverTrait; |
12
|
|
|
use GraphQLAPI\GraphQLAPI\EndpointResolvers\AbstractEndpointResolver; |
13
|
|
|
use WP_Post; |
14
|
|
|
|
15
|
|
|
class AdminEndpointResolver extends AbstractEndpointResolver |
16
|
|
|
{ |
17
|
|
|
use EndpointResolverTrait { |
18
|
|
|
EndpointResolverTrait::executeGraphQLQuery as upstreamExecuteGraphQLQuery; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Provide the query to execute and its variables |
23
|
|
|
* |
24
|
|
|
* @return mixed[] Array of 2 elements: [query, variables] |
25
|
|
|
*/ |
26
|
|
|
protected function getGraphQLQueryAndVariables(?WP_Post $graphQLQueryPost): array |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Extract the query from the BODY through standard GraphQL endpoint execution |
30
|
|
|
*/ |
31
|
|
|
return QueryExecutionHelpers::extractRequestedGraphQLQueryPayload(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Execute the GraphQL query when posting to: |
36
|
|
|
* /wp-admin/edit.php?page=graphql_api&action=execute_query |
37
|
|
|
* |
38
|
|
|
* @return boolean |
39
|
|
|
*/ |
40
|
|
|
protected function isGraphQLQueryExecution(): bool |
41
|
|
|
{ |
42
|
|
|
return EndpointHelpers::isRequestingAdminGraphQLEndpoint(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Initialize the resolver |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public function initialize(): void |
51
|
|
|
{ |
52
|
|
|
parent::initialize(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Print the global JS variables, required by the blocks |
56
|
|
|
*/ |
57
|
|
|
$this->printGlobalVariables(); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* If executing the GraphQL query, resolve it |
61
|
|
|
*/ |
62
|
|
|
if ($this->isGraphQLQueryExecution()) { |
63
|
|
|
$this->executeGraphQLQuery(); |
64
|
|
|
$this->printTemplateInAdminAndExit(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Print JS variables which are used by several blocks, |
71
|
|
|
* before the blocks are loaded |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
protected function printGlobalVariables(): void |
76
|
|
|
{ |
77
|
|
|
\add_action('admin_print_scripts', function () { |
78
|
|
|
// Make sure the user has access to the editor |
79
|
|
|
if (UserAuthorization::canAccessSchemaEditor()) { |
80
|
|
|
/** |
81
|
|
|
* The endpoint against which to execute GraphQL queries while on the WordPress admin |
82
|
|
|
*/ |
83
|
|
|
\printf( |
84
|
|
|
'<script type="text/javascript">var GRAPHQL_API_ADMIN_ENDPOINT = "%s"</script>', |
85
|
|
|
EndpointHelpers::getAdminGraphQLEndpoint() |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Execute the GraphQL query |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
protected function executeGraphQLQuery(): void |
97
|
|
|
{ |
98
|
|
|
/** |
99
|
|
|
* Only in "init" we can execute `wp_get_current_user`, to validate that the |
100
|
|
|
* user can execute the query |
101
|
|
|
*/ |
102
|
|
|
\add_action( |
103
|
|
|
'init', |
104
|
|
|
function () { |
105
|
|
|
// Make sure the user has access to the editor |
106
|
|
|
if (UserAuthorization::canAccessSchemaEditor()) { |
107
|
|
|
$this->upstreamExecuteGraphQLQuery(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* To print the JSON output, we use WordPress templates, |
115
|
|
|
* which are used only in the front-end. |
116
|
|
|
* When in the admin, we must manually load the template, |
117
|
|
|
* and then exit |
118
|
|
|
* |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
|
|
protected function printTemplateInAdminAndExit(): void |
122
|
|
|
{ |
123
|
|
|
\add_action( |
124
|
|
|
'admin_init', |
125
|
|
|
function () { |
126
|
|
|
// Make sure the user has access to the editor |
127
|
|
|
if (UserAuthorization::canAccessSchemaEditor()) { |
128
|
|
|
include TemplateHelpers::getTemplateFile(); |
129
|
|
|
die; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|