| 1 |
<?php |
| 2 |
|
| 3 |
namespace gutenify; |
| 4 |
|
| 5 |
|
| 6 |
defined('ABSPATH') || exit; |
| 7 |
|
| 8 |
class Button{ |
| 9 |
public static function init() { |
| 10 |
add_action('init', array(__CLASS__, 'register_block')); |
| 11 |
add_filter('gutenify_render_block_gutenify/button', array(__CLASS__, 'render_block'), 10, 4); |
| 12 |
} |
| 13 |
|
| 14 |
public static function register_block() { |
| 15 |
register_block_type(__DIR__); |
| 16 |
} |
| 17 |
|
| 18 |
public static function render_block($block_content, $block, $instance, $block_id){ |
| 19 |
|
| 20 |
$root_selector = '.' . $block_id; |
| 21 |
$css = ''; |
| 22 |
|
| 23 |
/** |
| 24 |
* Padding. |
| 25 |
*/ |
| 26 |
if ( ! empty( $block['attrs']['blockAdvanceOptions']['padding'] ) ) { |
| 27 |
$css .= Dynamic_Styles::get_spacing_with_media( $root_selector . ' .gutenify-button-link.wp-block-button__link', $block['attrs']['blockAdvanceOptions']['padding'], 'padding-' ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Margin. |
| 32 |
*/ |
| 33 |
if ( ! empty( $block['attrs']['blockAdvanceOptions']['margin'] ) ) { |
| 34 |
$css .= Dynamic_Styles::get_spacing_with_media( $root_selector . ' .gutenify-button-link.wp-block-button__link', $block['attrs']['blockAdvanceOptions']['margin'], 'margin-' ); |
| 35 |
} |
| 36 |
|
| 37 |
$css .= $root_selector . ' .gutenify-button-link.wp-block-button__link{font: inherit;'; |
| 38 |
|
| 39 |
/** |
| 40 |
* Normal state. |
| 41 |
*/ |
| 42 |
if (!empty($block['attrs']['blockAdvanceOptions']['textColor'])) { |
| 43 |
$css .= 'color:' . esc_attr( $block['attrs']['blockAdvanceOptions']['textColor'] ) . ';'; |
| 44 |
} |
| 45 |
if (!empty($block['attrs']['backgroundGradient'])) { |
| 46 |
$css .= 'background:' . esc_attr( $block['attrs']['backgroundGradient'] ) . ';'; |
| 47 |
}elseif (!empty($block['attrs']['backgroundColor'])) { |
| 48 |
$css .= 'background:' . esc_attr( $block['attrs']['backgroundColor'] ) . ';'; |
| 49 |
} elseif (!empty($block['attrs']['blockAdvanceOptions']['backgroundGradient'])) { |
| 50 |
$css .= 'background:' . esc_attr( $block['attrs']['blockAdvanceOptions']['backgroundGradient'] ) . ';'; |
| 51 |
} elseif (!empty($block['attrs']['blockAdvanceOptions']['backgroundColor'])) { |
| 52 |
$css .= 'background:' . esc_attr( $block['attrs']['blockAdvanceOptions']['backgroundColor'] ) . ';'; |
| 53 |
} else { |
| 54 |
$css .= ''; |
| 55 |
} |
| 56 |
|
| 57 |
// Border. |
| 58 |
if (!empty($block['attrs']['blockAdvanceOptions']['borderColor'])) { |
| 59 |
$css .= 'border-color:' . esc_attr( $block['attrs']['blockAdvanceOptions']['borderColor'] ) . ';'; |
| 60 |
} |
| 61 |
if ( ! empty( $block['attrs']['blockAdvanceOptions']['borderWidth'] ) ) { |
| 62 |
$css .= 'border-style:solid;'; |
| 63 |
if ( ! is_array( $block['attrs']['blockAdvanceOptions']['borderWidth'] ) ) { |
| 64 |
$css .= 'border-width: ' . esc_attr( $block['attrs']['blockAdvanceOptions']['borderWidth'] ) . 'px;'; |
| 65 |
} else { |
| 66 |
$css .= \gutenify\Style_Helpers::box_control( $block['attrs']['blockAdvanceOptions']['borderWidth'], 'border-', '-width'); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
if ( ! empty( $block['attrs']['blockAdvanceOptions']['borderRadius'] ) ) { |
| 71 |
$css .= \gutenify\Style_Helpers::border_radius_control( $block['attrs']['blockAdvanceOptions']['borderRadius'] ); |
| 72 |
} |
| 73 |
// Icon. |
| 74 |
if (!empty($block['attrs']['icon']['position']) && 'after' === $block['attrs']['icon']['position']) { |
| 75 |
$css .= 'flex-direction: row-reverse;'; |
| 76 |
} |
| 77 |
if (!empty($block['attrs']['icon']['spacing'])) { |
| 78 |
$css .= 'gap: ' . esc_attr( $block['attrs']['icon']['spacing'] ) . 'px;'; |
| 79 |
} |
| 80 |
|
| 81 |
$css .= '}'; |
| 82 |
|
| 83 |
/** |
| 84 |
* Hover state. |
| 85 |
*/ |
| 86 |
$css .= $root_selector . ' .wp-block-button__link:hover{ '; |
| 87 |
if (!empty($block['attrs']['blockAdvanceOptions']['hoverTextColor'])) { |
| 88 |
$css .= 'color:' . esc_attr( $block['attrs']['blockAdvanceOptions']['hoverTextColor'] ) . ';'; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
if (!empty($block['attrs']['hoverBackgroundGradient'])) { |
| 93 |
$css .= 'background:' . esc_attr( $block['attrs']['hoverBackgroundGradient'] ) . ';'; |
| 94 |
} elseif (!empty($block['attrs']['hoverBackgroundColor'])) { |
| 95 |
$css .= 'background:' . esc_attr( $block['attrs']['hoverBackgroundColor'] ) . ';'; |
| 96 |
}elseif (!empty($block['attrs']['blockAdvanceOptions']['hoverBackgroundGradient'])) { |
| 97 |
$css .= 'background:' . esc_attr( $block['attrs']['blockAdvanceOptions']['hoverBackgroundGradient'] ) . ';'; |
| 98 |
} elseif (!empty($block['attrs']['blockAdvanceOptions']['hoverBackgroundColor'])) { |
| 99 |
$css .= 'background:' . esc_attr( $block['attrs']['blockAdvanceOptions']['hoverBackgroundColor'] ) . ';'; |
| 100 |
} else { |
| 101 |
$css .= ''; |
| 102 |
} |
| 103 |
|
| 104 |
// Border. |
| 105 |
if (!empty($block['attrs']['blockAdvanceOptions']['hoverBorderColor'])) { |
| 106 |
$css .= 'border-color:' . esc_attr( $block['attrs']['blockAdvanceOptions']['hoverBorderColor'] ) . ';'; |
| 107 |
} |
| 108 |
$css .= '}'; |
| 109 |
|
| 110 |
$handle = 'gutenify_'. str_replace( '/', '_', $block['blockName'] ) . '_' . $block_id; |
| 111 |
wp_add_inline_style( $handle, $css); |
| 112 |
return $block_content; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
Button::init(); |
| 117 |
|
| 118 |
|