|
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
|
|
|
if ( ! in_array( $this->oForm->aArguments[ 'structure_type' ], array( 'widget' ) ) ) { |
|
73
|
|
|
new AdminPageFramework_Form_View__Resource__Head( $this->oForm, 'wp_head' ); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
private function _setAdminHooks() { |
|
78
|
|
|
|
|
79
|
|
|
// Hook the admin header to insert custom admin stylesheets and scripts. |
|
80
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, '_replyToEnqueueScripts' ) ); |
|
81
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, '_replyToEnqueueStyles' ) ); |
|
82
|
|
|
|
|
83
|
|
|
add_action( did_action( 'admin_print_styles' ) ? 'admin_print_footer_scripts' : 'admin_print_styles', array( $this, '_replyToAddStyle' ), 999 ); |
|
84
|
|
|
add_action( did_action( 'admin_print_scripts' ) ? 'admin_print_footer_scripts' : 'admin_print_scripts', array( $this, '_replyToAddScript' ), 999 ); |
|
85
|
|
|
|
|
86
|
|
|
// Take care of items that could not be added in the head tag. |
|
87
|
|
|
/// For wp-admin/customizer.php |
|
88
|
|
|
add_action( 'customize_controls_print_footer_scripts', array( $this, '_replyToEnqueueScripts' ) ); |
|
89
|
|
|
add_action( 'customize_controls_print_footer_scripts', array( $this, '_replyToEnqueueStyles' ) ); |
|
90
|
|
|
|
|
91
|
|
|
/// For admin pages other than wp-admin/customizer.php |
|
92
|
|
|
add_action( 'admin_footer', array( $this, '_replyToEnqueueScripts' ) ); |
|
93
|
|
|
add_action( 'admin_footer', array( $this, '_replyToEnqueueStyles' ) ); |
|
94
|
|
|
|
|
95
|
|
|
/// For all admin pages. |
|
96
|
|
|
add_action( 'admin_print_footer_scripts', array( $this, '_replyToAddStyle' ), 999 ); |
|
97
|
|
|
add_action( 'admin_print_footer_scripts', array( $this, '_replyToAddScript' ), 999 ); |
|
98
|
|
|
|
|
99
|
|
|
// Required scripts in the head tag. |
|
100
|
|
|
if ( ! in_array( $this->oForm->aArguments[ 'structure_type' ], array( 'widget' ) ) ) { |
|
101
|
|
|
new AdminPageFramework_Form_View__Resource__Head( $this->oForm, 'admin_head' ); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Enqueues page script resources. |
|
108
|
|
|
* |
|
109
|
|
|
* @since DEVVER |
|
110
|
|
|
*/ |
|
111
|
|
|
public function _replyToEnqueueScripts() { |
|
112
|
|
|
if ( ! $this->oForm->isInThePage() ) { |
|
113
|
|
|
return; |
|
114
|
|
|
} |
|
115
|
|
|
foreach( $this->oForm->getResources( 'src_scripts' ) as $_asEnqueue ) { |
|
116
|
|
|
$this->_enqueueScript( $_asEnqueue ); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
/** |
|
120
|
|
|
* Stores flags of enqueued items. |
|
121
|
|
|
* @since DEVVER |
|
122
|
|
|
*/ |
|
123
|
|
|
static private $_aEnqueued = array(); |
|
124
|
|
|
/** |
|
125
|
|
|
* @return void |
|
126
|
|
|
* @since DEVVER |
|
127
|
|
|
*/ |
|
128
|
|
|
private function _enqueueScript( $asEnqueue ) { |
|
129
|
|
|
|
|
130
|
|
|
$_aEnqueueItem = $this->_getFormattedEnqueueScript( $asEnqueue ); |
|
131
|
|
|
|
|
132
|
|
|
// Do not load the same items multiple times. |
|
133
|
|
|
if ( isset( self::$_aEnqueued[ $_aEnqueueItem[ 'src' ] ] ) ) { |
|
134
|
|
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
self::$_aEnqueued[ $_aEnqueueItem[ 'src' ] ] = $_aEnqueueItem; |
|
137
|
|
|
|
|
138
|
|
|
wp_enqueue_script( |
|
139
|
|
|
$_aEnqueueItem[ 'handle_id' ], |
|
140
|
|
|
$_aEnqueueItem[ 'src' ], |
|
141
|
|
|
$_aEnqueueItem[ 'dependencies' ], |
|
142
|
|
|
$_aEnqueueItem[ 'version' ], |
|
143
|
|
|
did_action( 'admin_body_class' ) |
|
144
|
|
|
? true |
|
145
|
|
|
: $_aEnqueueItem[ 'in_footer' ] |
|
146
|
|
|
); |
|
147
|
|
|
if ( $_aEnqueueItem[ 'translation' ] ) { |
|
148
|
|
|
wp_localize_script( |
|
149
|
|
|
$_aEnqueueItem[ 'handle_id' ], |
|
150
|
|
|
$_aEnqueueItem[ 'handle_id' ], |
|
151
|
|
|
$_aEnqueueItem[ 'translation' ] |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
/** |
|
157
|
|
|
* @return array |
|
158
|
|
|
* @since DEVVER |
|
159
|
|
|
*/ |
|
160
|
|
|
private function _getFormattedEnqueueScript( $asEnqueue ) { |
|
161
|
|
|
static $_iCallCount = 1; |
|
162
|
|
|
$_aEnqueueItem = $this->getAsArray( $asEnqueue ) + array( |
|
163
|
|
|
'handle_id' => 'script_' . $this->oForm->aArguments[ 'caller_id' ] . '_' . $_iCallCount, |
|
164
|
|
|
'src' => null, |
|
165
|
|
|
'dependencies' => null, |
|
166
|
|
|
'version' => null, |
|
167
|
|
|
'in_footer' => false, |
|
168
|
|
|
'translation' => null, |
|
169
|
|
|
); |
|
170
|
|
|
if ( is_string( $asEnqueue ) ) { |
|
171
|
|
|
$_aEnqueueItem[ 'src' ] = $asEnqueue; |
|
172
|
|
|
} |
|
173
|
|
|
$_aEnqueueItem[ 'src' ] = $this->getResolvedSRC( $_aEnqueueItem[ 'src' ] ); |
|
174
|
|
|
$_iCallCount++; |
|
175
|
|
|
return $_aEnqueueItem; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Enqueues page stylesheet resources. |
|
181
|
|
|
* |
|
182
|
|
|
* @since DEVVER |
|
183
|
|
|
*/ |
|
184
|
|
|
public function _replyToEnqueueStyles() { |
|
185
|
|
|
|
|
186
|
|
|
if ( ! $this->oForm->isInThePage() ) { |
|
187
|
|
|
return; |
|
188
|
|
|
} |
|
189
|
|
|
foreach( $this->oForm->getResources( 'src_styles' ) as $_asEnqueueItem ) { |
|
190
|
|
|
$this->_enqueueStyle( $_asEnqueueItem ); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
} |
|
194
|
|
|
private function _enqueueStyle( $asEnqueue ) { |
|
195
|
|
|
$_aEnqueueItem = $this->_getFormattedEnqueueStyle( $asEnqueue ); |
|
196
|
|
|
wp_enqueue_style( |
|
197
|
|
|
$_aEnqueueItem[ 'handle_id' ], |
|
198
|
|
|
$_aEnqueueItem[ 'src' ], |
|
199
|
|
|
$_aEnqueueItem[ 'dependencies' ], |
|
200
|
|
|
$_aEnqueueItem[ 'version' ], |
|
201
|
|
|
$_aEnqueueItem[ 'media' ] |
|
202
|
|
|
); |
|
203
|
|
|
} |
|
204
|
|
|
/** |
|
205
|
|
|
* @return array |
|
206
|
|
|
*/ |
|
207
|
|
|
private function _getFormattedEnqueueStyle( $asEnqueue ) { |
|
208
|
|
|
static $_iCallCount = 1; |
|
209
|
|
|
$_aEnqueueItem = $this->getAsArray( $asEnqueue ) + array( |
|
210
|
|
|
'handle_id' => 'style_' . $this->oForm->aArguments[ 'caller_id' ] . '_' . $_iCallCount, |
|
211
|
|
|
'src' => null, |
|
212
|
|
|
'dependencies' => null, |
|
213
|
|
|
'version' => null, |
|
214
|
|
|
'media' => null, |
|
215
|
|
|
); |
|
216
|
|
|
if ( is_string( $asEnqueue ) ) { |
|
217
|
|
|
$_aEnqueueItem[ 'src' ] = $asEnqueue; |
|
218
|
|
|
} |
|
219
|
|
|
$_aEnqueueItem[ 'src' ] = $this->getResolvedSRC( $_aEnqueueItem[ 'src' ] ); |
|
220
|
|
|
$_iCallCount++; |
|
221
|
|
|
return $_aEnqueueItem; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Enqueues inline styles. |
|
226
|
|
|
* |
|
227
|
|
|
* @since DEVVER |
|
228
|
|
|
*/ |
|
229
|
|
|
public function _replyToAddStyle() { |
|
230
|
|
|
|
|
231
|
|
|
if ( ! $this->oForm->isInThePage() ) { |
|
232
|
|
|
return; |
|
233
|
|
|
} |
|
234
|
|
|
$_sCSSRules = $this->_getFormattedInlineStyles( |
|
235
|
|
|
$this->oForm->getResources( 'inline_styles' ) |
|
236
|
|
|
); |
|
237
|
|
|
|
|
238
|
|
|
$_sID = $this->sanitizeSlug( strtolower( $this->oForm->aArguments[ 'caller_id' ] ) ); |
|
239
|
|
|
if ( $_sCSSRules ) { |
|
240
|
|
|
echo "<style type='text/css' id='inline-style-{$_sID}' class='admin-page-framework-form-style'>" |
|
241
|
|
|
. $_sCSSRules |
|
242
|
|
|
. "</style>"; |
|
243
|
|
|
} |
|
244
|
|
|
$_sIECSSRules = $this->_getFormattedInlineStyles( |
|
245
|
|
|
$this->oForm->getResources( 'inline_styles_ie' ) |
|
246
|
|
|
); |
|
247
|
|
|
if ( $_sIECSSRules ) { |
|
248
|
|
|
echo "<!--[if IE]><style type='text/css' id='inline-style-ie-{$_sID}' class='admin-page-framework-form-ie-style'>" |
|
249
|
|
|
. $_sIECSSRules |
|
250
|
|
|
. "</style><![endif]-->"; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
// Empty the values as this method can be called multiple times, in the head tag and the footer. |
|
254
|
|
|
$this->oForm->setResources( 'inline_styles', array() ); |
|
255
|
|
|
$this->oForm->setResources( 'inline_styles_ie', array() ); |
|
256
|
|
|
|
|
257
|
|
|
} |
|
258
|
|
|
/** |
|
259
|
|
|
* @since DEVVER |
|
260
|
|
|
* @string |
|
261
|
|
|
*/ |
|
262
|
|
|
private function _getFormattedInlineStyles( array $aInlineStyles ) { |
|
263
|
|
|
$_sCSSRules = implode( PHP_EOL, array_unique( $aInlineStyles ) ); |
|
264
|
|
|
return $this->isDebugMode() |
|
265
|
|
|
? $_sCSSRules |
|
266
|
|
|
: $this->minifyCSS( $_sCSSRules ); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Enqueues page inline scripts. |
|
271
|
|
|
* |
|
272
|
|
|
* @since DEVVER |
|
273
|
|
|
*/ |
|
274
|
|
|
public function _replyToAddScript() { |
|
275
|
|
|
|
|
276
|
|
|
if ( ! $this->oForm->isInThePage() ) { |
|
277
|
|
|
return; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
$_sScript = implode( PHP_EOL, array_unique( $this->oForm->getResources( 'inline_scripts' ) ) ); |
|
281
|
|
|
if ( $_sScript ) { |
|
282
|
|
|
$_sID = $this->sanitizeSlug( strtolower( $this->oForm->aArguments[ 'caller_id' ] ) ); |
|
283
|
|
|
echo "<script type='text/javascript' id='inline-script-{$_sID}' class='admin-page-framework-form-script'>" |
|
284
|
|
|
. '/* <![CDATA[ */' |
|
285
|
|
|
. $_sScript |
|
286
|
|
|
. '/* ]]> */' |
|
287
|
|
|
. "</script>"; |
|
288
|
|
|
} |
|
289
|
|
|
$this->oForm->setResources( 'inline_scripts', array() ); |
|
290
|
|
|
|
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
} |
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.