PluginProbe
Gutenberg / 22.3.0
Gutenberg v22.3.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / scripts / block-library / home-link.php

home-link.php in Gutenberg 22.3.0, at build/scripts/block-library/home-link.php

177 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @since 6.0.0
13 *
14 * @param array $context home link block context.
15 * @return array Colors CSS classes and inline styles.
16 */
17 function gutenberg_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 *
66 * @since 6.0.0
67 *
68 * @param array $context Home link block context.
69 * @return array Font size CSS classes and inline styles.
70 */
71 function gutenberg_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 *
95 * @since 6.0.0
96 *
97 * @param array $context Home link block context.
98 * @return string The li wrapper attributes.
99 */
100 function gutenberg_block_core_home_link_build_li_wrapper_attributes( $context ) {
101 $colors = gutenberg_block_core_home_link_build_css_colors( $context );
102 $font_sizes = gutenberg_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'] );
108 $classes[] = 'wp-block-navigation-item';
109
110 if ( is_front_page() ) {
111 $classes[] = 'current-menu-item';
112 } elseif ( is_home() && ( (int) get_option( 'page_for_posts' ) !== get_queried_object_id() ) ) {
113 // Edge case where the Reading settings has a posts page set but not a static homepage.
114 $classes[] = 'current-menu-item';
115 }
116
117 $wrapper_attributes = get_block_wrapper_attributes(
118 array(
119 'class' => implode( ' ', $classes ),
120 'style' => $style_attribute,
121 )
122 );
123
124 return $wrapper_attributes;
125 }
126
127 /**
128 * Renders the `core/home-link` block.
129 *
130 * @since 6.0.0
131 *
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 gutenberg_render_block_core_home_link( $attributes, $content, $block ) {
139 if ( empty( $attributes['label'] ) ) {
140 $attributes['label'] = __( 'Home' );
141 }
142 $aria_current = '';
143
144 if ( is_front_page() ) {
145 $aria_current = ' aria-current="page"';
146 } elseif ( is_home() && ( (int) get_option( 'page_for_posts' ) !== get_queried_object_id() ) ) {
147 // Edge case where the Reading settings has a posts page set but not a static homepage.
148 $aria_current = ' aria-current="page"';
149 }
150
151 return sprintf(
152 '<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>',
153 gutenberg_block_core_home_link_build_li_wrapper_attributes( $block->context ),
154 esc_url( home_url() ),
155 $aria_current,
156 wp_kses_post( $attributes['label'] )
157 );
158 }
159
160 /**
161 * Register the home block
162 *
163 * @since 6.0.0
164 *
165 * @uses gutenberg_render_block_core_home_link()
166 * @throws WP_Error An WP_Error exception parsing the block definition.
167 */
168 function gutenberg_register_block_core_home_link() {
169 register_block_type_from_metadata(
170 __DIR__ . '/home-link',
171 array(
172 'render_callback' => 'gutenberg_render_block_core_home_link',
173 )
174 );
175 }
176 add_action( 'init', 'gutenberg_register_block_core_home_link', 20 );
177