Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 113
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
BlockStyles
0.00% covered (danger)
0.00%
0 / 113
0.00% covered (danger)
0.00%
0 / 3
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 register_block_styles
0.00% covered (danger)
0.00%
0 / 70
0.00% covered (danger)
0.00%
0 / 1
12
 render_cover_block
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
156
1<?php
2
3namespace NewfoldLabs\WP\Module\Patterns;
4
5/**
6 * Class BlockStyles
7 *
8 * Registers custom block styles for the WordPress block editor.
9 * Provides additional styling options for core blocks.
10 */
11class BlockStyles {
12
13    /**
14     * Constructor to hook into WordPress.
15     */
16    public function __construct() {
17        add_action( 'init', array( $this, 'register_block_styles' ) );
18
19        add_filter( 'render_block_core/cover', array( $this, 'render_cover_block' ), 10, 3 );
20    }
21
22    /**
23     * Register custom block styles for the core/group block.
24     */
25    public function register_block_styles() {
26
27        $image_styles = array(
28            array(
29                'name'  => 'nfd-dots-bottom-left',
30                'label' => __( 'Dots BL', 'nfd-wonder-blocks' ),
31            ),
32            array(
33                'name'  => 'nfd-waves-bottom-left',
34                'label' => __( 'Waves BL', 'nfd-wonder-blocks' ),
35            ),
36            array(
37                'name'  => 'nfd-dots-bottom-right',
38                'label' => __( 'Dots BR', 'nfd-wonder-blocks' ),
39            ),
40            array(
41                'name'  => 'nfd-waves-bottom-right',
42                'label' => __( 'Waves BR', 'nfd-wonder-blocks' ),
43            ),
44            array(
45                'name'  => 'nfd-dots-top-left',
46                'label' => __( 'Dots TL', 'nfd-wonder-blocks' ),
47            ),
48            array(
49                'name'  => 'nfd-waves-top-left',
50                'label' => __( 'Waves TL', 'nfd-wonder-blocks' ),
51            ),
52            array(
53                'name'  => 'nfd-dots-top-right',
54                'label' => __( 'Dots TR', 'nfd-wonder-blocks' ),
55            ),
56            array(
57                'name'  => 'nfd-waves-top-right',
58                'label' => __( 'Waves TR', 'nfd-wonder-blocks' ),
59            ),
60        );
61
62        $theme_styles = array(
63            array(
64                'name'  => 'nfd-theme-white',
65                'label' => __( 'White', 'nfd-wonder-blocks' ),
66            ),
67            array(
68                'name'  => 'nfd-theme-light',
69                'label' => __( 'Light', 'nfd-wonder-blocks' ),
70            ),
71            array(
72                'name'  => 'nfd-theme-dark',
73                'label' => __( 'Dark', 'nfd-wonder-blocks' ),
74            ),
75            array(
76                'name'  => 'nfd-theme-darker',
77                'label' => __( 'Darker', 'nfd-wonder-blocks' ),
78            ),
79            array(
80                'name'  => 'nfd-theme-primary',
81                'label' => __( 'Primary', 'nfd-wonder-blocks' ),
82            ),
83            array(
84                'name'  => 'nfd-theme-primary-15',
85                'label' => __( 'Primary Light', 'nfd-wonder-blocks' ),
86            ),
87        );
88
89        foreach ( $image_styles as $image_style ) {
90            register_block_style(
91                array( 'core/group', 'core/image' ),
92                $image_style
93            );
94        }
95
96        foreach ( $theme_styles as $theme_style ) {
97            register_block_style(
98                'core/group',
99                $theme_style
100            );
101        }
102    }
103
104    /**
105     * Modifies the HTML output of the Cover block by adding link-related attributes and elements.
106     *
107     * @param string $html       The original HTML content of the Cover block.
108     * @param array  $block      The parsed block data, including its attributes.
109     * @param array  $attributes The block attributes passed to the Cover block.
110     * @return string The modified HTML content of the Cover block.
111     */
112    public function render_cover_block( $html, $block, $attributes ) {
113        if( empty( $block['attrs'] ) ) {
114            return $html;
115        }
116
117        $attrs = $block['attrs'];
118        $url = isset( $attrs['linkUrl'] ) ? trim( $attrs['linkUrl'] ) : '';
119        if( $url === '' ){
120            return $html;
121        }
122
123        $blank  = ! empty( $attrs['linkOpensInNewTab'] );
124        $target = $blank ? '_blank' : '_self';
125        $relBits = [];
126        if( $blank ) {
127            $relBits[] = 'noopener';
128        }
129        if( !empty( $attrs['linkRel'] ) ) {
130            $relBits[] = $attrs['linkRel'];
131        }
132
133        $rel = implode( ' ', array_unique( array_filter( $relBits ) ) );
134
135        $html = preg_replace_callback(
136            '/^(\s*<div\b[^>]*class="([^"]*)"(.*?)>)/i',
137            function ( $m ) use ( $url, $blank ) {
138                $openTag = $m[1];
139                $classes = $m[2];
140                $rest    = $m[3];
141
142                if( strpos( $classes, 'nfd-is-linked-group' ) === false ) {
143                    $classes .= ' nfd-is-linked-group';
144                }
145
146                $openTag = preg_replace( '/\sdata-link-url="[^"]*"/i', '', $openTag );
147                $openTag = preg_replace( '/\sdata-link-blank="[^"]*"/i', '', $openTag );
148                $openTag = preg_replace( '/class="[^"]*"/i', 'class="' . esc_attr( trim( $classes ) ) . '"', $openTag );
149                $openTag = rtrim( $openTag, '>' ) . ' data-link-url="' . esc_attr( $url ) . '" data-link-blank="' . ( $blank ? '1' : '' ) . '">';
150
151                return $openTag;
152            },
153            $html,
154            1
155        );
156
157        $linkTag = sprintf(
158            '<a class="wp-block-cover__link" href="%s"%s%s aria-hidden="true" tabindex="-1"></a>',
159            esc_url( $url ),
160            $target ? ' target="' . esc_attr( $target ) . '"' : '',
161            $rel ? ' rel="' . esc_attr( $rel ) . '"' : ''
162        );
163
164        $pos = strpos( $html, '>' );
165        if( $pos !== false ) {
166            $html = substr_replace( $html, '>' . $linkTag, $pos, 1 );
167        }
168
169        return $html;
170    }
171
172}