| 1 |
<?php |
| 2 |
|
| 3 |
namespace Getwid\Blocks; |
| 4 |
|
| 5 |
class ImageBox extends \Getwid\Blocks\AbstractBlock { |
| 6 |
|
| 7 |
protected static $blockName = 'getwid/image-box'; |
| 8 |
|
| 9 |
public function __construct() { |
| 10 |
|
| 11 |
parent::__construct( self::$blockName ); |
| 12 |
|
| 13 |
register_block_type( |
| 14 |
'getwid/image-box', |
| 15 |
array( |
| 16 |
'render_callback' => [ $this, 'render_callback' ] |
| 17 |
) |
| 18 |
); |
| 19 |
|
| 20 |
if ( $this->isEnabled() ) { |
| 21 |
|
| 22 |
add_filter( 'getwid/blocks_style_css/dependencies', [ $this, 'block_frontend_styles' ] ); |
| 23 |
|
| 24 |
//Register JS/CSS assets |
| 25 |
wp_register_style( |
| 26 |
'animate', |
| 27 |
getwid_get_plugin_url( 'vendors/animate.css/animate.min.css' ), |
| 28 |
[], |
| 29 |
'3.7.0' |
| 30 |
); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
public function getLabel() { |
| 35 |
return __('Image Box', 'getwid'); |
| 36 |
} |
| 37 |
|
| 38 |
public function block_frontend_styles($styles) { |
| 39 |
|
| 40 |
//animate.min.css |
| 41 |
if ( is_admin() && ! in_array( 'animate', $styles ) ) { |
| 42 |
array_push( $styles, 'animate' ); |
| 43 |
} |
| 44 |
|
| 45 |
return $styles; |
| 46 |
} |
| 47 |
|
| 48 |
public function block_frontend_assets() { |
| 49 |
|
| 50 |
if ( is_admin() ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
if ( ! wp_style_is( 'animate', 'enqueued' ) ) { |
| 55 |
wp_enqueue_style('animate'); |
| 56 |
} |
| 57 |
|
| 58 |
if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
add_filter( 'getwid/optimize/assets', |
| 63 |
function ( $assets ) { |
| 64 |
$assets[] = getwid()->settings()->getPrefix() . '-blocks-common'; |
| 65 |
|
| 66 |
return $assets; |
| 67 |
} |
| 68 |
); |
| 69 |
|
| 70 |
add_filter( 'getwid/optimize/should_load_common_css', '__return_true' ); |
| 71 |
|
| 72 |
$rtl = is_rtl() ? '.rtl' : ''; |
| 73 |
|
| 74 |
wp_enqueue_style( |
| 75 |
self::$blockName, |
| 76 |
getwid_get_plugin_url( 'assets/blocks/image-box/style' . $rtl . '.css' ), |
| 77 |
[ 'animate' ], |
| 78 |
getwid()->settings()->getVersion() |
| 79 |
); |
| 80 |
|
| 81 |
wp_enqueue_script( |
| 82 |
self::$blockName, |
| 83 |
getwid_get_plugin_url( 'assets/blocks/image-box/frontend.js' ), |
| 84 |
[ 'jquery' ], |
| 85 |
getwid()->settings()->getVersion(), |
| 86 |
true |
| 87 |
); |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
public function render_callback( $attributes, $content ) { |
| 92 |
|
| 93 |
$this->block_frontend_assets(); |
| 94 |
|
| 95 |
return $content; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
getwid()->blocksManager()->addBlock( |
| 100 |
new \Getwid\Blocks\ImageBox() |
| 101 |
); |
| 102 |
|