comments.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/comments` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/comments` block on the server.
  9. *
  10. * This render callback is mainly for rendering a dynamic, legacy version of
  11. * this block (the old `core/post-comments`). It uses the `comments_template()`
  12. * function to generate the output, in the same way as classic PHP themes.
  13. *
  14. * As this callback will always run during SSR, first we need to check whether
  15. * the block is in legacy mode. If not, the HTML generated in the editor is
  16. * returned instead.
  17. *
  18. * @param array $attributes Block attributes.
  19. * @param string $content Block default content.
  20. * @param WP_Block $block Block instance.
  21. * @return string Returns the filtered post comments for the current post wrapped inside "p" tags.
  22. */
  23. function render_block_core_comments( $attributes, $content, $block ) {
  24. global $post;
  25. $post_id = $block->context['postId'];
  26. if ( ! isset( $post_id ) ) {
  27. return '';
  28. }
  29. $comment_args = array(
  30. 'post_id' => $post_id,
  31. 'count' => true,
  32. 'status' => 'approve',
  33. );
  34. // Return early if there are no comments and comments are closed.
  35. if ( ! comments_open( $post_id ) && get_comments( $comment_args ) === 0 ) {
  36. return '';
  37. }
  38. // If this isn't the legacy block, we need to render the static version of this block.
  39. $is_legacy = 'core/post-comments' === $block->name || ! empty( $attributes['legacy'] );
  40. if ( ! $is_legacy ) {
  41. return $block->render( array( 'dynamic' => false ) );
  42. }
  43. $post_before = $post;
  44. $post = get_post( $post_id );
  45. setup_postdata( $post );
  46. ob_start();
  47. /*
  48. * There's a deprecation warning generated by WP Core.
  49. * Ideally this deprecation is removed from Core.
  50. * In the meantime, this removes it from the output.
  51. */
  52. add_filter( 'deprecated_file_trigger_error', '__return_false' );
  53. comments_template();
  54. remove_filter( 'deprecated_file_trigger_error', '__return_false' );
  55. $output = ob_get_clean();
  56. $post = $post_before;
  57. $classnames = array();
  58. // Adds the old class name for styles' backwards compatibility.
  59. if ( isset( $attributes['legacy'] ) ) {
  60. $classnames[] = 'wp-block-post-comments';
  61. }
  62. if ( isset( $attributes['textAlign'] ) ) {
  63. $classnames[] = 'has-text-align-' . $attributes['textAlign'];
  64. }
  65. $wrapper_attributes = get_block_wrapper_attributes(
  66. array( 'class' => implode( ' ', $classnames ) )
  67. );
  68. /*
  69. * Enqueues scripts and styles required only for the legacy version. That is
  70. * why they are not defined in `block.json`.
  71. */
  72. wp_enqueue_script( 'comment-reply' );
  73. enqueue_legacy_post_comments_block_styles( $block->name );
  74. return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $output );
  75. }
  76. /**
  77. * Registers the `core/comments` block on the server.
  78. */
  79. function register_block_core_comments() {
  80. register_block_type_from_metadata(
  81. __DIR__ . '/comments',
  82. array(
  83. 'render_callback' => 'render_block_core_comments',
  84. 'skip_inner_blocks' => true,
  85. )
  86. );
  87. }
  88. add_action( 'init', 'register_block_core_comments' );
  89. /**
  90. * Use the button block classes for the form-submit button.
  91. *
  92. * @param array $fields The default comment form arguments.
  93. *
  94. * @return array Returns the modified fields.
  95. */
  96. function comments_block_form_defaults( $fields ) {
  97. if ( wp_is_block_theme() ) {
  98. $fields['submit_button'] = '<input name="%1$s" type="submit" id="%2$s" class="%3$s wp-block-button__link ' . wp_theme_get_element_class_name( 'button' ) . '" value="%4$s" />';
  99. $fields['submit_field'] = '<p class="form-submit wp-block-button">%1$s %2$s</p>';
  100. }
  101. return $fields;
  102. }
  103. add_filter( 'comment_form_defaults', 'comments_block_form_defaults' );
  104. /**
  105. * Enqueues styles from the legacy `core/post-comments` block. These styles are
  106. * required only by the block's fallback.
  107. *
  108. * @param string $block_name Name of the new block type.
  109. */
  110. function enqueue_legacy_post_comments_block_styles( $block_name ) {
  111. static $are_styles_enqueued = false;
  112. if ( ! $are_styles_enqueued ) {
  113. $handles = array(
  114. 'wp-block-post-comments',
  115. 'wp-block-buttons',
  116. 'wp-block-button',
  117. );
  118. foreach ( $handles as $handle ) {
  119. wp_enqueue_block_style( $block_name, array( 'handle' => $handle ) );
  120. }
  121. $are_styles_enqueued = true;
  122. }
  123. }
  124. /**
  125. * Ensures backwards compatibility for any users running the Gutenberg plugin
  126. * who have used Post Comments before it was merged into Comments Query Loop.
  127. *
  128. * The same approach was followed when core/query-loop was renamed to
  129. * core/post-template.
  130. *
  131. * @see https://github.com/WordPress/gutenberg/pull/41807
  132. * @see https://github.com/WordPress/gutenberg/pull/32514
  133. */
  134. function register_legacy_post_comments_block() {
  135. $registry = WP_Block_Type_Registry::get_instance();
  136. /*
  137. * Remove the old `post-comments` block if it was already registered, as it
  138. * is about to be replaced by the type defined below.
  139. */
  140. if ( $registry->is_registered( 'core/post-comments' ) ) {
  141. unregister_block_type( 'core/post-comments' );
  142. }
  143. // Recreate the legacy block metadata.
  144. $metadata = array(
  145. 'name' => 'core/post-comments',
  146. 'category' => 'theme',
  147. 'attributes' => array(
  148. 'textAlign' => array(
  149. 'type' => 'string',
  150. ),
  151. ),
  152. 'uses_context' => array(
  153. 'postId',
  154. 'postType',
  155. ),
  156. 'supports' => array(
  157. 'html' => false,
  158. 'align' => array( 'wide', 'full' ),
  159. 'typography' => array(
  160. 'fontSize' => true,
  161. 'lineHeight' => true,
  162. '__experimentalFontStyle' => true,
  163. '__experimentalFontWeight' => true,
  164. '__experimentalLetterSpacing' => true,
  165. '__experimentalTextTransform' => true,
  166. '__experimentalDefaultControls' => array(
  167. 'fontSize' => true,
  168. ),
  169. ),
  170. 'color' => array(
  171. 'gradients' => true,
  172. 'link' => true,
  173. '__experimentalDefaultControls' => array(
  174. 'background' => true,
  175. 'text' => true,
  176. ),
  177. ),
  178. 'inserter' => false,
  179. ),
  180. 'style' => array(
  181. 'wp-block-post-comments',
  182. 'wp-block-buttons',
  183. 'wp-block-button',
  184. ),
  185. 'editorStyle' => 'wp-block-post-comments-editor',
  186. 'render_callback' => 'render_block_core_comments',
  187. 'skip_inner_blocks' => true,
  188. );
  189. /*
  190. * Filters the metadata object, the same way it's done inside
  191. * `register_block_type_from_metadata()`. This applies some default filters,
  192. * like `_wp_multiple_block_styles`, which is required in this case because
  193. * the block has multiple styles.
  194. */
  195. $metadata = apply_filters( 'block_type_metadata', $metadata );
  196. register_block_type( 'core/post-comments', $metadata );
  197. }
  198. add_action( 'init', 'register_legacy_post_comments_block', 21 );