PluginProbe
Depicter — Popup & Slider Builder / 1.6.2
Depicter — Popup & Slider Builder v1.6.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 / Document / Models / Common / Background.php

Background.php in Depicter — Popup & Slider Builder 1.6.2, at app/src/Document/Models/Common/Background.php

367 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Document\Models\Common;
3
4
5 use Averta\Core\Utility\Arr;
6 use Averta\WordPress\Utility\JSON;
7 use Depicter\Document\CSS\Breakpoints;
8 use Depicter\Document\CSS\Selector;
9 use Depicter\Document\Models\Traits\HasDataSheetTrait;
10 use Depicter\Html\Html;
11 use Depicter\Services\MediaBridge;
12
13 class Background
14 {
15 use HasDataSheetTrait;
16
17 /**
18 * @var Color
19 */
20 public $color;
21
22 /**
23 * @var string
24 */
25 public $fitMode;
26
27 /**
28 * @var object
29 */
30 public $video;
31
32 /**
33 * @var object
34 */
35 public $image;
36
37 /**
38 * @var Color
39 */
40 public $overlay;
41
42 /**
43 * @var Styles\Filter
44 */
45 public $filter;
46
47 /**
48 * @var array
49 */
50 public $kenBurnsData = [];
51
52 /**
53 * Check if section has background or not
54 *
55 * @return boolean
56 */
57 public function hasBackground() {
58 return !empty( $this->image->src ) || !empty( $this->video->src );
59 }
60
61
62 /**
63 * @return Html|string
64 */
65 public function render() {
66
67 $html = '';
68
69 if ( !empty( $this->image->src ) ) {
70 $originalSrc = $this->image->src;
71 $this->image->src = $this->maybeReplaceDataSheetTags( $this->image->src );
72 $html .= $this->renderImage( \Depicter::media()->getSourceUrl( $this->image->src ) );
73 // restore original image src
74 $this->image->src = $originalSrc;
75 }
76
77 if ( !empty( $this->video->src ) ) {
78 if ( $this->video->type == 'embedVideo') {
79 $html .= $this->renderEmbedVideo();
80 } else {
81 $html .= $this->renderVideo( \Depicter::media()->getSourceUrl( $this->video->src ) );
82 }
83 }
84
85 return $html;
86
87 }
88
89 /**
90 * render images
91 * @param $image
92 *
93 * @return Html
94 */
95 protected function renderImage( $imageUrl ) {
96 $imageSrcSet = is_numeric( $this->image->src ) ? \Depicter::media()->getSrcSet( $this->image->src, 'full' ) : '';
97
98 $args = [
99 'class' => 'depicter-bg',
100 'src' => \Depicter::media()::IMAGE_PLACEHOLDER_SRC,
101 'data-src' => $imageUrl,
102 'alt' => is_numeric( $this->image->src ) ? \Depicter::media()->getAltText( $this->image->src ) : ''
103 ];
104
105 if ( !empty( $this->kenBurnsData ) ) {
106 $args = Arr::merge( $args, $this->kenBurnsData );
107 }
108
109 if ( $imageSrcSet ) {
110 $args['data-srcset'] = $imageSrcSet;
111 }
112
113 $available_args = [
114 'responsiveArgs' => [
115 'data-object-fit' => 'fitMode',
116 'data-object-position' => 'position'
117 ]
118 ];
119
120 $args = $this->getElementAttributes( 'image', $args, $available_args );
121
122 $cropAttributes = $this->getCropAttributes( 'image' );
123
124 return Html::img( $imageUrl, Arr::merge( $cropAttributes, $args ) );
125 }
126
127 /**
128 * Renders video tag
129 *
130 * @param $videoUrl
131 *
132 * @return \TypeRocket\Html\Html
133 */
134 public function renderVideo( $videoUrl ) {
135
136 $args = [
137 'class' => Selector::prefixify( 'bg-video' ),
138 'src' => $videoUrl,
139 'preload' => 'metadata',
140 'playsinline' => "true"
141 ];
142
143 $available_args = [
144 'muted' => 'muted',
145 'loop' => 'loop',
146 'data-goto-next' => 'goNextSlide',
147 'data-auto-pause' => 'pause',
148 'responsiveArgs' => [
149 'data-object-fit' => 'fitMode',
150 'data-object-position' => 'position'
151 ]
152 ];
153
154 $args = $this->getElementAttributes( 'video', $args, $available_args );
155
156 return Html::video( $args );
157 }
158
159 /**
160 * Get crop attributes
161 *
162 * @param string $type
163 *
164 * @return array
165 */
166 protected function getCropAttributes( string $type = 'image' ){
167 if ( ! $this->hasCustomFitMode( $type ) ) {
168 return [];
169 }
170
171 $attributes = [];
172 $breakpointNames = Breakpoints::names();
173
174 foreach ( $breakpointNames as $device ) {
175 if( ! empty( $this->{$type}->cropData->{$device} ) ){
176 $attributeDeviceValue = $this->{$type}->cropData->{$device};
177 $attributeDeviceValue = is_object( $attributeDeviceValue ) || is_array( $attributeDeviceValue ) ? JSON::encode( $attributeDeviceValue ) : '';
178 $attributeDeviceName = $device === 'default' ? 'data-crop' : "data-{$device}-crop";
179 $attributes[ $attributeDeviceName ] = $attributeDeviceValue;
180 }
181 }
182
183 return $attributes;
184 }
185
186 /**
187 * Whether background types has custom fit mode or not
188 *
189 * @param string $type
190 *
191 * @return bool
192 */
193 protected function hasCustomFitMode( string $type = 'image' ){
194 if ( empty( $this->{$type}->fitMode->default ) ) {
195 return false;
196 }
197
198 $hasCustomFitMode= false;
199 $breakpointNames = Breakpoints::names();
200
201 foreach ( $breakpointNames as $device ) {
202 if( !empty( $this->{$type}->fitMode->{$device} ) && $this->{$type}->fitMode->{$device} === 'custom' ){
203 return true;
204 }
205 }
206
207 return false;
208 }
209
210 public function getElementAttributes( $element_type, $args, $available_args ) {
211 foreach ( $available_args as $attribute => $property ) {
212 // for object properties that has responsive value like fitMode and position
213 if ( $attribute == 'responsiveArgs' ) {
214 foreach ( $property as $key => $value ) {
215 $breakpointNames = Breakpoints::names();
216 if ( !empty( $this->{$element_type}->{$value} ) ) {
217 foreach ( $breakpointNames as $device ) {
218 $args[ $key ][] = !empty( $this->{$element_type}->{$value}->{$device} ) ? $this->{$element_type}->{$value}->{$device} : '';
219 }
220 $args[ $key ] = implode(',', $args[ $key ] );
221 }
222 }
223
224 // for simple object properties
225 } elseif ( !empty( $this->{$element_type}->{$property} ) ) {
226 $value = $this->{$element_type}->{$property} == "1" ? "true" : $this->{$element_type}->{$property};
227 $args[ $attribute ] = $value;
228 }
229
230 // In video background, if each of `muted` or `loop` are not set, consider it as `true` by default
231 if( $element_type === 'video' ){
232 if( in_array( $property, ['muted', 'loop'] ) ){
233 if( ! isset( $this->{$element_type}->{$property} ) ){
234 $args[ $attribute ] = 'true';
235 }
236 }
237 }
238 }
239 return $args;
240 }
241
242 /**
243 * Get css background color
244 *
245 * @return array
246 */
247 public function getColor() {
248 $devices = Breakpoints::names();
249
250 $css = [];
251 foreach ( $devices as $device ) {
252 if ( !empty( $this->color->{$device} ) ) {
253 if ( false == strpos( $this->color->{$device}, 'gradient' ) ) {
254 $css[ $device]['background-color'] = $this->color->{$device};
255 } else {
256 $css[ $device]['background-image'] = $this->color->{$device};
257 }
258 }
259 }
260
261 return $css;
262 }
263
264 /**
265 * Get background overlay styles
266 *
267 * @return array
268 */
269 public function getOverlayStyles() {
270 $default = [
271 "content" => '""',
272 "display" => "block",
273 "position"=> "absolute",
274 "top" => "0",
275 "bottom" => "0",
276 "right" => "0",
277 "left" => "0",
278 "z-index" => "1",
279 ];
280
281 $devices = Breakpoints::names();
282
283 $css = [];
284 foreach ( $devices as $device ) {
285 if ( !empty( $this->overlay->{$device} ) && ('transparent' !== $this->overlay->{$device} ) ) {
286 $css[ $device] = $default;
287 if ( false == strpos( $this->overlay->{$device}, 'gradient' ) ) {
288 $css[ $device]['background-color'] = $this->overlay->{$device};
289 } else {
290 $css[ $device]['background-image'] = $this->overlay->{$device};
291 }
292 }
293 }
294
295 return $css;
296 }
297
298 /**
299 * Get filter styles of background
300 *
301 * @return array
302 */
303 public function getSectionBackgroundFilter() {
304 if( empty( $this->filter ) ){
305 return [];
306 }
307 return $this->filter->set([]);
308 }
309
310 /**
311 * Get class name of background container
312 *
313 * @return string
314 */
315 public function getContainerClassName() {
316 if ( !empty( $this->video->src ) ) {
317 return Selector::prefixify('bg-video-container');
318 }
319 return Selector::prefixify('section-background');
320 }
321
322 /**
323 * Set kenburns data
324 * @param array $kenBurnsData
325 *
326 * @return void
327 */
328 public function setKenBurnsData( array $kenBurnsData = [] ) {
329 $this->kenBurnsData = $kenBurnsData;
330 }
331
332 /**
333 * Renders embed video tag
334 *
335 * @param $videoUrl
336 *
337 * @return \TypeRocket\Html\Html
338 */
339 public function renderEmbedVideo() {
340 $src = $this->video->src;
341 if ( $this->video->embedType == 'youtube' ) {
342 $src = add_query_arg([
343 'controls' => '0',
344 'mute' => '1',
345 'rel' => '0'
346 ], $this->video->src );
347 } else if ( $this->video->embedType == 'vimeo' ) {
348 $src = add_query_arg([
349 'controls' => '0',
350 'background' => '1'
351 ], $this->video->src );
352 }
353
354 $iframe = Html::iframe([
355 'src' => $src,
356 'data-type' => $this->video->embedType,
357 'data-width' => $this->video->size->width,
358 'data-height' => $this->video->size->height,
359 'data-goto-next' => $this->video->goNextSlide ?? false,
360 'data-auto-pause' => $this->video->pause ?? false,
361 'data-loop' => $this->video->loop ?? true
362 ]);
363
364 return Html::div(['class' => 'depicter-bg-embed'], $iframe );
365 }
366 }
367