Nessuna descrizione

post-title.php 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/post-title` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/post-title` block on the server.
  9. *
  10. * @param array $attributes Block attributes.
  11. * @param string $content Block default content.
  12. * @param WP_Block $block Block instance.
  13. *
  14. * @return string Returns the filtered post title for the current post wrapped inside "h1" tags.
  15. */
  16. function render_block_core_post_title( $attributes, $content, $block ) {
  17. if ( ! isset( $block->context['postId'] ) ) {
  18. return '';
  19. }
  20. $post_ID = $block->context['postId'];
  21. $tag_name = 'h2';
  22. $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
  23. if ( isset( $attributes['level'] ) ) {
  24. $tag_name = 0 === $attributes['level'] ? 'p' : 'h' . $attributes['level'];
  25. }
  26. $title = get_the_title( $post_ID );
  27. if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
  28. $title = sprintf( '<a href="%1s" target="%2s" rel="%3s">%4s</a>', get_the_permalink( $post_ID ), $attributes['linkTarget'], $attributes['rel'], $title );
  29. }
  30. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
  31. return sprintf(
  32. '<%1$s %2$s>%3$s</%1$s>',
  33. $tag_name,
  34. $wrapper_attributes,
  35. $title
  36. );
  37. }
  38. /**
  39. * Registers the `core/post-title` block on the server.
  40. */
  41. function register_block_core_post_title() {
  42. register_block_type_from_metadata(
  43. __DIR__ . '/post-title',
  44. array(
  45. 'render_callback' => 'render_block_core_post_title',
  46. )
  47. );
  48. }
  49. add_action( 'init', 'register_block_core_post_title' );