pattern.php 921 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/pattern` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Registers the `core/pattern` block on the server.
  9. *
  10. * @return void
  11. */
  12. function register_block_core_pattern() {
  13. register_block_type_from_metadata(
  14. __DIR__ . '/pattern',
  15. array(
  16. 'render_callback' => 'render_block_core_pattern',
  17. )
  18. );
  19. }
  20. /**
  21. * Renders the `core/pattern` block on the server.
  22. *
  23. * @param array $attributes Block attributes.
  24. *
  25. * @return string Returns the output of the pattern.
  26. */
  27. function render_block_core_pattern( $attributes ) {
  28. if ( empty( $attributes['slug'] ) ) {
  29. return '';
  30. }
  31. $slug = $attributes['slug'];
  32. $registry = WP_Block_Patterns_Registry::get_instance();
  33. if ( ! $registry->is_registered( $slug ) ) {
  34. return '';
  35. }
  36. $pattern = $registry->get_registered( $slug );
  37. return do_blocks( $pattern['content'] );
  38. }
  39. add_action( 'init', 'register_block_core_pattern' );