| 1 |
<?php |
| 2 |
declare( strict_types=1 ); |
| 3 |
|
| 4 |
namespace GutSlider\Blocks; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Static slider (gutsliders/content-slider): motion effects on the page. |
| 12 |
* |
| 13 |
* The block saves static markup and many are published, so the newer motion settings |
| 14 |
* (content reveal, pointer effect, magnetic buttons) are added to the saved HTML as it is |
| 15 |
* printed: classes on the block wrapper, which frontend.js and style.scss act on. |
| 16 |
* |
| 17 |
* @package GutSlider\Blocks |
| 18 |
* @since 3.2.0 |
| 19 |
*/ |
| 20 |
final class StaticSlider { |
| 21 |
|
| 22 |
/** Values the motion settings may take. */ |
| 23 |
const REVEALS = array( 'words', 'letters', 'blur', 'mask' ); |
| 24 |
const POINTERS = array( 'depth', 'tilt', 'spotlight' ); |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor. |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
add_filter( 'render_block_gutsliders/content-slider', array( $this, 'render' ), 20, 2 ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Add the motion classes to the slider wrapper. |
| 35 |
* |
| 36 |
* @param string $content Block HTML. |
| 37 |
* @param array $block Parsed block. |
| 38 |
* @return string |
| 39 |
*/ |
| 40 |
public function render( $content, $block ) { |
| 41 |
$attrs = $block['attrs'] ?? array(); |
| 42 |
$reveal = in_array( $attrs['textReveal'] ?? '', self::REVEALS, true ) ? $attrs['textReveal'] : ''; |
| 43 |
$pointer = in_array( $attrs['pointerEffect'] ?? '', self::POINTERS, true ) ? $attrs['pointerEffect'] : ''; |
| 44 |
$magnet = ! empty( $attrs['magneticButtons'] ); |
| 45 |
|
| 46 |
if ( ! $reveal && ! $pointer && ! $magnet ) { |
| 47 |
return $content; |
| 48 |
} |
| 49 |
|
| 50 |
$tags = new \WP_HTML_Tag_Processor( $content ); |
| 51 |
if ( ! $tags->next_tag( array( 'class_name' => 'wp-block-gutsliders-content-slider' ) ) ) { |
| 52 |
return $content; |
| 53 |
} |
| 54 |
|
| 55 |
// Read by motion.js and style.scss. |
| 56 |
if ( $reveal ) { |
| 57 |
$tags->add_class( 'gs-reveal' ); |
| 58 |
$tags->add_class( 'gs-reveal-' . $reveal ); |
| 59 |
if ( in_array( $attrs['revealSpeed'] ?? '', array( 'slow', 'fast' ), true ) ) { |
| 60 |
$tags->add_class( 'gs-reveal-' . $attrs['revealSpeed'] ); |
| 61 |
} |
| 62 |
} |
| 63 |
if ( $pointer ) { |
| 64 |
$tags->add_class( 'gs-pointer-' . $pointer ); |
| 65 |
} |
| 66 |
if ( $magnet ) { |
| 67 |
$tags->add_class( 'gs-magnetic' ); |
| 68 |
} |
| 69 |
|
| 70 |
return $tags->get_updated_html(); |
| 71 |
} |
| 72 |
} |
| 73 |
|