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


Generated : Sat Dec 21 08:20:01 2024 Cross-referenced by PHPXref