PluginProbe
Depicter — Popup & Slider Builder / 1.3.3
Depicter — Popup & Slider Builder v1.3.3
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Front / Assets.php

Assets.php in Depicter — Popup & Slider Builder 1.3.3, at app/src/Front/Assets.php

281 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Front;
3
4
5 use Averta\Core\Utility\Arr;
6
7
8 class Assets
9 {
10 /**
11 * @var string
12 */
13 private $baseAssetsUrl = '';
14
15 /**
16 * @var string
17 */
18 private $version = DEPICTER_VERSION;
19
20
21 public function bootstrap()
22 {
23 $this->baseAssetsUrl = \Depicter::core()->assets()->getUrl();
24 }
25
26 /**
27 * Retrieves a list of styles based on given group
28 *
29 * @param string|array $group A group name or list of groups in array
30 *
31 * @return array
32 */
33 public function getStyles( $group = 'common' )
34 {
35 $assets = $this->getStylesDictionary();
36 return $this->getAssetFromList( $group, $assets );
37 }
38
39 /**
40 * Retrieves a list of scripts based on given group
41 *
42 * @param string|array $group A group name or list of groups in array
43 *
44 * @return array
45 */
46 public function getScripts( $group = 'player' )
47 {
48 $assets = $this->getScriptsDictionary();
49 return $this->getAssetFromList( $group, $assets );
50 }
51
52 /**
53 * Enqueues a list of styles based on given group
54 *
55 * @param string|array $group A group name or list of groups in array
56 *
57 * @return array
58 */
59 public function enqueueStyles( $group = 'common' )
60 {
61 $styleUrls = $this->getStyles( $group );
62
63 foreach ( $styleUrls as $styleId => $styleUrl ) {
64 \Depicter::core()->assets()->enqueueStyle( $styleId, $styleUrl, [] );
65 }
66
67 do_action( 'depicter/after/enqueue/styles', $styleUrls );
68
69 return $styleUrls;
70 }
71
72 /**
73 * Enqueues a list of scripts based on given group
74 *
75 * @param string|array $group A group name or list of groups in array
76 *
77 * @return array
78 */
79 public function enqueueScripts( $group = 'player', $inFooter = true )
80 {
81 $scriptUrls = $this->getScripts( $group );
82
83 foreach ( $scriptUrls as $scriptId => $scriptUrl ){
84 \Depicter::core()->assets()->enqueueScript( $scriptId, $scriptUrl, [], $inFooter );
85 }
86
87 do_action( 'depicter/after/enqueue/scripts', $scriptUrls );
88
89 return $scriptUrls;
90 }
91
92 /**
93 * Retrieves a list of custom styles for given slider IDs
94 *
95 * @param string|array $documentIDs List of slider IDs for slugs
96 *
97 * @return array
98 */
99 public function getCustomStyles( $documentIDs )
100 {
101 if( empty( $documentIDs ) ){
102 return [];
103 }
104
105 if( $documentIDs = \Depicter::document()->getID( $documentIDs ) ){
106 $documentIDs = (array) $documentIDs;
107 $cssLinksToEnqueue = [];
108
109 foreach( $documentIDs as $documentID ){
110 if( false !== $cssLinkToEnqueue = \Depicter::cache('document')->get( $documentID . '_css_files' ) ){
111 $cssLinksToEnqueue = $cssLinksToEnqueue + $cssLinkToEnqueue;
112 }
113 }
114
115 return $cssLinksToEnqueue ;
116 }
117
118 return [];
119 }
120
121 /**
122 * Enqueues a list of custom styles based on given document IDs
123 *
124 * @param string|array $documentIDs List of slider IDs for slugs
125 *
126 * @return array
127 */
128 public function enqueueCustomStyles( $documentIDs )
129 {
130 $cssLinksToEnqueue = $this->getCustomStyles( $documentIDs );
131
132 foreach( $cssLinksToEnqueue as $cssId => $cssLink ){
133 \Depicter::core()->assets()->enqueueStyle( $cssId, $cssLink );
134 }
135
136 return $cssLinksToEnqueue;
137 }
138
139 /**
140 * Enqueues custom google fonts for given document IDs
141 *
142 * @param string|array $documentIDs List of slider IDs for slugs
143 *
144 * @return array
145 */
146 public function enqueueCustomGoogleFonts( $documentIDs )
147 {
148 $cssLinksToEnqueue = $this->getCustomStyles( $documentIDs );
149
150 foreach( $cssLinksToEnqueue as $cssId => $cssLink ){
151 if ( false !== strpos( $cssId, 'google-font' ) ) {
152 \Depicter::core()->assets()->enqueueStyle( $cssId, $cssLink );
153 }
154 }
155
156 return $cssLinksToEnqueue;
157 }
158
159 /**
160 * Enqueues custom styles of a document
161 *
162 * @param int $documentID
163 * @param bool $isPrivilegedUser
164 *
165 * @return void
166 */
167 public function enqueueCustomAssets( $documentID, $isPrivilegedUser = null ){
168 $isPrivilegedUser = is_null( $isPrivilegedUser ) ? \Depicter::authorization()->currentUserCanPublishDocument() : $isPrivilegedUser;
169
170 if ( $isPrivilegedUser ) {
171 $this->enqueueCustomGoogleFonts( $documentID );
172 } else {
173 $this->enqueueCustomStyles( $documentID );
174 }
175 }
176
177 /**
178 * Enqueues styles and scripts of a document
179 *
180 * @param int $documentID
181 * @param bool $isPrivilegedUser
182 *
183 * @return void
184 */
185 public function enqueueAssets( $documentID, $isPrivilegedUser = null ){
186 $isPrivilegedUser = is_null( $isPrivilegedUser ) ? \Depicter::authorization()->currentUserCanPublishDocument() : $isPrivilegedUser;
187
188 if ( ! did_action( 'depicter/after/enqueue/styles' ) ) {
189 $this->enqueueStyles();
190 $this->enqueueCustomAssets( $documentID, $isPrivilegedUser );
191 }
192 if ( ! did_action( 'depicter/after/enqueue/scripts' ) ) {
193 $this->enqueueScripts();
194 }
195 }
196
197 /**
198 * Searches for group(s) in list of given assets and returns result in a list
199 *
200 * @param string|array $group A group name or list of groups in array
201 * @param array $assets
202 *
203 * @return array
204 */
205 protected function getAssetFromList( $group = 'common', $assets = [] )
206 {
207 if( empty( $group ) ){
208 return $assets;
209 }
210
211 if( is_array( $group ) ){
212 if( count( $group ) > 1 ){
213 $result = [];
214 foreach ( $group as $groupKey ){
215 if( $value = Arr::searchKeyDeep( $assets, $groupKey ) ){
216 if( is_string( $value ) ){
217 $result[ $groupKey ] = $value;
218 } else {
219 $result = $result + $value;
220 }
221 }
222 }
223 return $result;
224 }
225 }
226
227 $group = (string) $group;
228 $result = [];
229
230 if( $value = Arr::searchKeyDeep( $assets, $group ) ){
231 if( is_string( $value ) ){
232 $result = [ $group => $value ];
233 } elseif( is_array( $value ) ) {
234 $result = $value;
235 }
236 }
237
238 return $result;
239 }
240
241 /**
242 * Full list of front styles
243 *
244 * @return array
245 */
246 protected function getStylesDictionary()
247 {
248 $stylesDictionary = [
249 'situational' => [
250 'minireset' => $this->baseAssetsUrl . '/resources/styles/frontend/reset.min.css',
251 'depicter-preview-style' => $this->baseAssetsUrl . '/resources/styles/player/preview.css'
252 ],
253 'common' => [
254 'depicter--front-common' => $this->baseAssetsUrl . '/resources/styles/player/depicter.css'
255 ]
256 ];
257
258 return $stylesDictionary;
259 }
260
261 /**
262 * Full list of front scripts
263 *
264 * @return array
265 */
266 protected function getScriptsDictionary()
267 {
268 $scriptsDictionary = [
269 'player' => [
270 'depicter--player' => $this->baseAssetsUrl . '/resources/scripts/player/depicter.js'
271 ],
272 'widget' => [
273 'depicter-widget' => $this->baseAssetsUrl . '/resources/scripts/widgets/elementor-widget.js'
274 ]
275 ];
276
277 return $scriptsDictionary;
278 }
279
280 }
281