PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / 1.0.2
FrontBlocks for Gutenberg/GeneratePress v1.0.2
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 / sticky-column / frontblocks-sticky-column.php
frontblocks / includes / sticky-column Last commit date
frontblocks-sticky-column-option.jsx 11 months ago frontblocks-sticky-column.css 11 months ago frontblocks-sticky-column.js 11 months ago frontblocks-sticky-column.php 11 months ago
frontblocks-sticky-column.php
171 lines
1 <?php
2 /**
3 * Class Sticky Column
4 *
5 * @package WordPress
6 * @author David Perez <david@close.technology>
7 * @copyright 2023 Closemarketing
8 * @version 1.0
9 */
10
11 defined( 'ABSPATH' ) || exit;
12
13 add_action( 'wp_enqueue_scripts', 'frbl_sticky_column_scripts', 99 );
14 /**
15 * Loads Scripts
16 *
17 * @return void
18 */
19 function frbl_sticky_column_scripts() {
20 $dist_dir = WP_DEBUG ? 'sticky-column/' : 'dist/';
21 wp_enqueue_style(
22 'frontblocks-sticky-column',
23 FRBL_PLUGIN_URL . 'includes/' . $dist_dir . 'frontblocks-sticky-column.css',
24 array(),
25 FRBL_VERSION
26 );
27
28 wp_enqueue_script(
29 'frontblocks-sticky-column-custom',
30 FRBL_PLUGIN_URL . 'includes/' . $dist_dir . ( WP_DEBUG ? 'frontblocks-sticky-column.js' : 'frontblocks-sticky-column-min.js' ),
31 array( ),
32 FRBL_VERSION,
33 true
34 );
35 }
36
37 add_action( 'enqueue_block_editor_assets', 'frbl_enqueue_sticky_column_editor_assets' );
38 /**
39 * Enqueue custom block editor script for sticky column
40 *
41 * @return void
42 */
43 function frbl_enqueue_sticky_column_editor_assets() {
44 wp_enqueue_script(
45 'frontblocks-sticky-column-editor',
46 FRBL_PLUGIN_URL . 'includes/sticky-column/frontblocks-sticky-column-option.jsx',
47 array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-data', 'wp-edit-post' ),
48 FRBL_VERSION,
49 true
50 );
51 }
52
53 add_filter( 'render_block_generateblocks/grid', 'frbl_add_sticky_attributes_to_grid_block', 10, 2 );
54 /**
55 * Hook to filter the block output on frontend for sticky columns.
56 *
57 * @param html $block_content Block content.
58 * @param array $block Block attributes.
59 *
60 * @return html
61 */
62 function frbl_add_sticky_attributes_to_grid_block( $block_content, $block ) {
63 $attrs = $block['attrs'] ?? array();
64 $sticky_enabled = isset( $attrs['frblStickyEnabled'] ) ? (bool) $attrs['frblStickyEnabled'] : false;
65 $sticky_offset = isset( $attrs['frblStickyOffset'] ) ? (int) $attrs['frblStickyOffset'] : 0;
66 $sticky_column_index = isset( $attrs['frblStickyColumnIndex'] ) ? (int) $attrs['frblStickyColumnIndex'] : 0;
67
68 // Add sticky attributes to the wrapper div if sticky is enabled.
69 if ( $sticky_enabled ) {
70 $block_content = preg_replace(
71 '/<div([^>]*)class="([^"]*gb-grid-wrapper[^"]*)"([^>]*)>/',
72 '<div$1class="$2 frontblocks-sticky-wrapper"$3' .
73 ' data-sticky-enabled="' . esc_attr( $sticky_enabled ? 'true' : 'false' ) . '"' .
74 ' data-sticky-offset="' . esc_attr( $sticky_offset ) . '"' .
75 ' data-sticky-column-index="' . esc_attr( $sticky_column_index ) . '"' .
76 '>',
77 $block_content,
78 1 // Only replace the first occurrence.
79 );
80 }
81
82 return $block_content;
83 }
84
85 /**
86 * Register sticky attributes for GenerateBlocks Grid block.
87 *
88 * @return void
89 */
90 add_action(
91 'init',
92 function () {
93 // Use priority 9 (before default 10).
94 add_filter(
95 'generateblocks_blocks_registered_block',
96 'frbl_register_sticky_attributes_for_grid_block',
97 9,
98 2
99 );
100
101 // Register attributes from frontend side as well.
102 add_action(
103 'enqueue_block_editor_assets',
104 function () {
105 wp_add_inline_script(
106 'wp-blocks',
107 "
108 wp.hooks.addFilter(
109 'blocks.registerBlockType',
110 'frontblocks/sticky-attributes',
111 function( settings, name ) {
112 if ( name !== 'generateblocks/grid' ) {
113 return settings;
114 }
115
116 settings.attributes = {
117 ...settings.attributes,
118 frblStickyEnabled: {
119 type: 'boolean',
120 default: false
121 },
122 frblStickyOffset: {
123 type: 'number',
124 default: 0
125 },
126 frblStickyColumnIndex: {
127 type: 'number',
128 default: 0
129 }
130 };
131
132 return settings;
133 }
134 );
135 "
136 );
137 }
138 );
139 },
140 5
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 * @author Closetechnology
150 */
151 function frbl_register_sticky_attributes_for_grid_block( $block_args, $block_type ) {
152 if ( 'generateblocks/grid' !== $block_type ) {
153 return $block_args;
154 }
155
156 $block_args['attributes']['frblStickyEnabled'] = array(
157 'type' => 'boolean',
158 'default' => false,
159 );
160 $block_args['attributes']['frblStickyOffset'] = array(
161 'type' => 'number',
162 'default' => 0,
163 );
164 $block_args['attributes']['frblStickyColumnIndex'] = array(
165 'type' => 'number',
166 'default' => 0,
167 );
168
169 return $block_args;
170 }
171