| 1 |
<?php |
| 2 |
|
| 3 |
namespace gutenify; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
class Hover_Card{ |
| 8 |
public static function init() { |
| 9 |
add_action('init', array(__CLASS__, 'register_block')); |
| 10 |
} |
| 11 |
|
| 12 |
public static function register_block( ) { |
| 13 |
if ( ! Helpers::is_pro_block_active() ) { |
| 14 |
return; |
| 15 |
} |
| 16 |
|
| 17 |
register_block_type( __DIR__, array( |
| 18 |
'render_callback' => array( __CLASS__, 'render_callback' ) |
| 19 |
)); |
| 20 |
} |
| 21 |
|
| 22 |
public static function render_callback( $attributes, $content ) { |
| 23 |
$allowed_animations = array( |
| 24 |
'slide-top-bottom', |
| 25 |
'slide-bottom-top', |
| 26 |
'slide-left-right', |
| 27 |
'slide-right-left', |
| 28 |
'zoom-in', |
| 29 |
'fade-in', |
| 30 |
'flip', |
| 31 |
); |
| 32 |
$animation = ! empty( $attributes['animation'] ) && in_array( $attributes['animation'], $allowed_animations, true ) |
| 33 |
? $attributes['animation'] |
| 34 |
: 'slide-top-bottom'; |
| 35 |
|
| 36 |
$trigger_mode = ! empty( $attributes['triggerMode'] ) && 'tap' === $attributes['triggerMode'] |
| 37 |
? 'tap' |
| 38 |
: 'hover'; |
| 39 |
|
| 40 |
$allowed_easings = array( 'ease', 'ease-in', 'ease-out', 'ease-in-out', 'linear' ); |
| 41 |
$easing = ! empty( $attributes['animationEasing'] ) && in_array( $attributes['animationEasing'], $allowed_easings, true ) |
| 42 |
? $attributes['animationEasing'] |
| 43 |
: 'ease'; |
| 44 |
|
| 45 |
$duration = isset( $attributes['animationDuration'] ) ? max( 0.1, min( 3, (float) $attributes['animationDuration'] ) ) : 0.5; |
| 46 |
$delay = isset( $attributes['animationDelay'] ) ? max( 0, min( 2, (float) $attributes['animationDelay'] ) ) : 0; |
| 47 |
|
| 48 |
$class_names = array( |
| 49 |
'gutenify-hover-card-animation-' . $animation, |
| 50 |
'gutenify-hover-card-version-2', |
| 51 |
); |
| 52 |
|
| 53 |
if ( 'tap' === $trigger_mode ) { |
| 54 |
$class_names[] = 'gutenify-hover-card-trigger-tap'; |
| 55 |
} |
| 56 |
|
| 57 |
$wrapper_args = array( |
| 58 |
'class' => implode( ' ', $class_names ), |
| 59 |
'style' => sprintf( |
| 60 |
'--gutenify-hover-card-duration:%ss;--gutenify-hover-card-delay:%ss;--gutenify-hover-card-easing:%s;', |
| 61 |
$duration, |
| 62 |
$delay, |
| 63 |
$easing |
| 64 |
), |
| 65 |
); |
| 66 |
|
| 67 |
// A named group lets screen readers announce the card as one unit; an unnamed one is just noise. |
| 68 |
if ( ! empty( $attributes['ariaLabel'] ) ) { |
| 69 |
$wrapper_args['role'] = 'group'; |
| 70 |
} |
| 71 |
|
| 72 |
$wrapper_attributes = get_block_wrapper_attributes( $wrapper_args ); |
| 73 |
$output = sprintf( '<div %s>', $wrapper_attributes ); |
| 74 |
$output .= $content; |
| 75 |
$output .= '</div>'; |
| 76 |
return $output; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
Hover_Card::init(); |
| 81 |
|