Нет описания

post-comments.php 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/post-comments` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/post-comments` 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 for the current post wrapped inside "p" tags.
  14. */
  15. function render_block_core_post_comments( $attributes, $content, $block ) {
  16. global $post;
  17. $post_id = $block->context['postId'];
  18. if ( ! isset( $post_id ) ) {
  19. return '';
  20. }
  21. $comment_args = array(
  22. 'post_id' => $post_id,
  23. 'count' => true,
  24. );
  25. // Return early if there are no comments and comments are closed.
  26. if ( ! comments_open( $post_id ) && get_comments( $comment_args ) === 0 ) {
  27. return '';
  28. }
  29. $post_before = $post;
  30. $post = get_post( $post_id );
  31. setup_postdata( $post );
  32. ob_start();
  33. // There's a deprecation warning generated by WP Core.
  34. // Ideally this deprecation is removed from Core.
  35. // In the meantime, this removes it from the output.
  36. add_filter( 'deprecated_file_trigger_error', '__return_false' );
  37. comments_template();
  38. remove_filter( 'deprecated_file_trigger_error', '__return_false' );
  39. $post = $post_before;
  40. $classes = '';
  41. if ( isset( $attributes['textAlign'] ) ) {
  42. $classes .= 'has-text-align-' . $attributes['textAlign'];
  43. }
  44. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
  45. $output = ob_get_clean();
  46. return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $output );
  47. }
  48. /**
  49. * Registers the `core/post-comments` block on the server.
  50. */
  51. function register_block_core_post_comments() {
  52. register_block_type_from_metadata(
  53. __DIR__ . '/post-comments',
  54. array(
  55. 'render_callback' => 'render_block_core_post_comments',
  56. )
  57. );
  58. }
  59. add_action( 'init', 'register_block_core_post_comments' );
  60. /**
  61. * Use the button block classes for the form-submit button.
  62. *
  63. * @param array $fields The default comment form arguments.
  64. *
  65. * @return array Returns the modified fields.
  66. */
  67. function post_comments_block_form_defaults( $fields ) {
  68. if ( wp_is_block_theme() ) {
  69. $fields['submit_button'] = '<input name="%1$s" type="submit" id="%2$s" class="%3$s wp-block-button__link" value="%4$s" />';
  70. $fields['submit_field'] = '<p class="form-submit wp-block-button">%1$s %2$s</p>';
  71. }
  72. return $fields;
  73. }
  74. add_filter( 'comment_form_defaults', 'post_comments_block_form_defaults' );