| 1 |
<?php |
| 2 |
class BBlocksMailto extends BBlocks{ |
| 3 |
public function __construct(){ |
| 4 |
add_action( 'init', [$this, 'register'] ); |
| 5 |
} |
| 6 |
|
| 7 |
function register(){ |
| 8 |
$this::registerScripts( 'mailto', array( |
| 9 |
'attributes' => $this->attributes(), |
| 10 |
'render_callback' => [$this, 'render'] |
| 11 |
) ); |
| 12 |
} |
| 13 |
|
| 14 |
function attributes(){ |
| 15 |
return array( |
| 16 |
'align' => array( 'type' => 'string', 'default' => '' ), |
| 17 |
'cId' => array( 'type' => 'string', 'default' => '' ), |
| 18 |
|
| 19 |
'mailtoAlign' => array( 'type' => 'string', 'default' => 'center' ), |
| 20 |
|
| 21 |
'receiverMail' => array( 'type' => 'string', 'default' => '' ), |
| 22 |
'style' => array( 'type' => 'string', 'default' => 'default' ), |
| 23 |
|
| 24 |
'btn' => array( 'type' => 'string', 'selector' => 'a.mailtoButton span', 'default' => 'Send E-mail' ), |
| 25 |
'btnTypo' => array( 'type' => 'object', 'default' => array( 'fontSize' => 22, 'fontWeight' => 600 ) ), |
| 26 |
'btnColors' => array( 'type' => 'object', 'default' => array( 'color' => '#fff', 'bg' => '#4527a4' ) ), |
| 27 |
'btnHovColors' => array( 'type' => 'object', 'default' => array( 'color' => '#fff', 'bg' => '#8344c5' ) ), |
| 28 |
'btnPadding' => array( 'type' => 'object', 'default' => array( 'vertical' => '18px', 'horizontal' => '45px' ) ), |
| 29 |
'btnBorder' => array( 'type' => 'object', 'default' => array( 'radius' => '50px' ) ), |
| 30 |
|
| 31 |
'icon' => array( 'type' => 'object', 'default' => array( 'class' => 'fa fa-paper-plane' ) ), |
| 32 |
'iconPosition' => array( 'type' => 'string', 'default' => 'left' ) |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
function render( $attributes ){ |
| 37 |
extract( $attributes ); |
| 38 |
|
| 39 |
// Generate Styles |
| 40 |
$mailtoStyle = new BBlocksStyleGenerator(); |
| 41 |
$mailtoStyle::addStyle( "#bBlocksMailto-$cId .bBlocksMailto", array( |
| 42 |
'text-align' => $mailtoAlign |
| 43 |
) ); |
| 44 |
$mailtoStyle::addStyle( "#bBlocksMailto-$cId .bBlocksMailto a.mailtoButton", array( |
| 45 |
$btnTypo['styles'] ?? 'font-size: 22px; font-weight: 600;' => '', |
| 46 |
$btnColors['styles'] ?? 'color: #fff; background: #4527a4;' => '', |
| 47 |
'padding' => $btnPadding['styles'] ?? '18px 45px', |
| 48 |
$btnBorder['styles'] ?? 'border-radius: 50px;' => '', |
| 49 |
'box-shadow' => '0px 5px 20px 0px '. $btnColors['bg'] |
| 50 |
) ); |
| 51 |
$mailtoStyle::addStyle( "#bBlocksMailto-$cId .bBlocksMailto a.mailtoButton:hover", array( |
| 52 |
$btnHovColors['styles'] ?? 'color: #fff; background: #8344c5;' => '', |
| 53 |
'box-shadow' => '0px 5px 20px 0px '. $btnHovColors['bg'] |
| 54 |
) ); |
| 55 |
|
| 56 |
ob_start(); ?> |
| 57 |
<div class='wp-block-b-blocks-mailto <?php echo 'align' . esc_attr( $align ); ?>' id='bBlocksMailto-<?php echo esc_attr( $cId ); ?>'> |
| 58 |
<style> |
| 59 |
@import url(<?php echo esc_url( $btnTypo['googleFontLink'] ?? '' ); ?>); |
| 60 |
<?php echo wp_kses( $mailtoStyle::renderStyle(), array() ); ?> |
| 61 |
</style> |
| 62 |
|
| 63 |
<div class='bBlocksMailto'> |
| 64 |
<a href='<?php echo 'mailto:' .sanitize_email( $receiverMail ); ?>' class='mailtoui mailtoButton'> |
| 65 |
<?php echo 'left' === $iconPosition && $icon['class'] ? "<i class='". esc_html( $icon['class'] ) ."'></i>" : ''; ?> |
| 66 |
|
| 67 |
<span><?php echo wp_kses_post( $btn ); ?></span> |
| 68 |
|
| 69 |
<?php echo 'right' === $iconPosition && $icon['class'] ? "<i class='". esc_html( $icon['class'] ) ."'></i>" : ''; ?> |
| 70 |
</a> |
| 71 |
</div> |
| 72 |
</div> |
| 73 |
<?php $mailtoStyle::$styles = array(); // Empty styles |
| 74 |
return ob_get_clean(); |
| 75 |
} |
| 76 |
} |
| 77 |
new BBlocksMailto(); |