| 1 |
<?php |
| 2 |
class BBlocksGIF extends BBlocks{ |
| 3 |
public function __construct(){ |
| 4 |
add_action( 'init', [$this, 'register'] ); |
| 5 |
} |
| 6 |
|
| 7 |
function register(){ |
| 8 |
$this::registerScripts( 'gif', 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 |
'gif' => array( 'type' => 'object', 'default' => NULL ), |
| 20 |
'gifAlign' => array( 'type' => 'string', 'default' => 'center' ), |
| 21 |
'gifWidth' => array( 'type' => 'string', 'default' => '0px' ) |
| 22 |
); |
| 23 |
} |
| 24 |
|
| 25 |
function render( $attributes ){ |
| 26 |
extract( $attributes ); |
| 27 |
|
| 28 |
// Generate Styles |
| 29 |
$gifStyle = new BBlocksStyleGenerator(); |
| 30 |
$gifStyle::addStyle( "#bBlocksGif-$cId .bBlocksGif", array( |
| 31 |
'text-align' => $gifAlign |
| 32 |
) ); |
| 33 |
$gifStyle::addStyle( "#bBlocksGif-$cId .bBlocksGif figure", array( |
| 34 |
'width' => '0px' === $gifWidth || '0%' === $gifWidth || '0em' === $gifWidth ? 'auto' : $gifWidth |
| 35 |
) ); |
| 36 |
|
| 37 |
ob_start(); ?> |
| 38 |
<div class='wp-block-b-blocks-gif <?php echo 'align' . esc_attr( $align ); ?>' id='bBlocksGif-<?php echo esc_attr( $cId ); ?>'> |
| 39 |
<style><?php echo wp_kses( $gifStyle::renderStyle(), array() ); ?></style> |
| 40 |
|
| 41 |
<div class='bBlocksGif'> |
| 42 |
<figure> |
| 43 |
<?php echo isset( $gif ) && $gif ? "<img src='". esc_url( $gif['images']['original']['url'] ?? '' ) ."' alt='". esc_attr( $gif['title'] ?? '' ) ."' />" : ''; ?> |
| 44 |
</figure> |
| 45 |
</div> |
| 46 |
</div> |
| 47 |
<?php $gifStyle::$styles = array(); // Empty styles |
| 48 |
return ob_get_clean(); |
| 49 |
} |
| 50 |
} |
| 51 |
new BBlocksGIF(); |