| @@ -1,0 +1,212 @@ | ||
| 1 | +<?php | |
| 2 | +namespace ABlocks\Blocks\StarRatings; | |
| 3 | + | |
| 4 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | + exit; | |
| 6 | +} | |
| 7 | + | |
| 8 | +use ABlocks\Classes\BlockBaseAbstract; | |
| 9 | +use ABlocks\Classes\CssGenerator; | |
| 10 | +use ABlocks\Classes\CssGeneratorV2; | |
| 11 | +use ABlocks\Controls\Alignment; | |
| 12 | +use ABlocks\Controls\TextShadow; | |
| 13 | +use ABlocks\Controls\TextStroke; | |
| 14 | +use ABlocks\Controls\Typography; | |
| 15 | +use ABlocks\Controls\Icon; | |
| 16 | +use ABlocks\Controls\Range; | |
| 17 | +use ABlocks\Controls\Color; | |
| 18 | + | |
| 19 | +class Block extends BlockBaseAbstract { | |
| 20 | + protected $block_name = 'star-ratings'; | |
| 21 | + | |
| 22 | + public function build_css_v1( $attributes ) { | |
| 23 | + | |
| 24 | + $css_generator = new CssGenerator( $attributes, $this->block_name ); | |
| 25 | + | |
| 26 | + $css_generator->add_class_styles( | |
| 27 | + '{{WRAPPER}} .ablocks-block-container', | |
| 28 | + $this->get_container_css( $attributes ), | |
| 29 | + $this->get_container_css( $attributes, 'Tablet' ), | |
| 30 | + $this->get_container_css( $attributes, 'Mobile' ), | |
| 31 | + ); | |
| 32 | + // rating icon css | |
| 33 | + $css_generator->add_class_styles( | |
| 34 | + '{{WRAPPER}} .ablocks-rating', | |
| 35 | + $this->get_rating_css( $attributes ), | |
| 36 | + $this->get_rating_css( $attributes, 'Tablet' ), | |
| 37 | + $this->get_rating_css( $attributes, 'Mobile' ), | |
| 38 | + ); | |
| 39 | + | |
| 40 | + $css_generator->add_class_styles( | |
| 41 | + '{{WRAPPER}} .ablocks-icon-wrap', | |
| 42 | + Icon::get_wrapper_css( $attributes ), | |
| 43 | + Icon::get_wrapper_css( $attributes, 'Tablet' ), | |
| 44 | + Icon::get_wrapper_css( $attributes, 'Mobile' ), | |
| 45 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return Icon::get_wrapper_css( $attributes, $device ); } ) | |
| 46 | + ); | |
| 47 | + | |
| 48 | + $css_generator->add_class_styles( | |
| 49 | + '{{WRAPPER}} .ablocks-icon-wrap img.ablocks-image-icon', | |
| 50 | + Icon::get_element_image_css( $attributes ), | |
| 51 | + Icon::get_element_image_css( $attributes, 'Tablet' ), | |
| 52 | + Icon::get_element_image_css( $attributes, 'Mobile' ), | |
| 53 | + ); | |
| 54 | + | |
| 55 | + // rating icon spacing css | |
| 56 | + $css_generator->add_class_styles( | |
| 57 | + '{{WRAPPER}} .ablocks-star-ratings-icons', | |
| 58 | + $this->get_rating_icon_spacing_css( $attributes ), | |
| 59 | + $this->get_rating_icon_spacing_css( $attributes, 'Tablet' ), | |
| 60 | + $this->get_rating_icon_spacing_css( $attributes, 'Mobile' ), | |
| 61 | + ); | |
| 62 | + | |
| 63 | + $get_desktop_rating_number_css = $this->get_rating_number_css( $attributes ); | |
| 64 | + if ( ! empty( $attributes['ratingNumberColor'] ) ) { | |
| 65 | + $get_desktop_rating_number_css['color'] = $attributes['ratingNumberColor']; | |
| 66 | + } | |
| 67 | + if ( ! empty( $attributes['ratingNumberPosition'] ) ) { | |
| 68 | + if ( 'left' === $attributes['ratingNumberPosition'] ) { | |
| 69 | + $get_desktop_rating_number_css['order'] = '-5'; | |
| 70 | + } else { | |
| 71 | + $get_desktop_rating_number_css['order'] = '10'; | |
| 72 | + } | |
| 73 | + } | |
| 74 | + // rating number css | |
| 75 | + $css_generator->add_class_styles( | |
| 76 | + '{{WRAPPER}} .ablocks-star-rating-number', | |
| 77 | + $get_desktop_rating_number_css, | |
| 78 | + $this->get_rating_number_css( $attributes, 'Tablet' ), | |
| 79 | + $this->get_rating_number_css( $attributes, 'Mobile' ), | |
| 80 | + ); | |
| 81 | + | |
| 82 | + return $css_generator->generate_css(); | |
| 83 | + } | |
| 84 | + public function build_css_v2( $attributes ) { | |
| 85 | + | |
| 86 | + $css_generator = new CssGeneratorV2( $attributes, $this->block_name ); | |
| 87 | + | |
| 88 | + $css_generator->add_class_styles( | |
| 89 | + '{{WRAPPER}}:not(.ablocks-has-block-container), {{WRAPPER}}.ablocks-block--star-ratings > .ablocks-block-container', | |
| 90 | + $this->get_container_css( $attributes ), | |
| 91 | + $this->get_container_css( $attributes, 'Tablet' ), | |
| 92 | + $this->get_container_css( $attributes, 'Mobile' ), | |
| 93 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_container_css( $attributes, $device ); } ) | |
| 94 | + ); | |
| 95 | + // rating icon css | |
| 96 | + $css_generator->add_class_styles( | |
| 97 | + '{{WRAPPER}} .ablocks-rating', | |
| 98 | + $this->get_rating_css( $attributes ), | |
| 99 | + $this->get_rating_css( $attributes, 'Tablet' ), | |
| 100 | + $this->get_rating_css( $attributes, 'Mobile' ), | |
| 101 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_rating_css( $attributes, $device ); } ) | |
| 102 | + ); | |
| 103 | + | |
| 104 | + $css_generator->add_class_styles( | |
| 105 | + '{{WRAPPER}} .ablocks-icon-wrap', | |
| 106 | + Icon::get_wrapper_css( $attributes ), | |
| 107 | + Icon::get_wrapper_css( $attributes, 'Tablet' ), | |
| 108 | + Icon::get_wrapper_css( $attributes, 'Mobile' ), | |
| 109 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return Icon::get_wrapper_css( $attributes, $device ); } ) | |
| 110 | + ); | |
| 111 | + | |
| 112 | + $css_generator->add_class_styles( | |
| 113 | + '{{WRAPPER}} .ablocks-icon-wrap img.ablocks-image-icon', | |
| 114 | + Icon::get_element_image_css( $attributes ), | |
| 115 | + Icon::get_element_image_css( $attributes, 'Tablet' ), | |
| 116 | + Icon::get_element_image_css( $attributes, 'Mobile' ), | |
| 117 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return Icon::get_element_image_css( $attributes, $device ); } ) | |
| 118 | + ); | |
| 119 | + | |
| 120 | + // rating icon spacing css | |
| 121 | + $css_generator->add_class_styles( | |
| 122 | + '{{WRAPPER}} .ablocks-star-ratings-icons', | |
| 123 | + $this->get_rating_icon_spacing_css( $attributes ), | |
| 124 | + $this->get_rating_icon_spacing_css( $attributes, 'Tablet' ), | |
| 125 | + $this->get_rating_icon_spacing_css( $attributes, 'Mobile' ), | |
| 126 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_rating_icon_spacing_css( $attributes, $device ); } ) | |
| 127 | + ); | |
| 128 | + // rating number css | |
| 129 | + $css_generator->add_class_styles( | |
| 130 | + '{{WRAPPER}} .ablocks-star-rating-number', | |
| 131 | + $this->get_rating_number_css( $attributes ), | |
| 132 | + $this->get_rating_number_css( $attributes, 'Tablet' ), | |
| 133 | + $this->get_rating_number_css( $attributes, 'Mobile' ), | |
| 134 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_rating_number_css( $attributes, $device ); } ) | |
| 135 | + ); | |
| 136 | + | |
| 137 | + return $css_generator->generate_css(); | |
| 138 | + } | |
| 139 | + public function build_css( $attributes ) { | |
| 140 | + if ( isset( $attributes['blockVersion'] ) && (int) $attributes['blockVersion'] === 2 ) { | |
| 141 | + return $this->build_css_v2( $attributes ); | |
| 142 | + } | |
| 143 | + return $this->build_css_v1( $attributes ); | |
| 144 | + } | |
| 145 | + | |
| 146 | + | |
| 147 | + public function get_container_css( $attributes, $device = '' ) { | |
| 148 | + $css = []; | |
| 149 | + $css['display'] = 'flex'; | |
| 150 | + $css['align-items'] = 'center'; | |
| 151 | + $css['flex-wrap'] = 'wrap'; | |
| 152 | + return array_merge( | |
| 153 | + $css, | |
| 154 | + Range::get_css([ | |
| 155 | + 'attributeValue' => $attributes['ratingNumberGap'], | |
| 156 | + 'attribute_object_key' => 'value', | |
| 157 | + 'isResponsive' => true, | |
| 158 | + 'defaultValue' => 0, | |
| 159 | + 'hasUnit' => true, | |
| 160 | + 'unitDefaultValue' => 'px', | |
| 161 | + 'property' => 'gap', | |
| 162 | + 'device' => $device, | |
| 163 | + ]), | |
| 164 | + isset( $attributes['alignment'] ) ? Alignment::get_css( $attributes['alignment'], 'justify-content', $device ) : [], | |
| 165 | + ); | |
| 166 | + } | |
| 167 | + | |
| 168 | + public function get_rating_number_css( $attributes, $device = '' ) { | |
| 169 | + $css = []; | |
| 170 | + if ( ! empty( $attributes['ratingNumberPosition'] ) ) { | |
| 171 | + if ( 'left' === $attributes['ratingNumberPosition'] ) { | |
| 172 | + $css['order'] = '-5'; | |
| 173 | + } else { | |
| 174 | + $css['order'] = '10'; | |
| 175 | + } | |
| 176 | + } | |
| 177 | + $typographyValueGlobal = ! empty( $attributes['ratingNumberTypographyGlobal'] ) ? $attributes['ratingNumberTypographyGlobal'] : array(); | |
| 178 | + // rating number css | |
| 179 | + return array_merge( | |
| 180 | + [ 'color' => Color::get_css( isset( $attributes['ratingNumberColor'] ) ? $attributes['ratingNumberColor'] : '' ) ], | |
| 181 | + Typography::get_css( $attributes['ratingNumberTypography'], '', $device, $typographyValueGlobal ) | |
| 182 | + ); | |
| 183 | + | |
| 184 | + } | |
| 185 | + | |
| 186 | + public function get_rating_css( $attributes, $device = '' ) { | |
| 187 | + $rating_css = []; | |
| 188 | + $size_value = $attributes['size'][ 'value' . $device ] ?? ''; | |
| 189 | + $size_unit = $attributes['size'][ 'valueUnit' . $device ] ?? 'px'; | |
| 190 | + $unit = ! empty( $attributes['gap'][ 'valueUnit' . $device ] ) ? $attributes['gap'][ 'valueUnit' . $device ] : 'px'; | |
| 191 | + | |
| 192 | + if ( ! empty( $size_value ) ) { | |
| 193 | + $rating_css['font-size'] = $size_value . $size_unit; | |
| 194 | + } | |
| 195 | + return $rating_css; | |
| 196 | + } | |
| 197 | + | |
| 198 | + | |
| 199 | + public function get_rating_icon_spacing_css( $attributes, $device = '' ) { | |
| 200 | + $spacing_value = Range::get_css([ | |
| 201 | + 'attributeValue' => $attributes['spacing'], | |
| 202 | + 'attribute_object_key' => 'value', | |
| 203 | + 'isResponsive' => true, | |
| 204 | + 'defaultValue' => 0, | |
| 205 | + 'hasUnit' => true, | |
| 206 | + 'unitDefaultValue' => 'px', | |
| 207 | + 'property' => 'gap', | |
| 208 | + 'device' => $device, | |
| 209 | + ]); | |
| 210 | + return $spacing_value; | |
| 211 | + } | |
| 212 | +} | |