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