comment-edit-link.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/comment-edit-link` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/comment-edit-link` 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. *
  14. * @return string Return the post comment's date.
  15. */
  16. function render_block_core_comment_edit_link( $attributes, $content, $block ) {
  17. if ( ! isset( $block->context['commentId'] ) || ! current_user_can( 'edit_comment', $block->context['commentId'] ) ) {
  18. return '';
  19. }
  20. $edit_comment_link = get_edit_comment_link( $block->context['commentId'] );
  21. $link_atts = '';
  22. if ( ! empty( $attributes['linkTarget'] ) ) {
  23. $link_atts .= sprintf( 'target="%s"', esc_attr( $attributes['linkTarget'] ) );
  24. }
  25. $classes = '';
  26. if ( isset( $attributes['textAlign'] ) ) {
  27. $classes .= 'has-text-align-' . $attributes['textAlign'];
  28. }
  29. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
  30. return sprintf(
  31. '<div %1$s><a href="%2$s" %3$s>%4$s</a></div>',
  32. $wrapper_attributes,
  33. esc_url( $edit_comment_link ),
  34. $link_atts,
  35. esc_html__( 'Edit' )
  36. );
  37. }
  38. /**
  39. * Registers the `core/comment-edit-link` block on the server.
  40. */
  41. function register_block_core_comment_edit_link() {
  42. register_block_type_from_metadata(
  43. __DIR__ . '/comment-edit-link',
  44. array(
  45. 'render_callback' => 'render_block_core_comment_edit_link',
  46. )
  47. );
  48. }
  49. add_action( 'init', 'register_block_core_comment_edit_link' );