PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / 1.3.6
FrontBlocks for Gutenberg/GeneratePress v1.3.6
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 1 month ago FaqSchema.php 1 month ago FluidTypography.php 1 month ago Gallery.php 8 months ago GravityFormsInline.php 1 month ago Headline.php 1 month ago InsertPost.php 1 month ago ProductCategories.php 1 month 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 SvgUpload.php 1 month ago Testimonials.php 8 months ago TextAnimation.php 1 month ago UserText.php 1 month ago
StickyColumn.php
266 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_filter( 'render_block_core/columns', array( $this, 'add_sticky_attributes_to_columns_block' ), 10, 2 );
39 add_action( 'init', array( $this, 'register_custom_attributes' ), 5 );
40 }
41
42 /**
43 * Register frontend scripts and styles for conditional enqueueing.
44 *
45 * @return void
46 */
47 public function register_scripts() {
48 wp_register_style(
49 'frontblocks-sticky-column',
50 FRBL_PLUGIN_URL . 'assets/sticky-column/frontblocks-sticky-column.css',
51 array(),
52 FRBL_VERSION
53 );
54
55 wp_register_script(
56 'frontblocks-sticky-column-custom',
57 FRBL_PLUGIN_URL . 'assets/sticky-column/frontblocks-sticky-column.js',
58 array(),
59 FRBL_VERSION,
60 true
61 );
62 }
63
64 /**
65 * Enqueue block editor assets.
66 *
67 * @return void
68 */
69 public function enqueue_block_editor_assets() {
70 wp_enqueue_script(
71 'frontblocks-sticky-column-editor',
72 FRBL_PLUGIN_URL . 'assets/sticky-column/frontblocks-sticky-column-option.js',
73 array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-data', 'wp-edit-post', 'wp-i18n', 'wp-hooks', 'wp-block-editor' ),
74 FRBL_VERSION,
75 false
76 );
77
78 // Set script translations for JavaScript.
79 wp_set_script_translations(
80 'frontblocks-sticky-column-editor',
81 'frontblocks'
82 );
83 }
84
85 /**
86 * Add sticky attributes to grid block.
87 *
88 * @param string $block_content Block content.
89 * @param array $block Block attributes.
90 * @return string
91 */
92 public function add_sticky_attributes_to_grid_block( $block_content, $block ) {
93 return $this->add_sticky_attributes( $block_content, $block, 'gb-grid-wrapper' );
94 }
95
96 /**
97 * Add sticky attributes to native core/columns block.
98 *
99 * @param string $block_content Block content.
100 * @param array $block Block attributes.
101 * @return string
102 */
103 public function add_sticky_attributes_to_columns_block( $block_content, $block ) {
104 return $this->add_sticky_attributes( $block_content, $block, 'wp-block-columns' );
105 }
106
107 /**
108 * Generic method to inject sticky wrapper class and data attributes.
109 *
110 * @param string $block_content Block content.
111 * @param array $block Block data.
112 * @param string $wrapper_class CSS class that identifies the wrapper div.
113 * @return string
114 */
115 private function add_sticky_attributes( $block_content, $block, $wrapper_class ) {
116 $attrs = $block['attrs'] ?? array();
117 $sticky_enabled = isset( $attrs['frblStickyEnabled'] ) ? (bool) $attrs['frblStickyEnabled'] : false;
118 $sticky_offset = isset( $attrs['frblStickyOffset'] ) ? (int) $attrs['frblStickyOffset'] : 0;
119 $sticky_column_index = isset( $attrs['frblStickyColumnIndex'] ) ? (int) $attrs['frblStickyColumnIndex'] : 0;
120
121 if ( ! $sticky_enabled ) {
122 return $block_content;
123 }
124
125 $block_content = preg_replace(
126 '/<div([^>]*)class="([^"]*' . preg_quote( $wrapper_class, '/' ) . '[^"]*)"([^>]*)>/',
127 '<div$1class="$2 frontblocks-sticky-wrapper"$3' .
128 ' data-sticky-enabled="true"' .
129 ' data-sticky-offset="' . esc_attr( $sticky_offset ) . '"' .
130 ' data-sticky-column-index="' . esc_attr( $sticky_column_index ) . '"' .
131 '>',
132 $block_content,
133 1
134 );
135
136 if ( ! wp_style_is( 'frontblocks-sticky-column', 'enqueued' ) ) {
137 wp_enqueue_style( 'frontblocks-sticky-column' );
138 }
139 if ( ! wp_script_is( 'frontblocks-sticky-column-custom', 'enqueued' ) ) {
140 wp_enqueue_script( 'frontblocks-sticky-column-custom' );
141 }
142
143 return $block_content;
144 }
145
146 /**
147 * Register custom attributes for blocks.
148 *
149 * @return void
150 */
151 public function register_custom_attributes() {
152 // Register attributes before GenerateBlocks registers its blocks.
153 add_filter(
154 'generateblocks_blocks_registered_block',
155 array( $this, 'register_sticky_attributes_for_grid_block' ),
156 9,
157 2
158 );
159
160 // Register attributes for native core/columns block.
161 add_filter( 'register_block_type_args', array( $this, 'register_sticky_attributes_for_columns_block' ), 10, 2 );
162
163 // Register attributes from frontend side as well.
164 add_action(
165 'enqueue_block_editor_assets',
166 array( $this, 'add_inline_script_for_attributes' )
167 );
168 }
169
170 /**
171 * Register sticky attributes for GenerateBlocks Grid block.
172 *
173 * @param array $block_args The block arguments.
174 * @param string $block_type The name of the block.
175 * @return array Modified block arguments.
176 */
177 public function register_sticky_attributes_for_grid_block( $block_args, $block_type ) {
178 if ( 'generateblocks/grid' !== $block_type ) {
179 return $block_args;
180 }
181
182 $block_args['attributes']['frblStickyEnabled'] = array(
183 'type' => 'boolean',
184 'default' => false,
185 );
186 $block_args['attributes']['frblStickyOffset'] = array(
187 'type' => 'number',
188 'default' => 0,
189 );
190 $block_args['attributes']['frblStickyColumnIndex'] = array(
191 'type' => 'number',
192 'default' => 0,
193 );
194
195 return $block_args;
196 }
197
198 /**
199 * Register sticky attributes for native core/columns block via register_block_type_args.
200 *
201 * @param array $args Block type arguments.
202 * @param string $block_type Block type name.
203 * @return array
204 */
205 public function register_sticky_attributes_for_columns_block( $args, $block_type ) {
206 if ( 'core/columns' !== $block_type ) {
207 return $args;
208 }
209
210 $args['attributes']['frblStickyEnabled'] = array(
211 'type' => 'boolean',
212 'default' => false,
213 );
214 $args['attributes']['frblStickyOffset'] = array(
215 'type' => 'number',
216 'default' => 0,
217 );
218 $args['attributes']['frblStickyColumnIndex'] = array(
219 'type' => 'number',
220 'default' => 0,
221 );
222
223 return $args;
224 }
225
226 /**
227 * Add inline script for block attributes.
228 *
229 * @return void
230 */
231 public function add_inline_script_for_attributes() {
232 wp_add_inline_script(
233 'wp-blocks',
234 "
235 wp.hooks.addFilter(
236 'blocks.registerBlockType',
237 'frontblocks/sticky-attributes',
238 function( settings, name ) {
239 if ( name !== 'generateblocks/grid' && name !== 'core/columns' ) {
240 return settings;
241 }
242
243 settings.attributes = {
244 ...settings.attributes,
245 frblStickyEnabled: {
246 type: 'boolean',
247 default: false
248 },
249 frblStickyOffset: {
250 type: 'number',
251 default: 0
252 },
253 frblStickyColumnIndex: {
254 type: 'number',
255 default: 0
256 }
257 };
258
259 return settings;
260 }
261 );
262 "
263 );
264 }
265 }
266