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-button-container.php
305 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles the Button Container block. |
| 4 | * |
| 5 | * @package GenerateBlocks |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Add Button Container related functions. |
| 14 | */ |
| 15 | class GenerateBlocks_Block_Button_Container { |
| 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 | 'alignment' => '', |
| 36 | 'alignmentTablet' => '', |
| 37 | 'alignmentMobile' => '', |
| 38 | 'marginTop' => '', |
| 39 | 'marginRight' => '', |
| 40 | 'marginBottom' => '', |
| 41 | 'marginLeft' => '', |
| 42 | 'marginTopTablet' => '', |
| 43 | 'marginRightTablet' => '', |
| 44 | 'marginBottomTablet' => '', |
| 45 | 'marginLeftTablet' => '', |
| 46 | 'marginTopMobile' => '', |
| 47 | 'marginRightMobile' => '', |
| 48 | 'marginBottomMobile' => '', |
| 49 | 'marginLeftMobile' => '', |
| 50 | 'marginUnit' => 'px', |
| 51 | 'stack' => false, |
| 52 | 'stackTablet' => false, |
| 53 | 'stackMobile' => false, |
| 54 | 'fillHorizontalSpace' => false, |
| 55 | 'fillHorizontalSpaceTablet' => false, |
| 56 | 'fillHorizontalSpaceMobile' => false, |
| 57 | ]; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Store our block ID in memory. |
| 62 | * |
| 63 | * @param string $id The block ID to store. |
| 64 | */ |
| 65 | public static function store_block_id( $id ) { |
| 66 | self::$block_ids[] = $id; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Check if our block ID exists. |
| 71 | * |
| 72 | * @param string $id The block ID to store. |
| 73 | */ |
| 74 | public static function block_id_exists( $id ) { |
| 75 | return in_array( $id, (array) self::$block_ids ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Compile our CSS data based on our block attributes. |
| 80 | * |
| 81 | * @param array $attributes Our block attributes. |
| 82 | */ |
| 83 | public static function get_css_data( $attributes ) { |
| 84 | $css = new GenerateBlocks_Dynamic_CSS(); |
| 85 | $desktop_css = new GenerateBlocks_Dynamic_CSS(); |
| 86 | $tablet_css = new GenerateBlocks_Dynamic_CSS(); |
| 87 | $tablet_only_css = new GenerateBlocks_Dynamic_CSS(); |
| 88 | $mobile_css = new GenerateBlocks_Dynamic_CSS(); |
| 89 | $css_data = []; |
| 90 | |
| 91 | $defaults = generateblocks_get_block_defaults(); |
| 92 | |
| 93 | $settings = wp_parse_args( |
| 94 | $attributes, |
| 95 | $defaults['buttonContainer'] |
| 96 | ); |
| 97 | |
| 98 | $id = $attributes['uniqueId']; |
| 99 | $blockVersion = ! empty( $settings['blockVersion'] ) ? $settings['blockVersion'] : 1; |
| 100 | |
| 101 | // Only add this CSS once. |
| 102 | if ( ! self::$singular_css_added ) { |
| 103 | $css->set_selector( '.gb-button-wrapper' ); |
| 104 | $css->add_property( 'display', 'flex' ); |
| 105 | $css->add_property( 'flex-wrap', 'wrap' ); |
| 106 | $css->add_property( 'align-items', 'flex-start' ); |
| 107 | $css->add_property( 'justify-content', 'flex-start' ); |
| 108 | $css->add_property( 'clear', 'both' ); |
| 109 | |
| 110 | do_action( |
| 111 | 'generateblocks_block_one_time_css_data', |
| 112 | 'button-container', |
| 113 | $settings, |
| 114 | $css |
| 115 | ); |
| 116 | |
| 117 | self::$singular_css_added = true; |
| 118 | } |
| 119 | |
| 120 | $css->set_selector( '.gb-button-wrapper-' . $id ); |
| 121 | $css->add_property( 'margin', array( $settings['marginTop'], $settings['marginRight'], $settings['marginBottom'], $settings['marginLeft'] ), $settings['marginUnit'] ); |
| 122 | $css->add_property( 'justify-content', generateblocks_get_flexbox_alignment( $settings['alignment'] ) ); |
| 123 | |
| 124 | $stack_desktop = $desktop_css; |
| 125 | $stack_tablet_only = $tablet_only_css; |
| 126 | |
| 127 | if ( $blockVersion < 2 ) { |
| 128 | $stack_desktop = $css; |
| 129 | $stack_tablet_only = $tablet_css; |
| 130 | } |
| 131 | |
| 132 | if ( $settings['stack'] ) { |
| 133 | $stack_desktop->set_selector( '.gb-button-wrapper-' . $id ); |
| 134 | $stack_desktop->add_property( 'flex-direction', 'column' ); |
| 135 | $stack_desktop->add_property( 'align-items', generateblocks_get_flexbox_alignment( $settings['alignment'] ) ); |
| 136 | } |
| 137 | |
| 138 | if ( $settings['fillHorizontalSpace'] ) { |
| 139 | $stack_desktop->set_selector( '.gb-button-wrapper-' . $id . ' > .gb-button' ); |
| 140 | $stack_desktop->add_property( 'flex', '1' ); |
| 141 | } |
| 142 | |
| 143 | if ( $settings['stack'] && $settings['fillHorizontalSpace'] ) { |
| 144 | $stack_desktop->add_property( 'width', '100%' ); |
| 145 | $stack_desktop->add_property( 'box-sizing', 'border-box' ); |
| 146 | } |
| 147 | |
| 148 | $tablet_css->set_selector( '.gb-button-wrapper-' . $id ); |
| 149 | $tablet_css->add_property( 'margin', array( $settings['marginTopTablet'], $settings['marginRightTablet'], $settings['marginBottomTablet'], $settings['marginLeftTablet'] ), $settings['marginUnit'] ); |
| 150 | $tablet_css->add_property( 'justify-content', generateblocks_get_flexbox_alignment( $settings['alignmentTablet'] ) ); |
| 151 | |
| 152 | if ( $settings['stackTablet'] ) { |
| 153 | $stack_tablet_only->set_selector( '.gb-button-wrapper-' . $id ); |
| 154 | $stack_tablet_only->add_property( 'flex-direction', 'column' ); |
| 155 | $stack_tablet_only->add_property( 'align-items', generateblocks_get_flexbox_alignment( $settings['alignmentTablet'] ) ); |
| 156 | } |
| 157 | |
| 158 | if ( $settings['fillHorizontalSpaceTablet'] ) { |
| 159 | $stack_tablet_only->set_selector( '.gb-button-wrapper-' . $id . ' > .gb-button' ); |
| 160 | $stack_tablet_only->add_property( 'flex', '1' ); |
| 161 | } |
| 162 | |
| 163 | if ( $settings['stackTablet'] && $settings['fillHorizontalSpaceTablet'] ) { |
| 164 | $stack_tablet_only->add_property( 'width', '100%' ); |
| 165 | $stack_tablet_only->add_property( 'box-sizing', 'border-box' ); |
| 166 | } |
| 167 | |
| 168 | $mobile_css->set_selector( '.gb-button-wrapper-' . $id ); |
| 169 | $mobile_css->add_property( 'margin', array( $settings['marginTopMobile'], $settings['marginRightMobile'], $settings['marginBottomMobile'], $settings['marginLeftMobile'] ), $settings['marginUnit'] ); |
| 170 | $mobile_css->add_property( 'justify-content', generateblocks_get_flexbox_alignment( $settings['alignmentMobile'] ) ); |
| 171 | |
| 172 | if ( $settings['stackMobile'] ) { |
| 173 | $mobile_css->add_property( 'flex-direction', 'column' ); |
| 174 | $mobile_css->add_property( 'align-items', generateblocks_get_flexbox_alignment( $settings['alignmentMobile'] ) ); |
| 175 | } |
| 176 | |
| 177 | if ( $settings['fillHorizontalSpaceMobile'] ) { |
| 178 | $mobile_css->set_selector( '.gb-button-wrapper-' . $id . ' > .gb-button' ); |
| 179 | $mobile_css->add_property( 'flex', '1' ); |
| 180 | } |
| 181 | |
| 182 | if ( $settings['stackMobile'] && $settings['fillHorizontalSpaceMobile'] ) { |
| 183 | $mobile_css->add_property( 'width', '100%' ); |
| 184 | $mobile_css->add_property( 'box-sizing', 'border-box' ); |
| 185 | } |
| 186 | |
| 187 | // Store this block ID in memory. |
| 188 | self::store_block_id( $id ); |
| 189 | |
| 190 | /** |
| 191 | * Do generateblocks_block_css_data hook |
| 192 | * |
| 193 | * @since 1.0 |
| 194 | * |
| 195 | * @param string $name The name of our block. |
| 196 | * @param array $settings The settings for the current block. |
| 197 | * @param object $css Our desktop/main CSS data. |
| 198 | * @param object $desktop_css Our desktop only CSS data. |
| 199 | * @param object $tablet_css Our tablet CSS data. |
| 200 | * @param object $tablet_only_css Our tablet only CSS data. |
| 201 | * @param object $mobile_css Our mobile CSS data. |
| 202 | */ |
| 203 | do_action( |
| 204 | 'generateblocks_block_css_data', |
| 205 | 'button-container', |
| 206 | $settings, |
| 207 | $css, |
| 208 | $desktop_css, |
| 209 | $tablet_css, |
| 210 | $tablet_only_css, |
| 211 | $mobile_css |
| 212 | ); |
| 213 | |
| 214 | return [ |
| 215 | 'main' => $css->css_output(), |
| 216 | 'desktop' => $desktop_css->css_output(), |
| 217 | 'tablet' => $tablet_css->css_output(), |
| 218 | 'tablet_only' => $tablet_only_css->css_output(), |
| 219 | 'mobile' => $mobile_css->css_output(), |
| 220 | ]; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Wrapper function for our dynamic buttons. |
| 225 | * |
| 226 | * @since 1.6.0 |
| 227 | * @param array $attributes The block attributes. |
| 228 | * @param string $content The dynamic text to display. |
| 229 | * @param WP_Block $block Block instance. |
| 230 | */ |
| 231 | public static function render_block( $attributes, $content, $block ) { |
| 232 | if ( ! isset( $attributes['isDynamic'] ) || ! $attributes['isDynamic'] ) { |
| 233 | // Add styles to this block if needed. |
| 234 | $content = generateblocks_maybe_add_block_css( |
| 235 | $content, |
| 236 | [ |
| 237 | 'class_name' => 'GenerateBlocks_Block_Button_Container', |
| 238 | 'attributes' => $attributes, |
| 239 | 'block_ids' => self::$block_ids, |
| 240 | ] |
| 241 | ); |
| 242 | |
| 243 | return $content; |
| 244 | } |
| 245 | |
| 246 | if ( isset( $block->parsed_block['innerBlocks'] ) ) { |
| 247 | $button_count = apply_filters( |
| 248 | 'generateblocks_button_count', |
| 249 | count( (array) $block->parsed_block['innerBlocks'] ), |
| 250 | $attributes, |
| 251 | $block |
| 252 | ); |
| 253 | |
| 254 | if ( 0 === $button_count ) { |
| 255 | return; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | $defaults = generateblocks_get_block_defaults(); |
| 260 | |
| 261 | $settings = wp_parse_args( |
| 262 | $attributes, |
| 263 | $defaults['buttonContainer'] |
| 264 | ); |
| 265 | |
| 266 | $classNames = array( |
| 267 | 'gb-button-wrapper', |
| 268 | 'gb-button-wrapper-' . $settings['uniqueId'], |
| 269 | ); |
| 270 | |
| 271 | if ( ! empty( $settings['className'] ) ) { |
| 272 | $classNames[] = $settings['className']; |
| 273 | } |
| 274 | |
| 275 | // Add styles to this block if needed. |
| 276 | $output = generateblocks_maybe_add_block_css( |
| 277 | '', |
| 278 | [ |
| 279 | 'class_name' => 'GenerateBlocks_Block_Button_Container', |
| 280 | 'attributes' => $attributes, |
| 281 | 'block_ids' => self::$block_ids, |
| 282 | ] |
| 283 | ); |
| 284 | |
| 285 | $output .= sprintf( |
| 286 | '<div %s>', |
| 287 | generateblocks_attr( |
| 288 | 'button-container', |
| 289 | array( |
| 290 | 'id' => isset( $settings['anchor'] ) ? $settings['anchor'] : null, |
| 291 | 'class' => implode( ' ', $classNames ), |
| 292 | ), |
| 293 | $settings, |
| 294 | $block |
| 295 | ) |
| 296 | ); |
| 297 | |
| 298 | $output .= $content; |
| 299 | |
| 300 | $output .= '</div>'; |
| 301 | |
| 302 | return $output; |
| 303 | } |
| 304 | } |
| 305 |