PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / 1.2.0
FrontBlocks for Gutenberg/GeneratePress v1.2.0
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 ReadingTime.php 8 months ago StickyColumn.php 8 months ago Testimonials.php 8 months ago
Counter.php
147 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 $final_number = isset( $attrs['finalNumber'] ) ? $attrs['finalNumber'] : '';
94 $number_prefix = isset( $attrs['numberPrefix'] ) ? $attrs['numberPrefix'] : '';
95 $number_suffix = isset( $attrs['numberSuffix'] ) ? $attrs['numberSuffix'] : '';
96
97 if ( $is_counter_active ) {
98 $class_to_add = 'frontblocks-counter-active';
99 $target_value_full = '';
100
101 if ( ! empty( $final_number ) ) {
102 $target_value_full = $final_number;
103 } elseif ( preg_match( '/<[^>]+>([^<]+)<\/[^>]+>/', $block_content, $matches ) ) {
104 $target_value_full = trim( $matches[1] );
105 } else {
106 return $block_content;
107 }
108
109 $data_attributes = ' data-counter-target="' . esc_attr( $target_value_full ) . '"' .
110 ' data-counter-duration="' . $animation_duration . '"' .
111 ' data-counter-prefix="' . esc_attr( $number_prefix ) . '"' .
112 ' data-counter-suffix="' . esc_attr( $number_suffix ) . '"';
113
114 $block_content = preg_replace(
115 '/<([a-z0-9]+)(\s[^>]*?)?>/i',
116 '<$1$2' . $data_attributes . '>',
117 $block_content,
118 1
119 );
120
121 $block_content = str_replace(
122 'class="',
123 'class="' . $class_to_add . ' ',
124 $block_content
125 );
126 }
127
128 return $block_content;
129 }
130
131 /**
132 * Enqueue the counter runtime script in the footer.
133 *
134 * @return void
135 */
136 public function enqueue_counter_script() {
137 $script_handle = 'frontblocks-counter-runtime';
138 $script_path = FRBL_PLUGIN_URL . 'assets/counter/frontblocks-counter-runtime.js';
139 $script_deps = array();
140 $script_version = FRBL_VERSION;
141
142 if ( ! wp_script_is( $script_handle, 'enqueued' ) ) {
143 wp_enqueue_script( $script_handle, $script_path, $script_deps, $script_version, true );
144 }
145 }
146 }
147