/
AdminPageFramework_Router.php
355 lines (303 loc) · 13.9 KB
/
AdminPageFramework_Router.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
<?php
/**
* Admin Page Framework
*
* http://en.michaeluno.jp/admin-page-framework/
* Copyright (c) 2013-2017 Michael Uno; Licensed MIT
*
*/
/**
* Deals with redirecting function calls and instantiating classes.
*
* @abstract
* @since 3.0.0
* @since 3.3.1 Changed from `AdminPageFramework_Base`.
* @package AdminPageFramework/Factory/AdminPage
* @internal
* @method _renderSectionDescription( $sMethodName ) defined in AdminPageFramework_Setting
* @method _renderSettingField( $_mFirstArg, $_sPageSlug ) defined in AdminPageFramework_Setting
* @method load()
*/
abstract class AdminPageFramework_Router extends AdminPageFramework_Factory {
/**'
* Sets up hooks and properties.
*
* @since 3.3.0
*/
public function __construct( $sOptionKey=null, $sCallerPath=null, $sCapability='manage_options', $sTextDomain='admin-page-framework' ) {
$_sProprtyClassName = isset( $this->aSubClassNames[ 'oProp' ] )
? $this->aSubClassNames[ 'oProp' ]
: 'AdminPageFramework_Property_' . $this->_sStructureType;
$this->oProp = new $_sProprtyClassName(
$this,
$sCallerPath,
get_class( $this ),
$sOptionKey,
$sCapability,
$sTextDomain
);
parent::__construct( $this->oProp );
// @deprecated 3.8.14
// if ( $this->oProp->bIsAdminAjax ) {
// return;
// }
if ( ! $this->oProp->bIsAdmin ) {
return;
}
add_action( 'wp_loaded', array( $this, '_replyToDetermineToLoad' ) );
add_action( 'set_up_' . $this->oProp->sClassName, array( $this, '_replyToLoadComponentsForAjax' ), 100 );
}
/**
* Loads page components for ajax calls.
*
* @since 3.8.14
* @remark It is assumed that the `setUp()` method is already called.
* @callback add_action wp_loaded
*/
public function _replyToLoadComponentsForAjax() {
if ( ! $this->oProp->bIsAdminAjax ) {
return;
}
new AdminPageFramework_Model_Menu__RegisterMenu( $this, 'pseudo_admin_menu' );
do_action( 'pseudo_admin_menu', '' );
do_action( 'pseudo_current_screen' );
}
/**
* Instantiates a link object based on the type.
*
* @since 3.7.10
* @internal
* @return null|object
*/
protected function _getLinkObject() {
$_sClassName = $this->aSubClassNames[ 'oLink' ];
return new $_sClassName( $this->oProp, $this->oMsg );
}
/**
* Instantiates a link object based on the type.
*
* @since 3.7.10
* @internal
* @return null|object
*/
protected function _getPageLoadObject() {
$_sClassName = $this->aSubClassNames[ 'oPageLoadInfo' ];
return new $_sClassName( $this->oProp, $this->oMsg );
}
/**
* Handles undefined function calls.
*
* This method redirects callback-function calls with the pre-defined prefixes for hooks to the appropriate methods.
*
* @access public
* @remark the users do not need to call or extend this method unless they know what they are doing.
* @param string the called method name.
* @param array the argument array. The first element holds the parameters passed to the called method.
* @return mixed depends on the called method. If the method name matches one of the hook prefixes, the redirected methods return value will be returned. Otherwise, none.
* @since 2.0.0
* @since 3.3.1 Moved from `AdminPageFramework_Base`.
* @internal
*/
public function __call( $sMethodName, $aArgs=null ) {
$_sPageSlug = $this->oProp->getCurrentPageSlug();
$_sTabSlug = $this->oProp->getCurrentTabSlug( $_sPageSlug );
$_mFirstArg = $this->oUtil->getElement( $aArgs, 0 );
$_aKnownMethodPrefixes = array(
'section_pre_',
'field_pre_',
'load_pre_',
);
switch( $this->_getCallbackName( $sMethodName, $_sPageSlug, $_aKnownMethodPrefixes ) ) {
// add_settings_section() callback
case 'section_pre_':
return $this->_renderSectionDescription( $sMethodName ); // defined in AdminPageFramework_Setting
// add_settings_field() callback
case 'field_pre_':
return $this->_renderSettingField( $_mFirstArg, $_sPageSlug ); // defined in AdminPageFramework_Setting
// load-{page} callback
case 'load_pre_':
return $this->_doPageLoadCall( $sMethodName, $_sPageSlug, $_sTabSlug, $_mFirstArg );
default:
return parent::__call( $sMethodName, $aArgs );
}
}
/**
* Attempts to find the factory class callback method for the given method name.
*
* @since 3.5.3
* @return string The found callback method name or the prefix of a known callback method name. An empty string if not found.
* @internal
*/
private function _getCallbackName( $sMethodName, $sPageSlug, array $aKnownMethodPrefixes=array() ) {
foreach( $aKnownMethodPrefixes as $_sMethodPrefix ) {
if ( $this->oUtil->hasPrefix( $_sMethodPrefix, $sMethodName ) ) {
return $_sMethodPrefix;
}
}
return '';
}
/**
* Redirects the callback of the load-{page} action hook to the framework's callback.
*
* @since 2.1.0
* @since 3.3.1 Moved from `AdminPageFramework_Base`.
* @since 3.5.3 Added the $sMethodName parameter.
*
* @access protected
* @internal
* @remark This method will be triggered before the header gets sent.
* @return void
* @internal
*/
protected function _doPageLoadCall( $sMethodName, $sPageSlug, $sTabSlug, $oScreen ) {
if ( ! $this->_isPageLoadCall( $sMethodName, $sPageSlug, $oScreen ) ) {
return;
}
// [3.4.6+] Set the page and tab slugs to the default form section so that added form fields without a section will appear in different pages and tabs.
$this->_setPageAndTabSlugsForForm( $sPageSlug, $sTabSlug );
$this->_setShowDebugInfoProperty( $sPageSlug ); // 3.8.8+
// Do actions in this order, class -> page -> in-page tab. This order is important as some methods rely on it.
$this->_load(
array(
"load_{$this->oProp->sClassName}",
"load_{$sPageSlug}",
)
);
// * Note that the in-page tabs handling method `_replyToFinalizeInPageTabs()` is called in the above action hook.
// Re-retrieve the current tab slug as in-page tabs may be added during the above `load_{...}` hooks.
// Note that if the tab is the first item and the user arrives the page by clicking on the sidebar menu,
// the tab slug will be empty unless an in-page tab is added.
$sTabSlug = $this->oProp->getCurrentTabSlug( $sPageSlug );
if ( strlen( $sTabSlug ) ) {
$this->_setShowDebugInfoProperty( $sPageSlug, $sTabSlug ); // 3.8.8+
$this->oUtil->addAndDoActions(
$this, // the caller object
array( "load_{$sPageSlug}_" . $sTabSlug ),
$this // the admin page object - this lets third-party scripts use the framework methods.
);
}
$this->oUtil->addAndDoActions(
$this, // the caller object
array(
"load_after_{$this->oProp->sClassName}",
"load_after_{$sPageSlug}", // 3.6.3+
),
$this // the admin page object - this lets third-party scripts use the framework methods.
);
}
/**
* Updates the `bShowDebugInfo` property based on the current page and in-page-tab arguments.
*
* If the `$sTabSlug` parameter is not set, it is considered for the current page. Otherwise, it is for the current tab.
*
* @remark This must be called before calling the `load()` method as which page to load is already determined at this point.
* And if the user wants to modify the property value manually, they can do so in the `load()` method.
* @since 3.8.8
* @return void
*/
private function _setShowDebugInfoProperty( $sPageSlug, $sTabSlug='' ) {
// For the page,
if ( ! strlen( $sTabSlug ) ) {
$this->oProp->bShowDebugInfo = $this->oUtil->getElement(
$this->oProp->aPages,
array( $sPageSlug, 'show_debug_info' ),
$this->oProp->bShowDebugInfo
);
return;
}
// For the in-page tab.
$this->oProp->bShowDebugInfo = $this->oUtil->getElement(
$this->oProp->aInPageTabs,
array( $sPageSlug, $sTabSlug, 'show_debug_info' ),
$this->oProp->bShowDebugInfo
);
}
/**
* Sets the page and tab slugs to the default form section
* so that added form fields without a section will appear in different pages and tabs.
*
* @internal
* @since 3.8.8
* @todo The `oForm` object will get instantiated even the user does not use a form.
* So look for a way to avoid calling `$oForm` unless the user uses a form.
*/
private function _setPageAndTabSlugsForForm( $sPageSlug, $sTabSlug ) {
$this->oForm->aSections[ '_default' ][ 'page_slug' ] = $sPageSlug ? $sPageSlug : null;
$this->oForm->aSections[ '_default' ][ 'tab_slug' ] = $sTabSlug ? $sTabSlug : null;
}
/**
* Determines whether the function call is of a page load.
* @since 3.5.3
* @internal
* @return boolean True if it is a page load call; othwrwise, false.
* @param string $sMethodName The undefined method name that is passed to the __call() overload method.
* @param string $sPageSlug The currently loading page slug.
* @param object|string $osScreenORPageHook The screen ID that the WordPress screen object gives.
*/
private function _isPageLoadCall( $sMethodName, $sPageSlug, $osScreenORPageHook ) {
if ( substr( $sMethodName, strlen( 'load_pre_' ) ) !== $sPageSlug ) {
return false;
}
if ( ! isset( $this->oProp->aPageHooks[ $sPageSlug ] ) ) {
return false;
}
$_sPageHook = is_object( $osScreenORPageHook )
? $osScreenORPageHook->id
: $sPageSlug; // for ajax calls
return $_sPageHook === $this->oProp->aPageHooks[ $sPageSlug ];
}
/* Shared methods */
/**
* Checks whether the class should be instantiated.
*
* @since 3.1.0
* @since 3.3.1 Moved from `AdminPageFramework_Base`.
* @internal
*/
protected function _isInstantiatable() {
// @deprecated 3.8.14
// Disable in admin-ajax.php
// if ( isset( $GLOBALS[ 'pagenow' ] ) && 'admin-ajax.php' === $GLOBALS[ 'pagenow' ] ) {
// return false;
// }
// Nothing to do in the network admin area.
return ! is_network_admin();
}
/**
* Checks whether the currently loading page is of the given pages.
*
* @since 3.0.2
* @since 3.2.0 Changed the scope to `public` from `protected` as the head tag object will access it.
* @since 3.3.1 Moved from `AdminPageFramework_Base`.
* @since 3.8.14 Changed the visibility scope to `protected` from `public` as there is the `isInThePage()` public method.
* @internal
*/
protected function _isInThePage() {
// 3.8.14+
if ( $this->oProp->bIsAdminAjax ) {
return true;
}
// If the setUp method is not loaded yet,
if ( ! did_action( 'set_up_' . $this->oProp->sClassName ) ) {
return true;
}
if ( ! isset( $_GET[ 'page' ] ) ) {
return false;
}
return $this->oProp->isPageAdded();
}
/**
* Determines whether the class component classes should be instantiated or not.
*
* @internal
* @callback action current_screen
* @return void
* @since 3.7.0
*/
public function _replyToLoadComponents( /* $oScreen */ ) {
if ( 'plugins.php' === $this->oProp->sPageNow ) {
$this->oLink = $this->_replyTpSetAndGetInstance_oLink();
}
parent::_replyToLoadComponents();
}
}