PluginProbe
Gutenberg / 12.6.0
Gutenberg v12.6.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 / block-library / blocks / home-link.php

home-link.php in Gutenberg 12.6.0, at build/block-library/blocks/home-link.php

181 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 * @param array $context home link block context.
13 * @return array Colors CSS classes and inline styles.
14 */
15 function gutenberg_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 gutenberg_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 gutenberg_block_core_home_link_build_li_wrapper_attributes( $context ) {
95 $colors = gutenberg_block_core_home_link_build_css_colors( $context );
96 $font_sizes = gutenberg_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 gutenberg_render_block_core_home_link( $attributes, $content, $block ) {
124 if ( empty( $attributes['label'] ) ) {
125 return '';
126 }
127
128 $wrapper_attributes = gutenberg_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(
142 $attributes['label'],
143 array(
144 'code' => array(),
145 'em' => array(),
146 'img' => array(
147 'scale' => array(),
148 'class' => array(),
149 'style' => array(),
150 'src' => array(),
151 'alt' => array(),
152 ),
153 's' => array(),
154 'span' => array(
155 'style' => array(),
156 ),
157 'strong' => array(),
158 )
159 );
160 }
161
162 $html .= '</a></li>';
163 return $html;
164 }
165
166 /**
167 * Register the home block
168 *
169 * @uses gutenberg_render_block_core_home_link()
170 * @throws WP_Error An WP_Error exception parsing the block definition.
171 */
172 function gutenberg_register_block_core_home_link() {
173 register_block_type_from_metadata(
174 __DIR__ . '/home-link',
175 array(
176 'render_callback' => 'gutenberg_render_block_core_home_link',
177 )
178 );
179 }
180 add_action( 'init', 'gutenberg_register_block_core_home_link', 20 );
181