-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
AbstractPlugin.php
executable file
·512 lines (457 loc) · 15.2 KB
/
AbstractPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<?php
declare(strict_types=1);
namespace GatoGraphQL\GatoGraphQL\PluginSkeleton;
use GatoGraphQL\ExternalDependencyWrappers\Composer\Semver\SemverWrapper;
use GatoGraphQL\GatoGraphQL\Facades\Registries\CustomPostTypeRegistryFacade;
use GatoGraphQL\GatoGraphQL\Module;
use GatoGraphQL\GatoGraphQL\ModuleConfiguration;
use GatoGraphQL\GatoGraphQL\PluginSkeleton\PluginInfoInterface;
use GatoGraphQL\GatoGraphQL\Services\CustomPostTypes\CustomPostTypeInterface;
use PoP\Root\App;
use PoP\Root\Helpers\ClassHelpers;
use PoP\Root\Module\ModuleInterface;
abstract class AbstractPlugin implements PluginInterface
{
public const PLUGIN_VERSION_COMMIT_HASH_IDENTIFIER = '#';
protected ?PluginInfoInterface $pluginInfo = null;
protected string $pluginBaseName;
protected string $pluginSlug;
protected string $pluginName;
public function __construct(
protected string $pluginFile, /** The main plugin file */
protected string $pluginVersion,
?string $pluginName = null,
protected ?string $commitHash = null, /** Useful for development to regenerate the container when testing the generated plugin */
) {
$this->pluginBaseName = \plugin_basename($pluginFile);
$this->pluginSlug = dirname($this->pluginBaseName);
$this->pluginName = $pluginName ?? $this->pluginBaseName;
// Have the Plugin set its own info on the corresponding PluginInfo
$this->initializeInfo();
}
/**
* Plugin name
*/
public function getPluginName(): string
{
return $this->pluginName;
}
/**
* Plugin base name
*/
public function getPluginBaseName(): string
{
return $this->pluginBaseName;
}
/**
* Plugin slug
*/
public function getPluginSlug(): string
{
return $this->pluginSlug;
}
/**
* Plugin main file
*/
public function getPluginFile(): string
{
return $this->pluginFile;
}
/**
* Dependencies on other plugins, to regenerate the schema
* when these are activated/deactived
*
* @return string[]
*/
public function getDependentOnPluginFiles(): array
{
return [];
}
/**
* Plugin version
*/
public function getPluginVersion(): string
{
return $this->pluginVersion;
}
/**
* Commit hash when merging PR in repo, injected during the CI run
* when generating the .zip plugin.
*/
public function getCommitHash(): ?string
{
return $this->commitHash;
}
/**
* Plugin version + "#{commit hash}" (if it exists)
*/
public function getPluginVersionWithCommitHash(): string
{
return $this->pluginVersion . (!empty($this->commitHash) ? self::PLUGIN_VERSION_COMMIT_HASH_IDENTIFIER . $this->commitHash : '');
}
/**
* Plugin dir
*/
public function getPluginDir(): string
{
return \dirname($this->pluginFile);
}
/**
* Plugin URL
*/
public function getPluginURL(): string
{
return \plugin_dir_url($this->pluginFile);
}
/**
* PluginInfo class for the Plugin
*/
public function getInfo(): ?PluginInfoInterface
{
return $this->pluginInfo;
}
protected function initializeInfo(): void
{
$pluginInfoClass = $this->getPluginInfoClass();
if ($pluginInfoClass === null) {
return;
}
$this->pluginInfo = new $pluginInfoClass($this);
}
/**
* PluginInfo class for the Plugin
*
* @return class-string<PluginInfoInterface>|null
*/
protected function getPluginInfoClass(): ?string
{
$classNamespace = ClassHelpers::getClassPSR4Namespace(\get_called_class());
$pluginInfoClass = $classNamespace . '\\' . $this->getPluginInfoClassName();
if (!class_exists($pluginInfoClass)) {
return null;
}
/** @var class-string<PluginInfoInterface> */
return $pluginInfoClass;
}
/**
* PluginInfo class name for the Plugin
*/
abstract protected function getPluginInfoClassName(): ?string;
/**
* Plugin's initialization
*/
public function initialize(): void
{
$this->initializeModuleClasses();
}
/**
* Initialize Plugin's modules
*/
protected function initializeModuleClasses(): void
{
// Initialize the containers
$moduleClasses = $this->getModuleClassesToInitialize();
App::getAppLoader()->addModuleClassesToInitialize($moduleClasses);
}
/**
* After initialized, and before booting,
* allow the modules to inject their own configuration
*/
public function configureComponents(): void
{
// Set the plugin folder on the plugin's Module
$pluginFolder = dirname($this->pluginFile);
$this->getPluginModule()->setPluginFolder($pluginFolder);
}
/**
* Plugin's Module
*/
protected function getPluginModule(): PluginModuleInterface
{
/** @var PluginModuleInterface */
return App::getModule($this->getModuleClass());
}
/**
* Package's Module class, of type PluginModuleInterface.
* By standard, it is "NamespaceOwner\Project\Module::class"
*
* @phpstan-return class-string<ModuleInterface>
*/
protected function getModuleClass(): string
{
$classNamespace = ClassHelpers::getClassPSR4Namespace(\get_called_class());
/** @var class-string<ModuleInterface> */
return $classNamespace . '\\Module';
}
/**
* Add Module classes to be initialized
*
* @return array<class-string<ModuleInterface>> List of `Module` class to initialize
*/
abstract protected function getModuleClassesToInitialize(): array;
/**
* Plugin configuration
*/
public function configure(): void
{
/**
* Configure the plugin. This defines hooks to set
* environment variables, so must be executed before
* those hooks are triggered for first time
* (in ModuleConfiguration classes)
*/
$this->callPluginInitializationConfiguration();
/**
* Only after initializing the System Container,
* we can obtain the configuration (which may depend on hooks).
*/
$appLoader = App::getAppLoader();
$appLoader->addModuleClassConfiguration(
$this->getModuleClassConfiguration()
);
$appLoader->addSchemaModuleClassesToSkip($this->getSchemaModuleClassesToSkip());
}
/**
* Configure the plugin.
*/
abstract protected function callPluginInitializationConfiguration(): void;
/**
* Plugin's booting
*/
public function boot(): void
{
// Function to override
}
/**
* Add configuration for the Module classes
*
* @return array<class-string<ModuleInterface>,mixed> [key]: Module class, [value]: Configuration
*/
public function getModuleClassConfiguration(): array
{
return [];
}
/**
* Add schema Module classes to skip initializing
*
* @return array<class-string<ModuleInterface>> List of `Module` class which must not initialize their Schema services
*/
abstract protected function getSchemaModuleClassesToSkip(): array;
/**
* Remove the CPTs from the DB
*/
protected function unregisterPluginCustomPostTypes(): void
{
// Unregister all CPTs from this plugin
if ($customPostTypes = $this->getPluginCustomPostTypes()) {
foreach ($customPostTypes as $customPostType) {
$customPostType->unregisterCustomPostType();
}
// Clear the permalinks to remove the CPT's rules from the database
\flush_rewrite_rules();
}
}
/**
* Get the CPTs from this plugin
*
* @return CustomPostTypeInterface[]
*/
public function getPluginCustomPostTypes(): array
{
$customPostTypeRegistry = CustomPostTypeRegistryFacade::getInstance();
// Filter the ones that belong to this plugin
// Use $serviceDefinitionID for if the class is overriden
return array_values(array_filter(
$customPostTypeRegistry->getCustomPostTypes(),
fn (string $serviceDefinitionID) => str_starts_with(
$serviceDefinitionID,
$this->getPluginNamespace() . '\\'
),
ARRAY_FILTER_USE_KEY
));
}
/**
* The PSR-4 namespace, with format "Vendor\Project"
*/
public function getPluginNamespace(): string
{
return ClassHelpers::getClassPSR4Namespace(get_called_class());
}
/**
* Plugin set-up, executed immediately when loading the plugin.
*/
public function setup(): void
{
// Functions to execute when activating/deactivating the plugin
\register_deactivation_hook($this->getPluginFile(), $this->deactivate(...));
/**
* PoP depends on hook "init" to set-up the endpoint rewrite,
* as in function `addRewriteEndpoints` in `AbstractEndpointHandler`
* However, activating the plugin takes place AFTER hooks "plugins_loaded"
* and "init". Hence, the code cannot flush the rewrite_rules when the plugin
* is activated, and any non-default GraphQL endpoint is not set.
*
* The solution (hack) is to check if the plugin has just been installed,
* and then apply the logic, on every request in the admin!
*
* @see https://developer.wordpress.org/reference/functions/register_activation_hook/#process-flow
*/
// register_activation_hook($this->getPluginFile(), $this->activate(...));
}
/**
* Remove permalinks when deactivating the plugin
*
* @see https://developer.wordpress.org/plugins/plugin-basics/activation-deactivation-hooks/
*/
public function deactivate(): void
{
$this->removePluginVersion();
$this->unregisterPluginCustomPostTypes();
/**
* No need to invalidate the cache here anymore,
* since AbstractMainPlugin already invalidates it
* for ANY deactivated plugin.
*/
// $this->purgeContainer();
}
/**
* Remove the plugin or extension's version from the wp_options table
*/
private function removePluginVersion(): void
{
$pluginVersions = \get_option(PluginOptions::PLUGIN_VERSIONS, []);
unset($pluginVersions[$this->pluginBaseName]);
\update_option(PluginOptions::PLUGIN_VERSIONS, $pluginVersions);
}
/**
* Execute logic after the plugin/extension has just been activated.
*
* Notice that this will be executed when first time activated, or
* reactivated (i.e. activated => deactivated => activated).
*
* Then, when installing setup data, we must first check that the entry
* does not already exist. This will also avoid duplicating setup data
* when downgrading the plugin to a lower version, and then upgrading
* again.
*/
public function pluginJustActivated(): void
{
/**
* Taxonomies are registered on "init", hence must insert
* data only after that.
*
* @see layers/GatoGraphQLForWP/plugins/gatographql/src/Services/Taxonomies/AbstractTaxonomy.php
*/
\add_action(
'init',
function (): void {
$this->maybeInstallPluginSetupData();
},
$this->getInstallPluginSetupDataInitHookPriority()
);
}
/**
* Allow functionality to be executed after
*/
protected function getInstallPluginSetupDataInitHookPriority(): int
{
return PHP_INT_MAX - 10;
}
/**
* Execute logic after the plugin/extension has just been updated
*/
public function pluginJustUpdated(string $newVersion, string $previousVersion): void
{
/**
* Taxonomies are registered on "init", hence must insert
* data only after that.
*
* @see layers/GatoGraphQLForWP/plugins/gatographql/src/Services/Taxonomies/AbstractTaxonomy.php
*/
\add_action(
'init',
function () use ($previousVersion): void {
// The version could contain the hash commit. Remove it!
$commitHashPos = strpos($previousVersion, self::PLUGIN_VERSION_COMMIT_HASH_IDENTIFIER);
if ($commitHashPos !== false) {
$previousVersion = substr($previousVersion, 0, $commitHashPos);
}
$this->maybeInstallPluginSetupData($previousVersion);
},
$this->getInstallPluginSetupDataInitHookPriority()
);
}
/**
* Install the initial plugin data
*/
protected function maybeInstallPluginSetupData(?string $previousVersion = null): void
{
/** @var ModuleConfiguration */
$moduleConfiguration = App::getModule(Module::class)->getConfiguration();
if (!$moduleConfiguration->installPluginSetupData()) {
return;
}
/**
* Use a transient to make sure that only one instance
* will install the data. Otherwise, two WP REST API
* requests happening simultaneously might both execute
* this logic
*/
$transientName = 'gatographql-installing-plugin-setup-data';
$transient = \get_transient($transientName);
if ($transient !== false) {
// Another instance is executing this code right now
return;
}
\set_transient($transientName, true, 30);
$this->installPluginSetupData($previousVersion);
\delete_transient($transientName);
}
/**
* Method to override.
*
* Provide the installation in stages, version by version, to
* be able to execute it both when installing/activating the plugin,
* or updating it to a new version with setup data.
*
* The plugin's setup data will be installed if:
*
* - $previousVersion = null => Activating the plugin
* - $previousVersion < someVersion => Updating to a new version that has data to install
*/
protected function installPluginSetupData(?string $previousVersion = null): void
{
$versionCallbacks = $this->getPluginSetupDataVersionCallbacks();
foreach ($versionCallbacks as $version => $callbacks) {
if ($previousVersion !== null && SemverWrapper::satisfies($previousVersion, '>= ' . $version)) {
continue;
}
foreach ($callbacks as $callback) {
$callback();
}
}
}
/**
* Method to override.
*
* Retrieve the callback functions to execute for every version
* of the main plugin, to install setup data.
*
* @return array<string,callable[]>
*/
protected function getPluginSetupDataVersionCallbacks(): array
{
return [];
}
/**
* @param array<array<string,mixed>> $blockDataItems
* @return array<array<string,mixed>>
*/
protected function addInnerContentToBlockAtts(array $blockDataItems): array
{
return array_map(
fn (array $blockDataItem) => [...$blockDataItem, 'innerContent' => []],
$blockDataItems
);
}
}