PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / ci-artifacts
FrontBlocks for Gutenberg/GeneratePress vci-artifacts
trunk 0.2.0 0.2.1 0.2.2 0.2.3 0.2.4 0.2.5 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 ci-artifacts
frontblocks / includes / Frontend / Headline.php
frontblocks / includes / Frontend Last commit date
Animations.php 1 month ago BackButton.php 7 months ago BeforeAfter.php 1 month ago BlockPatterns.php 4 months ago Carousel.php 1 month ago ContainerEdgeAlignment.php 1 month ago Counter.php 1 month ago Events.php 6 months ago FaqSchema.php 1 month ago FluidTypography.php 4 months ago Gallery.php 8 months ago GravityFormsInline.php 1 month ago Headline.php 1 month ago InsertPost.php 1 month ago ProductCategories.php 8 months ago ReadingProgress.php 7 months ago ReadingTime.php 8 months ago ShapeAnimations.php 1 month ago StackedImages.php 4 months ago StickyColumn.php 1 month ago Testimonials.php 8 months ago TextAnimation.php 1 month ago
Headline.php
172 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 }
32
33 /**
34 * Register block assets for backend editor.
35 *
36 * @return void
37 */
38 public function register_assets() {
39 wp_register_script(
40 'frontblocks-headline-editor',
41 FRBL_PLUGIN_URL . 'assets/headline/frontblocks-headline.js',
42 array(
43 'wp-blocks',
44 'wp-element',
45 'wp-editor',
46 'wp-components',
47 'wp-compose',
48 'wp-hooks',
49 'wp-data',
50 ),
51 FRBL_VERSION,
52 true
53 );
54
55 wp_register_style(
56 'frontblocks-headline-styles',
57 FRBL_PLUGIN_URL . 'assets/headline/frontblocks-headline.css',
58 array(),
59 FRBL_VERSION
60 );
61
62 wp_register_script(
63 'frontblocks-headline-marquee',
64 FRBL_PLUGIN_URL . 'assets/headline/frontblocks-headline-marquee.js',
65 array(),
66 FRBL_VERSION,
67 true
68 );
69 }
70 /**
71 * Editor assets
72 *
73 * @return void
74 */
75 public function enqueue_editor_assets() {
76 wp_enqueue_script( 'frontblocks-headline-editor' );
77
78 // Set script translations for JavaScript.
79 wp_set_script_translations(
80 'frontblocks-headline-editor',
81 'frontblocks'
82 );
83
84 wp_enqueue_style( 'frontblocks-headline-styles' );
85 }
86
87 /**
88 * Conditionally enqueue frontend assets when a GenerateBlocks headline/text block is rendered.
89 *
90 * @param string $block_content Block content.
91 * @param array $block Block data.
92 * @return string
93 */
94 public function maybe_enqueue_frontend_assets( $block_content, $block ) {
95 $attrs = $block['attrs'] ?? array();
96
97 // Enqueue headline styles when any headline/text block with frbl features is rendered.
98 if ( ! wp_style_is( 'frontblocks-headline-styles', 'enqueued' ) ) {
99 wp_enqueue_style( 'frontblocks-headline-styles' );
100 }
101
102 // Enqueue marquee script only when marquee is active on this block.
103 if ( ! empty( $attrs['frblMarqueeSpeed'] ) && ! wp_script_is( 'frontblocks-headline-marquee', 'enqueued' ) ) {
104 wp_enqueue_script( 'frontblocks-headline-marquee' );
105 }
106
107 return $block_content;
108 }
109
110 /**
111 * Add line class attribute.
112 *
113 * @param array $attributes Attributes values for the block.
114 * Add line class attribute to headline block.
115 *
116 * @return array
117 */
118 public function add_line_class_attribute( $attributes ) {
119 return $attributes;
120 }
121
122 /**
123 * Add marquee speed data attribute to text block.
124 *
125 * @param array $attributes HTML attributes.
126 * @param array $block_attributes Block attributes.
127 * @return array
128 */
129 public function add_marquee_speed_attribute( $attributes, $block_attributes ) {
130 // Speed presets mapping.
131 $speed_presets = array(
132 'fast' => 10,
133 'medium' => 20,
134 'slow' => 40,
135 );
136
137 $speed_value = 20; // Default.
138
139 // Check if speed is set in block attributes (can be preset string or number).
140 if ( isset( $block_attributes['frblMarqueeSpeed'] ) && ! empty( $block_attributes['frblMarqueeSpeed'] ) ) {
141 $speed_preset = $block_attributes['frblMarqueeSpeed'];
142 // Check if it's a preset string.
143 if ( isset( $speed_presets[ $speed_preset ] ) ) {
144 $speed_value = $speed_presets[ $speed_preset ];
145 } else {
146 // Try to parse as number.
147 $speed_value = absint( $speed_preset );
148 if ( $speed_value <= 0 ) {
149 $speed_value = 20;
150 }
151 }
152 }
153
154 // Also check if it's already in htmlAttributes (from editor) - this takes precedence.
155 if ( isset( $block_attributes['htmlAttributes'] ) && is_array( $block_attributes['htmlAttributes'] ) ) {
156 if ( isset( $block_attributes['htmlAttributes']['data-marquee-speed'] ) && ! empty( $block_attributes['htmlAttributes']['data-marquee-speed'] ) ) {
157 $html_speed = $block_attributes['htmlAttributes']['data-marquee-speed'];
158 // If it's a number, use it directly.
159 if ( is_numeric( $html_speed ) ) {
160 $speed_value = absint( $html_speed );
161 } elseif ( isset( $speed_presets[ $html_speed ] ) ) {
162 $speed_value = $speed_presets[ $html_speed ];
163 }
164 }
165 }
166
167 $attributes['data-marquee-speed'] = $speed_value;
168
169 return $attributes;
170 }
171 }
172