|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\EditorScripts; |
|
6
|
|
|
|
|
7
|
|
|
use GraphQLAPI\GraphQLAPI\General\EditorHelpers; |
|
8
|
|
|
use GraphQLAPI\GraphQLAPI\Scripts\AbstractScript; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Base class for a Gutenberg script. |
|
12
|
|
|
* The JS/CSS assets for each block is contained in folder {pluginDir}/editor-scripts/{scriptName}, and follows |
|
13
|
|
|
* the architecture from @wordpress/create-block package |
|
14
|
|
|
* |
|
15
|
|
|
* @see https://developer.wordpress.org/block-editor/packages/packages-create-block/ |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class AbstractEditorScript extends AbstractScript |
|
18
|
|
|
{ |
|
19
|
|
|
use HasDocumentationScriptTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Pass localized data to the block |
|
23
|
|
|
* |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected function getLocalizedData(): array |
|
27
|
|
|
{ |
|
28
|
|
|
return array_merge( |
|
29
|
|
|
parent::getLocalizedData(), |
|
30
|
|
|
$this->getDocsLocalizedData() |
|
31
|
|
|
); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* URL to the script |
|
36
|
|
|
* |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function getScriptDirURL(): string |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->getPluginURL() . '/editor-scripts/' . $this->getScriptName() . '/'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Docs are bundled as chunks by webpack, and loaded lazily |
|
46
|
|
|
* The `publicPath` property for `config.output` must be provided |
|
47
|
|
|
* pointing to the generated chunks folder, otherwise it is |
|
48
|
|
|
* by default resolved as /wp-admin/..., producing a 404. |
|
49
|
|
|
* |
|
50
|
|
|
* The public path will be set under global variable `__webpack_public_path__` in JS |
|
51
|
|
|
* |
|
52
|
|
|
* @see https://v4.webpack.js.org/guides/public-path/#on-the-fly |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function getScriptPublicPath(): string |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->getScriptDirURL() . 'build/'; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Where is the script stored |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function getScriptDir(): string |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->getPluginDir() . '/editor-scripts/' . $this->getScriptName(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Post types for which to register the script |
|
71
|
|
|
* |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function getAllowedPostTypes(): array |
|
75
|
|
|
{ |
|
76
|
|
|
return []; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Registers all script assets |
|
81
|
|
|
*/ |
|
82
|
|
|
public function initScript(): void |
|
83
|
|
|
{ |
|
84
|
|
|
/** |
|
85
|
|
|
* Maybe only load the script when creating/editing for some CPT only |
|
86
|
|
|
*/ |
|
87
|
|
|
if (\is_admin()) { |
|
88
|
|
|
if ($postTypes = $this->getAllowedPostTypes()) { |
|
89
|
|
|
if (!in_array(EditorHelpers::getEditingPostType(), $postTypes)) { |
|
90
|
|
|
return; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
parent::initScript(); |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Register the documentation (from under folder "docs/"), for the locale and the default language |
|
99
|
|
|
* IMPORTANT: Uncomment for webpack v5, to not duplicate the content of the docs inside build/index.js |
|
100
|
|
|
*/ |
|
101
|
|
|
// $this->initDocumentationScripts(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Register the documentation (from under folder "docs/"), for the locale and the default language |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function initDocumentationScripts(): void |
|
108
|
|
|
{ |
|
109
|
|
|
$dir = $this->getScriptDir(); |
|
110
|
|
|
$scriptName = $this->getScriptName(); |
|
111
|
|
|
$script_asset_path = "$dir/build/index.asset.php"; |
|
112
|
|
|
$url = $this->getScriptDirURL(); |
|
113
|
|
|
$script_asset = require($script_asset_path); |
|
114
|
|
|
|
|
115
|
|
|
$this->registerDocumentationScripts($scriptName, $url, $script_asset['dependencies'], $script_asset['version']); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|