PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.6.0
GenerateBlocks v1.6.0
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / utils / get-background-image / index.js
generateblocks / src / utils / get-background-image Last commit date
index.js 4 years ago
index.js
78 lines
1 import hexToRGBA from '../hex-to-rgba';
2 import getBackgroundImageUrl from '../get-background-image-url';
3
4 import {
5 applyFilters,
6 } from '@wordpress/hooks';
7
8 export default function getBackgroundImageCSS( type, props ) {
9 const attributes = applyFilters( 'generateblocks.editor.cssAttrs', props.attributes, props );
10
11 const {
12 backgroundColor,
13 backgroundColorOpacity,
14 bgImage,
15 bgImageInline,
16 gradient,
17 bgOptions,
18 gradientColorOne,
19 gradientColorOneOpacity,
20 gradientColorTwo,
21 gradientColorTwoOpacity,
22 gradientColorStopOne,
23 gradientColorStopTwo,
24 gradientDirection,
25 useDynamicData,
26 dynamicContentType,
27 } = attributes;
28
29 let gradientValue = '';
30
31 if ( gradient ) {
32 let gradientColorStopOneValue = '',
33 gradientColorStopTwoValue = '';
34
35 const gradientColorOneValue = hexToRGBA( gradientColorOne, gradientColorOneOpacity );
36 const gradientColorTwoValue = hexToRGBA( gradientColorTwo, gradientColorTwoOpacity );
37
38 if ( gradientColorOne && '' !== gradientColorStopOne ) {
39 gradientColorStopOneValue = ' ' + gradientColorStopOne + '%';
40 }
41
42 if ( gradientColorTwo && '' !== gradientColorStopTwo ) {
43 gradientColorStopTwoValue = ' ' + gradientColorStopTwo + '%';
44 }
45
46 gradientValue = 'linear-gradient(' + gradientDirection + 'deg, ' + gradientColorOneValue + gradientColorStopOneValue + ', ' + gradientColorTwoValue + gradientColorStopTwoValue + ')';
47 }
48
49 if ( 'gradient' === type ) {
50 return gradientValue;
51 }
52
53 let backgroundImage = false;
54
55 const backgroundColorValue = hexToRGBA( backgroundColor, backgroundColorOpacity );
56
57 if ( !! bgImage || ( useDynamicData && '' !== dynamicContentType ) ) {
58 const url = getBackgroundImageUrl( props );
59
60 if ( 'element' === bgOptions.selector && ( backgroundColorValue || gradient ) && 'undefined' !== typeof bgOptions.overlay && bgOptions.overlay ) {
61 // Old background image overlays mixed with our gradients.
62 if ( gradient ) {
63 backgroundImage = gradientValue + ', url(' + url + ')';
64 } else if ( backgroundColorValue ) {
65 backgroundImage = 'linear-gradient(0deg, ' + backgroundColorValue + ', ' + backgroundColorValue + '), url(' + url + ')';
66 }
67 } else {
68 backgroundImage = 'url(' + url + ')';
69
70 if ( bgImageInline && 'element' !== bgOptions.selector ) {
71 backgroundImage = 'var(--background-image)';
72 }
73 }
74 }
75
76 return backgroundImage;
77 }
78