Keine Beschreibung

cover.php 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/cover` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/cover` block on server.
  9. *
  10. * @param array $attributes The block attributes.
  11. * @param array $content The block rendered content.
  12. *
  13. * @return string Returns the cover block markup, if useFeaturedImage is true.
  14. */
  15. function render_block_core_cover( $attributes, $content ) {
  16. if ( 'image' !== $attributes['backgroundType'] || false === $attributes['useFeaturedImage'] ) {
  17. return $content;
  18. }
  19. if ( ! ( $attributes['hasParallax'] || $attributes['isRepeated'] ) ) {
  20. $attr = array(
  21. 'class' => 'wp-block-cover__image-background',
  22. 'data-object-fit' => 'cover',
  23. );
  24. if ( isset( $attributes['focalPoint'] ) ) {
  25. $object_position = round( $attributes['focalPoint']['x'] * 100 ) . '%' . ' ' . round( $attributes['focalPoint']['y'] * 100 ) . '%';
  26. $attr['data-object-position'] = $object_position;
  27. $attr['style'] = 'object-position: ' . $object_position;
  28. }
  29. $image = get_the_post_thumbnail( null, 'post-thumbnail', $attr );
  30. $content = str_replace(
  31. '</span><div',
  32. '</span>' . $image . '<div',
  33. $content
  34. );
  35. } else {
  36. if ( in_the_loop() ) {
  37. update_post_thumbnail_cache();
  38. }
  39. $current_featured_image = get_the_post_thumbnail_url();
  40. $content = preg_replace(
  41. '/class=\".*?\"/',
  42. '${0} style="background-image:url(' . esc_url( $current_featured_image ) . ')"',
  43. $content,
  44. 1
  45. );
  46. }
  47. return $content;
  48. }
  49. /**
  50. * Registers the `core/cover` block renderer on server.
  51. */
  52. function register_block_core_cover() {
  53. register_block_type_from_metadata(
  54. __DIR__ . '/cover',
  55. array(
  56. 'render_callback' => 'render_block_core_cover',
  57. )
  58. );
  59. }
  60. add_action( 'init', 'register_block_core_cover' );