author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
child 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
19 | 1 |
<?php |
2 |
/** |
|
3 |
* Server-side rendering of the `core/home-link` block. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
*/ |
|
7 |
||
8 |
/** |
|
9 |
* Build an array with CSS classes and inline styles defining the colors |
|
10 |
* which will be applied to the home link markup in the front-end. |
|
11 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
12 |
* @since 6.0.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
13 |
* |
19 | 14 |
* @param array $context home link block context. |
15 |
* @return array Colors CSS classes and inline styles. |
|
16 |
*/ |
|
17 |
function block_core_home_link_build_css_colors( $context ) { |
|
18 |
$colors = array( |
|
19 |
'css_classes' => array(), |
|
20 |
'inline_styles' => '', |
|
21 |
); |
|
22 |
||
23 |
// Text color. |
|
24 |
$has_named_text_color = array_key_exists( 'textColor', $context ); |
|
25 |
$has_custom_text_color = isset( $context['style']['color']['text'] ); |
|
26 |
||
27 |
// If has text color. |
|
28 |
if ( $has_custom_text_color || $has_named_text_color ) { |
|
29 |
// Add has-text-color class. |
|
30 |
$colors['css_classes'][] = 'has-text-color'; |
|
31 |
} |
|
32 |
||
33 |
if ( $has_named_text_color ) { |
|
34 |
// Add the color class. |
|
35 |
$colors['css_classes'][] = sprintf( 'has-%s-color', $context['textColor'] ); |
|
36 |
} elseif ( $has_custom_text_color ) { |
|
37 |
// Add the custom color inline style. |
|
38 |
$colors['inline_styles'] .= sprintf( 'color: %s;', $context['style']['color']['text'] ); |
|
39 |
} |
|
40 |
||
41 |
// Background color. |
|
42 |
$has_named_background_color = array_key_exists( 'backgroundColor', $context ); |
|
43 |
$has_custom_background_color = isset( $context['style']['color']['background'] ); |
|
44 |
||
45 |
// If has background color. |
|
46 |
if ( $has_custom_background_color || $has_named_background_color ) { |
|
47 |
// Add has-background class. |
|
48 |
$colors['css_classes'][] = 'has-background'; |
|
49 |
} |
|
50 |
||
51 |
if ( $has_named_background_color ) { |
|
52 |
// Add the background-color class. |
|
53 |
$colors['css_classes'][] = sprintf( 'has-%s-background-color', $context['backgroundColor'] ); |
|
54 |
} elseif ( $has_custom_background_color ) { |
|
55 |
// Add the custom background-color inline style. |
|
56 |
$colors['inline_styles'] .= sprintf( 'background-color: %s;', $context['style']['color']['background'] ); |
|
57 |
} |
|
58 |
||
59 |
return $colors; |
|
60 |
} |
|
61 |
||
62 |
/** |
|
63 |
* Build an array with CSS classes and inline styles defining the font sizes |
|
64 |
* which will be applied to the home link markup in the front-end. |
|
65 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
66 |
* @since 6.0.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
67 |
* |
19 | 68 |
* @param array $context Home link block context. |
69 |
* @return array Font size CSS classes and inline styles. |
|
70 |
*/ |
|
71 |
function block_core_home_link_build_css_font_sizes( $context ) { |
|
72 |
// CSS classes. |
|
73 |
$font_sizes = array( |
|
74 |
'css_classes' => array(), |
|
75 |
'inline_styles' => '', |
|
76 |
); |
|
77 |
||
78 |
$has_named_font_size = array_key_exists( 'fontSize', $context ); |
|
79 |
$has_custom_font_size = isset( $context['style']['typography']['fontSize'] ); |
|
80 |
||
81 |
if ( $has_named_font_size ) { |
|
82 |
// Add the font size class. |
|
83 |
$font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $context['fontSize'] ); |
|
84 |
} elseif ( $has_custom_font_size ) { |
|
85 |
// Add the custom font size inline style. |
|
86 |
$font_sizes['inline_styles'] = sprintf( 'font-size: %s;', $context['style']['typography']['fontSize'] ); |
|
87 |
} |
|
88 |
||
89 |
return $font_sizes; |
|
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* Builds an array with classes and style for the li wrapper |
|
94 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
95 |
* @since 6.0.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
96 |
* |
19 | 97 |
* @param array $context Home link block context. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
98 |
* @return string The li wrapper attributes. |
19 | 99 |
*/ |
100 |
function block_core_home_link_build_li_wrapper_attributes( $context ) { |
|
101 |
$colors = block_core_home_link_build_css_colors( $context ); |
|
102 |
$font_sizes = block_core_home_link_build_css_font_sizes( $context ); |
|
103 |
$classes = array_merge( |
|
104 |
$colors['css_classes'], |
|
105 |
$font_sizes['css_classes'] |
|
106 |
); |
|
107 |
$style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
108 |
$classes[] = 'wp-block-navigation-item'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
109 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
110 |
if ( is_front_page() ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
111 |
$classes[] = 'current-menu-item'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
112 |
} elseif ( is_home() && ( (int) get_option( 'page_for_posts' ) !== get_queried_object_id() ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
113 |
// Edge case where the Reading settings has a posts page set but not a static homepage. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
114 |
$classes[] = 'current-menu-item'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
115 |
} |
19 | 116 |
|
117 |
$wrapper_attributes = get_block_wrapper_attributes( |
|
118 |
array( |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
119 |
'class' => implode( ' ', $classes ), |
19 | 120 |
'style' => $style_attribute, |
121 |
) |
|
122 |
); |
|
123 |
||
124 |
return $wrapper_attributes; |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* Renders the `core/home-link` block. |
|
129 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
130 |
* @since 6.0.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
131 |
* |
19 | 132 |
* @param array $attributes The block attributes. |
133 |
* @param string $content The saved content. |
|
134 |
* @param WP_Block $block The parsed block. |
|
135 |
* |
|
136 |
* @return string Returns the post content with the home url added. |
|
137 |
*/ |
|
138 |
function render_block_core_home_link( $attributes, $content, $block ) { |
|
139 |
if ( empty( $attributes['label'] ) ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
140 |
// Using a fallback for the label attribute allows rendering the block even if no attributes have been set, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
141 |
// e.g. when using the block as a hooked block. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
142 |
// Note that the fallback value needs to be kept in sync with the one set in `edit.js` (upon first loading the block in the editor). |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
143 |
$attributes['label'] = __( 'Home' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
144 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
145 |
$aria_current = ''; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
146 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
147 |
if ( is_front_page() ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
148 |
$aria_current = ' aria-current="page"'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
149 |
} elseif ( is_home() && ( (int) get_option( 'page_for_posts' ) !== get_queried_object_id() ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
150 |
// Edge case where the Reading settings has a posts page set but not a static homepage. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
151 |
$aria_current = ' aria-current="page"'; |
19 | 152 |
} |
153 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
154 |
return sprintf( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
155 |
'<li %1$s><a class="wp-block-home-link__content wp-block-navigation-item__content" href="%2$s" rel="home"%3$s>%4$s</a></li>', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
156 |
block_core_home_link_build_li_wrapper_attributes( $block->context ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
157 |
esc_url( home_url() ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
158 |
$aria_current, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
159 |
wp_kses_post( $attributes['label'] ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
160 |
); |
19 | 161 |
} |
162 |
||
163 |
/** |
|
164 |
* Register the home block |
|
165 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
166 |
* @since 6.0.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
167 |
* |
19 | 168 |
* @uses render_block_core_home_link() |
169 |
* @throws WP_Error An WP_Error exception parsing the block definition. |
|
170 |
*/ |
|
171 |
function register_block_core_home_link() { |
|
172 |
register_block_type_from_metadata( |
|
173 |
__DIR__ . '/home-link', |
|
174 |
array( |
|
175 |
'render_callback' => 'render_block_core_home_link', |
|
176 |
) |
|
177 |
); |
|
178 |
} |
|
179 |
add_action( 'init', 'register_block_core_home_link' ); |