| 1 |
<?php |
| 2 |
|
| 3 |
namespace Getwid\Blocks; |
| 4 |
|
| 5 |
class AcfBackgroundImage extends \Getwid\Blocks\AbstractBlock { |
| 6 |
|
| 7 |
protected static $blockName = 'getwid/template-acf-background-image'; |
| 8 |
protected static $assetsHandle = 'getwid/template-parts/acf'; |
| 9 |
|
| 10 |
public function __construct() { |
| 11 |
|
| 12 |
parent::__construct( self::$blockName ); |
| 13 |
|
| 14 |
register_block_type( |
| 15 |
self::$blockName, |
| 16 |
array( |
| 17 |
'attributes' => array( |
| 18 |
'customField' => array( |
| 19 |
'type' => 'string' |
| 20 |
), |
| 21 |
'imageSize' => array( |
| 22 |
'type' => 'string', |
| 23 |
'default' => 'large' |
| 24 |
), |
| 25 |
|
| 26 |
//Content |
| 27 |
'minHeight' => array( |
| 28 |
'type' => 'string' |
| 29 |
), |
| 30 |
'contentMaxWidth' => array( |
| 31 |
'type' => 'number' |
| 32 |
), |
| 33 |
|
| 34 |
// Padding |
| 35 |
'paddingTopValue' => array( |
| 36 |
'type' => 'string' |
| 37 |
), |
| 38 |
'paddingBottomValue' => array( |
| 39 |
'type' => 'string' |
| 40 |
), |
| 41 |
'paddingLeftValue' => array( |
| 42 |
'type' => 'string' |
| 43 |
), |
| 44 |
'paddingRightValue' => array( |
| 45 |
'type' => 'string' |
| 46 |
), |
| 47 |
'paddingTop' => array( |
| 48 |
'type' => 'string', |
| 49 |
'default' => '' |
| 50 |
), |
| 51 |
'paddingBottom' => array( |
| 52 |
'type' => 'string', |
| 53 |
'default' => '' |
| 54 |
), |
| 55 |
'paddingLeft' => array( |
| 56 |
'type' => 'string', |
| 57 |
'default' => '' |
| 58 |
), |
| 59 |
'paddingRight' => array( |
| 60 |
'type' => 'string', |
| 61 |
'default' => '' |
| 62 |
), |
| 63 |
'paddingTopTablet' => array( |
| 64 |
'type' => 'string', |
| 65 |
'default' => '' |
| 66 |
), |
| 67 |
'paddingBottomTablet' => array( |
| 68 |
'type' => 'string', |
| 69 |
'default' => '' |
| 70 |
), |
| 71 |
'paddingLeftTablet' => array( |
| 72 |
'type' => 'string', |
| 73 |
'default' => '' |
| 74 |
), |
| 75 |
'paddingRightTablet' => array( |
| 76 |
'type' => 'string', |
| 77 |
'default' => '' |
| 78 |
), |
| 79 |
'paddingTopMobile' => array( |
| 80 |
'type' => 'string', |
| 81 |
'default' => '' |
| 82 |
), |
| 83 |
'paddingBottomMobile' => array( |
| 84 |
'type' => 'string', |
| 85 |
'default' => '' |
| 86 |
), |
| 87 |
'paddingLeftMobile' => array( |
| 88 |
'type' => 'string', |
| 89 |
'default' => '' |
| 90 |
), |
| 91 |
'paddingRightMobile' => array( |
| 92 |
'type' => 'string', |
| 93 |
'default' => '' |
| 94 |
), |
| 95 |
|
| 96 |
//Alignment |
| 97 |
'verticalAlign' => array( |
| 98 |
'type' => 'string', |
| 99 |
'default' => 'center' |
| 100 |
), |
| 101 |
'verticalAlignTablet' => array( |
| 102 |
'type' => 'string', |
| 103 |
'default' => '' |
| 104 |
), |
| 105 |
'verticalAlignMobile' => array( |
| 106 |
'type' => 'string', |
| 107 |
'default' => '' |
| 108 |
), |
| 109 |
'horizontalAlign' => array( |
| 110 |
'type' => 'string', |
| 111 |
'default' => 'center' |
| 112 |
), |
| 113 |
'horizontalAlignTablet' => array( |
| 114 |
'type' => 'string', |
| 115 |
'default' => '' |
| 116 |
), |
| 117 |
'horizontalAlignMobile' => array( |
| 118 |
'type' => 'string', |
| 119 |
'default' => '' |
| 120 |
), |
| 121 |
|
| 122 |
//foreground |
| 123 |
'foregroundOpacity' => array( |
| 124 |
'type' => 'number', |
| 125 |
'default' => 35 |
| 126 |
), |
| 127 |
'foregroundColor' => array( |
| 128 |
'type' => 'string' |
| 129 |
), |
| 130 |
'foregroundFilter' => array( |
| 131 |
'type' => 'string' |
| 132 |
), |
| 133 |
'foregroundGradientType' => array( |
| 134 |
'type' => 'string' |
| 135 |
), |
| 136 |
'foregroundGradientFirstColor' => array( |
| 137 |
'type' => 'string' |
| 138 |
), |
| 139 |
'foregroundGradientFirstColorLocation' => array( |
| 140 |
'type' => 'number', |
| 141 |
'default' => 0 |
| 142 |
), |
| 143 |
'foregroundGradientSecondColor' => array( |
| 144 |
'type' => 'string' |
| 145 |
), |
| 146 |
'foregroundGradientSecondColorLocation' => array( |
| 147 |
'type' => 'number', |
| 148 |
'default' => 100 |
| 149 |
), |
| 150 |
'foregroundGradientAngle' => array( |
| 151 |
'type' => 'number', |
| 152 |
'default' => 180 |
| 153 |
), |
| 154 |
'className' => array( |
| 155 |
'type' => 'string' |
| 156 |
), |
| 157 |
), |
| 158 |
'render_callback' => [$this, 'render_callback'], |
| 159 |
) |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
public function block_frontend_assets() { |
| 164 |
|
| 165 |
if ( is_admin() ) { |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) { |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
add_filter( 'getwid/optimize/assets', |
| 174 |
function ( $assets ) { |
| 175 |
$assets[] = self::$assetsHandle; |
| 176 |
$assets[] = getwid()->settings()->getPrefix() . '-blocks-common'; |
| 177 |
|
| 178 |
return $assets; |
| 179 |
} |
| 180 |
); |
| 181 |
|
| 182 |
add_filter( 'getwid/optimize/should_load_common_css', '__return_true' ); |
| 183 |
|
| 184 |
$rtl = is_rtl() ? '.rtl' : ''; |
| 185 |
|
| 186 |
wp_enqueue_style( |
| 187 |
self::$assetsHandle, |
| 188 |
getwid_get_plugin_url( 'assets/blocks/template-parts/acf/style' . $rtl . '.css' ), |
| 189 |
[], |
| 190 |
getwid()->settings()->getVersion() |
| 191 |
); |
| 192 |
} |
| 193 |
|
| 194 |
public function render_callback( $attributes, $content ) { |
| 195 |
|
| 196 |
//Not BackEnd render if we view from template page |
| 197 |
if ( (get_post_type() == getwid()->postTemplatePart()->postType) || (get_post_type() == 'revision') ) { |
| 198 |
return $content; |
| 199 |
} |
| 200 |
|
| 201 |
$block_name = 'wp-block-getwid-template-acf-background-image'; |
| 202 |
$wrapper_class = $block_name; |
| 203 |
|
| 204 |
if ( isset( $attributes['className'] ) ) { |
| 205 |
$wrapper_class .= ' ' . esc_attr( $attributes['className'] ); |
| 206 |
} |
| 207 |
|
| 208 |
if ( isset( $attributes['customField'] ) ) { |
| 209 |
$wrapper_class .= ' ' . 'custom-field-' . esc_attr( $attributes['customField'] ); |
| 210 |
} |
| 211 |
|
| 212 |
$wrapper_style = ''; |
| 213 |
//Classes |
| 214 |
if ( isset( $attributes['minHeight'] ) ) { |
| 215 |
$wrapper_style .= 'min-height: ' . esc_attr( $attributes['minHeight'] ) . ';'; |
| 216 |
} |
| 217 |
|
| 218 |
$imageSize = ((isset( $attributes['imageSize'] ) && $attributes['imageSize']) ? $attributes['imageSize'] : 'post-thumbnail'); |
| 219 |
|
| 220 |
$current_post = get_post( get_the_ID() ); |
| 221 |
|
| 222 |
//Content Slide style |
| 223 |
$content_container_style = ''; |
| 224 |
|
| 225 |
if ( isset( $attributes['contentMaxWidth'] ) ) { |
| 226 |
$content_container_style .= 'max-width: ' . esc_attr( $attributes['contentMaxWidth'] ) . 'px;'; |
| 227 |
} |
| 228 |
|
| 229 |
//Padding |
| 230 |
$content_container_class = $block_name . '__content'; |
| 231 |
|
| 232 |
getwid_custom_paddings_style_and_class( $wrapper_style, $wrapper_class, $attributes ); |
| 233 |
|
| 234 |
getwid_custom_alignment_classes( $wrapper_class, $attributes ); |
| 235 |
|
| 236 |
|
| 237 |
//Foreground style |
| 238 |
$foreground_style = ''; |
| 239 |
$foreground_class = $block_name . '__foreground'; |
| 240 |
|
| 241 |
if ( isset( $attributes['foregroundGradientType'] ) ) { |
| 242 |
getwid_custom_gradient_styles( 'foreground', $foreground_style, $attributes ); |
| 243 |
} |
| 244 |
|
| 245 |
if ( isset( $attributes['foregroundOpacity'] ) && $attributes['foregroundOpacity'] != 35 ) { |
| 246 |
$foreground_class .= ' getwid-opacity-' . esc_attr( $attributes['foregroundOpacity'] ); |
| 247 |
} |
| 248 |
|
| 249 |
if ( isset( $attributes['foregroundColor'] ) ) { |
| 250 |
$foreground_style .= 'background-color: ' . esc_attr( $attributes['foregroundColor'] ) . ';'; |
| 251 |
} |
| 252 |
|
| 253 |
if ( isset( $attributes['foregroundFilter'] ) ) { |
| 254 |
$foreground_style .= 'mix-blend-mode: ' . esc_attr( $attributes['foregroundFilter'] ) . ';'; |
| 255 |
} |
| 256 |
|
| 257 |
$result = ''; |
| 258 |
|
| 259 |
$extra_attr = array( |
| 260 |
'block_name' => $block_name, |
| 261 |
'wrapper_class' => $wrapper_class, |
| 262 |
'wrapper_style' => $wrapper_style, |
| 263 |
'current_post' => $current_post, |
| 264 |
'imageSize' => $imageSize, |
| 265 |
|
| 266 |
'content_container_style' => $content_container_style, |
| 267 |
'content_container_class' => $content_container_class, |
| 268 |
|
| 269 |
'foreground_style' => $foreground_style, |
| 270 |
'foreground_class' => $foreground_class, |
| 271 |
'content' => $content |
| 272 |
); |
| 273 |
|
| 274 |
if ( getwid_acf_is_active() && isset( $attributes['customField'] ) && (has_post_thumbnail()) || strlen( $content ) ) { |
| 275 |
ob_start(); |
| 276 |
|
| 277 |
getwid_get_template_part( 'template-parts/acf/background-image', $attributes, false, $extra_attr ); |
| 278 |
|
| 279 |
$result = ob_get_clean(); |
| 280 |
} |
| 281 |
|
| 282 |
$this->block_frontend_assets(); |
| 283 |
|
| 284 |
return $result; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
new \Getwid\Blocks\AcfBackgroundImage(); |
| 289 |
|