[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/blocks/ -> post-comments-form.php (source)

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


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1