Ei kuvausta

file.php 897B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/file` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * When the `core/file` block is rendering, check if we need to enqueue the `'wp-block-file-view` script.
  9. *
  10. * @param array $attributes The block attributes.
  11. * @param array $content The block content.
  12. *
  13. * @return string Returns the block content.
  14. */
  15. function render_block_core_file( $attributes, $content ) {
  16. $should_load_view_script = ! empty( $attributes['displayPreview'] ) && ! wp_script_is( 'wp-block-file-view' );
  17. if ( $should_load_view_script ) {
  18. wp_enqueue_script( 'wp-block-file-view' );
  19. }
  20. return $content;
  21. }
  22. /**
  23. * Registers the `core/file` block on server.
  24. */
  25. function register_block_core_file() {
  26. register_block_type_from_metadata(
  27. __DIR__ . '/file',
  28. array(
  29. 'render_callback' => 'render_block_core_file',
  30. )
  31. );
  32. }
  33. add_action( 'init', 'register_block_core_file' );