|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Admin Page Framework |
|
4
|
|
|
* |
|
5
|
|
|
* http://en.michaeluno.jp/admin-page-framework/ |
|
6
|
|
|
* Copyright (c) 2013-2016 Michael Uno; Licensed MIT |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Enqueues page resources set with the `style` and `script` arguments. |
|
12
|
|
|
* |
|
13
|
|
|
* @abstract |
|
14
|
|
|
* @since 3.6.3 |
|
15
|
|
|
* @package AdminPageFramework |
|
16
|
|
|
* @subpackage Factory/AdminPage/View |
|
17
|
|
|
* @internal |
|
18
|
|
|
* @extends AdminPageFramework_FrameworkUtility |
|
19
|
|
|
*/ |
|
20
|
|
|
class AdminPageFramework_View__Resource extends AdminPageFramework_FrameworkUtility { |
|
21
|
|
|
|
|
22
|
|
|
public $oFactory; |
|
23
|
|
|
public $sCurrentPageSlug; |
|
24
|
|
|
public $sCurrentTabSlug; |
|
25
|
|
|
|
|
26
|
|
|
public $aCSSRules = array(); |
|
27
|
|
|
public $aScripts = array(); |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Sets up properties. |
|
31
|
|
|
* @since 3.6.3 |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct( $oFactory ) { |
|
34
|
|
|
|
|
35
|
|
|
$this->oFactory = $oFactory; |
|
36
|
|
|
$this->sCurrentPageSlug = $oFactory->oProp->getCurrentPageSlug(); |
|
37
|
|
|
$this->sCurrentTabSlug = $oFactory->oProp->getCurrentTabSlug( $this->sCurrentPageSlug ); |
|
38
|
|
|
|
|
39
|
|
|
$this->_parseAssets( $oFactory ); |
|
40
|
|
|
$this->_setHooks(); |
|
41
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
/** |
|
44
|
|
|
* Sets up hooks. |
|
45
|
|
|
* @since 3.6.3 |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
|
|
private function _setHooks() { |
|
49
|
|
|
add_action( "style_{$this->sCurrentPageSlug}", array( $this, '_replyToAddInternalCSSRules' ) ); |
|
50
|
|
|
if ( $this->sCurrentTabSlug ) { |
|
51
|
|
|
add_action( "style_{$this->sCurrentPageSlug}_{$this->sCurrentTabSlug}", array( $this, '_replyToAddInternalCSSRules' ) ); |
|
52
|
|
|
} |
|
53
|
|
|
add_action( "script_{$this->sCurrentPageSlug}", array( $this, '_replyToAddInternalScripts' ) ); |
|
54
|
|
|
if ( $this->sCurrentTabSlug ) { |
|
55
|
|
|
add_action( "script_{$this->sCurrentPageSlug}_{$this->sCurrentTabSlug}", array( $this, '_replyToAddInternalScripts' ) ); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
public function _replyToAddInternalCSSRules( $sCSS ) { |
|
59
|
|
|
return $this->_appendInternalAssets( $sCSS, $this->aCSSRules ); |
|
60
|
|
|
} |
|
61
|
|
|
public function _replyToAddInternalScripts( $sScript ) { |
|
62
|
|
|
return $this->_appendInternalAssets( $sScript, $this->aScripts ); |
|
63
|
|
|
} |
|
64
|
|
|
/** |
|
65
|
|
|
* Appends internal contents (script/CSS) to the given value and updates the container array. |
|
66
|
|
|
* @return string |
|
67
|
|
|
* @since 3.6.3 |
|
68
|
|
|
*/ |
|
69
|
|
|
public function _appendInternalAssets( $sInternal, &$aContainer ) { |
|
70
|
|
|
$_aInternals = array_unique( $aContainer ); |
|
71
|
|
|
$sInternal = PHP_EOL . $sInternal; |
|
72
|
|
|
foreach( $_aInternals as $_iIndex => $_sInternal ) { |
|
73
|
|
|
$sInternal .= $_sInternal . PHP_EOL; |
|
74
|
|
|
unset( $_aInternals[ $_iIndex ] ); |
|
75
|
|
|
} |
|
76
|
|
|
$aContainer = $_aInternals; // update the container array. |
|
77
|
|
|
return $sInternal; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @since 3.6.3 |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
private function _parseAssets( $oFactory ) { |
|
85
|
|
|
|
|
86
|
|
|
// page |
|
87
|
|
|
$_aPageStyles = $this->getElementAsArray( |
|
88
|
|
|
$oFactory->oProp->aPages, |
|
89
|
|
|
array( $this->sCurrentPageSlug, 'style' ) |
|
90
|
|
|
); |
|
91
|
|
|
$this->_enqueuePageAssets( $_aPageStyles, 'style' ); |
|
92
|
|
|
|
|
93
|
|
|
$_aPageScripts = $this->getElementAsArray( |
|
94
|
|
|
$oFactory->oProp->aPages, |
|
95
|
|
|
array( $this->sCurrentPageSlug, 'script' ) |
|
96
|
|
|
); |
|
97
|
|
|
$this->_enqueuePageAssets( $_aPageScripts, 'script' ); |
|
98
|
|
|
|
|
99
|
|
|
// In-page tabs |
|
100
|
|
|
if ( ! $this->sCurrentTabSlug ) { |
|
101
|
|
|
return; |
|
102
|
|
|
} |
|
103
|
|
|
$_aInPageTabStyles = $this->getElementAsArray( |
|
104
|
|
|
$oFactory->oProp->aInPageTabs, |
|
105
|
|
|
array( $this->sCurrentPageSlug, $this->sCurrentTabSlug, 'style' ) |
|
106
|
|
|
); |
|
107
|
|
|
$this->_enqueuePageAssets( $_aInPageTabStyles, 'style' ); |
|
108
|
|
|
|
|
109
|
|
|
$_aInPageTabScripts = $this->getElementAsArray( |
|
110
|
|
|
$oFactory->oProp->aInPageTabs, |
|
111
|
|
|
array( $this->sCurrentPageSlug, $this->sCurrentTabSlug, 'script' ) |
|
112
|
|
|
); |
|
113
|
|
|
$this->_enqueuePageAssets( $_aInPageTabScripts, 'script' ); |
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
/** |
|
117
|
|
|
* @since 3.6.3 |
|
118
|
|
|
* @return void |
|
119
|
|
|
*/ |
|
120
|
|
|
private function _enqueuePageAssets( array $aPageAssets, $sType='style' ) { |
|
121
|
|
|
$_sMathodName = "_enqueueAsset_" . $sType; |
|
122
|
|
|
foreach( $aPageAssets as $_asPageAsset ) { |
|
123
|
|
|
$this->{$_sMathodName}( $_asPageAsset); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @since 3.6.3 |
|
129
|
|
|
* @return void |
|
130
|
|
|
*/ |
|
131
|
|
|
private function _enqueueAsset_style( $asPageStyle ) { |
|
132
|
|
|
|
|
133
|
|
|
$_oFormatter = new AdminPageFramework_Format_PageResource_Style( $asPageStyle ); |
|
134
|
|
|
$_aPageStyle = $_oFormatter->get(); |
|
135
|
|
|
$_sSRC = $_aPageStyle[ 'src' ]; |
|
136
|
|
|
|
|
137
|
|
|
// At this point, it may be a url/path or a text CSS rules. |
|
138
|
|
|
if ( file_exists( $_sSRC ) || filter_var( $_sSRC, FILTER_VALIDATE_URL ) ) { |
|
139
|
|
|
return $this->oFactory->enqueueStyle( |
|
140
|
|
|
$_sSRC, |
|
141
|
|
|
$this->sCurrentPageSlug, |
|
142
|
|
|
$this->sCurrentTabSlug, // tab slug |
|
143
|
|
|
$_aPageStyle |
|
144
|
|
|
); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
// Insert the CSS rule in the head tag. |
|
148
|
|
|
$this->aCSSRules[] = $_sSRC; |
|
149
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @since 3.6.3 |
|
154
|
|
|
* @return void |
|
155
|
|
|
*/ |
|
156
|
|
|
private function _enqueueAsset_script( $asPageScript ) { |
|
157
|
|
|
|
|
158
|
|
|
$_oFormatter = new AdminPageFramework_Format_PageResource_Script( $asPageScript ); |
|
159
|
|
|
$_aPageScript = $_oFormatter->get(); |
|
160
|
|
|
$_sSRC = $_aPageScript[ 'src' ]; |
|
161
|
|
|
|
|
162
|
|
|
// At this point, it may be a url/path or a text CSS rules. |
|
163
|
|
|
if ( $this->isResourcePath( $_sSRC ) ) { |
|
164
|
|
|
return $this->oFactory->enqueueScript( |
|
165
|
|
|
$_sSRC, |
|
166
|
|
|
$this->sCurrentPageSlug, |
|
167
|
|
|
$this->sCurrentTabSlug, // tab slug |
|
168
|
|
|
$_aPageScript |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
// Insert the scripts in the head tag. |
|
173
|
|
|
$this->aScripts[] = $_sSRC; |
|
174
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
|