PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / 1.0.3
FrontBlocks for Gutenberg/GeneratePress v1.0.3
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 9 months ago Carousel.php 9 months ago Gallery.php 9 months ago InsertPost.php 9 months ago StickyColumn.php 9 months ago Testimonials.php 9 months ago
StickyColumn.php
199 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 $dist_dir = WP_DEBUG ? 'sticky-column/' : 'dist/';
48
49 wp_enqueue_style(
50 'frontblocks-sticky-column',
51 FRBL_PLUGIN_URL . 'assets/' . $dist_dir . 'frontblocks-sticky-column.css',
52 array(),
53 FRBL_VERSION
54 );
55
56 wp_enqueue_script(
57 'frontblocks-sticky-column-custom',
58 FRBL_PLUGIN_URL . 'assets/' . $dist_dir . ( WP_DEBUG ? 'frontblocks-sticky-column.js' : 'frontblocks-sticky-column-min.js' ),
59 array(),
60 FRBL_VERSION,
61 true
62 );
63 }
64
65 /**
66 * Enqueue block editor assets.
67 *
68 * @return void
69 */
70 public function enqueue_block_editor_assets() {
71 wp_enqueue_script(
72 'frontblocks-sticky-column-editor',
73 FRBL_PLUGIN_URL . 'assets/sticky-column/frontblocks-sticky-column-option.jsx',
74 array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-data', 'wp-edit-post' ),
75 FRBL_VERSION,
76 true
77 );
78 }
79
80 /**
81 * Add sticky attributes to grid block.
82 *
83 * @param string $block_content Block content.
84 * @param array $block Block attributes.
85 * @return string
86 */
87 public function add_sticky_attributes_to_grid_block( $block_content, $block ) {
88 $attrs = $block['attrs'] ?? array();
89 $sticky_enabled = isset( $attrs['frblStickyEnabled'] ) ? (bool) $attrs['frblStickyEnabled'] : false;
90 $sticky_offset = isset( $attrs['frblStickyOffset'] ) ? (int) $attrs['frblStickyOffset'] : 0;
91 $sticky_column_index = isset( $attrs['frblStickyColumnIndex'] ) ? (int) $attrs['frblStickyColumnIndex'] : 0;
92
93 // Add sticky attributes to the wrapper div if sticky is enabled.
94 if ( $sticky_enabled ) {
95 $block_content = preg_replace(
96 '/<div([^>]*)class="([^"]*gb-grid-wrapper[^"]*)"([^>]*)>/',
97 '<div$1class="$2 frontblocks-sticky-wrapper"$3' .
98 ' data-sticky-enabled="' . esc_attr( $sticky_enabled ? 'true' : 'false' ) . '"' .
99 ' data-sticky-offset="' . esc_attr( $sticky_offset ) . '"' .
100 ' data-sticky-column-index="' . esc_attr( $sticky_column_index ) . '"' .
101 '>',
102 $block_content,
103 1 // Only replace the first occurrence.
104 );
105 }
106
107 return $block_content;
108 }
109
110 /**
111 * Register custom attributes for blocks.
112 *
113 * @return void
114 */
115 public function register_custom_attributes() {
116 // Register attributes before GenerateBlocks registers its blocks.
117 add_filter(
118 'generateblocks_blocks_registered_block',
119 array( $this, 'register_sticky_attributes_for_grid_block' ),
120 9,
121 2
122 );
123
124 // Register attributes from frontend side as well.
125 add_action(
126 'enqueue_block_editor_assets',
127 array( $this, 'add_inline_script_for_attributes' )
128 );
129 }
130
131 /**
132 * Register sticky attributes for GenerateBlocks Grid block.
133 *
134 * @param array $block_args The block arguments.
135 * @param string $block_type The name of the block.
136 * @return array Modified block arguments.
137 */
138 public function register_sticky_attributes_for_grid_block( $block_args, $block_type ) {
139 if ( 'generateblocks/grid' !== $block_type ) {
140 return $block_args;
141 }
142
143 $block_args['attributes']['frblStickyEnabled'] = array(
144 'type' => 'boolean',
145 'default' => false,
146 );
147 $block_args['attributes']['frblStickyOffset'] = array(
148 'type' => 'number',
149 'default' => 0,
150 );
151 $block_args['attributes']['frblStickyColumnIndex'] = array(
152 'type' => 'number',
153 'default' => 0,
154 );
155
156 return $block_args;
157 }
158
159 /**
160 * Add inline script for block attributes.
161 *
162 * @return void
163 */
164 public function add_inline_script_for_attributes() {
165 wp_add_inline_script(
166 'wp-blocks',
167 "
168 wp.hooks.addFilter(
169 'blocks.registerBlockType',
170 'frontblocks/sticky-attributes',
171 function( settings, name ) {
172 if ( name !== 'generateblocks/grid' ) {
173 return settings;
174 }
175
176 settings.attributes = {
177 ...settings.attributes,
178 frblStickyEnabled: {
179 type: 'boolean',
180 default: false
181 },
182 frblStickyOffset: {
183 type: 'number',
184 default: 0
185 },
186 frblStickyColumnIndex: {
187 type: 'number',
188 default: 0
189 }
190 };
191
192 return settings;
193 }
194 );
195 "
196 );
197 }
198 }
199