1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class for WordPoints apps. |
5
|
|
|
* |
6
|
|
|
* @package wordpoints-hooks-api |
7
|
|
|
* @since 1.0.0 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* An app for WordPoints. |
12
|
|
|
* |
13
|
|
|
* Apps are self-contained APIs that can include sub-apps. |
14
|
|
|
* |
15
|
|
|
* The sub-apps are not required to be instances of WordPoints_App themselves, they |
16
|
|
|
* can be any sort of object. |
17
|
|
|
* |
18
|
|
|
* @since 1.0.0 |
19
|
|
|
*/ |
20
|
|
|
class WordPoints_App { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The main app. |
24
|
|
|
* |
25
|
|
|
* @since 1.0.0 |
26
|
|
|
* |
27
|
|
|
* @var WordPoints_App |
28
|
|
|
*/ |
29
|
|
|
public static $main; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The slug of this app. |
33
|
|
|
* |
34
|
|
|
* @since 1.0.0 |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $slug; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The full slug of this app, prefixed with the slug of the parent app. |
42
|
|
|
* |
43
|
|
|
* @since 1.0.0 |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $full_slug; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* A registry for child apps. |
51
|
|
|
* |
52
|
|
|
* @since 1.0.0 |
53
|
|
|
* |
54
|
|
|
* @var WordPoints_Class_Registry_Persistent |
55
|
|
|
*/ |
56
|
|
|
protected $sub_apps; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The parent of this app, if this is a sub-app. |
60
|
|
|
* |
61
|
|
|
* @since 1.0.0 |
62
|
|
|
* |
63
|
|
|
* @var WordPoints_App |
64
|
|
|
*/ |
65
|
|
|
protected $parent; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Whether to skip calling an action when each registry is initialized. |
69
|
|
|
* |
70
|
|
|
* @since 1.0.0 |
71
|
|
|
* |
72
|
|
|
* @var bool |
73
|
|
|
*/ |
74
|
|
|
protected $silent = false; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Keeps track of which registries have been initialized. |
78
|
|
|
* |
79
|
|
|
* @since 1.0.0 |
80
|
|
|
* |
81
|
|
|
* @var bool[] |
82
|
|
|
*/ |
83
|
|
|
protected $did_init = array(); |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Whether properties are allowed to be set. |
87
|
|
|
* |
88
|
|
|
* @since 1.0.0 |
89
|
|
|
* |
90
|
|
|
* @var bool |
91
|
|
|
*/ |
92
|
|
|
protected $allow_set = false; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @since 1.0.0 |
96
|
|
|
*/ |
97
|
|
|
public function __construct( $slug, $parent = null ) { |
98
|
|
|
|
99
|
|
|
$this->slug = $slug; |
100
|
|
|
$this->full_slug = $slug; |
101
|
|
|
|
102
|
|
|
if ( $parent instanceof WordPoints_App ) { |
103
|
|
|
$this->parent = $parent; |
104
|
|
|
|
105
|
|
|
if ( 'apps' !== $this->parent->full_slug ) { |
106
|
|
|
$this->full_slug = $this->parent->full_slug . '-' . $this->full_slug; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->sub_apps = new WordPoints_Class_Registry_Persistent(); |
111
|
|
|
|
112
|
|
|
$this->init(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get a sub app. |
117
|
|
|
* |
118
|
|
|
* @since 1.0.0 |
119
|
|
|
* |
120
|
|
|
* @param string $slug The slug of the sub-app to get. |
121
|
|
|
* |
122
|
|
|
* @return null|object|WordPoints_App|WordPoints_Class_RegistryI|WordPoints_Class_Registry_ChildrenI The sub app, or null if not found. |
123
|
|
|
*/ |
124
|
|
|
public function get_sub_app( $slug ) { |
125
|
|
|
|
126
|
|
|
$sub_app = $this->sub_apps->get( $slug, array( $this ) ); |
127
|
|
|
|
128
|
|
|
if ( ! $sub_app ) { |
129
|
|
|
return null; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ( |
133
|
|
|
empty( $this->did_init[ $slug ] ) |
134
|
|
|
&& ! self::$main->silent |
135
|
|
|
&& $this->should_do_registry_init( $sub_app ) |
136
|
|
|
) { |
137
|
|
|
|
138
|
|
|
$this->did_init[ $slug ] = true; |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Initialization of an app registry. |
142
|
|
|
* |
143
|
|
|
* The $var is the slug of the registry. |
144
|
|
|
* |
145
|
|
|
* @since 1.0.0 |
146
|
|
|
* |
147
|
|
|
* @param WordPoints_Class_RegistryI|WordPoints_Class_Registry_ChildrenI |
148
|
|
|
* $registry The registry object. |
149
|
|
|
*/ |
150
|
|
|
do_action( "wordpoints_init_app_registry-{$this->full_slug}-{$slug}", $sub_app ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $sub_app; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get the sub apps registry. |
158
|
|
|
* |
159
|
|
|
* @since 1.0.0 |
160
|
|
|
* |
161
|
|
|
* @return WordPoints_Class_Registry_Persistent The sub apps registry. |
162
|
|
|
*/ |
163
|
|
|
public function sub_apps() { |
164
|
|
|
return $this->sub_apps; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Check whether to call the init action for a registry sub-app. |
169
|
|
|
* |
170
|
|
|
* @since 1.0.0 |
171
|
|
|
* |
172
|
|
|
* @param object $registry The sub-app object. |
173
|
|
|
* |
174
|
|
|
* @return bool Whether to call the init action or not. |
175
|
|
|
*/ |
176
|
|
|
protected function should_do_registry_init( $registry ) { |
177
|
|
|
return ( |
178
|
|
|
$registry instanceof WordPoints_Class_RegistryI |
179
|
|
|
|| $registry instanceof WordPoints_Class_Registry_ChildrenI |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Initialize this app. |
185
|
|
|
* |
186
|
|
|
* @since 1.0.0 |
187
|
|
|
*/ |
188
|
|
|
protected function init() { |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* WordPoints app initialized. |
192
|
|
|
* |
193
|
|
|
* The dynamic portion of the action is the slug of the app being |
194
|
|
|
* initialized. |
195
|
|
|
* |
196
|
|
|
* @since 1.0.0 |
197
|
|
|
* |
198
|
|
|
* @param WordPoints_App $app The app object. |
199
|
|
|
*/ |
200
|
|
|
do_action( "wordpoints_init_app-{$this->full_slug}", $this ); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// EOF |
205
|
|
|
|