[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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 = array( 'comment-respond' ); // See comment further below.
  26      if ( isset( $attributes['textAlign'] ) ) {
  27          $classes[] = 'has-text-align-' . $attributes['textAlign'];
  28      }
  29      if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
  30          $classes[] = 'has-link-color';
  31      }
  32      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
  33  
  34      add_filter( 'comment_form_defaults', 'post_comments_form_block_form_defaults' );
  35  
  36      ob_start();
  37      comment_form( array(), $block->context['postId'] );
  38      $form = ob_get_clean();
  39  
  40      remove_filter( 'comment_form_defaults', 'post_comments_form_block_form_defaults' );
  41  
  42      // We use the outermost wrapping `<div />` returned by `comment_form()`
  43      // which is identified by its default classname `comment-respond` to inject
  44      // our wrapper attributes. This way, it is guaranteed that all styling applied
  45      // to the block is carried along when the comment form is moved to the location
  46      // of the 'Reply' link that the user clicked by Core's `comment-reply.js` script.
  47      $form = str_replace( 'class="comment-respond"', $wrapper_attributes, $form );
  48  
  49      // Enqueue the comment-reply script.
  50      wp_enqueue_script( 'comment-reply' );
  51  
  52      return $form;
  53  }
  54  
  55  /**
  56   * Registers the `core/post-comments-form` block on the server.
  57   */
  58  function register_block_core_post_comments_form() {
  59      register_block_type_from_metadata(
  60          __DIR__ . '/post-comments-form',
  61          array(
  62              'render_callback' => 'render_block_core_post_comments_form',
  63          )
  64      );
  65  }
  66  add_action( 'init', 'register_block_core_post_comments_form' );
  67  
  68  /**
  69   * Use the button block classes for the form-submit button.
  70   *
  71   * @param array $fields The default comment form arguments.
  72   *
  73   * @return array Returns the modified fields.
  74   */
  75  function post_comments_form_block_form_defaults( $fields ) {
  76      if ( wp_is_block_theme() ) {
  77          $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" />';
  78          $fields['submit_field']  = '<p class="form-submit wp-block-button">%1$s %2$s</p>';
  79      }
  80  
  81      return $fields;
  82  }


Generated : Fri Mar 29 08:20:02 2024 Cross-referenced by PHPXref