PluginProbe
Gutenberg / 14.7.0
Gutenberg v14.7.0
24.0.0 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 All 403 releases
gutenberg / build / block-library / blocks / comments.php

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

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