PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / trunk
FrontBlocks for Gutenberg/GeneratePress vtrunk
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 / Counter.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 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
Counter.php
150 lines
1 <?php
2 /**
3 * Counter module for FrontBlocks (GenerateBlocks Counter 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 * Counter class to handle counter block functionality.
17 */
18 class Counter {
19 /**
20 * Constructor.
21 */
22 public function __construct() {
23 add_action( 'init', array( $this, 'register_assets' ) );
24 add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) );
25 add_filter( 'render_block', array( $this, 'render_block_counter' ), 10, 2 );
26 }
27
28 /**
29 * Register block assets for backend editor.
30 *
31 * @return void
32 */
33 public function register_assets() {
34 $dependencies = array(
35 'wp-blocks',
36 'wp-element',
37 'wp-editor',
38 'wp-components',
39 'wp-compose',
40 'wp-hooks',
41 'wp-data',
42 );
43
44 wp_register_script(
45 'frontblocks-counter-editor',
46 FRBL_PLUGIN_URL . 'assets/counter/frontblocks-counter.js',
47 $dependencies,
48 FRBL_VERSION,
49 true
50 );
51
52 wp_register_style(
53 'frontblocks-counter-styles',
54 FRBL_PLUGIN_URL . 'assets/counter/frontblocks-counter.css',
55 array(),
56 FRBL_VERSION
57 );
58 }
59
60 /**
61 * Enqueue block editor assets.
62 *
63 * @return void
64 */
65 public function enqueue_editor_assets() {
66 wp_enqueue_script( 'frontblocks-counter-editor' );
67
68 // Set script translations for JavaScript.
69 wp_set_script_translations(
70 'frontblocks-counter-editor',
71 'frontblocks'
72 );
73
74 wp_enqueue_style( 'frontblocks-counter-styles' );
75 }
76
77 /**
78 * Render the counter block with necessary attributes and classes.
79 *
80 * @param string $block_content The original block content.
81 * @param array $block The block data.
82 * @return string Modified block content with counter functionality.
83 */
84 public function render_block_counter( $block_content, $block ) {
85 $supported = array( 'generateblocks/text', 'generateblocks/headline', 'core/heading', 'core/paragraph' );
86 if ( ! in_array( $block['blockName'], $supported, true ) ) {
87 return $block_content;
88 }
89
90 $attrs = $block['attrs'];
91 $is_counter_active = isset( $attrs['isCounterActive'] ) && $attrs['isCounterActive'];
92 $animation_duration = isset( $attrs['animationDuration'] ) ? (int) $attrs['animationDuration'] : 2000;
93 $start_number = isset( $attrs['startNumber'] ) ? $attrs['startNumber'] : '0';
94 $final_number = isset( $attrs['finalNumber'] ) ? $attrs['finalNumber'] : '';
95 $number_prefix = isset( $attrs['numberPrefix'] ) ? $attrs['numberPrefix'] : '';
96 $number_suffix = isset( $attrs['numberSuffix'] ) ? $attrs['numberSuffix'] : '';
97
98 if ( $is_counter_active ) {
99 $this->enqueue_counter_script();
100 $class_to_add = 'frontblocks-counter-active';
101 $target_value_full = '';
102
103 if ( ! empty( $final_number ) ) {
104 $target_value_full = $final_number;
105 } elseif ( preg_match( '/<[^>]+>([^<]+)<\/[^>]+>/', $block_content, $matches ) ) {
106 $target_value_full = trim( $matches[1] );
107 } else {
108 return $block_content;
109 }
110
111 $data_attributes = ' data-counter-target="' . esc_attr( $target_value_full ) . '"' .
112 ' data-counter-start="' . esc_attr( $start_number ) . '"' .
113 ' data-counter-duration="' . $animation_duration . '"' .
114 ' data-counter-prefix="' . esc_attr( $number_prefix ) . '"' .
115 ' data-counter-suffix="' . esc_attr( $number_suffix ) . '"';
116
117 $block_content = preg_replace(
118 '/<([a-z0-9]+)(\s[^>]*?)?>/i',
119 '<$1$2' . $data_attributes . '>',
120 $block_content,
121 1
122 );
123
124 $block_content = str_replace(
125 'class="',
126 'class="' . $class_to_add . ' ',
127 $block_content
128 );
129 }
130
131 return $block_content;
132 }
133
134 /**
135 * Enqueue the counter runtime script in the footer.
136 *
137 * @return void
138 */
139 public function enqueue_counter_script() {
140 $script_handle = 'frontblocks-counter-runtime';
141 $script_path = FRBL_PLUGIN_URL . 'assets/counter/frontblocks-counter-runtime.js';
142 $script_deps = array();
143 $script_version = FRBL_VERSION;
144
145 if ( ! wp_script_is( $script_handle, 'enqueued' ) ) {
146 wp_enqueue_script( $script_handle, $script_path, $script_deps, $script_version, true );
147 }
148 }
149 }
150