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 / StickyColumn.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
StickyColumn.php
211 lines
1 <?php
2 /**
3 * Sticky Column module for FrontBlocks.
4 *
5 * @package FrontBlocks
6 * @author David Perez <david@close.technology>
7 * @copyright 2023 Closemarketing
8 * @version 1.0
9 */
10
11 namespace FrontBlocks\Frontend;
12
13 defined( 'ABSPATH' ) || exit;
14
15 /**
16 * StickyColumn class.
17 *
18 * @since 1.0.0
19 */
20 class StickyColumn {
21
22 /**
23 * Constructor.
24 */
25 public function __construct() {
26 $this->init_hooks();
27 }
28
29 /**
30 * Initialize hooks.
31 *
32 * @return void
33 */
34 private function init_hooks() {
35 add_action( 'init', array( $this, 'register_scripts' ) );
36 add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
37 add_filter( 'render_block_generateblocks/grid', array( $this, 'add_sticky_attributes_to_grid_block' ), 10, 2 );
38 add_action( 'init', array( $this, 'register_custom_attributes' ), 5 );
39 }
40
41 /**
42 * Register frontend scripts and styles for conditional enqueueing.
43 *
44 * @return void
45 */
46 public function register_scripts() {
47 wp_register_style(
48 'frontblocks-sticky-column',
49 FRBL_PLUGIN_URL . 'assets/sticky-column/frontblocks-sticky-column.css',
50 array(),
51 FRBL_VERSION
52 );
53
54 wp_register_script(
55 'frontblocks-sticky-column-custom',
56 FRBL_PLUGIN_URL . 'assets/sticky-column/frontblocks-sticky-column.js',
57 array(),
58 FRBL_VERSION,
59 true
60 );
61 }
62
63 /**
64 * Enqueue block editor assets.
65 *
66 * @return void
67 */
68 public function enqueue_block_editor_assets() {
69 wp_enqueue_script(
70 'frontblocks-sticky-column-editor',
71 FRBL_PLUGIN_URL . 'assets/sticky-column/frontblocks-sticky-column-option.js',
72 array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-data', 'wp-edit-post', 'wp-i18n', 'wp-hooks', 'wp-block-editor' ),
73 FRBL_VERSION,
74 false
75 );
76
77 // Set script translations for JavaScript.
78 wp_set_script_translations(
79 'frontblocks-sticky-column-editor',
80 'frontblocks'
81 );
82 }
83
84 /**
85 * Add sticky attributes to grid block.
86 *
87 * @param string $block_content Block content.
88 * @param array $block Block attributes.
89 * @return string
90 */
91 public function add_sticky_attributes_to_grid_block( $block_content, $block ) {
92 $attrs = $block['attrs'] ?? array();
93 $sticky_enabled = isset( $attrs['frblStickyEnabled'] ) ? (bool) $attrs['frblStickyEnabled'] : false;
94 $sticky_offset = isset( $attrs['frblStickyOffset'] ) ? (int) $attrs['frblStickyOffset'] : 0;
95 $sticky_column_index = isset( $attrs['frblStickyColumnIndex'] ) ? (int) $attrs['frblStickyColumnIndex'] : 0;
96
97 // Add sticky attributes to the wrapper div if sticky is enabled.
98 if ( $sticky_enabled ) {
99 $block_content = preg_replace(
100 '/<div([^>]*)class="([^"]*gb-grid-wrapper[^"]*)"([^>]*)>/',
101 '<div$1class="$2 frontblocks-sticky-wrapper"$3' .
102 ' data-sticky-enabled="' . esc_attr( $sticky_enabled ? 'true' : 'false' ) . '"' .
103 ' data-sticky-offset="' . esc_attr( $sticky_offset ) . '"' .
104 ' data-sticky-column-index="' . esc_attr( $sticky_column_index ) . '"' .
105 '>',
106 $block_content,
107 1 // Only replace the first occurrence.
108 );
109
110 // Enqueue frontend assets only when a sticky column block is detected.
111 if ( ! wp_style_is( 'frontblocks-sticky-column', 'enqueued' ) ) {
112 wp_enqueue_style( 'frontblocks-sticky-column' );
113 }
114 if ( ! wp_script_is( 'frontblocks-sticky-column-custom', 'enqueued' ) ) {
115 wp_enqueue_script( 'frontblocks-sticky-column-custom' );
116 }
117 }
118
119 return $block_content;
120 }
121
122 /**
123 * Register custom attributes for blocks.
124 *
125 * @return void
126 */
127 public function register_custom_attributes() {
128 // Register attributes before GenerateBlocks registers its blocks.
129 add_filter(
130 'generateblocks_blocks_registered_block',
131 array( $this, 'register_sticky_attributes_for_grid_block' ),
132 9,
133 2
134 );
135
136 // Register attributes from frontend side as well.
137 add_action(
138 'enqueue_block_editor_assets',
139 array( $this, 'add_inline_script_for_attributes' )
140 );
141 }
142
143 /**
144 * Register sticky attributes for GenerateBlocks Grid block.
145 *
146 * @param array $block_args The block arguments.
147 * @param string $block_type The name of the block.
148 * @return array Modified block arguments.
149 */
150 public function register_sticky_attributes_for_grid_block( $block_args, $block_type ) {
151 if ( 'generateblocks/grid' !== $block_type ) {
152 return $block_args;
153 }
154
155 $block_args['attributes']['frblStickyEnabled'] = array(
156 'type' => 'boolean',
157 'default' => false,
158 );
159 $block_args['attributes']['frblStickyOffset'] = array(
160 'type' => 'number',
161 'default' => 0,
162 );
163 $block_args['attributes']['frblStickyColumnIndex'] = array(
164 'type' => 'number',
165 'default' => 0,
166 );
167
168 return $block_args;
169 }
170
171 /**
172 * Add inline script for block attributes.
173 *
174 * @return void
175 */
176 public function add_inline_script_for_attributes() {
177 wp_add_inline_script(
178 'wp-blocks',
179 "
180 wp.hooks.addFilter(
181 'blocks.registerBlockType',
182 'frontblocks/sticky-attributes',
183 function( settings, name ) {
184 if ( name !== 'generateblocks/grid' ) {
185 return settings;
186 }
187
188 settings.attributes = {
189 ...settings.attributes,
190 frblStickyEnabled: {
191 type: 'boolean',
192 default: false
193 },
194 frblStickyOffset: {
195 type: 'number',
196 default: 0
197 },
198 frblStickyColumnIndex: {
199 type: 'number',
200 default: 0
201 }
202 };
203
204 return settings;
205 }
206 );
207 "
208 );
209 }
210 }
211