|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\Blocks; |
|
6
|
|
|
|
|
7
|
|
|
use GraphQLAPI\GraphQLAPI\Blocks\AbstractBlock; |
|
8
|
|
|
use GraphQLAPI\GraphQLAPI\ComponentConfiguration; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Base Control block |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class AbstractControlBlock extends AbstractBlock |
|
14
|
|
|
{ |
|
15
|
|
|
use WithTypeFieldControlBlockTrait; |
|
16
|
|
|
|
|
17
|
|
|
public const ATTRIBUTE_NAME_TYPE_FIELDS = 'typeFields'; |
|
18
|
|
|
public const ATTRIBUTE_NAME_DIRECTIVES = 'directives'; |
|
19
|
|
|
|
|
20
|
|
|
protected function isDynamicBlock(): bool |
|
21
|
|
|
{ |
|
22
|
|
|
return true; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
protected function disableFields(): bool |
|
26
|
|
|
{ |
|
27
|
|
|
return false; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
protected function disableDirectives(): bool |
|
31
|
|
|
{ |
|
32
|
|
|
return false; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Block align class |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getAlignClass(): string |
|
39
|
|
|
{ |
|
40
|
|
|
return 'alignwide'; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param array<string, mixed> $attributes |
|
45
|
|
|
*/ |
|
46
|
|
|
public function renderBlock(array $attributes, string $content): string |
|
47
|
|
|
{ |
|
48
|
|
|
// Append "-front" because this style must be used only on the client, not on the admin |
|
49
|
|
|
$className = $this->getBlockClassName() . '-front'; |
|
50
|
|
|
$fieldTypeContent = $directiveContent = ''; |
|
51
|
|
|
if (!$this->disableFields()) { |
|
52
|
|
|
$fieldTypeContent = ComponentConfiguration::getEmptyLabel(); |
|
53
|
|
|
$typeFields = $attributes[self::ATTRIBUTE_NAME_TYPE_FIELDS] ?? []; |
|
54
|
|
|
if ($typeFields) { |
|
55
|
|
|
$typeFieldsForPrint = $this->getTypeFieldsForPrint($typeFields); |
|
56
|
|
|
/** |
|
57
|
|
|
* If $groupFieldsUnderTypeForPrint is true, combine all types under their shared typeName |
|
58
|
|
|
* If $groupFieldsUnderTypeForPrint is false, replace namespacedTypeName for typeName and "." for "/" |
|
59
|
|
|
* */ |
|
60
|
|
|
$groupFieldsUnderTypeForPrint = ComponentConfiguration::groupFieldsUnderTypeForPrint(); |
|
61
|
|
|
if ($groupFieldsUnderTypeForPrint) { |
|
62
|
|
|
/** |
|
63
|
|
|
* Cast object so PHPStan doesn't throw error |
|
64
|
|
|
* @var array<string,array> |
|
65
|
|
|
*/ |
|
66
|
|
|
$typeFieldsForPrint = $typeFieldsForPrint; |
|
67
|
|
|
$fieldTypeContent = ''; |
|
68
|
|
|
foreach ($typeFieldsForPrint as $typeName => $fields) { |
|
69
|
|
|
$fieldTypeContent .= sprintf( |
|
70
|
|
|
'<strong>%s</strong><ul><li><code>%s</code></li></ul>', |
|
71
|
|
|
$typeName, |
|
72
|
|
|
implode( |
|
73
|
|
|
'</code></li><li><code>', |
|
74
|
|
|
$fields |
|
75
|
|
|
) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} else { |
|
79
|
|
|
/** |
|
80
|
|
|
* Cast object so PHPStan doesn't throw error |
|
81
|
|
|
* @var string[]; |
|
82
|
|
|
*/ |
|
83
|
|
|
$typeFieldsForPrint = $typeFieldsForPrint; |
|
84
|
|
|
$fieldTypeContent = sprintf( |
|
85
|
|
|
'<ul><li>%s</li></ul>', |
|
86
|
|
|
implode( |
|
87
|
|
|
'</li><li>', |
|
88
|
|
|
$typeFieldsForPrint |
|
89
|
|
|
) |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
if (!$this->disableDirectives()) { |
|
95
|
|
|
$directiveContent = ComponentConfiguration::getEmptyLabel(); |
|
96
|
|
|
$directives = $attributes[self::ATTRIBUTE_NAME_DIRECTIVES] ?? []; |
|
97
|
|
|
if ($directives) { |
|
98
|
|
|
// // Notice we are adding the "@" symbol for GraphQL directives |
|
99
|
|
|
$directiveContent = sprintf( |
|
100
|
|
|
// '<ul><li><code>@%s</code></li></ul>', |
|
101
|
|
|
// implode('</code></li><li><code>@', $directives) |
|
102
|
|
|
'<ul><li><code>%s</code></li></ul>', |
|
103
|
|
|
implode('</code></li><li><code>', $directives) |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
$blockDataContent = ''; |
|
108
|
|
|
if (!$this->disableFields() && !$this->disableDirectives()) { |
|
109
|
|
|
$blockDataPlaceholder = <<<EOT |
|
110
|
|
|
<h4>%s</h4> |
|
111
|
|
|
%s |
|
112
|
|
|
<h4>%s</h4> |
|
113
|
|
|
%s |
|
114
|
|
|
EOT; |
|
115
|
|
|
$blockDataContent = sprintf( |
|
116
|
|
|
$blockDataPlaceholder, |
|
117
|
|
|
__('Fields', 'graphql-api'), |
|
118
|
|
|
$fieldTypeContent, |
|
119
|
|
|
__('Directives', 'graphql-api'), |
|
120
|
|
|
$directiveContent |
|
121
|
|
|
); |
|
122
|
|
|
} elseif (!$this->disableFields()) { |
|
123
|
|
|
$blockDataContent = $fieldTypeContent; |
|
124
|
|
|
} elseif (!$this->disableDirectives()) { |
|
125
|
|
|
$blockDataContent = $directiveContent; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$blockContentPlaceholder = <<<EOT |
|
129
|
|
|
<div class="%s"> |
|
130
|
|
|
<div class="%s"> |
|
131
|
|
|
<h3 class="%s">%s</h3> |
|
132
|
|
|
%s |
|
133
|
|
|
</div> |
|
134
|
|
|
<div class="%s"> |
|
135
|
|
|
<h3 class="%s">%s</h3> |
|
136
|
|
|
%s |
|
137
|
|
|
</div> |
|
138
|
|
|
</div> |
|
139
|
|
|
EOT; |
|
140
|
|
|
return sprintf( |
|
141
|
|
|
$blockContentPlaceholder, |
|
142
|
|
|
$className . ' ' . $this->getAlignClass(), |
|
143
|
|
|
$className . '__data', |
|
144
|
|
|
$className . '__title', |
|
145
|
|
|
$this->getBlockDataTitle(), |
|
146
|
|
|
$blockDataContent, |
|
147
|
|
|
$className . '__content', |
|
148
|
|
|
$className . '__title', |
|
149
|
|
|
$this->getBlockContentTitle(), |
|
150
|
|
|
$this->getBlockContent($attributes, $content) |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
protected function getBlockDataTitle(): string |
|
155
|
|
|
{ |
|
156
|
|
|
return \__('Select fields and directives:', 'graphql-api'); |
|
157
|
|
|
} |
|
158
|
|
|
protected function getBlockContentTitle(): string |
|
159
|
|
|
{ |
|
160
|
|
|
return \__('Configuration:', 'graphql-api'); |
|
161
|
|
|
} |
|
162
|
|
|
/** |
|
163
|
|
|
* @param array<string, mixed> $attributes |
|
164
|
|
|
*/ |
|
165
|
|
|
abstract protected function getBlockContent(array $attributes, string $content): string; |
|
166
|
|
|
} |
|
167
|
|
|
|