1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\ConditionalOnEnvironment\Admin\Services\MenuPages; |
6
|
|
|
|
7
|
|
|
use GraphQLAPI\GraphQLAPI\ConditionalOnEnvironment\Admin\Services\MenuPages\MenuPageInterface; |
8
|
|
|
use PoP\Root\Services\AbstractAutomaticallyInstantiatedService; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Menu page |
12
|
|
|
*/ |
13
|
|
|
abstract class AbstractMenuPage extends AbstractAutomaticallyInstantiatedService implements MenuPageInterface |
14
|
|
|
{ |
15
|
|
|
protected ?string $hookName = null; |
16
|
|
|
|
17
|
|
|
public function setHookName(string $hookName): void |
18
|
|
|
{ |
19
|
|
|
$this->hookName = $hookName; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getHookName(): ?string |
23
|
|
|
{ |
24
|
|
|
return $this->hookName; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Initialize menu page. Function to override |
29
|
|
|
*/ |
30
|
|
|
public function initialize(): void |
31
|
|
|
{ |
32
|
|
|
\add_action( |
33
|
|
|
'admin_enqueue_scripts', |
34
|
|
|
[$this, 'maybeEnqueueAssets'] |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Maybe enqueue the required assets and initialize the localized scripts |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
public function maybeEnqueueAssets(): void |
44
|
|
|
{ |
45
|
|
|
// Enqueue assets if we are on that page |
46
|
|
|
if ($this->isCurrentScreen()) { |
47
|
|
|
$this->enqueueAssets(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
abstract public function getMenuName(): string; |
52
|
|
|
abstract public function getMenuPageSlug(): string; |
53
|
|
|
|
54
|
|
|
protected function isCurrentScreen(): bool |
55
|
|
|
{ |
56
|
|
|
// Check we are on the specific screen |
57
|
|
|
$currentScreen = \get_current_screen(); |
58
|
|
|
if (is_null($currentScreen)) { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
$screenID = $this->getScreenID(); |
62
|
|
|
// If it is the top level page, the current screen is prepended with "toplevel_page_" |
63
|
|
|
// If not, the current screen is prepended with the section name |
64
|
|
|
// Then, check that the screen ends with the requested screen ID |
65
|
|
|
return substr($currentScreen->id, -1 * strlen($screenID)) == $screenID; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getScreenID(): string |
69
|
|
|
{ |
70
|
|
|
return sprintf( |
71
|
|
|
'%s_%s', |
72
|
|
|
$this->getMenuName(), |
73
|
|
|
$this->getMenuPageSlug() |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Enqueue the required assets and initialize the localized scripts |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
protected function enqueueAssets(): void |
83
|
|
|
{ |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|