Animations.php
1 month ago
BackButton.php
7 months ago
BeforeAfter.php
1 month ago
BlockPatterns.php
4 months ago
Carousel.php
1 week ago
ColumnsSameHeight.php
1 week ago
ContainerEdgeAlignment.php
4 weeks ago
Counter.php
1 month ago
DownloadButton.php
1 week ago
Events.php
4 weeks ago
FaqSchema.php
1 week ago
FluidTypography.php
4 weeks ago
Gallery.php
8 months ago
GravityFormsInline.php
1 month ago
Headline.php
4 weeks ago
InsertPost.php
1 month ago
ProductCategories.php
4 weeks ago
ReadingProgress.php
7 months ago
ReadingTime.php
8 months ago
ShapeAnimations.php
4 weeks ago
StackedImages.php
4 months ago
StickyColumn.php
4 weeks ago
SvgUpload.php
4 weeks ago
Testimonials.php
8 months ago
TextAnimation.php
1 month ago
UserText.php
4 weeks ago
Headline.php
242 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_filter( 'generateblocks_attr_headline', array( $this, 'add_line_class_attribute' ), 10 ); |
| 28 | add_filter( 'generateblocks_attr_text', array( $this, 'add_marquee_speed_attribute' ), 10, 2 ); |
| 29 | add_filter( 'render_block_generateblocks/text', array( $this, 'maybe_enqueue_frontend_assets' ), 10, 2 ); |
| 30 | add_filter( 'render_block_generateblocks/headline', array( $this, 'maybe_enqueue_frontend_assets' ), 10, 2 ); |
| 31 | add_filter( 'register_block_type_args', array( $this, 'register_native_block_attributes' ), 10, 2 ); |
| 32 | add_filter( 'render_block_core/paragraph', array( $this, 'apply_marquee_to_native_block' ), 10, 2 ); |
| 33 | add_filter( 'render_block_core/heading', array( $this, 'apply_marquee_to_native_block' ), 10, 2 ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Register block assets for backend editor. |
| 38 | * |
| 39 | * @return void |
| 40 | */ |
| 41 | public function register_assets() { |
| 42 | wp_register_script( |
| 43 | 'frontblocks-headline-editor', |
| 44 | FRBL_PLUGIN_URL . 'assets/headline/frontblocks-headline.js', |
| 45 | array( |
| 46 | 'wp-blocks', |
| 47 | 'wp-element', |
| 48 | 'wp-editor', |
| 49 | 'wp-components', |
| 50 | 'wp-compose', |
| 51 | 'wp-hooks', |
| 52 | 'wp-data', |
| 53 | ), |
| 54 | FRBL_VERSION, |
| 55 | true |
| 56 | ); |
| 57 | |
| 58 | wp_register_style( |
| 59 | 'frontblocks-headline-styles', |
| 60 | FRBL_PLUGIN_URL . 'assets/headline/frontblocks-headline.css', |
| 61 | array(), |
| 62 | FRBL_VERSION |
| 63 | ); |
| 64 | |
| 65 | wp_register_script( |
| 66 | 'frontblocks-headline-marquee', |
| 67 | FRBL_PLUGIN_URL . 'assets/headline/frontblocks-headline-marquee.js', |
| 68 | array(), |
| 69 | FRBL_VERSION, |
| 70 | true |
| 71 | ); |
| 72 | } |
| 73 | /** |
| 74 | * Editor assets |
| 75 | * |
| 76 | * @return void |
| 77 | */ |
| 78 | public function enqueue_editor_assets() { |
| 79 | wp_enqueue_script( 'frontblocks-headline-editor' ); |
| 80 | |
| 81 | // Set script translations for JavaScript. |
| 82 | wp_set_script_translations( |
| 83 | 'frontblocks-headline-editor', |
| 84 | 'frontblocks' |
| 85 | ); |
| 86 | |
| 87 | wp_enqueue_style( 'frontblocks-headline-styles' ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Conditionally enqueue frontend assets when a GenerateBlocks headline/text block is rendered. |
| 92 | * |
| 93 | * @param string $block_content Block content. |
| 94 | * @param array $block Block data. |
| 95 | * @return string |
| 96 | */ |
| 97 | public function maybe_enqueue_frontend_assets( $block_content, $block ) { |
| 98 | $attrs = $block['attrs'] ?? array(); |
| 99 | |
| 100 | // Enqueue headline styles when any headline/text block with frbl features is rendered. |
| 101 | if ( ! wp_style_is( 'frontblocks-headline-styles', 'enqueued' ) ) { |
| 102 | wp_enqueue_style( 'frontblocks-headline-styles' ); |
| 103 | } |
| 104 | |
| 105 | // Enqueue marquee script only when marquee is active on this block. |
| 106 | if ( ! empty( $attrs['frblMarqueeSpeed'] ) && ! wp_script_is( 'frontblocks-headline-marquee', 'enqueued' ) ) { |
| 107 | wp_enqueue_script( 'frontblocks-headline-marquee' ); |
| 108 | } |
| 109 | |
| 110 | return $block_content; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Add line class attribute. |
| 115 | * |
| 116 | * @param array $attributes Attributes values for the block. |
| 117 | * Add line class attribute to headline block. |
| 118 | * |
| 119 | * @return array |
| 120 | */ |
| 121 | public function add_line_class_attribute( $attributes ) { |
| 122 | return $attributes; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Register frblMarqueeSpeed attribute server-side for paragraph and heading. |
| 127 | * |
| 128 | * @param array $args Block type args. |
| 129 | * @param string $block_type Block type name. |
| 130 | * @return array |
| 131 | */ |
| 132 | public function register_native_block_attributes( $args, $block_type ) { |
| 133 | if ( 'core/paragraph' !== $block_type && 'core/heading' !== $block_type ) { |
| 134 | return $args; |
| 135 | } |
| 136 | |
| 137 | if ( ! isset( $args['attributes'] ) ) { |
| 138 | $args['attributes'] = array(); |
| 139 | } |
| 140 | |
| 141 | $args['attributes']['frblMarqueeSpeed'] = array( |
| 142 | 'type' => 'string', |
| 143 | 'default' => '', |
| 144 | ); |
| 145 | |
| 146 | return $args; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Inject data-marquee-speed and enqueue scripts for native paragraph/heading blocks. |
| 151 | * |
| 152 | * @param string $block_content Block HTML. |
| 153 | * @param array $block Block data. |
| 154 | * @return string |
| 155 | */ |
| 156 | public function apply_marquee_to_native_block( $block_content, $block ) { |
| 157 | $attrs = $block['attrs'] ?? array(); |
| 158 | $class_name = $attrs['className'] ?? ''; |
| 159 | |
| 160 | if ( false === strpos( $class_name, 'gb-marquee-infinite-scroll' ) ) { |
| 161 | return $block_content; |
| 162 | } |
| 163 | |
| 164 | $speed_presets = array( |
| 165 | 'fast' => 10, |
| 166 | 'medium' => 20, |
| 167 | 'slow' => 40, |
| 168 | ); |
| 169 | |
| 170 | $speed = isset( $attrs['frblMarqueeSpeed'] ) && ! empty( $attrs['frblMarqueeSpeed'] ) ? sanitize_text_field( $attrs['frblMarqueeSpeed'] ) : 'medium'; |
| 171 | $speed_value = isset( $speed_presets[ $speed ] ) ? $speed_presets[ $speed ] : 20; |
| 172 | |
| 173 | // Inject data-marquee-speed into the first opening tag. |
| 174 | $block_content = preg_replace( |
| 175 | '/(<[a-z][a-z0-9]*(?:\s[^>]*)?)(\s*>)/i', |
| 176 | '$1 data-marquee-speed="' . esc_attr( $speed_value ) . '"$2', |
| 177 | $block_content, |
| 178 | 1 |
| 179 | ); |
| 180 | |
| 181 | if ( ! wp_script_is( 'frontblocks-headline-marquee', 'enqueued' ) ) { |
| 182 | wp_enqueue_script( 'frontblocks-headline-marquee' ); |
| 183 | } |
| 184 | |
| 185 | if ( ! wp_style_is( 'frontblocks-headline-styles', 'enqueued' ) ) { |
| 186 | wp_enqueue_style( 'frontblocks-headline-styles' ); |
| 187 | } |
| 188 | |
| 189 | return $block_content; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Add marquee speed data attribute to text block. |
| 194 | * |
| 195 | * @param array $attributes HTML attributes. |
| 196 | * @param array $block_attributes Block attributes. |
| 197 | * @return array |
| 198 | */ |
| 199 | public function add_marquee_speed_attribute( $attributes, $block_attributes ) { |
| 200 | // Speed presets mapping. |
| 201 | $speed_presets = array( |
| 202 | 'fast' => 10, |
| 203 | 'medium' => 20, |
| 204 | 'slow' => 40, |
| 205 | ); |
| 206 | |
| 207 | $speed_value = 20; // Default. |
| 208 | |
| 209 | // Check if speed is set in block attributes (can be preset string or number). |
| 210 | if ( isset( $block_attributes['frblMarqueeSpeed'] ) && ! empty( $block_attributes['frblMarqueeSpeed'] ) ) { |
| 211 | $speed_preset = $block_attributes['frblMarqueeSpeed']; |
| 212 | // Check if it's a preset string. |
| 213 | if ( isset( $speed_presets[ $speed_preset ] ) ) { |
| 214 | $speed_value = $speed_presets[ $speed_preset ]; |
| 215 | } else { |
| 216 | // Try to parse as number. |
| 217 | $speed_value = absint( $speed_preset ); |
| 218 | if ( $speed_value <= 0 ) { |
| 219 | $speed_value = 20; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // Also check if it's already in htmlAttributes (from editor) - this takes precedence. |
| 225 | if ( isset( $block_attributes['htmlAttributes'] ) && is_array( $block_attributes['htmlAttributes'] ) ) { |
| 226 | if ( isset( $block_attributes['htmlAttributes']['data-marquee-speed'] ) && ! empty( $block_attributes['htmlAttributes']['data-marquee-speed'] ) ) { |
| 227 | $html_speed = $block_attributes['htmlAttributes']['data-marquee-speed']; |
| 228 | // If it's a number, use it directly. |
| 229 | if ( is_numeric( $html_speed ) ) { |
| 230 | $speed_value = absint( $html_speed ); |
| 231 | } elseif ( isset( $speed_presets[ $html_speed ] ) ) { |
| 232 | $speed_value = $speed_presets[ $html_speed ]; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | $attributes['data-marquee-speed'] = $speed_value; |
| 238 | |
| 239 | return $attributes; |
| 240 | } |
| 241 | } |
| 242 |