Animation.php
1 year ago
Background.php
1 year ago
BackgroundImage.php
1 year ago
Border.php
2 years ago
BoxShadow.php
1 year ago
ColumnWidth.php
1 year ago
CustomHeight.php
1 year ago
CustomSize.php
4 years ago
Gap.php
1 year ago
Height.php
1 year ago
JustifyContent.php
1 year ago
MaxWidth.php
1 year ago
MultipleImage.php
1 year ago
ObjectCss.php
1 year ago
Opacity.php
1 year ago
Property.php
1 year ago
PropertyBase.php
1 year ago
Size.php
1 year ago
Stroke.php
1 year ago
TBLR.php
1 year ago
TextShadow.php
1 year ago
Transform.php
2 years ago
Transition.php
1 year ago
Typography.php
1 month ago
UnitValuePercentage.php
1 year ago
UnitValuePx.php
1 year ago
ValueProxy.php
4 years ago
Width.php
1 year ago
BackgroundImage.php
200 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Core\StyleManager\Props; |
| 4 | |
| 5 | use Kubio\Core\StyleManager\ParserUtils; |
| 6 | use function is_string; |
| 7 | use function join; |
| 8 | |
| 9 | /** |
| 10 | * This class is capable of managing string type values for `background-size` like 'contain` or `cover` but it can |
| 11 | * also return a x/y based size like `50% 50%` from an array like ['x' => ['value' => 50], y => ['value' => 50]] |
| 12 | * |
| 13 | * Class BackgroundImageSize |
| 14 | * @package KubioCore\StyleManager\Props |
| 15 | */ |
| 16 | class BackgroundImageSize extends Property { |
| 17 | |
| 18 | public $properties = array( |
| 19 | 'anyOf' => array( |
| 20 | array( |
| 21 | 'type' => 'string', |
| 22 | 'default' => 'auto', |
| 23 | ), |
| 24 | array( |
| 25 | 'type' => 'CustomSize', |
| 26 | 'default' => 'auto', |
| 27 | ), |
| 28 | ), |
| 29 | ); |
| 30 | |
| 31 | public function __toString() { |
| 32 | if ( is_string( $this->value ) ) { |
| 33 | return $this->value; |
| 34 | } |
| 35 | |
| 36 | return $this->computeCustomSizeStyle( $this->value ); |
| 37 | } |
| 38 | |
| 39 | public function computeCustomSizeStyle( $customSize ) { |
| 40 | if ( ! is_array( $customSize ) ) { |
| 41 | return ''; |
| 42 | } |
| 43 | |
| 44 | $bgSizeX = ParserUtils::toValueUnitString( $customSize['x'], 'auto' ); |
| 45 | $bgSizeY = ParserUtils::toValueUnitString( $customSize['y'], 'auto' ); |
| 46 | |
| 47 | return join( ' ', array( $bgSizeX, $bgSizeY ) ); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | class BackgroundImagePosition extends Property { |
| 52 | |
| 53 | public function __toString() { |
| 54 | if ( is_string( $this->value ) ) { |
| 55 | return $this->value; |
| 56 | } |
| 57 | |
| 58 | return ParserUtils::toJoinedValueUnitString( |
| 59 | array( |
| 60 | array( |
| 61 | 'value' => $this->get( 'x' ), |
| 62 | 'unit' => '%', |
| 63 | ), |
| 64 | array( |
| 65 | 'value' => $this->get( 'y' ), |
| 66 | 'unit' => '%', |
| 67 | ), |
| 68 | ) |
| 69 | ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | class BackgroundImageSource extends Property { |
| 74 | |
| 75 | public function __toString() { |
| 76 | if ( is_string( $this->value ) ) { |
| 77 | return $this->value; |
| 78 | } |
| 79 | |
| 80 | return $this->getBackgroundImage( (object) $this->value ); |
| 81 | } |
| 82 | |
| 83 | public function getBackgroundImage( $source ) { |
| 84 | switch ( $source->type ) { |
| 85 | case 'image': |
| 86 | return property_exists( $source, 'url' ) ? 'url("' . $source->url . '")' : ''; |
| 87 | case 'gradient': |
| 88 | return property_exists( $source, 'gradient' ) ? $source->gradient : ''; |
| 89 | } |
| 90 | |
| 91 | return ''; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | class FeaturedImageBackgroundImageSource extends Property { |
| 96 | public function __toString() { |
| 97 | if ( is_string( $this->value ) ) { |
| 98 | return $this->value; |
| 99 | } |
| 100 | |
| 101 | return $this->getBackgroundImage( (object) $this->value ); |
| 102 | } |
| 103 | |
| 104 | public function getBackgroundImage( $source ) { |
| 105 | global $post; |
| 106 | |
| 107 | $page_for_posts = get_option( 'page_for_posts' ); |
| 108 | $page_id = $post->ID; |
| 109 | |
| 110 | // Handle the case when we are on blog page |
| 111 | if ( is_home() && $page_for_posts ) { |
| 112 | $page_id = $page_for_posts; |
| 113 | } |
| 114 | |
| 115 | $featured_image = get_the_post_thumbnail_url( $page_id, 'full' ); |
| 116 | |
| 117 | // if we have a featured image we use it as a source. |
| 118 | if ( ! empty( $featured_image ) ) { |
| 119 | switch ( $source->type ) { |
| 120 | case 'image': |
| 121 | return 'url("' . $featured_image . '")'; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // otherwise, we keep returning the default image. |
| 126 | switch ( $source->type ) { |
| 127 | case 'image': |
| 128 | return 'url("' . $source->url . '")'; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | class BackgroundImage extends Property { |
| 134 | public $properties = array( |
| 135 | 'size' => array( 'BackgroundImageSize' ), |
| 136 | 'position' => array( 'BackgroundImagePosition' ), |
| 137 | 'source' => array( 'BackgroundImageSource' ), |
| 138 | ); |
| 139 | |
| 140 | public $map = array( |
| 141 | 'backgroundImage' => 'source', |
| 142 | 'backgroundSize' => 'size', |
| 143 | 'backgroundPosition' => 'position', |
| 144 | 'backgroundAttachment' => 'attachment', |
| 145 | 'backgroundRepeat' => 'repeat', |
| 146 | ); |
| 147 | |
| 148 | public function __construct( $value, $default = array() ) { |
| 149 | parent::__construct( $value, $default ); |
| 150 | |
| 151 | $source = $this->get( 'source' ); |
| 152 | if ( $this->get( 'useFeaturedImage' ) && $source['type'] === 'image' ) { |
| 153 | $this->properties['source'] = array( 'FeaturedImageBackgroundImageSource' ); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | public function toStyle() { |
| 158 | $computedStyle = array(); |
| 159 | $source = $this->get( 'source' ); |
| 160 | $map = $this->map; |
| 161 | if ( $source['type'] === 'gradient' ) { |
| 162 | $map = array( |
| 163 | 'backgroundImage' => 'source', |
| 164 | 'backgroundAttachment' => 'attachment', |
| 165 | ); |
| 166 | } |
| 167 | |
| 168 | $bgImage = $source['url']; |
| 169 | if ( ! empty( $bgImage ) ) { |
| 170 | $translatedImageUrl = kubio_wpml_get_translated_media_url( $bgImage ); |
| 171 | $this->value['source']['url'] = $translatedImageUrl; |
| 172 | } |
| 173 | foreach ( $map as $name => $property ) { |
| 174 | if ( isset( $this->properties[ $property ] ) ) { |
| 175 | $propertyDefinition = $this->properties[ $property ]; |
| 176 | $class = |
| 177 | 'Kubio\\Core\\StyleManager\\Props\\' . |
| 178 | $propertyDefinition[0]; |
| 179 | $instance = new $class( $this->get( $property ) ); |
| 180 | $computedStyle[ $name ] = $instance->__toString(); |
| 181 | |
| 182 | // a custom background is actually an array with x and y values, so we need to treat that case. |
| 183 | if ( |
| 184 | $propertyDefinition[0] === 'BackgroundImageSize' && |
| 185 | $computedStyle[ $name ] === 'custom' |
| 186 | ) { |
| 187 | $customSizeInstance = new BackgroundImageSize( |
| 188 | $this->get( 'sizeCustom' ) |
| 189 | ); |
| 190 | $computedStyle[ $name ] = $customSizeInstance->__toString(); |
| 191 | } |
| 192 | } else { |
| 193 | $computedStyle[ $name ] = $this->get( $property ); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return $computedStyle; |
| 198 | } |
| 199 | } |
| 200 |