|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\Blocks; |
|
6
|
|
|
|
|
7
|
|
|
use GraphQLAPI\GraphQLAPI\Blocks\AbstractControlBlock; |
|
8
|
|
|
use GraphQLAPI\GraphQLAPI\Blocks\GraphQLByPoPBlockTrait; |
|
9
|
|
|
use PoP\ComponentModel\Facades\Instances\InstanceManagerFacade; |
|
10
|
|
|
use GraphQLAPI\GraphQLAPI\BlockCategories\AbstractBlockCategory; |
|
11
|
|
|
use GraphQLAPI\GraphQLAPI\BlockCategories\CacheControlBlockCategory; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Cache Control block |
|
15
|
|
|
*/ |
|
16
|
|
|
class CacheControlBlock extends AbstractControlBlock |
|
17
|
|
|
{ |
|
18
|
|
|
use GraphQLByPoPBlockTrait; |
|
19
|
|
|
|
|
20
|
|
|
public const ATTRIBUTE_NAME_CACHE_CONTROL_MAX_AGE = 'cacheControlMaxAge'; |
|
21
|
|
|
|
|
22
|
|
|
protected function getBlockName(): string |
|
23
|
|
|
{ |
|
24
|
|
|
return 'cache-control'; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
protected function getBlockCategory(): ?AbstractBlockCategory |
|
28
|
|
|
{ |
|
29
|
|
|
$instanceManager = InstanceManagerFacade::getInstance(); |
|
30
|
|
|
/** |
|
31
|
|
|
* @var CacheControlBlockCategory |
|
32
|
|
|
*/ |
|
33
|
|
|
$blockCategory = $instanceManager->getInstance(CacheControlBlockCategory::class); |
|
34
|
|
|
return $blockCategory; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function registerCommonStyleCSS(): bool |
|
38
|
|
|
{ |
|
39
|
|
|
return true; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function registerEditorCSS(): bool |
|
43
|
|
|
{ |
|
44
|
|
|
return true; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Add the locale language to the localized data? |
|
49
|
|
|
* |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function addLocalLanguage(): bool |
|
53
|
|
|
{ |
|
54
|
|
|
return true; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Default language for the script/component's documentation |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function getDefaultLanguage(): ?string |
|
61
|
|
|
{ |
|
62
|
|
|
// English |
|
63
|
|
|
return 'en'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function getBlockDataTitle(): string |
|
67
|
|
|
{ |
|
68
|
|
|
return \__('Cache-control header for:', 'graphql-api'); |
|
69
|
|
|
} |
|
70
|
|
|
protected function getBlockContentTitle(): string |
|
71
|
|
|
{ |
|
72
|
|
|
return \__('Max-age:', 'graphql-api'); |
|
73
|
|
|
} |
|
74
|
|
|
/** |
|
75
|
|
|
* @param array<string, mixed> $attributes |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function getBlockContent(array $attributes, string $content): string |
|
78
|
|
|
{ |
|
79
|
|
|
$blockContentPlaceholder = <<<EOF |
|
80
|
|
|
<div class="%s"> |
|
81
|
|
|
%s |
|
82
|
|
|
</div> |
|
83
|
|
|
EOF; |
|
84
|
|
|
$cacheControlMaxAge = $attributes[self::ATTRIBUTE_NAME_CACHE_CONTROL_MAX_AGE] ?? null; |
|
85
|
|
|
if (is_null($cacheControlMaxAge) || $cacheControlMaxAge < 0) { |
|
86
|
|
|
$cacheControlMaxAgeText = sprintf( |
|
87
|
|
|
'<em>%s</em>', |
|
88
|
|
|
\__('(not set)', 'graphql-api') |
|
89
|
|
|
); |
|
90
|
|
|
} elseif ($cacheControlMaxAge === 0) { |
|
91
|
|
|
$cacheControlMaxAgeText = sprintf( |
|
92
|
|
|
\__('%s seconds (<code>no-store</code>)', 'graphql-api'), |
|
93
|
|
|
$cacheControlMaxAge |
|
94
|
|
|
); |
|
95
|
|
|
} else { |
|
96
|
|
|
$cacheControlMaxAgeText = sprintf( |
|
97
|
|
|
\__('%s seconds', 'graphql-api'), |
|
98
|
|
|
$cacheControlMaxAge |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
return sprintf( |
|
102
|
|
|
$blockContentPlaceholder, |
|
103
|
|
|
$this->getBlockClassName() . '__content', |
|
104
|
|
|
$cacheControlMaxAgeText |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|