PluginProbe
Gutenberg / 10.5.3
Gutenberg v10.5.3
24.0.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 All 403 releases
gutenberg / build / block-library / blocks / post-navigation-link.php

post-navigation-link.php in Gutenberg 10.5.3, at build/block-library/blocks/post-navigation-link.php

73 lines 2.5 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/post-navigation-link` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Renders the `core/post-navigation-link` block on the server.
10 *
11 * @param array $attributes Block attributes.
12 * @param string $content Block default content.
13 *
14 * @return string Returns the next or previous post link that is adjacent to the current post.
15 */
16 function gutenberg_render_block_core_post_navigation_link( $attributes, $content ) {
17 if ( ! is_singular() ) {
18 return '';
19 }
20
21 // Get the nagigation type to show the proper link. Available options are `next|previous`.
22 $navigation_type = isset( $attributes['type'] ) ? $attributes['type'] : 'next';
23 // Allow only `next` and `previous` in `$navigation_type`.
24 if ( ! in_array( $navigation_type, array( 'next', 'previous' ), true ) ) {
25 return '';
26 }
27 $classes = "post-navigation-link-$navigation_type";
28 if ( isset( $attributes['textAlign'] ) ) {
29 $classes .= " has-text-align-{$attributes['textAlign']}";
30 }
31 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
32 // Set default values.
33 $format = '%link';
34 $link = 'next' === $navigation_type ? _x( 'Next', 'label for next post link', 'gutenberg' ) : _x( 'Previous', 'label for previous post link', 'gutenberg' );
35 $label = '';
36 // If a custom label is provided, make this a link.
37 // `$label` is used to prepend the provided label, if we want to show the page title as well.
38 if ( isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ) {
39 $label = "{$attributes['label']}";
40 $link = $label;
41 }
42
43 // If we want to also show the page title, make the page title a link and prepend the label.
44 if ( isset( $attributes['showTitle'] ) && $attributes['showTitle'] ) {
45 if ( $label ) {
46 $format = "$label %link";
47 }
48 $link = '%title';
49 }
50 // The dynamic portion of the function name, `$navigation_type`,
51 // refers to the type of adjacency, 'next' or 'previous'.
52 $get_link_function = "get_{$navigation_type}_post_link";
53 $content = $get_link_function( $format, $link );
54 return sprintf(
55 '<div %1$s>%2$s</div>',
56 $wrapper_attributes,
57 $content
58 );
59 }
60
61 /**
62 * Registers the `core/post-navigation-link` block on the server.
63 */
64 function gutenberg_register_block_core_post_navigation_link() {
65 register_block_type_from_metadata(
66 __DIR__ . '/post-navigation-link',
67 array(
68 'render_callback' => 'gutenberg_render_block_core_post_navigation_link',
69 )
70 );
71 }
72 add_action( 'init', 'gutenberg_register_block_core_post_navigation_link', 20 );
73