Sin descripción

query-title.php 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/query-title` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/query-title` block on the server.
  9. * For now it only supports Archive title,
  10. * using queried object information
  11. *
  12. * @param array $attributes Block attributes.
  13. *
  14. * @return string Returns the query title based on the queried object.
  15. */
  16. function render_block_core_query_title( $attributes ) {
  17. $type = isset( $attributes['type'] ) ? $attributes['type'] : null;
  18. $is_archive = is_archive();
  19. if ( ! $type || ( 'archive' === $type && ! $is_archive ) ) {
  20. return '';
  21. }
  22. $title = '';
  23. if ( $is_archive ) {
  24. $title = get_the_archive_title();
  25. }
  26. $tag_name = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h1';
  27. $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
  28. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
  29. return sprintf(
  30. '<%1$s %2$s>%3$s</%1$s>',
  31. $tag_name,
  32. $wrapper_attributes,
  33. $title
  34. );
  35. }
  36. /**
  37. * Registers the `core/query-title` block on the server.
  38. */
  39. function register_block_core_query_title() {
  40. register_block_type_from_metadata(
  41. __DIR__ . '/query-title',
  42. array(
  43. 'render_callback' => 'render_block_core_query_title',
  44. )
  45. );
  46. }
  47. add_action( 'init', 'register_block_core_query_title' );