1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\Blocks; |
6
|
|
|
|
7
|
|
|
use PoP\AccessControl\Schema\SchemaModes; |
8
|
|
|
use PoP\AccessControl\ComponentConfiguration; |
9
|
|
|
use GraphQLAPI\GraphQLAPI\Blocks\AbstractControlBlock; |
10
|
|
|
use GraphQLAPI\GraphQLAPI\Facades\ModuleRegistryFacade; |
11
|
|
|
use GraphQLAPI\GraphQLAPI\Blocks\GraphQLByPoPBlockTrait; |
12
|
|
|
use PoP\ComponentModel\Facades\Instances\InstanceManagerFacade; |
13
|
|
|
use GraphQLAPI\GraphQLAPI\BlockCategories\AbstractBlockCategory; |
14
|
|
|
use GraphQLAPI\GraphQLAPI\BlockCategories\AccessControlBlockCategory; |
15
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleResolvers\AccessControlFunctionalityModuleResolver; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Access Control block |
19
|
|
|
*/ |
20
|
|
|
class AccessControlBlock extends AbstractControlBlock |
21
|
|
|
{ |
22
|
|
|
use GraphQLByPoPBlockTrait; |
23
|
|
|
|
24
|
|
|
public const ATTRIBUTE_NAME_SCHEMA_MODE = 'schemaMode'; |
25
|
|
|
|
26
|
|
|
protected function getBlockName(): string |
27
|
|
|
{ |
28
|
|
|
return 'access-control'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function getBlockCategory(): ?AbstractBlockCategory |
32
|
|
|
{ |
33
|
|
|
$instanceManager = InstanceManagerFacade::getInstance(); |
34
|
|
|
/** |
35
|
|
|
* @var AccessControlBlockCategory |
36
|
|
|
*/ |
37
|
|
|
$blockCategory = $instanceManager->getInstance(AccessControlBlockCategory::class); |
38
|
|
|
return $blockCategory; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function registerEditorCSS(): bool |
42
|
|
|
{ |
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function registerCommonStyleCSS(): bool |
47
|
|
|
{ |
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function getBlockDataTitle(): string |
52
|
|
|
{ |
53
|
|
|
return \__('Define access for:', 'graphql-api'); |
54
|
|
|
} |
55
|
|
|
protected function getBlockContentTitle(): string |
56
|
|
|
{ |
57
|
|
|
if (ComponentConfiguration::enableIndividualControlForPublicPrivateSchemaMode()) { |
58
|
|
|
return \__('Access Control Rules:', 'graphql-api'); |
59
|
|
|
} |
60
|
|
|
return \__('Who can access:', 'graphql-api'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Pass localized data to the block |
65
|
|
|
* |
66
|
|
|
* @return array<string, mixed> |
67
|
|
|
*/ |
68
|
|
|
protected function getLocalizedData(): array |
69
|
|
|
{ |
70
|
|
|
return array_merge( |
71
|
|
|
parent::getLocalizedData(), |
72
|
|
|
[ |
73
|
|
|
'isIndividualControlForSchemaModeEnabled' => ComponentConfiguration::enableIndividualControlForPublicPrivateSchemaMode(), |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return the nested blocks' content |
80
|
|
|
* |
81
|
|
|
* @param array<string, mixed> $attributes |
82
|
|
|
*/ |
83
|
|
|
protected function getBlockContent(array $attributes, string $content): string |
84
|
|
|
{ |
85
|
|
|
$maybeSchemaModeContent = ''; |
86
|
|
|
if (ComponentConfiguration::enableIndividualControlForPublicPrivateSchemaMode()) { |
87
|
|
|
$blockContentPlaceholder = <<<EOT |
88
|
|
|
<p><strong>%s</strong> %s</p> |
89
|
|
|
<h4 class="%s">%s</h4> |
90
|
|
|
EOT; |
91
|
|
|
$className = $this->getBlockClassName() . '-front'; |
92
|
|
|
$schemaModeLabels = [ |
93
|
|
|
SchemaModes::PUBLIC_SCHEMA_MODE => \__('Public', 'graphql-api'), |
94
|
|
|
SchemaModes::PRIVATE_SCHEMA_MODE => \__('Private', 'graphql-api'), |
95
|
|
|
]; |
96
|
|
|
$maybeSchemaModeContent = sprintf( |
97
|
|
|
$blockContentPlaceholder, |
98
|
|
|
\__('Public/Private Schema:', 'graphql-api'), |
99
|
|
|
isset($attributes[self::ATTRIBUTE_NAME_SCHEMA_MODE]) ? |
100
|
|
|
$schemaModeLabels[$attributes[self::ATTRIBUTE_NAME_SCHEMA_MODE]] |
101
|
|
|
: \__('Default', 'graphql-api'), |
102
|
|
|
$className . '__title', |
103
|
|
|
\__('Who can access:', 'graphql-api') |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
if ($content) { |
107
|
|
|
return $maybeSchemaModeContent . $content; |
108
|
|
|
} |
109
|
|
|
return $maybeSchemaModeContent . sprintf( |
110
|
|
|
'<em>%s</em>', |
111
|
|
|
\__('(not set)', 'graphql-api') |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|