author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
18 | 1 |
<?php |
2 |
||
3 |
/** |
|
19 | 4 |
* Sets a custom slug when creating auto-draft template parts. |
5 |
* |
|
6 |
* This is only needed for auto-drafts created by the regular WP editor. |
|
7 |
* If this page is to be removed, this will not be necessary. |
|
8 |
* |
|
9 |
* @since 5.9.0 |
|
10 |
* |
|
11 |
* @param int $post_id Post ID. |
|
12 |
*/ |
|
13 |
function wp_set_unique_slug_on_create_template_part( $post_id ) { |
|
14 |
$post = get_post( $post_id ); |
|
15 |
if ( 'auto-draft' !== $post->post_status ) { |
|
16 |
return; |
|
17 |
} |
|
18 |
||
19 |
if ( ! $post->post_name ) { |
|
20 |
wp_update_post( |
|
21 |
array( |
|
22 |
'ID' => $post_id, |
|
23 |
'post_name' => 'custom_slug_' . uniqid(), |
|
24 |
) |
|
25 |
); |
|
26 |
} |
|
27 |
||
28 |
$terms = get_the_terms( $post_id, 'wp_theme' ); |
|
29 |
if ( ! is_array( $terms ) || ! count( $terms ) ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
30 |
wp_set_post_terms( $post_id, get_stylesheet(), 'wp_theme' ); |
19 | 31 |
} |
32 |
} |
|
33 |
||
34 |
/** |
|
18 | 35 |
* Generates a unique slug for templates. |
36 |
* |
|
37 |
* @access private |
|
38 |
* @since 5.8.0 |
|
39 |
* |
|
40 |
* @param string $override_slug The filtered value of the slug (starts as `null` from apply_filter). |
|
41 |
* @param string $slug The original/un-filtered slug (post_name). |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
42 |
* @param int $post_id Post ID. |
18 | 43 |
* @param string $post_status No uniqueness checks are made if the post is still draft or pending. |
44 |
* @param string $post_type Post type. |
|
45 |
* @return string The original, desired slug. |
|
46 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
47 |
function wp_filter_wp_template_unique_post_slug( $override_slug, $slug, $post_id, $post_status, $post_type ) { |
19 | 48 |
if ( 'wp_template' !== $post_type && 'wp_template_part' !== $post_type ) { |
18 | 49 |
return $override_slug; |
50 |
} |
|
51 |
||
52 |
if ( ! $override_slug ) { |
|
53 |
$override_slug = $slug; |
|
54 |
} |
|
55 |
||
56 |
/* |
|
57 |
* Template slugs must be unique within the same theme. |
|
58 |
* TODO - Figure out how to update this to work for a multi-theme environment. |
|
59 |
* Unfortunately using `get_the_terms()` for the 'wp-theme' term does not work |
|
60 |
* in the case of new entities since is too early in the process to have been saved |
|
61 |
* to the entity. So for now we use the currently activated theme for creation. |
|
62 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
63 |
$theme = get_stylesheet(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
64 |
$terms = get_the_terms( $post_id, 'wp_theme' ); |
18 | 65 |
if ( $terms && ! is_wp_error( $terms ) ) { |
66 |
$theme = $terms[0]->name; |
|
67 |
} |
|
68 |
||
69 |
$check_query_args = array( |
|
70 |
'post_name__in' => array( $override_slug ), |
|
71 |
'post_type' => $post_type, |
|
72 |
'posts_per_page' => 1, |
|
73 |
'no_found_rows' => true, |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
74 |
'post__not_in' => array( $post_id ), |
18 | 75 |
'tax_query' => array( |
76 |
array( |
|
77 |
'taxonomy' => 'wp_theme', |
|
78 |
'field' => 'name', |
|
79 |
'terms' => $theme, |
|
80 |
), |
|
81 |
), |
|
82 |
); |
|
83 |
$check_query = new WP_Query( $check_query_args ); |
|
84 |
$posts = $check_query->posts; |
|
85 |
||
86 |
if ( count( $posts ) > 0 ) { |
|
87 |
$suffix = 2; |
|
88 |
do { |
|
89 |
$query_args = $check_query_args; |
|
90 |
$alt_post_name = _truncate_post_slug( $override_slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
|
91 |
$query_args['post_name__in'] = array( $alt_post_name ); |
|
92 |
$query = new WP_Query( $query_args ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
93 |
++$suffix; |
18 | 94 |
} while ( count( $query->posts ) > 0 ); |
95 |
$override_slug = $alt_post_name; |
|
96 |
} |
|
97 |
||
98 |
return $override_slug; |
|
99 |
} |
|
100 |
||
101 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
102 |
* Enqueues the skip-link script & styles. |
18 | 103 |
* |
104 |
* @access private |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
105 |
* @since 6.4.0 |
18 | 106 |
* |
107 |
* @global string $_wp_current_template_content |
|
108 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
109 |
function wp_enqueue_block_template_skip_link() { |
18 | 110 |
global $_wp_current_template_content; |
111 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
112 |
// Back-compat for plugins that disable functionality by unhooking this action. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
113 |
if ( ! has_action( 'wp_footer', 'the_block_template_skip_link' ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
114 |
return; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
115 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
116 |
remove_action( 'wp_footer', 'the_block_template_skip_link' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
117 |
|
18 | 118 |
// Early exit if not a block theme. |
119 |
if ( ! current_theme_supports( 'block-templates' ) ) { |
|
120 |
return; |
|
121 |
} |
|
122 |
||
123 |
// Early exit if not a block template. |
|
124 |
if ( ! $_wp_current_template_content ) { |
|
125 |
return; |
|
126 |
} |
|
127 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
128 |
$skip_link_styles = ' |
18 | 129 |
.skip-link.screen-reader-text { |
130 |
border: 0; |
|
131 |
clip-path: inset(50%); |
|
132 |
height: 1px; |
|
133 |
margin: -1px; |
|
134 |
overflow: hidden; |
|
135 |
padding: 0; |
|
136 |
position: absolute !important; |
|
137 |
width: 1px; |
|
138 |
word-wrap: normal !important; |
|
139 |
} |
|
140 |
||
141 |
.skip-link.screen-reader-text:focus { |
|
142 |
background-color: #eee; |
|
143 |
clip-path: none; |
|
144 |
color: #444; |
|
145 |
display: block; |
|
146 |
font-size: 1em; |
|
147 |
height: auto; |
|
148 |
left: 5px; |
|
149 |
line-height: normal; |
|
150 |
padding: 15px 23px 14px; |
|
151 |
text-decoration: none; |
|
152 |
top: 5px; |
|
153 |
width: auto; |
|
154 |
z-index: 100000; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
155 |
}'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
156 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
157 |
$handle = 'wp-block-template-skip-link'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
158 |
|
18 | 159 |
/** |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
160 |
* Print the skip-link styles. |
18 | 161 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
162 |
wp_register_style( $handle, false ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
163 |
wp_add_inline_style( $handle, $skip_link_styles ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
164 |
wp_enqueue_style( $handle ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
165 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
166 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
167 |
* Enqueue the skip-link script. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
168 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
169 |
ob_start(); |
18 | 170 |
?> |
171 |
<script> |
|
172 |
( function() { |
|
173 |
var skipLinkTarget = document.querySelector( 'main' ), |
|
19 | 174 |
sibling, |
18 | 175 |
skipLinkTargetID, |
176 |
skipLink; |
|
177 |
||
178 |
// Early exit if a skip-link target can't be located. |
|
179 |
if ( ! skipLinkTarget ) { |
|
180 |
return; |
|
181 |
} |
|
182 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
183 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
184 |
* Get the site wrapper. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
185 |
* The skip-link will be injected in the beginning of it. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
186 |
*/ |
19 | 187 |
sibling = document.querySelector( '.wp-site-blocks' ); |
18 | 188 |
|
189 |
// Early exit if the root element was not found. |
|
19 | 190 |
if ( ! sibling ) { |
18 | 191 |
return; |
192 |
} |
|
193 |
||
194 |
// Get the skip-link target's ID, and generate one if it doesn't exist. |
|
195 |
skipLinkTargetID = skipLinkTarget.id; |
|
196 |
if ( ! skipLinkTargetID ) { |
|
197 |
skipLinkTargetID = 'wp--skip-link--target'; |
|
198 |
skipLinkTarget.id = skipLinkTargetID; |
|
199 |
} |
|
200 |
||
201 |
// Create the skip link. |
|
202 |
skipLink = document.createElement( 'a' ); |
|
203 |
skipLink.classList.add( 'skip-link', 'screen-reader-text' ); |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
204 |
skipLink.id = 'wp-skip-link'; |
18 | 205 |
skipLink.href = '#' + skipLinkTargetID; |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
206 |
skipLink.innerText = '<?php /* translators: Hidden accessibility text. Do not use HTML entities ( , etc.). */ esc_html_e( 'Skip to content' ); ?>'; |
18 | 207 |
|
208 |
// Inject the skip link. |
|
19 | 209 |
sibling.parentElement.insertBefore( skipLink, sibling ); |
18 | 210 |
}() ); |
211 |
</script> |
|
212 |
<?php |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
213 |
$skip_link_script = wp_remove_surrounding_empty_script_tags( ob_get_clean() ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
214 |
$script_handle = 'wp-block-template-skip-link'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
215 |
wp_register_script( $script_handle, false, array(), false, array( 'in_footer' => true ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
216 |
wp_add_inline_script( $script_handle, $skip_link_script ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
217 |
wp_enqueue_script( $script_handle ); |
18 | 218 |
} |
219 |
||
220 |
/** |
|
19 | 221 |
* Enables the block templates (editor mode) for themes with theme.json by default. |
18 | 222 |
* |
223 |
* @access private |
|
224 |
* @since 5.8.0 |
|
225 |
*/ |
|
226 |
function wp_enable_block_templates() { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
227 |
if ( wp_is_block_theme() || wp_theme_has_theme_json() ) { |
18 | 228 |
add_theme_support( 'block-templates' ); |
229 |
} |
|
230 |
} |