| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Server-side rendering of the `core/post-navigation-link` block. 4 * 5 * @package WordPress 6 */ 7 8 /** 9 * Renders the `core/post-navigation-link` block on the server. 10 * 11 * @since 5.9.0 12 * 13 * @param array $attributes Block attributes. 14 * @param string $content Block default content. 15 * 16 * @return string Returns the next or previous post link that is adjacent to the current post. 17 */ 18 function render_block_core_post_navigation_link( $attributes, $content ) { 19 if ( ! is_singular() ) { 20 return ''; 21 } 22 23 // Get the navigation type to show the proper link. Available options are `next|previous`. 24 $navigation_type = $attributes['type'] ?? 'next'; 25 // Allow only `next` and `previous` in `$navigation_type`. 26 if ( ! in_array( $navigation_type, array( 'next', 'previous' ), true ) ) { 27 return ''; 28 } 29 $classes = "post-navigation-link-$navigation_type"; 30 if ( isset( $attributes['textAlign'] ) ) { 31 $classes .= " has-text-align-{$attributes['textAlign']}"; 32 } 33 34 // Set default values. 35 $format = '%link'; 36 $link = 'next' === $navigation_type ? _x( 'Next', 'label for next post link' ) : _x( 'Previous', 'label for previous post link' ); 37 $label = ''; 38 39 // Only use hardcoded values here, otherwise we need to add escaping where these values are used. 40 $arrow_map = array( 41 'none' => '', 42 'arrow' => array( 43 'next' => '→', 44 'previous' => '←', 45 ), 46 'chevron' => array( 47 'next' => '»', 48 'previous' => '«', 49 ), 50 ); 51 52 // If a custom label is provided, make this a link. 53 // `$label` is used to prepend the provided label, if we want to show the page title as well. 54 if ( isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ) { 55 $label = "{$attributes['label']}"; 56 $link = $label; 57 } 58 59 // If we want to also show the page title, make the page title a link and prepend the label. 60 if ( isset( $attributes['showTitle'] ) && $attributes['showTitle'] ) { 61 /* 62 * If the label link option is not enabled but there is a custom label, 63 * display the custom label as text before the linked title. 64 */ 65 if ( ! $attributes['linkLabel'] ) { 66 if ( $label ) { 67 $format = '<span class="post-navigation-link__label">' . wp_kses_post( $label ) . '</span> %link'; 68 } 69 $link = '%title'; 70 } elseif ( isset( $attributes['linkLabel'] ) && $attributes['linkLabel'] ) { 71 // If the label link option is enabled and there is a custom label, display it before the title. 72 if ( $label ) { 73 $link = '<span class="post-navigation-link__label">' . wp_kses_post( $label ) . '</span> <span class="post-navigation-link__title">%title</span>'; 74 } else { 75 /* 76 * If the label link option is enabled and there is no custom label, 77 * add a colon between the label and the post title. 78 */ 79 $label = 'next' === $navigation_type ? _x( 'Next:', 'label before the title of the next post' ) : _x( 'Previous:', 'label before the title of the previous post' ); 80 $link = sprintf( 81 '<span class="post-navigation-link__label">%1$s</span> <span class="post-navigation-link__title">%2$s</span>', 82 wp_kses_post( $label ), 83 '%title' 84 ); 85 } 86 } 87 } 88 89 // Display arrows. 90 if ( isset( $attributes['arrow'] ) && 'none' !== $attributes['arrow'] && isset( $arrow_map[ $attributes['arrow'] ] ) ) { 91 $arrow = $arrow_map[ $attributes['arrow'] ][ $navigation_type ]; 92 93 if ( 'next' === $navigation_type ) { 94 $format = '%link<span class="wp-block-post-navigation-link__arrow-next is-arrow-' . $attributes['arrow'] . '" aria-hidden="true">' . $arrow . '</span>'; 95 } else { 96 $format = '<span class="wp-block-post-navigation-link__arrow-previous is-arrow-' . $attributes['arrow'] . '" aria-hidden="true">' . $arrow . '</span>%link'; 97 } 98 } 99 100 /* 101 * The dynamic portion of the function name, `$navigation_type`, 102 * Refers to the type of adjacency, 'next' or 'previous'. 103 * 104 * @see https://developer.wordpress.org/reference/functions/get_previous_post_link/ 105 * @see https://developer.wordpress.org/reference/functions/get_next_post_link/ 106 */ 107 $get_link_function = "get_{$navigation_type}_post_link"; 108 109 if ( ! empty( $attributes['taxonomy'] ) ) { 110 $content = $get_link_function( $format, $link, true, '', $attributes['taxonomy'] ); 111 } else { 112 $content = $get_link_function( $format, $link ); 113 } 114 115 // The `{next,previous}_post_link` filters can return null, which renders as no link. 116 $content = (string) $content; 117 118 /* 119 * Border, shadow and spacing serialization is skipped for this block so 120 * those styles can be withheld from the empty wrapper rendered when there is 121 * no adjacent post. The wrapper itself is kept for backward compatibility. 122 */ 123 $support_styles = '' === $content 124 ? array( 125 'class' => '', 126 'style' => '', 127 ) 128 : block_core_post_navigation_link_get_support_styles( $attributes ); 129 130 if ( '' !== $support_styles['class'] ) { 131 $classes .= " {$support_styles['class']}"; 132 } 133 134 $wrapper_attributes = get_block_wrapper_attributes( 135 array( 136 'class' => $classes, 137 'style' => $support_styles['style'], 138 ) 139 ); 140 141 return sprintf( 142 '<div %1$s>%2$s</div>', 143 $wrapper_attributes, 144 $content 145 ); 146 } 147 148 /** 149 * Generates the border, shadow and spacing class names and inline styles for 150 * the `core/post-navigation-link` block. 151 * 152 * Mirrors the default border, shadow and spacing block support serialization, 153 * which the block opts out of so the styles are only applied when a link is 154 * rendered. 155 * 156 * @since 7.2.0 157 * 158 * @param array $attributes The block attributes. 159 * @return array { 160 * Border, shadow and spacing class names and inline styles. 161 * 162 * @type string $class Class names generated by the style engine, or an empty string. 163 * @type string $style Inline styles generated by the style engine, or an empty string. 164 * } 165 */ 166 function block_core_post_navigation_link_get_support_styles( $attributes ) { 167 $block_styles = $attributes['style'] ?? array(); 168 $border = $block_styles['border'] ?? array(); 169 $border_styles = array(); 170 171 // Border radius and width accept unitless numbers from the original implementation. 172 if ( isset( $border['radius'] ) ) { 173 $border_styles['radius'] = is_numeric( $border['radius'] ) ? "{$border['radius']}px" : $border['radius']; 174 } 175 176 if ( isset( $border['width'] ) ) { 177 $border_styles['width'] = is_numeric( $border['width'] ) ? "{$border['width']}px" : $border['width']; 178 } 179 180 if ( isset( $border['style'] ) ) { 181 $border_styles['style'] = $border['style']; 182 } 183 184 // A preset border color is stored in its own attribute rather than under `style`. 185 $border_styles['color'] = isset( $attributes['borderColor'] ) 186 ? "var:preset|color|{$attributes['borderColor']}" 187 : ( $border['color'] ?? null ); 188 189 // Individual border sides e.g. top, left etc. 190 foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) { 191 $border_styles[ $side ] = array( 192 'width' => $border[ $side ]['width'] ?? null, 193 'color' => $border[ $side ]['color'] ?? null, 194 'style' => $border[ $side ]['style'] ?? null, 195 ); 196 } 197 198 $styles = wp_style_engine_get_styles( 199 array( 200 'border' => $border_styles, 201 'shadow' => $block_styles['shadow'] ?? null, 202 'spacing' => array( 203 'margin' => $block_styles['spacing']['margin'] ?? null, 204 'padding' => $block_styles['spacing']['padding'] ?? null, 205 ), 206 ) 207 ); 208 209 return array( 210 'class' => $styles['classnames'] ?? '', 211 'style' => $styles['css'] ?? '', 212 ); 213 } 214 215 /** 216 * Registers the `core/post-navigation-link` block on the server. 217 * 218 * @since 5.9.0 219 */ 220 function register_block_core_post_navigation_link() { 221 register_block_type_from_metadata( 222 __DIR__ . '/post-navigation-link', 223 array( 224 'render_callback' => 'render_block_core_post_navigation_link', 225 ) 226 ); 227 } 228 add_action( 'init', 'register_block_core_post_navigation_link' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Fri Oct 9 08:20:33 2026 | Cross-referenced by PHPXref |