|
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 * |
|
12 * @param array $context home link block context. |
|
13 * @return array Colors CSS classes and inline styles. |
|
14 */ |
|
15 function block_core_home_link_build_css_colors( $context ) { |
|
16 $colors = array( |
|
17 'css_classes' => array(), |
|
18 'inline_styles' => '', |
|
19 ); |
|
20 |
|
21 // Text color. |
|
22 $has_named_text_color = array_key_exists( 'textColor', $context ); |
|
23 $has_custom_text_color = isset( $context['style']['color']['text'] ); |
|
24 |
|
25 // If has text color. |
|
26 if ( $has_custom_text_color || $has_named_text_color ) { |
|
27 // Add has-text-color class. |
|
28 $colors['css_classes'][] = 'has-text-color'; |
|
29 } |
|
30 |
|
31 if ( $has_named_text_color ) { |
|
32 // Add the color class. |
|
33 $colors['css_classes'][] = sprintf( 'has-%s-color', $context['textColor'] ); |
|
34 } elseif ( $has_custom_text_color ) { |
|
35 // Add the custom color inline style. |
|
36 $colors['inline_styles'] .= sprintf( 'color: %s;', $context['style']['color']['text'] ); |
|
37 } |
|
38 |
|
39 // Background color. |
|
40 $has_named_background_color = array_key_exists( 'backgroundColor', $context ); |
|
41 $has_custom_background_color = isset( $context['style']['color']['background'] ); |
|
42 |
|
43 // If has background color. |
|
44 if ( $has_custom_background_color || $has_named_background_color ) { |
|
45 // Add has-background class. |
|
46 $colors['css_classes'][] = 'has-background'; |
|
47 } |
|
48 |
|
49 if ( $has_named_background_color ) { |
|
50 // Add the background-color class. |
|
51 $colors['css_classes'][] = sprintf( 'has-%s-background-color', $context['backgroundColor'] ); |
|
52 } elseif ( $has_custom_background_color ) { |
|
53 // Add the custom background-color inline style. |
|
54 $colors['inline_styles'] .= sprintf( 'background-color: %s;', $context['style']['color']['background'] ); |
|
55 } |
|
56 |
|
57 return $colors; |
|
58 } |
|
59 |
|
60 /** |
|
61 * Build an array with CSS classes and inline styles defining the font sizes |
|
62 * which will be applied to the home link markup in the front-end. |
|
63 * |
|
64 * @param array $context Home link block context. |
|
65 * @return array Font size CSS classes and inline styles. |
|
66 */ |
|
67 function block_core_home_link_build_css_font_sizes( $context ) { |
|
68 // CSS classes. |
|
69 $font_sizes = array( |
|
70 'css_classes' => array(), |
|
71 'inline_styles' => '', |
|
72 ); |
|
73 |
|
74 $has_named_font_size = array_key_exists( 'fontSize', $context ); |
|
75 $has_custom_font_size = isset( $context['style']['typography']['fontSize'] ); |
|
76 |
|
77 if ( $has_named_font_size ) { |
|
78 // Add the font size class. |
|
79 $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $context['fontSize'] ); |
|
80 } elseif ( $has_custom_font_size ) { |
|
81 // Add the custom font size inline style. |
|
82 $font_sizes['inline_styles'] = sprintf( 'font-size: %s;', $context['style']['typography']['fontSize'] ); |
|
83 } |
|
84 |
|
85 return $font_sizes; |
|
86 } |
|
87 |
|
88 /** |
|
89 * Builds an array with classes and style for the li wrapper |
|
90 * |
|
91 * @param array $context Home link block context. |
|
92 * @return array The li wrapper attributes. |
|
93 */ |
|
94 function block_core_home_link_build_li_wrapper_attributes( $context ) { |
|
95 $colors = block_core_home_link_build_css_colors( $context ); |
|
96 $font_sizes = block_core_home_link_build_css_font_sizes( $context ); |
|
97 $classes = array_merge( |
|
98 $colors['css_classes'], |
|
99 $font_sizes['css_classes'] |
|
100 ); |
|
101 $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] ); |
|
102 $css_classes = trim( implode( ' ', $classes ) ) . ' wp-block-navigation-item'; |
|
103 |
|
104 $wrapper_attributes = get_block_wrapper_attributes( |
|
105 array( |
|
106 'class' => $css_classes, |
|
107 'style' => $style_attribute, |
|
108 ) |
|
109 ); |
|
110 |
|
111 return $wrapper_attributes; |
|
112 } |
|
113 |
|
114 /** |
|
115 * Renders the `core/home-link` block. |
|
116 * |
|
117 * @param array $attributes The block attributes. |
|
118 * @param string $content The saved content. |
|
119 * @param WP_Block $block The parsed block. |
|
120 * |
|
121 * @return string Returns the post content with the home url added. |
|
122 */ |
|
123 function render_block_core_home_link( $attributes, $content, $block ) { |
|
124 if ( empty( $attributes['label'] ) ) { |
|
125 return ''; |
|
126 } |
|
127 |
|
128 $wrapper_attributes = block_core_home_link_build_li_wrapper_attributes( $block->context ); |
|
129 |
|
130 $aria_current = is_home() || ( is_front_page() && 'page' === get_option( 'show_on_front' ) ) ? ' aria-current="page"' : ''; |
|
131 |
|
132 $html = '<li ' . $wrapper_attributes . '><a class="wp-block-home-link__content wp-block-navigation-item__content" rel="home"' . $aria_current; |
|
133 |
|
134 // Start appending HTML attributes to anchor tag. |
|
135 $html .= ' href="' . esc_url( home_url() ) . '"'; |
|
136 |
|
137 // End appending HTML attributes to anchor tag. |
|
138 $html .= '>'; |
|
139 |
|
140 if ( isset( $attributes['label'] ) ) { |
|
141 $html .= wp_kses_post( $attributes['label'] ); |
|
142 } |
|
143 |
|
144 $html .= '</a></li>'; |
|
145 return $html; |
|
146 } |
|
147 |
|
148 /** |
|
149 * Register the home block |
|
150 * |
|
151 * @uses render_block_core_home_link() |
|
152 * @throws WP_Error An WP_Error exception parsing the block definition. |
|
153 */ |
|
154 function register_block_core_home_link() { |
|
155 register_block_type_from_metadata( |
|
156 __DIR__ . '/home-link', |
|
157 array( |
|
158 'render_callback' => 'render_block_core_home_link', |
|
159 ) |
|
160 ); |
|
161 } |
|
162 add_action( 'init', 'register_block_core_home_link' ); |