-
Notifications
You must be signed in to change notification settings - Fork 0
/
Context.php
96 lines (85 loc) · 2.01 KB
/
Context.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
<?php //phpcs:disable PHPCompatibility.Variables.ForbiddenThisUseContexts
/**
* Context enum file.
*
* @package
* @subpackage Enum
*/
namespace XWP\Contracts\Hook;
/**
* Determines the current WP execution context.
*/
enum Context: int implements Context_Interface {
/**
* Frontend context.
*/
case Frontend = 1;
/**
* Admin context.
*/
case Admin = 2;
/**
* AJAX context.
*/
case Ajax = 4;
/**
* Cron context.
*/
case Cron = 8;
/**
* REST API context.
*/
case REST = 16;
/**
* WP CLI context.
*/
case CLI = 32;
/**
* Global context.
*/
case Global = 63;
/**
* Format the context for display.
*
* @return string
*/
public function display_name(): string {
return match ( $this ) {
self::Frontend => 'Frontend',
self::Admin => 'Admin',
self::Ajax => 'Ajax',
self::Cron => 'Cron',
self::REST => 'REST',
self::CLI => 'CLI',
self::Global => 'Global',
};
}
/**
* Create a context from a slug.
*
* @param string $ctx_slug The context slug.
* @return Context
*/
public static function fromSlug( string $ctx_slug ): Context {
return match ( $ctx_slug ) {
'front', 'frontend',
'public', 'website' => self::Frontend,
'admin', 'dashboard',
'administration' => self::Admin,
'ajax' => self::Ajax,
'cron' => self::Cron,
'rest' => self::REST,
'cli' => self::CLI,
'global' => self::Global,
};
}
/**
* Check if the current context is valid.
*
* @param int $ctx The context to check.
* @return bool
*/
public function is_valid( int $ctx ): bool {
return 0 !== ( $this->value & $ctx );
}
}