post-comments-form.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/post-comments-form` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/post-comments-form` 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 filtered post comments form for the current post.
  14. */
  15. function render_block_core_post_comments_form( $attributes, $content, $block ) {
  16. if ( ! isset( $block->context['postId'] ) ) {
  17. return '';
  18. }
  19. if ( post_password_required( $block->context['postId'] ) ) {
  20. return;
  21. }
  22. $classes = 'comment-respond'; // See comment further below.
  23. if ( isset( $attributes['textAlign'] ) ) {
  24. $classes .= ' has-text-align-' . $attributes['textAlign'];
  25. }
  26. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
  27. add_filter( 'comment_form_defaults', 'post_comments_form_block_form_defaults' );
  28. ob_start();
  29. comment_form( array(), $block->context['postId'] );
  30. $form = ob_get_clean();
  31. remove_filter( 'comment_form_defaults', 'post_comments_form_block_form_defaults' );
  32. // We use the outermost wrapping `<div />` returned by `comment_form()`
  33. // which is identified by its default classname `comment-respond` to inject
  34. // our wrapper attributes. This way, it is guaranteed that all styling applied
  35. // to the block is carried along when the comment form is moved to the location
  36. // of the 'Reply' link that the user clicked by Core's `comment-reply.js` script.
  37. $form = str_replace( 'class="comment-respond"', $wrapper_attributes, $form );
  38. // Enqueue the comment-reply script.
  39. wp_enqueue_script( 'comment-reply' );
  40. return $form;
  41. }
  42. /**
  43. * Registers the `core/post-comments-form` block on the server.
  44. */
  45. function register_block_core_post_comments_form() {
  46. register_block_type_from_metadata(
  47. __DIR__ . '/post-comments-form',
  48. array(
  49. 'render_callback' => 'render_block_core_post_comments_form',
  50. )
  51. );
  52. }
  53. add_action( 'init', 'register_block_core_post_comments_form' );
  54. /**
  55. * Use the button block classes for the form-submit button.
  56. *
  57. * @param array $fields The default comment form arguments.
  58. *
  59. * @return array Returns the modified fields.
  60. */
  61. function post_comments_form_block_form_defaults( $fields ) {
  62. if ( wp_is_block_theme() ) {
  63. $fields['submit_button'] = '<input name="%1$s" type="submit" id="%2$s" class="wp-block-button__link ' . wp_theme_get_element_class_name( 'button' ) . '" value="%4$s" />';
  64. $fields['submit_field'] = '<p class="form-submit wp-block-button">%1$s %2$s</p>';
  65. }
  66. return $fields;
  67. }