| 1 |
<?php |
| 2 |
|
| 3 |
namespace Getwid\Blocks; |
| 4 |
|
| 5 |
class ImageHotspot extends \Getwid\Blocks\AbstractBlock { |
| 6 |
|
| 7 |
protected static $blockName = 'getwid/image-hotspot'; |
| 8 |
|
| 9 |
public function __construct() { |
| 10 |
|
| 11 |
parent::__construct( self::$blockName ); |
| 12 |
|
| 13 |
register_block_type( |
| 14 |
'getwid/image-hotspot', |
| 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 |
add_filter( 'getwid/blocks_style_css/dependencies', [ $this, 'block_frontend_styles' ] ); |
| 24 |
|
| 25 |
//Register JS/CSS assets |
| 26 |
wp_register_script( |
| 27 |
'popper', |
| 28 |
getwid_get_plugin_url( 'vendors/tippy.js/popper.min.js' ), |
| 29 |
[ 'jquery' ], |
| 30 |
'2.4.0', |
| 31 |
true |
| 32 |
); |
| 33 |
|
| 34 |
wp_register_script( |
| 35 |
'tippy', |
| 36 |
getwid_get_plugin_url( 'vendors/tippy.js/tippy-bundle.umd.min.js' ), |
| 37 |
[ 'jquery', 'popper' ], |
| 38 |
'6.2.3', |
| 39 |
true |
| 40 |
); |
| 41 |
|
| 42 |
wp_register_script( |
| 43 |
'waypoints', |
| 44 |
getwid_get_plugin_url( 'vendors/waypoints/lib/jquery.waypoints.min.js' ), |
| 45 |
[ 'jquery' ], |
| 46 |
'4.0.1', |
| 47 |
true |
| 48 |
); |
| 49 |
|
| 50 |
wp_register_script( |
| 51 |
'unescape', |
| 52 |
getwid_get_plugin_url( 'vendors/lodash.unescape/unescape.min.js' ), |
| 53 |
[], |
| 54 |
'4.0.1', |
| 55 |
true |
| 56 |
); |
| 57 |
|
| 58 |
wp_register_style( |
| 59 |
'tippy-themes', |
| 60 |
getwid_get_plugin_url( 'vendors/tippy.js/themes.css' ), |
| 61 |
[], |
| 62 |
'6.2.3' |
| 63 |
); |
| 64 |
|
| 65 |
wp_register_style( |
| 66 |
'tippy-animation', |
| 67 |
getwid_get_plugin_url( 'vendors/tippy.js/animations.css' ), |
| 68 |
[], |
| 69 |
'6.2.3' |
| 70 |
); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
public function getLabel() { |
| 75 |
return __('Image Hotspot', 'getwid'); |
| 76 |
} |
| 77 |
|
| 78 |
public function block_frontend_styles($styles) { |
| 79 |
|
| 80 |
//fontawesome |
| 81 |
$styles = getwid()->fontIconsManager()->enqueueFonts( $styles ); |
| 82 |
|
| 83 |
//themes.css |
| 84 |
if ( is_admin() && ! in_array( 'tippy-themes', $styles ) ) { |
| 85 |
array_push( $styles, 'tippy-themes' ); |
| 86 |
} |
| 87 |
|
| 88 |
//animation.css |
| 89 |
if ( is_admin() && ! in_array( 'tippy-animation', $styles ) ) { |
| 90 |
array_push( $styles, 'tippy-animation' ); |
| 91 |
} |
| 92 |
|
| 93 |
return $styles; |
| 94 |
} |
| 95 |
|
| 96 |
public function block_editor_scripts($scripts) { |
| 97 |
|
| 98 |
//popper.min.js |
| 99 |
if ( ! in_array( 'popper', $scripts ) ) { |
| 100 |
array_push( $scripts, 'popper' ); |
| 101 |
} |
| 102 |
|
| 103 |
//tippy-bundle.umd.min.js |
| 104 |
if ( ! in_array( 'tippy', $scripts ) ) { |
| 105 |
array_push( $scripts, 'tippy' ); |
| 106 |
} |
| 107 |
|
| 108 |
return $scripts; |
| 109 |
} |
| 110 |
|
| 111 |
public function block_frontend_assets() { |
| 112 |
|
| 113 |
if ( is_admin() ) { |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
//popper.min.js |
| 118 |
if ( ! wp_script_is( 'popper', 'enqueued' ) ) { |
| 119 |
wp_enqueue_script('popper'); |
| 120 |
} |
| 121 |
|
| 122 |
//tippy-bundle.umd.min.js |
| 123 |
if ( ! wp_script_is( 'tippy', 'enqueued' ) ) { |
| 124 |
wp_enqueue_script('tippy'); |
| 125 |
} |
| 126 |
|
| 127 |
//jquery.waypoints.min.js |
| 128 |
if ( ! wp_script_is( 'waypoints', 'enqueued' ) ) { |
| 129 |
wp_enqueue_script('waypoints'); |
| 130 |
} |
| 131 |
|
| 132 |
//unescape.min.js |
| 133 |
if ( ! wp_script_is( 'unescape', 'enqueued' ) ) { |
| 134 |
wp_enqueue_script( 'unescape' ); |
| 135 |
} |
| 136 |
|
| 137 |
//themes.css |
| 138 |
if ( ! wp_style_is( 'tippy-themes', 'enqueued' ) ) { |
| 139 |
wp_enqueue_style( 'tippy-themes' ); |
| 140 |
} |
| 141 |
|
| 142 |
//animation.css |
| 143 |
if ( ! wp_style_is( 'tippy-animation', 'enqueued' ) ) { |
| 144 |
wp_enqueue_style( 'tippy-animation' ); |
| 145 |
} |
| 146 |
|
| 147 |
if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
$deps_css = [ |
| 152 |
'tippy-themes', 'tippy-animation' |
| 153 |
]; |
| 154 |
|
| 155 |
$deps_js = [ 'jquery', 'popper', 'tippy', 'waypoints', 'unescape' ]; |
| 156 |
|
| 157 |
//fontawesome |
| 158 |
$deps_css = getwid()->fontIconsManager()->enqueueFonts( $deps_css ); |
| 159 |
|
| 160 |
add_filter( 'getwid/optimize/assets', |
| 161 |
function ( $assets ) { |
| 162 |
$assets[] = getwid()->settings()->getPrefix() . '-blocks-common'; |
| 163 |
|
| 164 |
return $assets; |
| 165 |
} |
| 166 |
); |
| 167 |
|
| 168 |
add_filter( 'getwid/optimize/should_load_common_css', '__return_true' ); |
| 169 |
|
| 170 |
$rtl = is_rtl() ? '.rtl' : ''; |
| 171 |
|
| 172 |
wp_enqueue_style( |
| 173 |
self::$blockName, |
| 174 |
getwid_get_plugin_url( 'assets/blocks/image-hotspot/style' . $rtl . '.css' ), |
| 175 |
$deps_css, |
| 176 |
getwid()->settings()->getVersion() |
| 177 |
); |
| 178 |
|
| 179 |
wp_enqueue_script( |
| 180 |
self::$blockName, |
| 181 |
getwid_get_plugin_url( 'assets/blocks/image-hotspot/frontend.js' ), |
| 182 |
$deps_js, |
| 183 |
getwid()->settings()->getVersion(), |
| 184 |
true |
| 185 |
); |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
public function render_callback( $attributes, $content ) { |
| 190 |
|
| 191 |
$this->block_frontend_assets(); |
| 192 |
|
| 193 |
return $content; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
getwid()->blocksManager()->addBlock( |
| 198 |
new \Getwid\Blocks\ImageHotspot() |
| 199 |
); |
| 200 |
|