PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / 1.3.2
FrontBlocks for Gutenberg/GeneratePress v1.3.2
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 4 months ago BackButton.php 7 months ago BlockPatterns.php 4 months ago Carousel.php 6 months ago ContainerEdgeAlignment.php 7 months ago Counter.php 4 months ago Events.php 6 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
Counter.php
149 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 add_action( 'wp_footer', array( $this, 'enqueue_counter_script' ) );
27 }
28
29 /**
30 * Register block assets for backend editor.
31 *
32 * @return void
33 */
34 public function register_assets() {
35 $dependencies = array(
36 'wp-blocks',
37 'wp-element',
38 'wp-editor',
39 'wp-components',
40 'wp-compose',
41 'wp-hooks',
42 'wp-data',
43 );
44
45 wp_register_script(
46 'frontblocks-counter-editor',
47 FRBL_PLUGIN_URL . 'assets/counter/frontblocks-counter.js',
48 $dependencies,
49 FRBL_VERSION,
50 true
51 );
52
53 wp_register_style(
54 'frontblocks-counter-styles',
55 FRBL_PLUGIN_URL . 'assets/counter/frontblocks-counter.css',
56 array(),
57 FRBL_VERSION
58 );
59 }
60
61 /**
62 * Enqueue block editor assets.
63 *
64 * @return void
65 */
66 public function enqueue_editor_assets() {
67 wp_enqueue_script( 'frontblocks-counter-editor' );
68
69 // Set script translations for JavaScript.
70 wp_set_script_translations(
71 'frontblocks-counter-editor',
72 'frontblocks'
73 );
74
75 wp_enqueue_style( 'frontblocks-counter-styles' );
76 }
77
78 /**
79 * Render the counter block with necessary attributes and classes.
80 *
81 * @param string $block_content The original block content.
82 * @param array $block The block data.
83 * @return string Modified block content with counter functionality.
84 */
85 public function render_block_counter( $block_content, $block ) {
86 if ( 'generateblocks/text' !== $block['blockName'] ) {
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 $class_to_add = 'frontblocks-counter-active';
100 $target_value_full = '';
101
102 if ( ! empty( $final_number ) ) {
103 $target_value_full = $final_number;
104 } elseif ( preg_match( '/<[^>]+>([^<]+)<\/[^>]+>/', $block_content, $matches ) ) {
105 $target_value_full = trim( $matches[1] );
106 } else {
107 return $block_content;
108 }
109
110 $data_attributes = ' data-counter-target="' . esc_attr( $target_value_full ) . '"' .
111 ' data-counter-start="' . esc_attr( $start_number ) . '"' .
112 ' data-counter-duration="' . $animation_duration . '"' .
113 ' data-counter-prefix="' . esc_attr( $number_prefix ) . '"' .
114 ' data-counter-suffix="' . esc_attr( $number_suffix ) . '"';
115
116 $block_content = preg_replace(
117 '/<([a-z0-9]+)(\s[^>]*?)?>/i',
118 '<$1$2' . $data_attributes . '>',
119 $block_content,
120 1
121 );
122
123 $block_content = str_replace(
124 'class="',
125 'class="' . $class_to_add . ' ',
126 $block_content
127 );
128 }
129
130 return $block_content;
131 }
132
133 /**
134 * Enqueue the counter runtime script in the footer.
135 *
136 * @return void
137 */
138 public function enqueue_counter_script() {
139 $script_handle = 'frontblocks-counter-runtime';
140 $script_path = FRBL_PLUGIN_URL . 'assets/counter/frontblocks-counter-runtime.js';
141 $script_deps = array();
142 $script_version = FRBL_VERSION;
143
144 if ( ! wp_script_is( $script_handle, 'enqueued' ) ) {
145 wp_enqueue_script( $script_handle, $script_path, $script_deps, $script_version, true );
146 }
147 }
148 }
149