post-navigation-link.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/post-navigation-link` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/post-navigation-link` block on the server.
  9. *
  10. * @param array $attributes Block attributes.
  11. * @param string $content Block default content.
  12. *
  13. * @return string Returns the next or previous post link that is adjacent to the current post.
  14. */
  15. function render_block_core_post_navigation_link( $attributes, $content ) {
  16. if ( ! is_singular() ) {
  17. return '';
  18. }
  19. // Get the navigation type to show the proper link. Available options are `next|previous`.
  20. $navigation_type = isset( $attributes['type'] ) ? $attributes['type'] : 'next';
  21. // Allow only `next` and `previous` in `$navigation_type`.
  22. if ( ! in_array( $navigation_type, array( 'next', 'previous' ), true ) ) {
  23. return '';
  24. }
  25. $classes = "post-navigation-link-$navigation_type";
  26. if ( isset( $attributes['textAlign'] ) ) {
  27. $classes .= " has-text-align-{$attributes['textAlign']}";
  28. }
  29. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
  30. // Set default values.
  31. $format = '%link';
  32. $link = 'next' === $navigation_type ? _x( 'Next', 'label for next post link' ) : _x( 'Previous', 'label for previous post link' );
  33. $label = '';
  34. // If a custom label is provided, make this a link.
  35. // `$label` is used to prepend the provided label, if we want to show the page title as well.
  36. if ( isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ) {
  37. $label = "{$attributes['label']}";
  38. $link = $label;
  39. }
  40. // If we want to also show the page title, make the page title a link and prepend the label.
  41. if ( isset( $attributes['showTitle'] ) && $attributes['showTitle'] ) {
  42. /*
  43. * If the label link option is not enabled but there is a custom label,
  44. * display the custom label as text before the linked title.
  45. */
  46. if ( ! $attributes['linkLabel'] ) {
  47. if ( $label ) {
  48. $format = '<span class="post-navigation-link__label">' . wp_kses_post( $label ) . '</span> %link';
  49. }
  50. $link = '%title';
  51. } elseif ( isset( $attributes['linkLabel'] ) && $attributes['linkLabel'] ) {
  52. // If the label link option is enabled and there is a custom label, display it before the title.
  53. if ( $label ) {
  54. $link = '<span class="post-navigation-link__label">' . wp_kses_post( $label ) . '</span> <span class="post-navigation-link__title">%title</span>';
  55. } else {
  56. /*
  57. * If the label link option is enabled and there is no custom label,
  58. * add a colon between the label and the post title.
  59. */
  60. $label = 'next' === $navigation_type ? _x( 'Next:', 'label before the title of the next post' ) : _x( 'Previous:', 'label before the title of the previous post' );
  61. $link = sprintf(
  62. '<span class="post-navigation-link__label">%1$s</span> <span class="post-navigation-link__title">%2$s</span>',
  63. wp_kses_post( $label ),
  64. '%title'
  65. );
  66. }
  67. }
  68. }
  69. // The dynamic portion of the function name, `$navigation_type`,
  70. // refers to the type of adjacency, 'next' or 'previous'.
  71. $get_link_function = "get_{$navigation_type}_post_link";
  72. $content = $get_link_function( $format, $link );
  73. return sprintf(
  74. '<div %1$s>%2$s</div>',
  75. $wrapper_attributes,
  76. $content
  77. );
  78. }
  79. /**
  80. * Registers the `core/post-navigation-link` block on the server.
  81. */
  82. function register_block_core_post_navigation_link() {
  83. register_block_type_from_metadata(
  84. __DIR__ . '/post-navigation-link',
  85. array(
  86. 'render_callback' => 'render_block_core_post_navigation_link',
  87. )
  88. );
  89. }
  90. add_action( 'init', 'register_block_core_post_navigation_link' );