class-button-container.php
3 years ago
class-button.php
3 years ago
class-container.php
3 years ago
class-grid.php
3 years ago
class-headline.php
3 years ago
class-image.php
3 years ago
class-query-loop.php
3 years ago
class-image.php
341 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles the Image block. |
| 4 | * |
| 5 | * @package GenerateBlocks |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Add Image related functions. |
| 14 | */ |
| 15 | class GenerateBlocks_Block_Image { |
| 16 | /** |
| 17 | * Keep track of all blocks of this type on the page. |
| 18 | * |
| 19 | * @var array $block_ids The current block id. |
| 20 | */ |
| 21 | private static $block_ids = []; |
| 22 | |
| 23 | /** |
| 24 | * Keep track of CSS we want to output once per block type. |
| 25 | * |
| 26 | * @var boolean |
| 27 | */ |
| 28 | private static $singular_css_added = false; |
| 29 | |
| 30 | /** |
| 31 | * Block defaults. |
| 32 | */ |
| 33 | public static function defaults() { |
| 34 | return [ |
| 35 | 'mediaId' => '', |
| 36 | 'sizeSlug' => '', |
| 37 | 'width' => '', |
| 38 | 'widthTablet' => '', |
| 39 | 'widthMobile' => '', |
| 40 | 'height' => '', |
| 41 | 'heightTablet' => '', |
| 42 | 'heightMobile' => '', |
| 43 | 'borderColor' => '', |
| 44 | 'objectFit' => '', |
| 45 | 'objectFitTablet' => '', |
| 46 | 'objectFitMobile' => '', |
| 47 | 'align' => '', |
| 48 | 'alignment' => '', |
| 49 | 'alignmentTablet' => '', |
| 50 | 'alignmentMobile' => '', |
| 51 | ]; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Store our block ID in memory. |
| 56 | * |
| 57 | * @param string $id The block ID to store. |
| 58 | */ |
| 59 | public static function store_block_id( $id ) { |
| 60 | self::$block_ids[] = $id; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Check if our block ID exists. |
| 65 | * |
| 66 | * @param string $id The block ID to store. |
| 67 | */ |
| 68 | public static function block_id_exists( $id ) { |
| 69 | return in_array( $id, (array) self::$block_ids ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Compile our CSS data based on our block attributes. |
| 74 | * |
| 75 | * @param array $attributes Our block attributes. |
| 76 | */ |
| 77 | public static function get_css_data( $attributes ) { |
| 78 | $css = new GenerateBlocks_Dynamic_CSS(); |
| 79 | $desktop_css = new GenerateBlocks_Dynamic_CSS(); |
| 80 | $tablet_css = new GenerateBlocks_Dynamic_CSS(); |
| 81 | $tablet_only_css = new GenerateBlocks_Dynamic_CSS(); |
| 82 | $mobile_css = new GenerateBlocks_Dynamic_CSS(); |
| 83 | $css_data = []; |
| 84 | |
| 85 | $defaults = generateblocks_get_block_defaults(); |
| 86 | |
| 87 | $settings = wp_parse_args( |
| 88 | $attributes, |
| 89 | $defaults['image'] |
| 90 | ); |
| 91 | |
| 92 | $id = $attributes['uniqueId']; |
| 93 | |
| 94 | // Only add this CSS once. |
| 95 | if ( ! self::$singular_css_added ) { |
| 96 | $css->set_selector( '.gb-block-image img' ); |
| 97 | $css->add_property( 'vertical-align', 'middle' ); |
| 98 | |
| 99 | do_action( |
| 100 | 'generateblocks_block_one_time_css_data', |
| 101 | 'image', |
| 102 | $settings, |
| 103 | $css |
| 104 | ); |
| 105 | |
| 106 | self::$singular_css_added = true; |
| 107 | } |
| 108 | |
| 109 | $css->set_selector( '.gb-block-image-' . $id ); |
| 110 | $css->add_property( 'padding', array( $settings['paddingTop'], $settings['paddingRight'], $settings['paddingBottom'], $settings['paddingLeft'] ), $settings['paddingUnit'] ); |
| 111 | $css->add_property( 'margin', array( $settings['marginTop'], $settings['marginRight'], $settings['marginBottom'], $settings['marginLeft'] ), $settings['marginUnit'] ); |
| 112 | |
| 113 | // Set a flag we'll update later if we disable floats. |
| 114 | $disable_float = false; |
| 115 | $has_desktop_float = 'floatLeft' === $settings['alignment'] || 'floatRight' === $settings['alignment']; |
| 116 | $has_tablet_float = 'floatLeft' === $settings['alignmentTablet'] || 'floatRight' === $settings['alignmentTablet']; |
| 117 | |
| 118 | if ( $has_desktop_float ) { |
| 119 | $css->add_property( 'float', generateblocks_get_float_alignment( $settings['alignment'] ) ); |
| 120 | } else { |
| 121 | $css->add_property( 'text-align', $settings['alignment'] ); |
| 122 | } |
| 123 | |
| 124 | $css->set_selector( '.gb-image-' . $id ); |
| 125 | $css->add_property( 'border-radius', array( $settings['borderRadiusTopLeft'], $settings['borderRadiusTopRight'], $settings['borderRadiusBottomRight'], $settings['borderRadiusBottomLeft'] ), $settings['borderRadiusUnit'] ); |
| 126 | $css->add_property( 'border-width', array( $settings['borderSizeTop'], $settings['borderSizeRight'], $settings['borderSizeBottom'], $settings['borderSizeLeft'] ), 'px' ); |
| 127 | $css->add_property( 'border-color', $settings['borderColor'] ); |
| 128 | $css->add_property( 'width', $settings['width'] ); |
| 129 | $css->add_property( 'height', $settings['height'] ); |
| 130 | $css->add_property( 'object-fit', $settings['objectFit'] ); |
| 131 | |
| 132 | $tablet_css->set_selector( '.gb-block-image-' . $id ); |
| 133 | $tablet_css->add_property( 'padding', array( $settings['paddingTopTablet'], $settings['paddingRightTablet'], $settings['paddingBottomTablet'], $settings['paddingLeftTablet'] ), $settings['paddingUnit'] ); |
| 134 | $tablet_css->add_property( 'margin', array( $settings['marginTopTablet'], $settings['marginRightTablet'], $settings['marginBottomTablet'], $settings['marginLeftTablet'] ), $settings['marginUnit'] ); |
| 135 | |
| 136 | if ( $has_tablet_float ) { |
| 137 | $tablet_css->add_property( 'float', generateblocks_get_float_alignment( $settings['alignmentTablet'] ) ); |
| 138 | } else { |
| 139 | $tablet_css->add_property( 'text-align', $settings['alignmentTablet'] ); |
| 140 | |
| 141 | if ( $settings['alignmentTablet'] && $has_desktop_float ) { |
| 142 | $tablet_css->add_property( 'float', 'none' ); |
| 143 | $disable_float = true; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | $tablet_css->set_selector( '.gb-image-' . $id ); |
| 148 | $tablet_css->add_property( 'border-radius', array( $settings['borderRadiusTopLeftTablet'], $settings['borderRadiusTopRightTablet'], $settings['borderRadiusBottomRightTablet'], $settings['borderRadiusBottomLeftTablet'] ), $settings['borderRadiusUnit'] ); |
| 149 | $tablet_css->add_property( 'border-width', array( $settings['borderSizeTopTablet'], $settings['borderSizeRightTablet'], $settings['borderSizeBottomTablet'], $settings['borderSizeLeftTablet'] ), 'px' ); |
| 150 | $tablet_css->add_property( 'width', $settings['widthTablet'] ); |
| 151 | $tablet_css->add_property( 'height', $settings['heightTablet'] ); |
| 152 | $tablet_css->add_property( 'object-fit', $settings['objectFitTablet'] ); |
| 153 | |
| 154 | $mobile_css->set_selector( '.gb-block-image-' . $id ); |
| 155 | $mobile_css->add_property( 'padding', array( $settings['paddingTopMobile'], $settings['paddingRightMobile'], $settings['paddingBottomMobile'], $settings['paddingLeftMobile'] ), $settings['paddingUnit'] ); |
| 156 | $mobile_css->add_property( 'margin', array( $settings['marginTopMobile'], $settings['marginRightMobile'], $settings['marginBottomMobile'], $settings['marginLeftMobile'] ), $settings['marginUnit'] ); |
| 157 | |
| 158 | if ( 'floatLeft' === $settings['alignmentMobile'] || 'floatRight' === $settings['alignmentMobile'] ) { |
| 159 | $mobile_css->add_property( 'float', generateblocks_get_float_alignment( $settings['alignmentMobile'] ) ); |
| 160 | } else { |
| 161 | $mobile_css->add_property( 'text-align', $settings['alignmentMobile'] ); |
| 162 | |
| 163 | if ( |
| 164 | $settings['alignmentMobile'] && |
| 165 | ! $disable_float && |
| 166 | ( |
| 167 | $has_desktop_float || |
| 168 | $has_tablet_float |
| 169 | ) |
| 170 | ) { |
| 171 | $mobile_css->add_property( 'float', 'none' ); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | $mobile_css->set_selector( '.gb-image-' . $id ); |
| 176 | $mobile_css->add_property( 'border-radius', array( $settings['borderRadiusTopLeftMobile'], $settings['borderRadiusTopRightMobile'], $settings['borderRadiusBottomRightMobile'], $settings['borderRadiusBottomLeftMobile'] ), $settings['borderRadiusUnit'] ); |
| 177 | $mobile_css->add_property( 'border-width', array( $settings['borderSizeTopMobile'], $settings['borderSizeRightMobile'], $settings['borderSizeBottomMobile'], $settings['borderSizeLeftMobile'] ), 'px' ); |
| 178 | $mobile_css->add_property( 'width', $settings['widthMobile'] ); |
| 179 | $mobile_css->add_property( 'height', $settings['heightMobile'] ); |
| 180 | $mobile_css->add_property( 'object-fit', $settings['objectFitMobile'] ); |
| 181 | |
| 182 | // Store this block ID in memory. |
| 183 | self::store_block_id( $id ); |
| 184 | |
| 185 | /** |
| 186 | * Do generateblocks_block_css_data hook |
| 187 | * |
| 188 | * @since 1.0 |
| 189 | * |
| 190 | * @param string $name The name of our block. |
| 191 | * @param array $settings The settings for the current block. |
| 192 | * @param object $css Our desktop/main CSS data. |
| 193 | * @param object $desktop_css Our desktop only CSS data. |
| 194 | * @param object $tablet_css Our tablet CSS data. |
| 195 | * @param object $tablet_only_css Our tablet only CSS data. |
| 196 | * @param object $mobile_css Our mobile CSS data. |
| 197 | */ |
| 198 | do_action( |
| 199 | 'generateblocks_block_css_data', |
| 200 | 'image', |
| 201 | $settings, |
| 202 | $css, |
| 203 | $desktop_css, |
| 204 | $tablet_css, |
| 205 | $tablet_only_css, |
| 206 | $mobile_css |
| 207 | ); |
| 208 | |
| 209 | return [ |
| 210 | 'main' => $css->css_output(), |
| 211 | 'desktop' => $desktop_css->css_output(), |
| 212 | 'tablet' => $tablet_css->css_output(), |
| 213 | 'tablet_only' => $tablet_only_css->css_output(), |
| 214 | 'mobile' => $mobile_css->css_output(), |
| 215 | ]; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Wrapper function for our dynamic buttons. |
| 220 | * |
| 221 | * @since 1.6.0 |
| 222 | * @param array $attributes The block attributes. |
| 223 | * @param string $content The dynamic text to display. |
| 224 | * @param WP_Block $block Block instance. |
| 225 | */ |
| 226 | public static function render_block( $attributes, $content, $block ) { |
| 227 | if ( empty( $attributes['useDynamicData'] ) ) { |
| 228 | // Add styles to this block if needed. |
| 229 | $content = generateblocks_maybe_add_block_css( |
| 230 | $content, |
| 231 | [ |
| 232 | 'class_name' => 'GenerateBlocks_Block_Image', |
| 233 | 'attributes' => $attributes, |
| 234 | 'block_ids' => self::$block_ids, |
| 235 | ] |
| 236 | ); |
| 237 | |
| 238 | return generateblocks_filter_images( $content, $attributes ); |
| 239 | } |
| 240 | |
| 241 | $image = empty( $attributes['dynamicContentType'] ) |
| 242 | ? generateblocks_filter_images( GenerateBlocks_Dynamic_Content::get_static_content( $content ), $attributes ) |
| 243 | : GenerateBlocks_Dynamic_Content::get_dynamic_image( $attributes, $block ); |
| 244 | |
| 245 | if ( ! $image ) { |
| 246 | return ''; |
| 247 | } |
| 248 | |
| 249 | $defaults = generateblocks_get_block_defaults(); |
| 250 | |
| 251 | $settings = wp_parse_args( |
| 252 | $attributes, |
| 253 | $defaults['image'] |
| 254 | ); |
| 255 | |
| 256 | // Add styles to this block if needed. |
| 257 | $output = generateblocks_maybe_add_block_css( |
| 258 | '', |
| 259 | [ |
| 260 | 'class_name' => 'GenerateBlocks_Block_Image', |
| 261 | 'attributes' => $attributes, |
| 262 | 'block_ids' => self::$block_ids, |
| 263 | ] |
| 264 | ); |
| 265 | |
| 266 | $output .= sprintf( |
| 267 | '<figure %s>', |
| 268 | generateblocks_attr( |
| 269 | 'image-figure', |
| 270 | array( |
| 271 | 'class' => implode( |
| 272 | ' ', |
| 273 | array( |
| 274 | 'gb-block-image', |
| 275 | 'gb-block-image-' . $settings['uniqueId'], |
| 276 | ) |
| 277 | ), |
| 278 | ), |
| 279 | $settings, |
| 280 | $block |
| 281 | ) |
| 282 | ); |
| 283 | |
| 284 | $dynamic_link = GenerateBlocks_Dynamic_Content::get_dynamic_url( $attributes, $block ); |
| 285 | |
| 286 | if ( $dynamic_link ) { |
| 287 | $relAttributes = array(); |
| 288 | |
| 289 | if ( ! empty( $settings['relNoFollow'] ) ) { |
| 290 | $relAttributes[] = 'nofollow'; |
| 291 | } |
| 292 | |
| 293 | if ( ! empty( $settings['openInNewWindow'] ) ) { |
| 294 | $relAttributes[] = 'noopener'; |
| 295 | $relAttributes[] = 'noreferrer'; |
| 296 | } |
| 297 | |
| 298 | if ( ! empty( $settings['relSponsored'] ) ) { |
| 299 | $relAttributes[] = 'sponsored'; |
| 300 | } |
| 301 | |
| 302 | $image = sprintf( |
| 303 | '<a %s>%s</a>', |
| 304 | generateblocks_attr( |
| 305 | 'image-link', |
| 306 | array( |
| 307 | 'class' => '', |
| 308 | 'href' => $dynamic_link, |
| 309 | 'rel' => ! empty( $relAttributes ) ? implode( ' ', $relAttributes ) : null, |
| 310 | 'target' => ! empty( $settings['openInNewWindow'] ) ? '_blank' : null, |
| 311 | ), |
| 312 | $settings, |
| 313 | $block |
| 314 | ), |
| 315 | $image |
| 316 | ); |
| 317 | } |
| 318 | |
| 319 | $output .= $image; |
| 320 | |
| 321 | if ( isset( $block->parsed_block['innerBlocks'][0]['attrs'] ) ) { |
| 322 | $image_id = GenerateBlocks_Dynamic_Content::get_dynamic_image_id( $attributes ); |
| 323 | $block->parsed_block['innerBlocks'][0]['attrs']['dynamicImage'] = $image_id; |
| 324 | |
| 325 | $caption = ( |
| 326 | new WP_Block( |
| 327 | $block->parsed_block['innerBlocks'][0] |
| 328 | ) |
| 329 | )->render( array( 'dynamic' => true ) ); |
| 330 | |
| 331 | if ( $caption ) { |
| 332 | $output .= $caption; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | $output .= '</figure>'; |
| 337 | |
| 338 | return $output; |
| 339 | } |
| 340 | } |
| 341 |