PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / 1.0.4
FrontBlocks for Gutenberg/GeneratePress v1.0.4
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 8 months ago Carousel.php 8 months ago Counter.php 8 months ago Gallery.php 8 months ago Headline.php 8 months ago InsertPost.php 8 months ago ProductCategories.php 8 months ago StickyColumn.php 8 months ago Testimonials.php 8 months ago
Counter.php
140 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 wp_enqueue_style( 'frontblocks-counter-styles' );
69 }
70
71 /**
72 * Render the counter block with necessary attributes and classes.
73 *
74 * @param string $block_content The original block content.
75 * @param array $block The block data.
76 * @return string Modified block content with counter functionality.
77 */
78 public function render_block_counter( $block_content, $block ) {
79 if ( 'generateblocks/text' !== $block['blockName'] ) {
80 return $block_content;
81 }
82
83 $attrs = $block['attrs'];
84 $is_counter_active = isset( $attrs['isCounterActive'] ) && $attrs['isCounterActive'];
85 $animation_duration = isset( $attrs['animationDuration'] ) ? (int) $attrs['animationDuration'] : 2000;
86 $final_number = isset( $attrs['finalNumber'] ) ? $attrs['finalNumber'] : '';
87 $number_prefix = isset( $attrs['numberPrefix'] ) ? $attrs['numberPrefix'] : '';
88 $number_suffix = isset( $attrs['numberSuffix'] ) ? $attrs['numberSuffix'] : '';
89
90 if ( $is_counter_active ) {
91 $class_to_add = 'frontblocks-counter-active';
92 $target_value_full = '';
93
94 if ( ! empty( $final_number ) ) {
95 $target_value_full = $final_number;
96 } elseif ( preg_match( '/<[^>]+>([^<]+)<\/[^>]+>/', $block_content, $matches ) ) {
97 $target_value_full = trim( $matches[1] );
98 } else {
99 return $block_content;
100 }
101
102 $data_attributes = ' data-counter-target="' . esc_attr( $target_value_full ) . '"' .
103 ' data-counter-duration="' . $animation_duration . '"' .
104 ' data-counter-prefix="' . esc_attr( $number_prefix ) . '"' .
105 ' data-counter-suffix="' . esc_attr( $number_suffix ) . '"';
106
107 $block_content = preg_replace(
108 '/<([a-z0-9]+)(\s[^>]*?)?>/i',
109 '<$1$2' . $data_attributes . '>',
110 $block_content,
111 1
112 );
113
114 $block_content = str_replace(
115 'class="',
116 'class="' . $class_to_add . ' ',
117 $block_content
118 );
119 }
120
121 return $block_content;
122 }
123
124 /**
125 * Enqueue the counter runtime script in the footer.
126 *
127 * @return void
128 */
129 public function enqueue_counter_script() {
130 $script_handle = 'frontblocks-counter-runtime';
131 $script_path = FRBL_PLUGIN_URL . 'assets/counter/frontblocks-counter-runtime.js';
132 $script_deps = array();
133 $script_version = FRBL_VERSION;
134
135 if ( ! wp_script_is( $script_handle, 'enqueued' ) ) {
136 wp_enqueue_script( $script_handle, $script_path, $script_deps, $script_version, true );
137 }
138 }
139 }
140