|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\ConditionalOnEnvironment\Admin\ConditionalOnEnvironment\GraphiQLExplorerInAdminClient\Overrides\Services\MenuPages; |
|
6
|
|
|
|
|
7
|
|
|
use GraphQLAPI\GraphQLAPI\ConditionalOnEnvironment\Admin\Services\Clients\AdminGraphiQLWithExplorerClient; |
|
8
|
|
|
use GraphQLAPI\GraphQLAPI\ConditionalOnEnvironment\Admin\Services\MenuPages\GraphiQLMenuPage as UpstreamGraphiQLMenuPage; |
|
9
|
|
|
use GraphQLAPI\GraphQLAPI\Facades\ModuleRegistryFacade; |
|
10
|
|
|
use GraphQLAPI\GraphQLAPI\Facades\UserSettingsManagerFacade; |
|
11
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleResolvers\ClientFunctionalityModuleResolver; |
|
12
|
|
|
use PoP\ComponentModel\Facades\Instances\InstanceManagerFacade; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* GraphiQL with Explorer page |
|
16
|
|
|
*/ |
|
17
|
|
|
class GraphiQLMenuPage extends UpstreamGraphiQLMenuPage |
|
18
|
|
|
{ |
|
19
|
|
|
protected function useGraphiQLExplorer(): bool |
|
20
|
|
|
{ |
|
21
|
|
|
$moduleRegistry = ModuleRegistryFacade::getInstance(); |
|
22
|
|
|
$userSettingsManager = UserSettingsManagerFacade::getInstance(); |
|
23
|
|
|
return |
|
24
|
|
|
$moduleRegistry->isModuleEnabled(ClientFunctionalityModuleResolver::GRAPHIQL_EXPLORER) |
|
25
|
|
|
&& $userSettingsManager->getSetting( |
|
26
|
|
|
ClientFunctionalityModuleResolver::GRAPHIQL_EXPLORER, |
|
27
|
|
|
ClientFunctionalityModuleResolver::OPTION_USE_IN_ADMIN_CLIENT |
|
28
|
|
|
); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected function getGraphiQLWithExplorerClientHTML(): string |
|
32
|
|
|
{ |
|
33
|
|
|
$instanceManager = InstanceManagerFacade::getInstance(); |
|
34
|
|
|
/** |
|
35
|
|
|
* @var AdminGraphiQLWithExplorerClient |
|
36
|
|
|
*/ |
|
37
|
|
|
$client = $instanceManager->getInstance(AdminGraphiQLWithExplorerClient::class); |
|
38
|
|
|
return $client->getClientHTML(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function print(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$htmlContent = $this->getGraphiQLWithExplorerClientHTML(); |
|
44
|
|
|
// Extract the HTML inside <body> |
|
45
|
|
|
$matches = []; |
|
46
|
|
|
preg_match('/<body([^>]+)?>(.*?)<\/body>/s', $htmlContent, $matches); |
|
47
|
|
|
$bodyHTMLContent = $matches[2]; |
|
48
|
|
|
// Remove all JS/CSS assets, since they are enqueued |
|
49
|
|
|
$bodyHTMLContent = preg_replace( |
|
50
|
|
|
[ |
|
51
|
|
|
'/<link[^>]*>(.*)<\/link>"/s', |
|
52
|
|
|
'/<script[^>]*>(.*)<\/script>/s', |
|
53
|
|
|
], |
|
54
|
|
|
'', |
|
55
|
|
|
$bodyHTMLContent |
|
56
|
|
|
); |
|
57
|
|
|
echo $bodyHTMLContent; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Enqueue the required assets and initialize the localized scripts |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function enqueueGraphiQLCustomAssets(): void |
|
66
|
|
|
{ |
|
67
|
|
|
// Common settings to both clients (with/out Explorer) |
|
68
|
|
|
$scriptSettings = array( |
|
69
|
|
|
'nonce' => \wp_create_nonce('wp_rest'), |
|
70
|
|
|
'response' => $this->getResponse(), |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
// Print the HTML from the Client |
|
74
|
|
|
$htmlContent = $this->getGraphiQLWithExplorerClientHTML(); |
|
75
|
|
|
// Extract the JS/CSS assets, from either the <head> or the <head> |
|
76
|
|
|
$matches = []; |
|
77
|
|
|
preg_match_all('/<link[^>]+href="([^">]+)"/s', $htmlContent, $matches); |
|
78
|
|
|
$cssFileURLs = $matches[1]; |
|
79
|
|
|
foreach ($cssFileURLs as $index => $cssFileURL) { |
|
80
|
|
|
\wp_enqueue_style( |
|
81
|
|
|
'graphql-api-graphiql-with-explorer-' . $index, |
|
82
|
|
|
$cssFileURL, |
|
83
|
|
|
array(), |
|
84
|
|
|
\GRAPHQL_API_VERSION |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
preg_match_all('/<script[^>]+src="([^">]+)"/s', $htmlContent, $matches); |
|
88
|
|
|
$jsFileURLs = $matches[1]; |
|
89
|
|
|
foreach ($jsFileURLs as $index => $jsFileURL) { |
|
90
|
|
|
\wp_enqueue_script( |
|
91
|
|
|
'graphql-api-graphiql-with-explorer-' . $index, |
|
92
|
|
|
$jsFileURL, |
|
93
|
|
|
array(), |
|
94
|
|
|
\GRAPHQL_API_VERSION, |
|
95
|
|
|
true |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// Override styles for the admin, so load last |
|
100
|
|
|
\wp_enqueue_style( |
|
101
|
|
|
'graphql-api-graphiql-with-explorer-client', |
|
102
|
|
|
\GRAPHQL_API_URL . 'assets/css/graphiql-with-explorer-client.css', |
|
103
|
|
|
array(), |
|
104
|
|
|
\GRAPHQL_API_VERSION |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
|
|
// Load data into the script. Because no script is enqueued since it is |
|
108
|
|
|
// in the body, then localize it to React |
|
109
|
|
|
\wp_localize_script( |
|
110
|
|
|
'graphql-api-graphiql-with-explorer-0', |
|
111
|
|
|
'graphiQLWithExplorerClientForWP', |
|
112
|
|
|
$scriptSettings |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|