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 / StickyColumn.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
StickyColumn.php
198 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( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 99 );
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 * Enqueue scripts and styles.
43 *
44 * @return void
45 */
46 public function enqueue_scripts() {
47
48 wp_enqueue_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_enqueue_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
79 /**
80 * Add sticky attributes to grid block.
81 *
82 * @param string $block_content Block content.
83 * @param array $block Block attributes.
84 * @return string
85 */
86 public function add_sticky_attributes_to_grid_block( $block_content, $block ) {
87 $attrs = $block['attrs'] ?? array();
88 $sticky_enabled = isset( $attrs['frblStickyEnabled'] ) ? (bool) $attrs['frblStickyEnabled'] : false;
89 $sticky_offset = isset( $attrs['frblStickyOffset'] ) ? (int) $attrs['frblStickyOffset'] : 0;
90 $sticky_column_index = isset( $attrs['frblStickyColumnIndex'] ) ? (int) $attrs['frblStickyColumnIndex'] : 0;
91
92 // Add sticky attributes to the wrapper div if sticky is enabled.
93 if ( $sticky_enabled ) {
94 $block_content = preg_replace(
95 '/<div([^>]*)class="([^"]*gb-grid-wrapper[^"]*)"([^>]*)>/',
96 '<div$1class="$2 frontblocks-sticky-wrapper"$3' .
97 ' data-sticky-enabled="' . esc_attr( $sticky_enabled ? 'true' : 'false' ) . '"' .
98 ' data-sticky-offset="' . esc_attr( $sticky_offset ) . '"' .
99 ' data-sticky-column-index="' . esc_attr( $sticky_column_index ) . '"' .
100 '>',
101 $block_content,
102 1 // Only replace the first occurrence.
103 );
104 }
105
106 return $block_content;
107 }
108
109 /**
110 * Register custom attributes for blocks.
111 *
112 * @return void
113 */
114 public function register_custom_attributes() {
115 // Register attributes before GenerateBlocks registers its blocks.
116 add_filter(
117 'generateblocks_blocks_registered_block',
118 array( $this, 'register_sticky_attributes_for_grid_block' ),
119 9,
120 2
121 );
122
123 // Register attributes from frontend side as well.
124 add_action(
125 'enqueue_block_editor_assets',
126 array( $this, 'add_inline_script_for_attributes' )
127 );
128 }
129
130 /**
131 * Register sticky attributes for GenerateBlocks Grid block.
132 *
133 * @param array $block_args The block arguments.
134 * @param string $block_type The name of the block.
135 * @return array Modified block arguments.
136 */
137 public function register_sticky_attributes_for_grid_block( $block_args, $block_type ) {
138 if ( 'generateblocks/grid' !== $block_type ) {
139 return $block_args;
140 }
141
142 $block_args['attributes']['frblStickyEnabled'] = array(
143 'type' => 'boolean',
144 'default' => false,
145 );
146 $block_args['attributes']['frblStickyOffset'] = array(
147 'type' => 'number',
148 'default' => 0,
149 );
150 $block_args['attributes']['frblStickyColumnIndex'] = array(
151 'type' => 'number',
152 'default' => 0,
153 );
154
155 return $block_args;
156 }
157
158 /**
159 * Add inline script for block attributes.
160 *
161 * @return void
162 */
163 public function add_inline_script_for_attributes() {
164 wp_add_inline_script(
165 'wp-blocks',
166 "
167 wp.hooks.addFilter(
168 'blocks.registerBlockType',
169 'frontblocks/sticky-attributes',
170 function( settings, name ) {
171 if ( name !== 'generateblocks/grid' ) {
172 return settings;
173 }
174
175 settings.attributes = {
176 ...settings.attributes,
177 frblStickyEnabled: {
178 type: 'boolean',
179 default: false
180 },
181 frblStickyOffset: {
182 type: 'number',
183 default: 0
184 },
185 frblStickyColumnIndex: {
186 type: 'number',
187 default: 0
188 }
189 };
190
191 return settings;
192 }
193 );
194 "
195 );
196 }
197 }
198