Açıklama Yok

post-featured-image.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/post-featured-image` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/post-featured-image` 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. * @return string Returns the featured image for the current post.
  14. */
  15. function render_block_core_post_featured_image( $attributes, $content, $block ) {
  16. if ( ! isset( $block->context['postId'] ) ) {
  17. return '';
  18. }
  19. $post_ID = $block->context['postId'];
  20. $size_slug = isset( $attributes['sizeSlug'] ) ? $attributes['sizeSlug'] : 'post-thumbnail';
  21. $post_title = trim( strip_tags( get_the_title( $post_ID ) ) );
  22. $featured_image = get_the_post_thumbnail( $post_ID, $size_slug, array( 'alt' => $post_title ) );
  23. if ( ! $featured_image ) {
  24. return '';
  25. }
  26. $wrapper_attributes = get_block_wrapper_attributes();
  27. if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
  28. $featured_image = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $featured_image );
  29. }
  30. $has_width = ! empty( $attributes['width'] );
  31. $has_height = ! empty( $attributes['height'] );
  32. if ( ! $has_height && ! $has_width ) {
  33. return "<figure $wrapper_attributes>$featured_image</figure>";
  34. }
  35. if ( $has_width ) {
  36. $wrapper_attributes = get_block_wrapper_attributes( array( 'style' => "width:{$attributes['width']};" ) );
  37. }
  38. if ( $has_height ) {
  39. $image_styles = "height:{$attributes['height']};";
  40. if ( ! empty( $attributes['scale'] ) ) {
  41. $image_styles .= "object-fit:{$attributes['scale']};";
  42. }
  43. $featured_image = str_replace( 'src=', 'style="' . esc_attr( $image_styles ) . '" src=', $featured_image );
  44. }
  45. return "<figure $wrapper_attributes>$featured_image</figure>";
  46. }
  47. /**
  48. * Registers the `core/post-featured-image` block on the server.
  49. */
  50. function register_block_core_post_featured_image() {
  51. register_block_type_from_metadata(
  52. __DIR__ . '/post-featured-image',
  53. array(
  54. 'render_callback' => 'render_block_core_post_featured_image',
  55. )
  56. );
  57. }
  58. add_action( 'init', 'register_block_core_post_featured_image' );