-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopup.php
More file actions
156 lines (128 loc) · 4 KB
/
popup.php
File metadata and controls
156 lines (128 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* Plugin Name: Popup Block
* Description: A container block that displays its contents as a popup.
* Requires at least: 6.1
* Requires PHP: 7.0
* Version: __VERSION__
* Author: Human Made Limited
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: hm-popup
*
* @package hm-popup
*/
namespace HM\Popup;
use WP_HTML_Tag_Processor;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function init() {
register_block_type( __DIR__ . '/build' );
}
add_action( 'init', __NAMESPACE__ . '\\init' );
/**
* Fires when scripts and styles are enqueued.
*
*/
function enqueue_scripts() : void {
wp_script_add_data( 'hm-popup-view-script', 'strategy', 'defer' );
}
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\\enqueue_scripts' );
/**
* Filters the HTML tags that are allowed for a given context.
*
* @param array[] $html Allowed HTML tags.
* @param string $context Context name.
* @return array[] Allowed HTML tags.
*/
function filter_wp_kses_allowed_html( array $html, string $context ) : array {
$html['dialog'] = _wp_add_global_attributes( [
'open' => [
'valueless' => 'y',
],
'closedby' => true,
] );
$html['button']['commandfor'] = true;
$html['button']['command'] = true;
return $html;
}
add_filter( 'wp_kses_allowed_html', __NAMESPACE__ . '\\filter_wp_kses_allowed_html', 10, 2 );
/**
* Filters the content of the block.
*
* @param string $block_content The block content.
* @param array $block The full block, including name and attributes.
* @param \WP_Block $instance The block instance.
* @return string The block content.
*/
function filter_render_block( $block_content, $block, \WP_Block $instance ) {
$classname = 'wp-elements-' . md5( maybe_serialize( $block['attrs'] ) );
$styles = [
'background-position' => 'center',
'background-size' => 'cover',
'background-repeat' => 'no-repeat',
];
if ( ! empty( $block['attrs']['backgroundColor'] ) ) {
$styles['background-color'] = "var(--wp--preset--color--{$block['attrs']['backgroundColor']}) !important";
}
$gradient = $block['attrs']['style']['color']['gradient'] ?? '';
if ( ! empty( $gradient ) ) {
$styles['background-color'] = 'transparent !important';
$styles['background-image'] = "{$gradient} !important";
}
$styles = array_map( function ( $style, $prop ) {
return "{$prop}: {$style}";
}, $styles, array_keys( $styles ) );
$open_styles = [
'opacity' => ( $block['attrs']['opacity'] ?? '0' ) . '%',
];
$open_styles = array_map( function ( $style, $prop ) {
return "{$prop}: {$style}";
}, $open_styles, array_keys( $open_styles ) );
$style = implode( ';', $styles );
$style = ".{$classname}::backdrop{{$style}}";
$open_style = implode( ';', $open_styles );
$style .= ".{$classname}[open]::backdrop{{$open_style}}";
wp_enqueue_block_support_styles( $style );
$block_content = new WP_HTML_Tag_Processor( $block_content );
$block_content->next_tag();
$block_content->add_class( $classname );
return (string) $block_content;
}
add_filter( 'render_block_hm/popup', __NAMESPACE__ . '\\filter_render_block', 10, 3 );
add_action( 'init', __NAMESPACE__ . '\\action_init' );
/**
* Fires after WordPress has finished loading but before any headers are sent.
*
*/
function action_init() : void {
register_block_style(
'hm/popup',
[
'name' => 'side--left',
'label' => __( 'Left Side', 'hm-popup' ),
]
);
register_block_style(
'hm/popup',
[
'name' => 'side--right',
'label' => __( 'Right Side', 'hm-popup' ),
]
);
register_block_style(
'hm/popup',
[
'name' => 'anchored',
'label' => __( 'Anchored', 'hm-popup' ),
]
);
}