PluginProbe
Gutenberg / 22.9.0
Gutenberg v22.9.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.9.0, at build/scripts/block-library/home-link.php

158 lines 4.9 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 require_once __DIR__ . '/navigation-link/shared/build-css-font-sizes.php';
9
10 /**
11 * Build an array with CSS classes and inline styles defining the colors
12 * which will be applied to the home link markup in the front-end.
13 *
14 * @since 6.0.0
15 *
16 * @param array $context home link block context.
17 * @return array Colors CSS classes and inline styles.
18 */
19 function gutenberg_block_core_home_link_build_css_colors( $context ) {
20 $colors = array(
21 'css_classes' => array(),
22 'inline_styles' => '',
23 );
24
25 // Text color.
26 $has_named_text_color = array_key_exists( 'textColor', $context );
27 $has_custom_text_color = isset( $context['style']['color']['text'] );
28
29 // If has text color.
30 if ( $has_custom_text_color || $has_named_text_color ) {
31 // Add has-text-color class.
32 $colors['css_classes'][] = 'has-text-color';
33 }
34
35 if ( $has_named_text_color ) {
36 // Add the color class.
37 $colors['css_classes'][] = sprintf( 'has-%s-color', $context['textColor'] );
38 } elseif ( $has_custom_text_color ) {
39 // Add the custom color inline style.
40 $colors['inline_styles'] .= sprintf( 'color: %s;', $context['style']['color']['text'] );
41 }
42
43 // Background color.
44 $has_named_background_color = array_key_exists( 'backgroundColor', $context );
45 $has_custom_background_color = isset( $context['style']['color']['background'] );
46
47 // If has background color.
48 if ( $has_custom_background_color || $has_named_background_color ) {
49 // Add has-background class.
50 $colors['css_classes'][] = 'has-background';
51 }
52
53 if ( $has_named_background_color ) {
54 // Add the background-color class.
55 $colors['css_classes'][] = sprintf( 'has-%s-background-color', $context['backgroundColor'] );
56 } elseif ( $has_custom_background_color ) {
57 // Add the custom background-color inline style.
58 $colors['inline_styles'] .= sprintf( 'background-color: %s;', $context['style']['color']['background'] );
59 }
60
61 return $colors;
62 }
63
64 /**
65 * Builds an array with classes and style for the li wrapper
66 *
67 * @since 6.0.0
68 *
69 * @param array $context Home link block context.
70 * @return string The li wrapper attributes.
71 */
72 function gutenberg_block_core_home_link_build_li_wrapper_attributes( $context ) {
73 $colors = gutenberg_block_core_home_link_build_css_colors( $context );
74 // The build system prefixes this function with "gutenberg_" to avoid
75 // collisions with the core version. Until this function is backported to
76 // core, we need to guard it's use and only call the prefixed name in
77 // the plugin.
78 if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
79 $font_sizes = gutenberg_block_core_shared_navigation_build_css_font_sizes( $context );
80 } else {
81 $font_sizes = block_core_shared_navigation_build_css_font_sizes( $context );
82 }
83 $classes = array_merge(
84 $colors['css_classes'],
85 $font_sizes['css_classes']
86 );
87
88 $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] );
89 $classes[] = 'wp-block-navigation-item';
90
91 if ( is_front_page() ) {
92 $classes[] = 'current-menu-item';
93 } elseif ( is_home() && ( (int) get_option( 'page_for_posts' ) !== get_queried_object_id() ) ) {
94 // Edge case where the Reading settings has a posts page set but not a static homepage.
95 $classes[] = 'current-menu-item';
96 }
97
98 $wrapper_attributes = get_block_wrapper_attributes(
99 array(
100 'class' => implode( ' ', $classes ),
101 'style' => $style_attribute,
102 )
103 );
104
105 return $wrapper_attributes;
106 }
107
108 /**
109 * Renders the `core/home-link` block.
110 *
111 * @since 6.0.0
112 *
113 * @param array $attributes The block attributes.
114 * @param string $content The saved content.
115 * @param WP_Block $block The parsed block.
116 *
117 * @return string Returns the post content with the home url added.
118 */
119 function gutenberg_render_block_core_home_link( $attributes, $content, $block ) {
120 if ( empty( $attributes['label'] ) ) {
121 $attributes['label'] = __( 'Home' );
122 }
123 $aria_current = '';
124
125 if ( is_front_page() ) {
126 $aria_current = ' aria-current="page"';
127 } elseif ( is_home() && ( (int) get_option( 'page_for_posts' ) !== get_queried_object_id() ) ) {
128 // Edge case where the Reading settings has a posts page set but not a static homepage.
129 $aria_current = ' aria-current="page"';
130 }
131
132 return sprintf(
133 '<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>',
134 gutenberg_block_core_home_link_build_li_wrapper_attributes( $block->context ),
135 esc_url( home_url() ),
136 $aria_current,
137 wp_kses_post( $attributes['label'] )
138 );
139 }
140
141 /**
142 * Register the home block
143 *
144 * @since 6.0.0
145 *
146 * @uses gutenberg_render_block_core_home_link()
147 * @throws WP_Error An WP_Error exception parsing the block definition.
148 */
149 function gutenberg_register_block_core_home_link() {
150 register_block_type_from_metadata(
151 __DIR__ . '/home-link',
152 array(
153 'render_callback' => 'gutenberg_render_block_core_home_link',
154 )
155 );
156 }
157 add_action( 'init', 'gutenberg_register_block_core_home_link', 20 );
158