-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
108 lines (93 loc) · 2.99 KB
/
functions.php
File metadata and controls
108 lines (93 loc) · 2.99 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
<?php
/**
* Plugin functions — the code snippets from the article.
*
* These functions are deliberately kept filter-free so they can be called
* directly in unit tests without side-effects.
*/
declare( strict_types=1 );
/**
* Adds loading="lazy" to every <img> tag in a content string.
*
* @param string $content HTML content.
* @return string Modified HTML content.
*/
function my_plugin_lazy_load_images( string $content ): string {
$processor = new WP_HTML_Tag_Processor( $content );
while ( $processor->next_tag( 'img' ) ) {
$processor->set_attribute( 'loading', 'lazy' );
}
return $processor->get_updated_html();
}
/**
* Marks external links with data-external="true" and rel="noopener noreferrer".
*
* Internal links (same host as home_url()) are left untouched.
*
* @param string $content HTML content.
* @return string Modified HTML content.
*/
function my_plugin_mark_external_links( string $content ): string {
$processor = new WP_HTML_Tag_Processor( $content );
while ( $processor->next_tag( 'a' ) ) {
$href = $processor->get_attribute( 'href' );
if ( $href && str_starts_with( $href, 'http' ) && ! str_contains( $href, home_url() ) ) {
$processor->set_attribute( 'data-external', 'true' );
$rel = $processor->get_attribute( 'rel' );
$processor->set_attribute( 'rel', trim( ( $rel ?? '' ) . ' noopener noreferrer' ) );
}
}
return $processor->get_updated_html();
}
/**
* Renders the "my-plugin/card" dynamic block.
*
* @param array $attributes Block attributes.
* @param string $content Inner block content.
* @return string Block HTML output.
*/
function my_plugin_render_card_block( array $attributes, string $content ): string {
$tag = new WP_HTML_Tag_Processor(
'<div class="wp-block-my-plugin-card"></div>'
);
$tag->next_tag();
if ( ! empty( $attributes['backgroundColor'] ) ) {
$tag->set_attribute(
'style',
'background-color: ' . esc_attr( $attributes['backgroundColor'] ) . ';'
);
}
if ( ! empty( $attributes['className'] ) ) {
foreach ( explode( ' ', $attributes['className'] ) as $class ) {
$tag->add_class( $class );
}
}
return str_replace(
'</div>',
$content . '</div>',
$tag->get_updated_html()
);
}
/**
* Injects Interactivity API directives into core/list block HTML.
*
* Hooked to render_block in the main plugin file.
*
* @param string $block_content Rendered block HTML.
* @param array $block Block data including blockName and attrs.
* @return string Modified block HTML.
*/
function my_plugin_add_list_interactivity( string $block_content, array $block ): string {
if ( 'core/list' !== $block['blockName'] ) {
return $block_content;
}
$p = new WP_HTML_Tag_Processor( $block_content );
if ( $p->next_tag( 'ul' ) ) {
$p->set_attribute( 'data-wp-interactive', 'my-plugin/list' );
$p->set_attribute( 'data-wp-context', wp_json_encode( array( 'expanded' => false ) ) );
}
while ( $p->next_tag( 'li' ) ) {
$p->set_attribute( 'data-wp-on--click', 'actions.toggle' );
}
return $p->get_updated_html();
}