1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Admin Page Framework |
4
|
|
|
* |
5
|
|
|
* http://en.michaeluno.jp/admin-page-framework/ |
6
|
|
|
* Copyright (c) 2013-2015 Michael Uno; Licensed MIT |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Provides methods to build forms. |
12
|
|
|
* |
13
|
|
|
* This is a delegation class of `AdminPageFramework_Form_View`. |
14
|
|
|
* |
15
|
|
|
* @package AdminPageFramework |
16
|
|
|
* @subpackage Form |
17
|
|
|
* @since DEVVER |
18
|
|
|
*/ |
19
|
|
|
class AdminPageFramework_Form_View__Resource extends AdminPageFramework_WPUtility { |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Stores the form object to let this class access the resource array. |
23
|
|
|
*/ |
24
|
|
|
public $oForm; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Sets up hooks. |
28
|
|
|
* @since DEVVER |
29
|
|
|
*/ |
30
|
|
|
public function __construct( $oForm ) { |
31
|
|
|
|
32
|
|
|
$this->oForm = $oForm; |
33
|
|
|
|
34
|
|
|
// If it is loaded in the background, no need to load scripts and styles. |
35
|
|
|
if ( $this->isDoingAjax() ) { |
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->_setHooks(); |
40
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @since DEVVER |
45
|
|
|
*/ |
46
|
|
|
private function _setHooks() { |
|
|
|
|
47
|
|
|
|
48
|
|
|
if ( is_admin() ) { |
49
|
|
|
$this->_setAdminHooks(); |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Hook the admin header to insert custom admin stylesheets and scripts. |
54
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, '_replyToEnqueueScripts' ) ); |
55
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, '_replyToEnqueueStyles' ) ); |
56
|
|
|
|
57
|
|
|
/// A low priority is required to let dependencies loaded fast especially in customizer.php. |
58
|
|
|
add_action( did_action( 'wp_print_styles' ) ? 'wp_print_footer_scripts' : 'wp_print_styles', array( $this, '_replyToAddStyle' ), 999 ); |
59
|
|
|
add_action( did_action( 'wp_print_scripts' ) ? 'wp_print_footer_scripts' : 'wp_print_scripts', array( $this, '_replyToAddScript' ), 999 ); |
60
|
|
|
|
61
|
|
|
// Take care of items that could not be added in the head tag. |
62
|
|
|
|
63
|
|
|
/// For admin pages other than wp-admin/customizer.php |
64
|
|
|
add_action( 'wp_footer', array( $this, '_replyToEnqueueScripts' ) ); |
65
|
|
|
add_action( 'wp_footer', array( $this, '_replyToEnqueueStyles' ) ); |
66
|
|
|
|
67
|
|
|
/// For all admin pages. |
68
|
|
|
add_action( 'wp_print_footer_scripts', array( $this, '_replyToAddStyle' ), 999 ); |
69
|
|
|
add_action( 'wp_print_footer_scripts', array( $this, '_replyToAddScript' ), 999 ); |
70
|
|
|
|
71
|
|
|
// Required scripts in the head tag. |
72
|
|
|
add_action( 'wp_head', array( $this, '_replyToInsertRequiredInlineScripts' ) ); |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
private function _setAdminHooks() { |
|
|
|
|
76
|
|
|
|
77
|
|
|
// Hook the admin header to insert custom admin stylesheets and scripts. |
78
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, '_replyToEnqueueScripts' ) ); |
79
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, '_replyToEnqueueStyles' ) ); |
80
|
|
|
|
81
|
|
|
add_action( did_action( 'admin_print_styles' ) ? 'admin_print_footer_scripts' : 'admin_print_styles', array( $this, '_replyToAddStyle' ), 999 ); |
82
|
|
|
add_action( did_action( 'admin_print_scripts' ) ? 'admin_print_footer_scripts' : 'admin_print_scripts', array( $this, '_replyToAddScript' ), 999 ); |
83
|
|
|
|
84
|
|
|
// Take care of items that could not be added in the head tag. |
85
|
|
|
/// For wp-admin/customizer.php |
86
|
|
|
add_action( 'customize_controls_print_footer_scripts', array( $this, '_replyToEnqueueScripts' ) ); |
87
|
|
|
add_action( 'customize_controls_print_footer_scripts', array( $this, '_replyToEnqueueStyles' ) ); |
88
|
|
|
|
89
|
|
|
/// For admin pages other than wp-admin/customizer.php |
90
|
|
|
add_action( 'admin_footer', array( $this, '_replyToEnqueueScripts' ) ); |
91
|
|
|
add_action( 'admin_footer', array( $this, '_replyToEnqueueStyles' ) ); |
92
|
|
|
|
93
|
|
|
/// For all admin pages. |
94
|
|
|
add_action( 'admin_print_footer_scripts', array( $this, '_replyToAddStyle' ), 999 ); |
95
|
|
|
add_action( 'admin_print_footer_scripts', array( $this, '_replyToAddScript' ), 999 ); |
96
|
|
|
|
97
|
|
|
// Required scripts in the head tag. |
98
|
|
|
add_action( 'admin_head', array( $this, '_replyToInsertRequiredInlineScripts' ) ); |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
/** |
102
|
|
|
* Inserts JavaScript scripts whihc must be inserted head. |
103
|
|
|
* @since DEVVER |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function _replyToInsertRequiredInlineScripts() { |
|
|
|
|
107
|
|
|
|
108
|
|
|
// Ensure to load only once per page load |
109
|
|
|
if ( self::$_bLoaded ) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
self::$_bLoaded = true; |
113
|
|
|
|
114
|
|
|
echo "<script type='text/javascript' class='admin-page-framework-form-script-required-in-head'>" |
115
|
|
|
. '/* <![CDATA[ */' |
116
|
|
|
. $this->_getScripts_RequiredInHead() |
117
|
|
|
. '/* ]]> */' |
118
|
|
|
. "</script>"; |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
static private $_bLoaded = false; |
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @since DEVVER |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
private function _getScripts_RequiredInHead() { |
|
|
|
|
128
|
|
|
return 'document.write( "<style class=\'admin-page-framework-js-embedded-inline-style\'>' |
129
|
|
|
. esc_js( $this->_getInlineCSS() ) |
130
|
|
|
. '</style>" );'; |
131
|
|
|
} |
132
|
|
|
/** |
133
|
|
|
* @return string |
134
|
|
|
* @since DEVVER |
135
|
|
|
*/ |
136
|
|
|
private function _getInlineCSS() { |
|
|
|
|
137
|
|
|
$_oLoadingCSS = new AdminPageFramework_Form_View___CSS_Loading; |
138
|
|
|
$_oLoadingCSS->add( $this->_getScriptElementConcealerCSSRules() ); |
139
|
|
|
return $_oLoadingCSS->get(); |
140
|
|
|
} |
141
|
|
|
/** |
142
|
|
|
* Hides the form initially to prevent unformatted layouts being displayed during document load. |
143
|
|
|
* @remark Use visibility to reserve the element area in the screen. |
144
|
|
|
* @return string |
145
|
|
|
* @since DEVVER |
146
|
|
|
*/ |
147
|
|
|
private function _getScriptElementConcealerCSSRules() { |
|
|
|
|
148
|
|
|
return ".admin-page-framework-form-js-on { visibility: hidden; }"; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Enqueues page script resources. |
153
|
|
|
* |
154
|
|
|
* @since DEVVER |
155
|
|
|
*/ |
156
|
|
|
public function _replyToEnqueueScripts() { |
|
|
|
|
157
|
|
|
if ( ! $this->oForm->isInThePage() ) { |
158
|
|
|
return; |
159
|
|
|
} |
160
|
|
|
foreach( $this->oForm->getResources( 'src_scripts' ) as $_asEnqueue ) { |
161
|
|
|
$this->_enqueueScript( $_asEnqueue ); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
/** |
165
|
|
|
* Stores flags of enqueued items. |
166
|
|
|
* @since DEVVER |
167
|
|
|
*/ |
168
|
|
|
static private $_aEnqueued = array(); |
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return void |
171
|
|
|
* @since DEVVER |
172
|
|
|
*/ |
173
|
|
|
private function _enqueueScript( $asEnqueue ) { |
|
|
|
|
174
|
|
|
|
175
|
|
|
$_aEnqueueItem = $this->_getFormattedEnqueueScript( $asEnqueue ); |
176
|
|
|
|
177
|
|
|
// Do not load the same items multiple times. |
178
|
|
|
if ( isset( self::$_aEnqueued[ $_aEnqueueItem[ 'src' ] ] ) ) { |
179
|
|
|
return; |
180
|
|
|
} |
181
|
|
|
self::$_aEnqueued[ $_aEnqueueItem[ 'src' ] ] = $_aEnqueueItem; |
182
|
|
|
|
183
|
|
|
wp_enqueue_script( |
184
|
|
|
$_aEnqueueItem[ 'handle_id' ], |
185
|
|
|
$_aEnqueueItem[ 'src' ], |
186
|
|
|
$_aEnqueueItem[ 'dependencies' ], |
187
|
|
|
$_aEnqueueItem[ 'version' ], |
188
|
|
|
did_action( 'admin_body_class' ) |
189
|
|
|
? true |
190
|
|
|
: $_aEnqueueItem[ 'in_footer' ] |
191
|
|
|
); |
192
|
|
|
if ( $_aEnqueueItem[ 'translation' ] ) { |
193
|
|
|
wp_localize_script( |
194
|
|
|
$_aEnqueueItem[ 'handle_id' ], |
195
|
|
|
$_aEnqueueItem[ 'handle_id' ], |
196
|
|
|
$_aEnqueueItem[ 'translation' ] |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
} |
201
|
|
|
/** |
202
|
|
|
* @return array |
203
|
|
|
* @since DEVVER |
204
|
|
|
*/ |
205
|
|
|
private function _getFormattedEnqueueScript( $asEnqueue ) { |
|
|
|
|
206
|
|
|
static $_iCallCount = 1; |
207
|
|
|
$_aEnqueueItem = $this->getAsArray( $asEnqueue ) + array( |
208
|
|
|
'handle_id' => 'script_' . $this->oForm->aArguments[ 'caller_id' ] . '_' . $_iCallCount, |
209
|
|
|
'src' => null, |
210
|
|
|
'dependencies' => null, |
211
|
|
|
'version' => null, |
212
|
|
|
'in_footer' => false, |
213
|
|
|
'translation' => null, |
214
|
|
|
); |
215
|
|
|
if ( is_string( $asEnqueue ) ) { |
216
|
|
|
$_aEnqueueItem[ 'src' ] = $asEnqueue; |
217
|
|
|
} |
218
|
|
|
$_aEnqueueItem[ 'src' ] = $this->getResolvedSRC( $_aEnqueueItem[ 'src' ] ); |
219
|
|
|
$_iCallCount++; |
220
|
|
|
return $_aEnqueueItem; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Enqueues page stylesheet resources. |
226
|
|
|
* |
227
|
|
|
* @since DEVVER |
228
|
|
|
*/ |
229
|
|
|
public function _replyToEnqueueStyles() { |
|
|
|
|
230
|
|
|
|
231
|
|
|
if ( ! $this->oForm->isInThePage() ) { |
232
|
|
|
return; |
233
|
|
|
} |
234
|
|
|
foreach( $this->oForm->getResources( 'src_styles' ) as $_asEnqueueItem ) { |
235
|
|
|
$this->_enqueueStyle( $_asEnqueueItem ); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
} |
239
|
|
|
private function _enqueueStyle( $asEnqueue ) { |
|
|
|
|
240
|
|
|
$_aEnqueueItem = $this->_getFormattedEnqueueStyle( $asEnqueue ); |
241
|
|
|
wp_enqueue_style( |
242
|
|
|
$_aEnqueueItem[ 'handle_id' ], |
243
|
|
|
$_aEnqueueItem[ 'src' ], |
244
|
|
|
$_aEnqueueItem[ 'dependencies' ], |
245
|
|
|
$_aEnqueueItem[ 'version' ], |
246
|
|
|
$_aEnqueueItem[ 'media' ] |
247
|
|
|
); |
248
|
|
|
} |
249
|
|
|
/** |
250
|
|
|
* @return array |
251
|
|
|
*/ |
252
|
|
|
private function _getFormattedEnqueueStyle( $asEnqueue ) { |
|
|
|
|
253
|
|
|
static $_iCallCount = 1; |
254
|
|
|
$_aEnqueueItem = $this->getAsArray( $asEnqueue ) + array( |
255
|
|
|
'handle_id' => 'style_' . $this->oForm->aArguments[ 'caller_id' ] . '_' . $_iCallCount, |
256
|
|
|
'src' => null, |
257
|
|
|
'dependencies' => null, |
258
|
|
|
'version' => null, |
259
|
|
|
'media' => null, |
260
|
|
|
); |
261
|
|
|
if ( is_string( $asEnqueue ) ) { |
262
|
|
|
$_aEnqueueItem[ 'src' ] = $asEnqueue; |
263
|
|
|
} |
264
|
|
|
$_aEnqueueItem[ 'src' ] = $this->getResolvedSRC( $_aEnqueueItem[ 'src' ] ); |
265
|
|
|
$_iCallCount++; |
266
|
|
|
return $_aEnqueueItem; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Enqueues inline styles. |
271
|
|
|
* |
272
|
|
|
* @since DEVVER |
273
|
|
|
*/ |
274
|
|
|
public function _replyToAddStyle() { |
|
|
|
|
275
|
|
|
|
276
|
|
|
if ( ! $this->oForm->isInThePage() ) { |
277
|
|
|
return; |
278
|
|
|
} |
279
|
|
|
$_sCSSRules = $this->_getFormattedInlineStyles( |
280
|
|
|
$this->oForm->getResources( 'inline_styles' ) |
281
|
|
|
); |
282
|
|
|
|
283
|
|
|
$_sID = $this->sanitizeSlug( strtolower( $this->oForm->aArguments[ 'caller_id' ] ) ); |
284
|
|
|
if ( $_sCSSRules ) { |
285
|
|
|
echo "<style type='text/css' id='inline-style-{$_sID}' class='admin-page-framework-form-style'>" |
286
|
|
|
. $_sCSSRules |
287
|
|
|
. "</style>"; |
288
|
|
|
} |
289
|
|
|
$_sIECSSRules = $this->_getFormattedInlineStyles( |
290
|
|
|
$this->oForm->getResources( 'inline_styles_ie' ) |
291
|
|
|
); |
292
|
|
|
if ( $_sIECSSRules ) { |
293
|
|
|
echo "<!--[if IE]><style type='text/css' id='inline-style-ie-{$_sID}' class='admin-page-framework-form-ie-style'>" |
294
|
|
|
. $_sIECSSRules |
295
|
|
|
. "</style><![endif]-->"; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
// Empty the values as this method can be called multiple times, in the head tag and the footer. |
299
|
|
|
$this->oForm->setResources( 'inline_styles', array() ); |
300
|
|
|
$this->oForm->setResources( 'inline_styles_ie', array() ); |
301
|
|
|
|
302
|
|
|
} |
303
|
|
|
/** |
304
|
|
|
* @since DEVVER |
305
|
|
|
* @string |
306
|
|
|
*/ |
307
|
|
|
private function _getFormattedInlineStyles( array $aInlineStyles ) { |
|
|
|
|
308
|
|
|
$_sCSSRules = implode( PHP_EOL, array_unique( $aInlineStyles ) ); |
309
|
|
|
return $this->isDebugMode() |
310
|
|
|
? $_sCSSRules |
311
|
|
|
: $this->minifyCSS( $_sCSSRules ); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Enqueues page inline scripts. |
316
|
|
|
* |
317
|
|
|
* @since DEVVER |
318
|
|
|
*/ |
319
|
|
|
public function _replyToAddScript() { |
|
|
|
|
320
|
|
|
|
321
|
|
|
if ( ! $this->oForm->isInThePage() ) { |
322
|
|
|
return; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
$_sScript = implode( PHP_EOL, array_unique( $this->oForm->getResources( 'inline_scripts' ) ) ); |
326
|
|
|
if ( $_sScript ) { |
327
|
|
|
$_sID = $this->sanitizeSlug( strtolower( $this->oForm->aArguments[ 'caller_id' ] ) ); |
328
|
|
|
echo "<script type='text/javascript' id='inline-script-{$_sID}' class='admin-page-framework-form-script'>" |
329
|
|
|
. '/* <![CDATA[ */' |
330
|
|
|
. $_sScript |
331
|
|
|
. '/* ]]> */' |
332
|
|
|
. "</script>"; |
333
|
|
|
} |
334
|
|
|
$this->oForm->setResources( 'inline_scripts', array() ); |
335
|
|
|
|
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
} |
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.