Animations.php
4 months ago
BackButton.php
7 months ago
BlockPatterns.php
4 months ago
Carousel.php
7 months ago
ContainerEdgeAlignment.php
7 months ago
Counter.php
4 months ago
Events.php
7 months ago
FluidTypography.php
4 months ago
Gallery.php
8 months ago
GravityFormsInline.php
7 months ago
Headline.php
4 months ago
InsertPost.php
8 months ago
ProductCategories.php
8 months ago
ReadingProgress.php
7 months ago
ReadingTime.php
8 months ago
ShapeAnimations.php
7 months ago
StackedImages.php
4 months ago
StickyColumn.php
8 months ago
Testimonials.php
8 months ago
Headline.php
158 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Headline module for FrontBlocks (GenerateBlocks Headline enhancements). |
| 4 | * |
| 5 | * @package FrontBlocks |
| 6 | * @author Alex castellón <castellon@close.technology> |
| 7 | * @copyright 2025 Closemarketing |
| 8 | * @version 1.0.0 |
| 9 | */ |
| 10 | |
| 11 | namespace FrontBlocks\Frontend; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * Headline class. |
| 17 | * |
| 18 | * @since 1.1.0 |
| 19 | */ |
| 20 | class Headline { |
| 21 | /** |
| 22 | * Constructor. |
| 23 | */ |
| 24 | public function __construct() { |
| 25 | add_action( 'init', array( $this, 'register_assets' ) ); |
| 26 | add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) ); |
| 27 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_styles' ), 100 ); |
| 28 | add_filter( 'generateblocks_attr_headline', array( $this, 'add_line_class_attribute' ), 10 ); |
| 29 | add_filter( 'generateblocks_attr_text', array( $this, 'add_marquee_speed_attribute' ), 10, 2 ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Register block assets for backend editor. |
| 34 | * |
| 35 | * @return void |
| 36 | */ |
| 37 | public function register_assets() { |
| 38 | wp_register_script( |
| 39 | 'frontblocks-headline-editor', |
| 40 | FRBL_PLUGIN_URL . 'assets/headline/frontblocks-headline.js', |
| 41 | array( |
| 42 | 'wp-blocks', |
| 43 | 'wp-element', |
| 44 | 'wp-editor', |
| 45 | 'wp-components', |
| 46 | 'wp-compose', |
| 47 | 'wp-hooks', |
| 48 | 'wp-data', |
| 49 | ), |
| 50 | FRBL_VERSION, |
| 51 | true |
| 52 | ); |
| 53 | |
| 54 | wp_register_style( |
| 55 | 'frontblocks-headline-styles', |
| 56 | FRBL_PLUGIN_URL . 'assets/headline/frontblocks-headline.css', |
| 57 | array(), |
| 58 | FRBL_VERSION |
| 59 | ); |
| 60 | |
| 61 | wp_register_script( |
| 62 | 'frontblocks-headline-marquee', |
| 63 | FRBL_PLUGIN_URL . 'assets/headline/frontblocks-headline-marquee.js', |
| 64 | array(), |
| 65 | FRBL_VERSION, |
| 66 | true |
| 67 | ); |
| 68 | } |
| 69 | /** |
| 70 | * Editor assets |
| 71 | * |
| 72 | * @return void |
| 73 | */ |
| 74 | public function enqueue_editor_assets() { |
| 75 | wp_enqueue_script( 'frontblocks-headline-editor' ); |
| 76 | |
| 77 | // Set script translations for JavaScript. |
| 78 | wp_set_script_translations( |
| 79 | 'frontblocks-headline-editor', |
| 80 | 'frontblocks' |
| 81 | ); |
| 82 | |
| 83 | wp_enqueue_style( 'frontblocks-headline-styles' ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Enqueue frontend styles and scripts. |
| 88 | * |
| 89 | * @return void |
| 90 | */ |
| 91 | public function enqueue_frontend_styles() { |
| 92 | wp_enqueue_style( 'frontblocks-headline-styles' ); |
| 93 | wp_enqueue_script( 'frontblocks-headline-marquee' ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Add line class attribute. |
| 98 | * |
| 99 | * @param array $attributes Attributes values for the block. |
| 100 | * Add line class attribute to headline block. |
| 101 | * |
| 102 | * @return array |
| 103 | */ |
| 104 | public function add_line_class_attribute( $attributes ) { |
| 105 | return $attributes; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Add marquee speed data attribute to text block. |
| 110 | * |
| 111 | * @param array $attributes HTML attributes. |
| 112 | * @param array $block_attributes Block attributes. |
| 113 | * @return array |
| 114 | */ |
| 115 | public function add_marquee_speed_attribute( $attributes, $block_attributes ) { |
| 116 | // Speed presets mapping. |
| 117 | $speed_presets = array( |
| 118 | 'fast' => 10, |
| 119 | 'medium' => 20, |
| 120 | 'slow' => 40, |
| 121 | ); |
| 122 | |
| 123 | $speed_value = 20; // Default. |
| 124 | |
| 125 | // Check if speed is set in block attributes (can be preset string or number). |
| 126 | if ( isset( $block_attributes['frblMarqueeSpeed'] ) && ! empty( $block_attributes['frblMarqueeSpeed'] ) ) { |
| 127 | $speed_preset = $block_attributes['frblMarqueeSpeed']; |
| 128 | // Check if it's a preset string. |
| 129 | if ( isset( $speed_presets[ $speed_preset ] ) ) { |
| 130 | $speed_value = $speed_presets[ $speed_preset ]; |
| 131 | } else { |
| 132 | // Try to parse as number. |
| 133 | $speed_value = absint( $speed_preset ); |
| 134 | if ( $speed_value <= 0 ) { |
| 135 | $speed_value = 20; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // Also check if it's already in htmlAttributes (from editor) - this takes precedence. |
| 141 | if ( isset( $block_attributes['htmlAttributes'] ) && is_array( $block_attributes['htmlAttributes'] ) ) { |
| 142 | if ( isset( $block_attributes['htmlAttributes']['data-marquee-speed'] ) && ! empty( $block_attributes['htmlAttributes']['data-marquee-speed'] ) ) { |
| 143 | $html_speed = $block_attributes['htmlAttributes']['data-marquee-speed']; |
| 144 | // If it's a number, use it directly. |
| 145 | if ( is_numeric( $html_speed ) ) { |
| 146 | $speed_value = absint( $html_speed ); |
| 147 | } elseif ( isset( $speed_presets[ $html_speed ] ) ) { |
| 148 | $speed_value = $speed_presets[ $html_speed ]; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | $attributes['data-marquee-speed'] = $speed_value; |
| 154 | |
| 155 | return $attributes; |
| 156 | } |
| 157 | } |
| 158 |