author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:52:52 +0200 | |
changeset 22 | 8c2e4d02f4ef |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
19 | 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 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
11 |
* @since 5.9.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
12 |
* |
19 | 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 = isset( $attributes['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 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
33 |
$wrapper_attributes = get_block_wrapper_attributes( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
34 |
array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
35 |
'class' => $classes, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
36 |
) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
37 |
); |
19 | 38 |
// Set default values. |
39 |
$format = '%link'; |
|
40 |
$link = 'next' === $navigation_type ? _x( 'Next', 'label for next post link' ) : _x( 'Previous', 'label for previous post link' ); |
|
41 |
$label = ''; |
|
42 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
43 |
// Only use hardcoded values here, otherwise we need to add escaping where these values are used. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
44 |
$arrow_map = array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
45 |
'none' => '', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
46 |
'arrow' => array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
47 |
'next' => '→', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
48 |
'previous' => '←', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
49 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
50 |
'chevron' => array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
51 |
'next' => '»', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
52 |
'previous' => '«', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
53 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
54 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
55 |
|
19 | 56 |
// If a custom label is provided, make this a link. |
57 |
// `$label` is used to prepend the provided label, if we want to show the page title as well. |
|
58 |
if ( isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ) { |
|
59 |
$label = "{$attributes['label']}"; |
|
60 |
$link = $label; |
|
61 |
} |
|
62 |
||
63 |
// If we want to also show the page title, make the page title a link and prepend the label. |
|
64 |
if ( isset( $attributes['showTitle'] ) && $attributes['showTitle'] ) { |
|
65 |
/* |
|
66 |
* If the label link option is not enabled but there is a custom label, |
|
67 |
* display the custom label as text before the linked title. |
|
68 |
*/ |
|
69 |
if ( ! $attributes['linkLabel'] ) { |
|
70 |
if ( $label ) { |
|
71 |
$format = '<span class="post-navigation-link__label">' . wp_kses_post( $label ) . '</span> %link'; |
|
72 |
} |
|
73 |
$link = '%title'; |
|
74 |
} elseif ( isset( $attributes['linkLabel'] ) && $attributes['linkLabel'] ) { |
|
75 |
// If the label link option is enabled and there is a custom label, display it before the title. |
|
76 |
if ( $label ) { |
|
77 |
$link = '<span class="post-navigation-link__label">' . wp_kses_post( $label ) . '</span> <span class="post-navigation-link__title">%title</span>'; |
|
78 |
} else { |
|
79 |
/* |
|
80 |
* If the label link option is enabled and there is no custom label, |
|
81 |
* add a colon between the label and the post title. |
|
82 |
*/ |
|
83 |
$label = 'next' === $navigation_type ? _x( 'Next:', 'label before the title of the next post' ) : _x( 'Previous:', 'label before the title of the previous post' ); |
|
84 |
$link = sprintf( |
|
85 |
'<span class="post-navigation-link__label">%1$s</span> <span class="post-navigation-link__title">%2$s</span>', |
|
86 |
wp_kses_post( $label ), |
|
87 |
'%title' |
|
88 |
); |
|
89 |
} |
|
90 |
} |
|
91 |
} |
|
92 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
93 |
// Display arrows. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
94 |
if ( isset( $attributes['arrow'] ) && 'none' !== $attributes['arrow'] && isset( $arrow_map[ $attributes['arrow'] ] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
95 |
$arrow = $arrow_map[ $attributes['arrow'] ][ $navigation_type ]; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
96 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
97 |
if ( 'next' === $navigation_type ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
98 |
$format = '%link<span class="wp-block-post-navigation-link__arrow-next is-arrow-' . $attributes['arrow'] . '" aria-hidden="true">' . $arrow . '</span>'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
99 |
} else { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
100 |
$format = '<span class="wp-block-post-navigation-link__arrow-previous is-arrow-' . $attributes['arrow'] . '" aria-hidden="true">' . $arrow . '</span>%link'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
101 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
102 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
103 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
104 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
105 |
* The dynamic portion of the function name, `$navigation_type`, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
106 |
* Refers to the type of adjacency, 'next' or 'previous'. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
107 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
108 |
* @see https://developer.wordpress.org/reference/functions/get_previous_post_link/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
109 |
* @see https://developer.wordpress.org/reference/functions/get_next_post_link/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
110 |
*/ |
19 | 111 |
$get_link_function = "get_{$navigation_type}_post_link"; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
112 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
113 |
if ( ! empty( $attributes['taxonomy'] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
114 |
$content = $get_link_function( $format, $link, true, '', $attributes['taxonomy'] ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
115 |
} else { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
116 |
$content = $get_link_function( $format, $link ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
117 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
118 |
|
19 | 119 |
return sprintf( |
120 |
'<div %1$s>%2$s</div>', |
|
121 |
$wrapper_attributes, |
|
122 |
$content |
|
123 |
); |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Registers the `core/post-navigation-link` block on the server. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
128 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
129 |
* @since 5.9.0 |
19 | 130 |
*/ |
131 |
function register_block_core_post_navigation_link() { |
|
132 |
register_block_type_from_metadata( |
|
133 |
__DIR__ . '/post-navigation-link', |
|
134 |
array( |
|
135 |
'render_callback' => 'render_block_core_post_navigation_link', |
|
136 |
) |
|
137 |
); |
|
138 |
} |
|
139 |
add_action( 'init', 'register_block_core_post_navigation_link' ); |