-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
233 lines (201 loc) · 6.55 KB
/
Copy pathfunctions.php
File metadata and controls
233 lines (201 loc) · 6.55 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* Abira — functions.php
*
* @package Abira
* @since 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// -----------------------------------------------------------------------
// Theme setup
// -----------------------------------------------------------------------
/**
* Sets up theme defaults and registers support for various WordPress features.
*/
function abira_setup() {
// Make theme available for translation.
load_theme_textdomain( 'abira', get_template_directory() . '/languages' );
// Add support for block styles.
add_theme_support( 'wp-block-styles' );
// Add support for editor styles.
add_theme_support( 'editor-styles' );
// Add support for post thumbnails.
add_theme_support( 'post-thumbnails' );
// Add support for HTML5 markup.
add_theme_support(
'html5',
array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
'style',
'script',
'navigation-widgets',
)
);
// Add support for responsive embeds.
add_theme_support( 'responsive-embeds' );
// Remove core block patterns — starter theme ships clean.
remove_theme_support( 'core-block-patterns' );
// Editor stylesheets (frontend parity + editor-only adjustments).
add_editor_style( array( 'style.css', 'assets/css/editor-style.css' ) );
}
add_action( 'after_setup_theme', 'abira_setup' );
// -----------------------------------------------------------------------
// Enqueue
// -----------------------------------------------------------------------
/**
* Enqueue theme stylesheet.
*
* Block themes don't need a heavy frontend stylesheet.
* style.css acts as theme registration; add custom CSS here only when
* theme.json alone is not enough.
*/
function abira_enqueue_styles() {
wp_enqueue_style(
'abira-style',
get_stylesheet_uri(),
array(),
wp_get_theme()->get( 'Version' )
);
}
add_action( 'wp_enqueue_scripts', 'abira_enqueue_styles' );
// -----------------------------------------------------------------------
// Block styles
// -----------------------------------------------------------------------
/**
* Enqueue block-specific styles.
*/
function abira_enqueue_block_styles() {
wp_enqueue_block_style(
'core/navigation',
array(
'handle' => 'abira-block-navigation',
'src' => get_template_directory_uri() . '/assets/css/block-style.css',
'ver' => wp_get_theme()->get( 'Version' ),
)
);
// カスタムブロックスタイル(button / separator).
wp_enqueue_style(
'abira-block-styles',
get_template_directory_uri() . '/assets/css/block-styles.css',
array(),
wp_get_theme()->get( 'Version' )
);
}
add_action( 'init', 'abira_enqueue_block_styles' );
// -----------------------------------------------------------------------
// Register block styles
// -----------------------------------------------------------------------
/**
* Register custom block styles.
*
* Adds style variations to core blocks so editors can choose
* from the block toolbar in the site editor.
*/
function abira_register_block_styles() {
// Button: Outline.
register_block_style(
'core/button',
array(
'name' => 'outline',
'label' => __( 'Outline', 'abira' ),
)
);
// Separator: Wavy(波線).
register_block_style(
'core/separator',
array(
'name' => 'wavy',
'label' => __( 'Wavy', 'abira' ),
)
);
}
add_action( 'init', 'abira_register_block_styles' );
// -----------------------------------------------------------------------
// Block patterns
// -----------------------------------------------------------------------
/**
* Register block pattern category and patterns.
*
* WordPress 6.0+ auto-discovers patterns in /patterns via file headers.
* Only the category needs to be registered explicitly.
*/
function abira_register_patterns() {
register_block_pattern_category(
'abira',
array( 'label' => __( 'Abira', 'abira' ) )
);
}
add_action( 'init', 'abira_register_patterns' );
// -----------------------------------------------------------------------
// Accessibility: fix list structure in navigation blocks
// -----------------------------------------------------------------------
/**
* Ensure <ul> elements inside navigation and page-list blocks
* contain only valid children (<li>, <script>, <template>).
*
* WordPress core may render non-<li> elements (buttons, divs) as
* direct children of <ul>, which violates HTML spec and fails
* accessibility audits. This filter wraps any invalid children
* in <li> elements using DOMDocument for safe HTML manipulation.
*
* @param string $block_content Rendered block HTML.
* @param array $block Block data.
* @return string Sanitised block HTML.
*/
function abira_fix_nav_list_structure( $block_content, $block ) {
if ( empty( $block_content ) || false === strpos( $block_content, '<ul' ) ) {
return $block_content;
}
$dom = new DOMDocument();
libxml_use_internal_errors( true );
$dom->loadHTML(
'<html><head><meta charset="UTF-8"></head><body>' . $block_content . '</body></html>',
LIBXML_HTML_NODEFDTD
);
libxml_clear_errors();
$changed = false;
$valid_tags = array( 'li', 'script', 'template' );
$uls = $dom->getElementsByTagName( 'ul' );
foreach ( $uls as $ul ) {
// Collect invalid direct children first — cannot modify while iterating.
$invalid = array();
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
foreach ( $ul->childNodes as $child ) {
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
if ( XML_ELEMENT_NODE !== $child->nodeType ) {
continue;
}
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
if ( ! in_array( strtolower( $child->nodeName ), $valid_tags, true ) ) {
$invalid[] = $child;
}
}
foreach ( $invalid as $node ) {
$li = $dom->createElement( 'li' );
$li->setAttribute( 'class', 'abira-nav-item-wrap' );
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$node->parentNode->replaceChild( $li, $node );
$li->appendChild( $node );
$changed = true;
}
}
if ( ! $changed ) {
return $block_content;
}
// Extract only <body> inner content to avoid <html> wrapper.
$body = $dom->getElementsByTagName( 'body' )->item( 0 );
$output = '';
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
foreach ( $body->childNodes as $child ) {
$output .= $dom->saveHTML( $child );
}
return $output;
}
add_filter( 'render_block_core/navigation', 'abira_fix_nav_list_structure', 10, 2 );
add_filter( 'render_block_core/page-list', 'abira_fix_nav_list_structure', 10, 2 );