Animations.php
4 months ago
BackButton.php
7 months ago
BlockPatterns.php
4 months ago
Carousel.php
6 months ago
ContainerEdgeAlignment.php
7 months ago
Counter.php
4 months ago
Events.php
6 months ago
FluidTypography.php
4 months ago
Gallery.php
8 months ago
GravityFormsInline.php
7 months ago
Headline.php
4 months ago
InsertPost.php
8 months ago
ProductCategories.php
8 months ago
ReadingProgress.php
7 months ago
ReadingTime.php
8 months ago
ShapeAnimations.php
7 months ago
StackedImages.php
4 months ago
StickyColumn.php
8 months ago
Testimonials.php
8 months ago
StickyColumn.php
204 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 | // 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 | $attrs = $block['attrs'] ?? array(); |
| 94 | $sticky_enabled = isset( $attrs['frblStickyEnabled'] ) ? (bool) $attrs['frblStickyEnabled'] : false; |
| 95 | $sticky_offset = isset( $attrs['frblStickyOffset'] ) ? (int) $attrs['frblStickyOffset'] : 0; |
| 96 | $sticky_column_index = isset( $attrs['frblStickyColumnIndex'] ) ? (int) $attrs['frblStickyColumnIndex'] : 0; |
| 97 | |
| 98 | // Add sticky attributes to the wrapper div if sticky is enabled. |
| 99 | if ( $sticky_enabled ) { |
| 100 | $block_content = preg_replace( |
| 101 | '/<div([^>]*)class="([^"]*gb-grid-wrapper[^"]*)"([^>]*)>/', |
| 102 | '<div$1class="$2 frontblocks-sticky-wrapper"$3' . |
| 103 | ' data-sticky-enabled="' . esc_attr( $sticky_enabled ? 'true' : 'false' ) . '"' . |
| 104 | ' data-sticky-offset="' . esc_attr( $sticky_offset ) . '"' . |
| 105 | ' data-sticky-column-index="' . esc_attr( $sticky_column_index ) . '"' . |
| 106 | '>', |
| 107 | $block_content, |
| 108 | 1 // Only replace the first occurrence. |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | return $block_content; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Register custom attributes for blocks. |
| 117 | * |
| 118 | * @return void |
| 119 | */ |
| 120 | public function register_custom_attributes() { |
| 121 | // Register attributes before GenerateBlocks registers its blocks. |
| 122 | add_filter( |
| 123 | 'generateblocks_blocks_registered_block', |
| 124 | array( $this, 'register_sticky_attributes_for_grid_block' ), |
| 125 | 9, |
| 126 | 2 |
| 127 | ); |
| 128 | |
| 129 | // Register attributes from frontend side as well. |
| 130 | add_action( |
| 131 | 'enqueue_block_editor_assets', |
| 132 | array( $this, 'add_inline_script_for_attributes' ) |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Register sticky attributes for GenerateBlocks Grid block. |
| 138 | * |
| 139 | * @param array $block_args The block arguments. |
| 140 | * @param string $block_type The name of the block. |
| 141 | * @return array Modified block arguments. |
| 142 | */ |
| 143 | public function register_sticky_attributes_for_grid_block( $block_args, $block_type ) { |
| 144 | if ( 'generateblocks/grid' !== $block_type ) { |
| 145 | return $block_args; |
| 146 | } |
| 147 | |
| 148 | $block_args['attributes']['frblStickyEnabled'] = array( |
| 149 | 'type' => 'boolean', |
| 150 | 'default' => false, |
| 151 | ); |
| 152 | $block_args['attributes']['frblStickyOffset'] = array( |
| 153 | 'type' => 'number', |
| 154 | 'default' => 0, |
| 155 | ); |
| 156 | $block_args['attributes']['frblStickyColumnIndex'] = array( |
| 157 | 'type' => 'number', |
| 158 | 'default' => 0, |
| 159 | ); |
| 160 | |
| 161 | return $block_args; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Add inline script for block attributes. |
| 166 | * |
| 167 | * @return void |
| 168 | */ |
| 169 | public function add_inline_script_for_attributes() { |
| 170 | wp_add_inline_script( |
| 171 | 'wp-blocks', |
| 172 | " |
| 173 | wp.hooks.addFilter( |
| 174 | 'blocks.registerBlockType', |
| 175 | 'frontblocks/sticky-attributes', |
| 176 | function( settings, name ) { |
| 177 | if ( name !== 'generateblocks/grid' ) { |
| 178 | return settings; |
| 179 | } |
| 180 | |
| 181 | settings.attributes = { |
| 182 | ...settings.attributes, |
| 183 | frblStickyEnabled: { |
| 184 | type: 'boolean', |
| 185 | default: false |
| 186 | }, |
| 187 | frblStickyOffset: { |
| 188 | type: 'number', |
| 189 | default: 0 |
| 190 | }, |
| 191 | frblStickyColumnIndex: { |
| 192 | type: 'number', |
| 193 | default: 0 |
| 194 | } |
| 195 | }; |
| 196 | |
| 197 | return settings; |
| 198 | } |
| 199 | ); |
| 200 | " |
| 201 | ); |
| 202 | } |
| 203 | } |
| 204 |