PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
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 trunk, at app/src/Front/Assets.php

377 lines 9.1 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 use Averta\WordPress\Utility\Sanitize;
7
8
9 class Assets
10 {
11 /**
12 * @var string
13 */
14 private $baseAssetsUrl = '';
15
16 /**
17 * @var string
18 */
19 private $version = DEPICTER_VERSION;
20
21
22 public function bootstrap()
23 {
24 $this->baseAssetsUrl = \Depicter::core()->assets()->getUrl();
25 }
26
27 /**
28 * Retrieves a list of styles based on given group
29 *
30 * @param string|array $group A group name or list of groups in array
31 *
32 * @return array
33 */
34 public function getStyles( $group = 'common' )
35 {
36 $assets = $this->getStylesDictionary();
37 return $this->getAssetFromList( $group, $assets );
38 }
39
40 /**
41 * Retrieves a list of scripts based on given group
42 *
43 * @param string|array $group A group name or list of groups in array
44 *
45 * @return array
46 */
47 public function getScripts( $group = 'player' )
48 {
49 $assets = $this->getScriptsDictionary();
50 return $this->getAssetFromList( $group, $assets );
51 }
52
53 /**
54 * Enqueues a list of styles based on given group
55 *
56 * @param string|array $group A group name or list of groups in array
57 *
58 * @return array
59 */
60 public function enqueueStyles( $group = 'common' )
61 {
62 $styleUrls = $this->getStyles( $group );
63
64 foreach ( $styleUrls as $styleId => $styleUrl ) {
65 \Depicter::core()->assets()->enqueueStyle( $styleId, $styleUrl, [] );
66 }
67
68 do_action( 'depicter/after/enqueue/styles', $styleUrls );
69
70 return $styleUrls;
71 }
72
73 /**
74 * Enqueues a list of scripts based on given group
75 *
76 * @param string|array $group A group name or list of groups in array
77 *
78 * @return array
79 */
80 public function enqueueScripts( $group = 'player', $inFooter = true )
81 {
82 $scriptUrls = $this->getScripts( $group );
83
84 foreach ( $scriptUrls as $scriptId => $scriptUrl ){
85 \Depicter::core()->assets()->enqueueScript( $scriptId, $scriptUrl, [], $inFooter );
86 }
87
88 do_action( 'depicter/after/enqueue/scripts', $scriptUrls );
89
90 return $scriptUrls;
91 }
92
93 /**
94 * Retrieves a list of custom styles for given slider IDs
95 *
96 * @param string|array $documentIDs List of slider IDs for slugs
97 *
98 * @return array
99 */
100 public function getCustomStyles( $documentIDs )
101 {
102 if( empty( $documentIDs ) ){
103 return [];
104 }
105
106 if( $documentIDs = \Depicter::document()->getID( $documentIDs ) ){
107 $documentIDs = (array) $documentIDs;
108 $cssLinksToEnqueue = [];
109
110 foreach( $documentIDs as $documentID ){
111 if( false !== $cssLinkToEnqueue = \Depicter::cache('document')->get( $documentID . '_css_files' ) ){
112 $cssLinksToEnqueue = $cssLinksToEnqueue + $cssLinkToEnqueue;
113 } else {
114 \Depicter::document()->cacheCustomStyles( $documentID );
115 if( false !== $cssLinkToEnqueue = \Depicter::cache('document')->get( $documentID . '_css_files' ) ){
116 $cssLinksToEnqueue = $cssLinksToEnqueue + $cssLinkToEnqueue;
117 }
118 }
119 }
120
121 return $cssLinksToEnqueue ;
122 }
123
124 return [];
125 }
126
127 /**
128 * Enqueues a list of custom styles based on given document IDs
129 *
130 * @param string|array $documentIDs List of slider IDs for slugs
131 *
132 * @return array
133 */
134 public function enqueueCustomStyles( $documentIDs )
135 {
136 $cssLinksToEnqueue = $this->getCustomStyles( $documentIDs );
137
138 foreach( $cssLinksToEnqueue as $cssId => $cssLink ){
139 \Depicter::core()->assets()->enqueueStyle( $cssId, $cssLink );
140 }
141
142 return $cssLinksToEnqueue;
143 }
144
145 /**
146 * Enqueues custom google fonts for given document IDs
147 *
148 * @param string|array $documentIDs List of slider IDs for slugs
149 *
150 * @return array
151 */
152 public function enqueueCustomGoogleFonts( $documentIDs ){
153 $useGoogleFonts = \Depicter::options()->get('use_google_fonts', 'on');
154 $cssLinksToEnqueue = [];
155
156 if ( $useGoogleFonts === 'save_locally' ) {
157 $cssLinksToEnqueue = $this->makeGoogleFontLinksSelfHosted( $documentIDs );
158 foreach( $cssLinksToEnqueue as $cssId => $cssLink ){
159 \Depicter::core()->assets()->enqueueStyle( $cssId, $cssLink );
160 }
161 } elseif( $useGoogleFonts === 'on' ) {
162 $cssLinksToEnqueue = $this->getCustomStyles( $documentIDs );
163 foreach( $cssLinksToEnqueue as $cssId => $cssLink ){
164 if ( false !== strpos( $cssId, 'google-font' ) ) {
165 \Depicter::core()->assets()->enqueueStyle( $cssId, $cssLink );
166 }
167 }
168 }
169
170 return $cssLinksToEnqueue;
171 }
172
173 /**
174 * Download and enqueue google fonts styles
175 *
176 * @param $documentIDs
177 *
178 * @return array
179 */
180 public function makeGoogleFontLinksSelfHosted( $documentIDs ) {
181
182 if( empty( $documentIDs ) ){
183 return [];
184 }
185
186 if( $documentIDs = \Depicter::document()->getID( $documentIDs ) ){
187 $documentIDs = (array) $documentIDs;
188 $localLinks = [];
189
190 foreach( $documentIDs as $documentID ){
191 if( false !== $cssLinksToEnqueue = \Depicter::cache('document')->get( $documentID . '_css_files' ) ) {
192 $localLinks = Arr::merge( \Depicter::googleFontsService()->swapToLocalLinks( $documentID, $cssLinksToEnqueue ), $localLinks );
193 }
194 }
195
196 return $localLinks ;
197 }
198
199 return [];
200 }
201
202 /**
203 * Enqueues custom styles of a document
204 *
205 * @param int $documentID
206 * @param bool $isPrivilegedUser
207 *
208 * @return void
209 */
210 public function enqueueCustomAssets( $documentID, $isPrivilegedUser = null ){
211 $isPrivilegedUser = is_null( $isPrivilegedUser ) ? \Depicter::authorization()->currentUserCanPublishDocument() : $isPrivilegedUser;
212
213 if ( $isPrivilegedUser ) {
214 $this->enqueueCustomGoogleFonts( $documentID );
215 } else {
216 $this->enqueueCustomStyles( $documentID );
217 }
218
219 do_action( 'depicter/after/enqueue/styles/' . $documentID );
220 }
221
222 /**
223 * Enqueues styles and scripts of a document
224 *
225 * @param int $documentID
226 * @param bool $isPrivilegedUser
227 *
228 * @return void
229 */
230 public function enqueueAssets( $documentID, $isPrivilegedUser = null ){
231 $isPrivilegedUser = is_null( $isPrivilegedUser ) ? \Depicter::authorization()->currentUserCanPublishDocument() : $isPrivilegedUser;
232
233 if ( ! did_action( 'depicter/after/enqueue/styles' ) ) {
234 $this->enqueueStyles();
235 }
236
237 if ( !did_action('depicter/after/enqueue/styles/' . $documentID ) ) {
238 $this->enqueueCustomAssets( $documentID, $isPrivilegedUser );
239 }
240
241 if ( ! did_action( 'depicter/after/enqueue/scripts' ) ) {
242 $this->enqueueScripts();
243 }
244 }
245
246 /**
247 * Print media preload tags of a document if exists
248 *
249 * @param int $documentID
250 *
251 * @return void
252 */
253 public function printPreloadTags( $documentID ) {
254 if( false !== $preloadTags = \Depicter::cache('document')->get( $documentID . '_preload_tags' ) ){
255 echo Sanitize::html( $preloadTags );
256 }
257 }
258
259 /**
260 *
261 * @param int $documentID
262 *
263 * @return void
264 */
265 public function enqueuePreloadTags( $documentID ) {
266 add_action( 'wp_head', function () use ( $documentID ) {
267 $this->printPreloadTags( $documentID );
268 }, 10 );
269 }
270
271 /**
272 * Enqueue inline styles right after css with id of $cssID enqueued
273 *
274 * @param $cssID
275 * @param $inlineStyles
276 *
277 * @return void
278 */
279 public function addInlineStyle( $cssID, $inlineStyles ) {
280 wp_add_inline_style( $cssID, $inlineStyles );
281 }
282
283 /**
284 * Searches for group(s) in list of given assets and returns result in a list
285 *
286 * @param string|array $group A group name or list of groups in array
287 * @param array $assets
288 *
289 * @return array
290 */
291 protected function getAssetFromList( $group = 'common', $assets = [] )
292 {
293 if( empty( $group ) ){
294 return $assets;
295 }
296
297 if( is_array( $group ) ){
298 if( count( $group ) > 1 ){
299 $result = [];
300 foreach ( $group as $groupKey ){
301 if( $value = Arr::searchKeyDeep( $assets, $groupKey ) ){
302 if( is_string( $value ) ){
303 $result[ $groupKey ] = $value;
304 } else {
305 $result = $result + $value;
306 }
307 }
308 }
309 return $result;
310 }
311 }
312
313 $group = (string) $group;
314 $result = [];
315
316 if( $value = Arr::searchKeyDeep( $assets, $group ) ){
317 if( is_string( $value ) ){
318 $result = [ $group => $value ];
319 } elseif( is_array( $value ) ) {
320 $result = $value;
321 }
322 }
323
324 return $result;
325 }
326
327 /**
328 * Full list of front styles
329 *
330 * @return array
331 */
332 protected function getStylesDictionary()
333 {
334 $stylesDictionary = [
335 'situational' => [
336 'minireset' => $this->baseAssetsUrl . '/resources/styles/frontend/reset.min.css',
337 'depicter-preview-style' => $this->baseAssetsUrl . '/resources/styles/player/preview.css'
338 ],
339 'common' => [
340 'depicter-front-pre' => $this->baseAssetsUrl . '/resources/styles/player/depicter-pre.css',
341 'depicter--front-common' => $this->baseAssetsUrl . '/resources/styles/player/depicter.css'
342 ]
343 ];
344
345 return $stylesDictionary;
346 }
347
348 /**
349 * Full list of front scripts
350 *
351 * @return array
352 */
353 protected function getScriptsDictionary()
354 {
355 $scriptsDictionary = [
356 'injector' => [
357 'depicter--injector' => $this->baseAssetsUrl . '/resources/scripts/player/injector.js'
358 ],
359 'player' => [
360 'depicter--player' => $this->baseAssetsUrl . '/resources/scripts/player/depicter.js'
361 ],
362 'widget' => [
363 'depicter-widget' => $this->baseAssetsUrl . '/resources/scripts/widgets/elementor-widget.js'
364 ],
365 'iframe-resizer' => [
366 'depicter-iframe-resizer' => $this->baseAssetsUrl . '/resources/scripts/admin/iframeResizer.min.js'
367 ],
368 'iframe-resizer-content' => [
369 'depicter-iframe-resizer-content' => $this->baseAssetsUrl . '/resources/scripts/admin/iframeResizer.contentWindow.min.js'
370 ]
371 ];
372
373 return $scriptsDictionary;
374 }
375
376 }
377