PluginProbe
Gutenberg / 16.5.1
Gutenberg v16.5.1
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / block-library / blocks / comments.php

comments.php in Gutenberg 16.5.1, at build/block-library/blocks/comments.php

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