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

314 lines 7.2 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 }
114 }
115
116 return $cssLinksToEnqueue ;
117 }
118
119 return [];
120 }
121
122 /**
123 * Enqueues a list of custom styles based on given document IDs
124 *
125 * @param string|array $documentIDs List of slider IDs for slugs
126 *
127 * @return array
128 */
129 public function enqueueCustomStyles( $documentIDs )
130 {
131 $cssLinksToEnqueue = $this->getCustomStyles( $documentIDs );
132
133 foreach( $cssLinksToEnqueue as $cssId => $cssLink ){
134 \Depicter::core()->assets()->enqueueStyle( $cssId, $cssLink );
135 }
136
137 return $cssLinksToEnqueue;
138 }
139
140 /**
141 * Enqueues custom google fonts for given document IDs
142 *
143 * @param string|array $documentIDs List of slider IDs for slugs
144 *
145 * @return array
146 */
147 public function enqueueCustomGoogleFonts( $documentIDs )
148 {
149 $cssLinksToEnqueue = $this->getCustomStyles( $documentIDs );
150
151 foreach( $cssLinksToEnqueue as $cssId => $cssLink ){
152 if ( false !== strpos( $cssId, 'google-font' ) ) {
153 \Depicter::core()->assets()->enqueueStyle( $cssId, $cssLink );
154 }
155 }
156
157 return $cssLinksToEnqueue;
158 }
159
160 /**
161 * Enqueues custom styles of a document
162 *
163 * @param int $documentID
164 * @param bool $isPrivilegedUser
165 *
166 * @return void
167 */
168 public function enqueueCustomAssets( $documentID, $isPrivilegedUser = null ){
169 $isPrivilegedUser = is_null( $isPrivilegedUser ) ? \Depicter::authorization()->currentUserCanPublishDocument() : $isPrivilegedUser;
170
171 if ( $isPrivilegedUser ) {
172 $this->enqueueCustomGoogleFonts( $documentID );
173 } else {
174 $this->enqueueCustomStyles( $documentID );
175 }
176
177 do_action( 'depicter/after/enqueue/styles/' . $documentID );
178 }
179
180 /**
181 * Enqueues styles and scripts of a document
182 *
183 * @param int $documentID
184 * @param bool $isPrivilegedUser
185 *
186 * @return void
187 */
188 public function enqueueAssets( $documentID, $isPrivilegedUser = null ){
189 $isPrivilegedUser = is_null( $isPrivilegedUser ) ? \Depicter::authorization()->currentUserCanPublishDocument() : $isPrivilegedUser;
190
191 if ( ! did_action( 'depicter/after/enqueue/styles' ) ) {
192 $this->enqueueStyles();
193 }
194
195 if ( !did_action('depicter/after/enqueue/styles/' . $documentID ) ) {
196 $this->enqueueCustomAssets( $documentID, $isPrivilegedUser );
197 }
198
199 if ( ! did_action( 'depicter/after/enqueue/scripts' ) ) {
200 $this->enqueueScripts();
201 }
202 }
203
204 /**
205 * Print media preload tags of a document if exists
206 *
207 * @param int $documentID
208 *
209 * @return void
210 */
211 public function printPreloadTags( $documentID ) {
212 if( false !== $preloadTags = \Depicter::cache('document')->get( $documentID . '_preload_tags' ) ){
213 echo Sanitize::html( $preloadTags );
214 }
215 }
216
217 /**
218 *
219 * @param int $documentID
220 *
221 * @return void
222 */
223 public function enqueuePreloadTags( $documentID ) {
224 add_action( 'wp_head', function () use ( $documentID ) {
225 $this->printPreloadTags( $documentID );
226 }, 10 );
227 }
228
229 /**
230 * Searches for group(s) in list of given assets and returns result in a list
231 *
232 * @param string|array $group A group name or list of groups in array
233 * @param array $assets
234 *
235 * @return array
236 */
237 protected function getAssetFromList( $group = 'common', $assets = [] )
238 {
239 if( empty( $group ) ){
240 return $assets;
241 }
242
243 if( is_array( $group ) ){
244 if( count( $group ) > 1 ){
245 $result = [];
246 foreach ( $group as $groupKey ){
247 if( $value = Arr::searchKeyDeep( $assets, $groupKey ) ){
248 if( is_string( $value ) ){
249 $result[ $groupKey ] = $value;
250 } else {
251 $result = $result + $value;
252 }
253 }
254 }
255 return $result;
256 }
257 }
258
259 $group = (string) $group;
260 $result = [];
261
262 if( $value = Arr::searchKeyDeep( $assets, $group ) ){
263 if( is_string( $value ) ){
264 $result = [ $group => $value ];
265 } elseif( is_array( $value ) ) {
266 $result = $value;
267 }
268 }
269
270 return $result;
271 }
272
273 /**
274 * Full list of front styles
275 *
276 * @return array
277 */
278 protected function getStylesDictionary()
279 {
280 $stylesDictionary = [
281 'situational' => [
282 'minireset' => $this->baseAssetsUrl . '/resources/styles/frontend/reset.min.css',
283 'depicter-preview-style' => $this->baseAssetsUrl . '/resources/styles/player/preview.css'
284 ],
285 'common' => [
286 'depicter-front-pre' => $this->baseAssetsUrl . '/resources/styles/player/depicter-pre.css',
287 'depicter--front-common' => $this->baseAssetsUrl . '/resources/styles/player/depicter.css'
288 ]
289 ];
290
291 return $stylesDictionary;
292 }
293
294 /**
295 * Full list of front scripts
296 *
297 * @return array
298 */
299 protected function getScriptsDictionary()
300 {
301 $scriptsDictionary = [
302 'player' => [
303 'depicter--player' => $this->baseAssetsUrl . '/resources/scripts/player/depicter.js'
304 ],
305 'widget' => [
306 'depicter-widget' => $this->baseAssetsUrl . '/resources/scripts/widgets/elementor-widget.js'
307 ]
308 ];
309
310 return $scriptsDictionary;
311 }
312
313 }
314