| 1 |
<?php |
| 2 |
class BBlocksImageComparison extends BBlocks{ |
| 3 |
public function __construct(){ |
| 4 |
add_action( 'init', [$this, 'register'] ); |
| 5 |
} |
| 6 |
|
| 7 |
function register(){ |
| 8 |
$this::registerScripts( 'image-comparison', 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 |
'alignment' => array( 'type' => 'string', 'default' => 'center' ), |
| 19 |
|
| 20 |
'width' => array( 'type' => 'string', 'default' => '80%' ), |
| 21 |
'border' => array( 'type' => 'object', 'default' => array() ), |
| 22 |
|
| 23 |
'beforeImg' => array( 'type' => 'object', 'default' => array( 'id' => NULL, 'url' => '', 'alt' => '', 'title' => '' ) ), |
| 24 |
'afterImg' => array( 'type' => 'object', 'default' => array( 'id' => NULL, 'url' => '', 'alt' => '', 'title' => '' ) ), |
| 25 |
|
| 26 |
'isSwap' => array( 'type' => 'boolean', 'default' => false ), |
| 27 |
|
| 28 |
'isLabel' => array( 'type' => 'boolean', 'default' => true ), |
| 29 |
'beforeLabel' => array( 'type' => 'string', 'selector' => '.beforeLabel', 'default' => '' ), |
| 30 |
'afterLabel' => array( 'type' => 'string', 'selector' => '.afterLabel', 'default' => '' ), |
| 31 |
'labelTypo' => array( 'type' => 'object', 'default' => array( 'fontSize' => 15 ) ), |
| 32 |
'labelColors' => array( 'type' => 'object', 'default' => array( 'color' => '#fff', 'bg' => '#00000080' ) ), |
| 33 |
'labelPadding' => array( 'type' => 'object', 'default' => array( 'vertical' => '10px', 'horizontal' => '15px' ) ), |
| 34 |
|
| 35 |
'isCap' => array( 'type' => 'boolean', 'default' => true ), |
| 36 |
'cap' => array( 'type' => 'string', 'selector' => '.caption', 'default' => '' ), |
| 37 |
'capAlign' => array( 'type' => 'string', 'default' => 'center' ), |
| 38 |
'capTypo' => array( 'type' => 'object', 'default' => array( 'fontSize' => 14 ) ), |
| 39 |
'capColors' => array( 'type' => 'object', 'default' => array( 'color' => '#333', 'bg' => '#f4f4f4' ) ), |
| 40 |
'capPadding' => array( 'type' => 'object', 'default' => array( 'vertical' => '10px', 'horizontal' => '15px' ) ) |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
function render( $attributes ){ |
| 45 |
extract( $attributes ); |
| 46 |
|
| 47 |
// Generate Styles |
| 48 |
$imageComparisonStyle = new BBlocksStyleGenerator(); |
| 49 |
$imageComparisonStyle::addStyle( "#bBlocksImageComparison-$cId", array( |
| 50 |
'text-align' => $alignment |
| 51 |
) ); |
| 52 |
$imageComparisonStyle::addStyle( "#bBlocksImageComparison-$cId .bBlocksImageComparison", array( |
| 53 |
'width' => $width, |
| 54 |
$border['styles'] ?? '' => '' |
| 55 |
) ); |
| 56 |
$imageComparisonStyle::addStyle( "#bBlocksImageComparison-$cId .bBlocksImageComparison .overlay", array( |
| 57 |
$labelTypo['styles'] ?? 'font-size: 15px;' => '', |
| 58 |
$labelColors['styles'] ?? 'color: #fff; background: #00000080;' => '', |
| 59 |
'padding' => $labelPadding['styles'] ?? '10px 15px' |
| 60 |
) ); |
| 61 |
$imageComparisonStyle::addStyle( "#bBlocksImageComparison-$cId .bBlocksImageComparison .caption", array( |
| 62 |
$capTypo['styles'] ?? 'font-size: 14px;' => '', |
| 63 |
$capColors['styles'] ?? 'color: #333; background: #f4f4f4;' => '', |
| 64 |
'padding' => $capPadding['styles'] ?? '10px 15px' |
| 65 |
) ); |
| 66 |
|
| 67 |
ob_start(); ?> |
| 68 |
<div class='wp-block-b-blocks-image-comparison <?php echo 'align' . esc_attr( $align ); ?>' id='bBlocksImageComparison-<?php echo esc_attr( $cId ); ?>'> |
| 69 |
<style> |
| 70 |
@import url(<?php echo esc_url( $labelTypo['googleFontLink'] ?? '' ); ?>); |
| 71 |
@import url(<?php echo esc_url( $capTypo['googleFontLink'] ?? '' ); ?>); |
| 72 |
<?php echo wp_kses( $imageComparisonStyle::renderStyle(), array() ); ?> |
| 73 |
</style> |
| 74 |
|
| 75 |
<div class='bBlocksImageComparison'> |
| 76 |
<div class='comparison-slider'> |
| 77 |
<?php echo $isLabel && $afterLabel ? "<div class='overlay afterLabel'>". wp_kses_post( $afterLabel ) ."</div>" : ''; ?> |
| 78 |
|
| 79 |
<?php echo $afterImg['url'] ? "<img class='afterImg' src='". esc_url( $afterImg['url'] ) ."' alt='". esc_attr( $afterImg['alt'] ) ."' />" : ''; ?> |
| 80 |
|
| 81 |
<div class='resize'> |
| 82 |
<?php echo $isLabel && $afterLabel ? "<div class='overlay beforeLabel'>". wp_kses_post( $beforeLabel ) ."</div>" : ''; ?> |
| 83 |
|
| 84 |
<?php echo $beforeImg['url'] ? "<img class='beforeImg' src='". esc_url( $beforeImg['url'] ) ."' alt='". esc_attr( $beforeImg['alt'] ) ."' />" : ''; ?> |
| 85 |
</div> |
| 86 |
|
| 87 |
<div class='divider'></div> |
| 88 |
</div> |
| 89 |
|
| 90 |
<?php echo $isCap && $cap ? "<div class='caption'>". wp_kses_post( $cap ) ."</div>" : ''; ?> |
| 91 |
</div> |
| 92 |
</div> |
| 93 |
|
| 94 |
<?php $imageComparisonStyle::$styles = array(); // Empty styles |
| 95 |
return ob_get_clean(); |
| 96 |
} |
| 97 |
} |
| 98 |
new BBlocksImageComparison(); |