|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\Blocks; |
|
6
|
|
|
|
|
7
|
|
|
use GraphQLAPI\GraphQLAPI\General\CPTUtils; |
|
8
|
|
|
use GraphQLAPI\GraphQLAPI\Facades\ModuleRegistryFacade; |
|
9
|
|
|
use GraphQLAPI\GraphQLAPI\General\BlockRenderingHelpers; |
|
10
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleResolvers\EndpointFunctionalityModuleResolver; |
|
11
|
|
|
use PoP\ComponentModel\Facades\Instances\InstanceManagerFacade; |
|
12
|
|
|
use GraphQLAPI\GraphQLAPI\BlockCategories\AbstractBlockCategory; |
|
13
|
|
|
use GraphQLAPI\GraphQLAPI\BlockCategories\QueryExecutionBlockCategory; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* SchemaConfiguration block |
|
17
|
|
|
*/ |
|
18
|
|
|
class SchemaConfigurationBlock extends AbstractBlock |
|
19
|
|
|
{ |
|
20
|
|
|
use GraphQLByPoPBlockTrait; |
|
21
|
|
|
|
|
22
|
|
|
public const ATTRIBUTE_NAME_SCHEMA_CONFIGURATION = 'schemaConfiguration'; |
|
23
|
|
|
/** |
|
24
|
|
|
* These consts must be integer! |
|
25
|
|
|
*/ |
|
26
|
|
|
public const ATTRIBUTE_VALUE_SCHEMA_CONFIGURATION_DEFAULT = 0; |
|
27
|
|
|
public const ATTRIBUTE_VALUE_SCHEMA_CONFIGURATION_NONE = -1; |
|
28
|
|
|
public const ATTRIBUTE_VALUE_SCHEMA_CONFIGURATION_INHERIT = -2; |
|
29
|
|
|
|
|
30
|
|
|
protected function getBlockName(): string |
|
31
|
|
|
{ |
|
32
|
|
|
return 'schema-configuration'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function getBlockCategory(): ?AbstractBlockCategory |
|
36
|
|
|
{ |
|
37
|
|
|
$instanceManager = InstanceManagerFacade::getInstance(); |
|
38
|
|
|
/** |
|
39
|
|
|
* @var QueryExecutionBlockCategory |
|
40
|
|
|
*/ |
|
41
|
|
|
$blockCategory = $instanceManager->getInstance(QueryExecutionBlockCategory::class); |
|
42
|
|
|
return $blockCategory; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function isDynamicBlock(): bool |
|
46
|
|
|
{ |
|
47
|
|
|
return true; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Pass localized data to the block |
|
52
|
|
|
* |
|
53
|
|
|
* @return array<string, mixed> |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function getLocalizedData(): array |
|
56
|
|
|
{ |
|
57
|
|
|
$moduleRegistry = ModuleRegistryFacade::getInstance(); |
|
58
|
|
|
return array_merge( |
|
59
|
|
|
parent::getLocalizedData(), |
|
60
|
|
|
[ |
|
61
|
|
|
'isAPIHierarchyEnabled' => $moduleRegistry->isModuleEnabled(EndpointFunctionalityModuleResolver::API_HIERARCHY), |
|
62
|
|
|
] |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param array<string, mixed> $attributes |
|
68
|
|
|
*/ |
|
69
|
|
|
public function renderBlock(array $attributes, string $content): string |
|
70
|
|
|
{ |
|
71
|
|
|
/** |
|
72
|
|
|
* Print the list of all the contained Access Control blocks |
|
73
|
|
|
*/ |
|
74
|
|
|
$blockContentPlaceholder = <<<EOF |
|
75
|
|
|
<div class="%s"> |
|
76
|
|
|
<h3 class="%s">%s</strong></h3> |
|
77
|
|
|
%s |
|
78
|
|
|
</div> |
|
79
|
|
|
EOF; |
|
80
|
|
|
$schemaConfigurationContent = ''; |
|
81
|
|
|
$schemaConfigurationID = $attributes[self::ATTRIBUTE_NAME_SCHEMA_CONFIGURATION] ?? null; |
|
82
|
|
|
if ($schemaConfigurationID == self::ATTRIBUTE_VALUE_SCHEMA_CONFIGURATION_DEFAULT) { |
|
83
|
|
|
$schemaConfigurationContent = \__('Default', 'graphql-api'); |
|
84
|
|
|
} elseif ($schemaConfigurationID == self::ATTRIBUTE_VALUE_SCHEMA_CONFIGURATION_NONE) { |
|
85
|
|
|
$schemaConfigurationContent = \__('None', 'graphql-api'); |
|
86
|
|
|
} elseif ($schemaConfigurationID == self::ATTRIBUTE_VALUE_SCHEMA_CONFIGURATION_INHERIT) { |
|
87
|
|
|
$schemaConfigurationContent = \__('Inherit from parent', 'graphql-api'); |
|
88
|
|
|
} elseif ($schemaConfigurationID > 0) { |
|
89
|
|
|
$schemaConfigurationObject = \get_post($schemaConfigurationID); |
|
90
|
|
|
if (!is_null($schemaConfigurationObject)) { |
|
91
|
|
|
$schemaConfigurationDescription = CPTUtils::getCustomPostDescription($schemaConfigurationObject); |
|
92
|
|
|
$permalink = \get_permalink($schemaConfigurationObject->ID); |
|
93
|
|
|
$schemaConfigurationContent = ($permalink ? |
|
94
|
|
|
\sprintf( |
|
95
|
|
|
'<code><a href="%s">%s</a></code>', |
|
96
|
|
|
$permalink, |
|
97
|
|
|
BlockRenderingHelpers::getCustomPostTitle($schemaConfigurationObject) |
|
98
|
|
|
) : |
|
99
|
|
|
\sprintf( |
|
100
|
|
|
'<code>%s</code>', |
|
101
|
|
|
BlockRenderingHelpers::getCustomPostTitle($schemaConfigurationObject) |
|
102
|
|
|
) |
|
103
|
|
|
) . ($schemaConfigurationDescription ? |
|
104
|
|
|
'<br/><small>' . $schemaConfigurationDescription . '</small>' |
|
105
|
|
|
: '' |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
$className = $this->getBlockClassName(); |
|
110
|
|
|
return sprintf( |
|
111
|
|
|
$blockContentPlaceholder, |
|
112
|
|
|
$className, |
|
113
|
|
|
$className . '-front', |
|
114
|
|
|
\__('Schema Configuration', 'graphql-api'), |
|
115
|
|
|
$schemaConfigurationContent |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|