5 * @package WordPress |
5 * @package WordPress |
6 */ |
6 */ |
7 |
7 |
8 /** |
8 /** |
9 * Renders the `core/post-navigation-link` block on the server. |
9 * Renders the `core/post-navigation-link` block on the server. |
|
10 * |
|
11 * @since 5.9.0 |
10 * |
12 * |
11 * @param array $attributes Block attributes. |
13 * @param array $attributes Block attributes. |
12 * @param string $content Block default content. |
14 * @param string $content Block default content. |
13 * |
15 * |
14 * @return string Returns the next or previous post link that is adjacent to the current post. |
16 * @return string Returns the next or previous post link that is adjacent to the current post. |
26 } |
28 } |
27 $classes = "post-navigation-link-$navigation_type"; |
29 $classes = "post-navigation-link-$navigation_type"; |
28 if ( isset( $attributes['textAlign'] ) ) { |
30 if ( isset( $attributes['textAlign'] ) ) { |
29 $classes .= " has-text-align-{$attributes['textAlign']}"; |
31 $classes .= " has-text-align-{$attributes['textAlign']}"; |
30 } |
32 } |
31 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) ); |
33 $wrapper_attributes = get_block_wrapper_attributes( |
|
34 array( |
|
35 'class' => $classes, |
|
36 ) |
|
37 ); |
32 // Set default values. |
38 // Set default values. |
33 $format = '%link'; |
39 $format = '%link'; |
34 $link = 'next' === $navigation_type ? _x( 'Next', 'label for next post link' ) : _x( 'Previous', 'label for previous post link' ); |
40 $link = 'next' === $navigation_type ? _x( 'Next', 'label for next post link' ) : _x( 'Previous', 'label for previous post link' ); |
35 $label = ''; |
41 $label = ''; |
|
42 |
|
43 // Only use hardcoded values here, otherwise we need to add escaping where these values are used. |
|
44 $arrow_map = array( |
|
45 'none' => '', |
|
46 'arrow' => array( |
|
47 'next' => '→', |
|
48 'previous' => '←', |
|
49 ), |
|
50 'chevron' => array( |
|
51 'next' => '»', |
|
52 'previous' => '«', |
|
53 ), |
|
54 ); |
36 |
55 |
37 // If a custom label is provided, make this a link. |
56 // If a custom label is provided, make this a link. |
38 // `$label` is used to prepend the provided label, if we want to show the page title as well. |
57 // `$label` is used to prepend the provided label, if we want to show the page title as well. |
39 if ( isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ) { |
58 if ( isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ) { |
40 $label = "{$attributes['label']}"; |
59 $label = "{$attributes['label']}"; |
69 ); |
88 ); |
70 } |
89 } |
71 } |
90 } |
72 } |
91 } |
73 |
92 |
74 // The dynamic portion of the function name, `$navigation_type`, |
93 // Display arrows. |
75 // refers to the type of adjacency, 'next' or 'previous'. |
94 if ( isset( $attributes['arrow'] ) && 'none' !== $attributes['arrow'] && isset( $arrow_map[ $attributes['arrow'] ] ) ) { |
|
95 $arrow = $arrow_map[ $attributes['arrow'] ][ $navigation_type ]; |
|
96 |
|
97 if ( 'next' === $navigation_type ) { |
|
98 $format = '%link<span class="wp-block-post-navigation-link__arrow-next is-arrow-' . $attributes['arrow'] . '" aria-hidden="true">' . $arrow . '</span>'; |
|
99 } else { |
|
100 $format = '<span class="wp-block-post-navigation-link__arrow-previous is-arrow-' . $attributes['arrow'] . '" aria-hidden="true">' . $arrow . '</span>%link'; |
|
101 } |
|
102 } |
|
103 |
|
104 /* |
|
105 * The dynamic portion of the function name, `$navigation_type`, |
|
106 * Refers to the type of adjacency, 'next' or 'previous'. |
|
107 * |
|
108 * @see https://developer.wordpress.org/reference/functions/get_previous_post_link/ |
|
109 * @see https://developer.wordpress.org/reference/functions/get_next_post_link/ |
|
110 */ |
76 $get_link_function = "get_{$navigation_type}_post_link"; |
111 $get_link_function = "get_{$navigation_type}_post_link"; |
77 $content = $get_link_function( $format, $link ); |
112 |
|
113 if ( ! empty( $attributes['taxonomy'] ) ) { |
|
114 $content = $get_link_function( $format, $link, true, '', $attributes['taxonomy'] ); |
|
115 } else { |
|
116 $content = $get_link_function( $format, $link ); |
|
117 } |
|
118 |
78 return sprintf( |
119 return sprintf( |
79 '<div %1$s>%2$s</div>', |
120 '<div %1$s>%2$s</div>', |
80 $wrapper_attributes, |
121 $wrapper_attributes, |
81 $content |
122 $content |
82 ); |
123 ); |
83 } |
124 } |
84 |
125 |
85 /** |
126 /** |
86 * Registers the `core/post-navigation-link` block on the server. |
127 * Registers the `core/post-navigation-link` block on the server. |
|
128 * |
|
129 * @since 5.9.0 |
87 */ |
130 */ |
88 function register_block_core_post_navigation_link() { |
131 function register_block_core_post_navigation_link() { |
89 register_block_type_from_metadata( |
132 register_block_type_from_metadata( |
90 __DIR__ . '/post-navigation-link', |
133 __DIR__ . '/post-navigation-link', |
91 array( |
134 array( |