PluginProbe
Gutenberg / 12.7.2
Gutenberg v12.7.2
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 / lib / compat / experimental / blocks.php

blocks.php in Gutenberg 12.7.2, at lib/compat/experimental/blocks.php

163 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Temporary compatibility shims for features present in Gutenberg.
4 *
5 * @package gutenberg
6 */
7
8 if ( ! function_exists( 'build_comment_query_vars_from_block' ) ) {
9 /**
10 * Helper function that constructs a comment query vars array from the passed block properties.
11 *
12 * It's used with the Comment Query Loop inner blocks.
13 *
14 * @param WP_Block $block Block instance.
15 *
16 * @return array Returns the comment query parameters to use with the WP_Comment_Query constructor.
17 */
18 function build_comment_query_vars_from_block( $block ) {
19
20 $comment_args = array(
21 'orderby' => 'comment_date_gmt',
22 'order' => 'ASC',
23 'status' => 'approve',
24 'no_found_rows' => false,
25 'update_comment_meta_cache' => false, // We lazy-load comment meta for performance.
26 );
27
28 if ( ! empty( $block->context['postId'] ) ) {
29 $comment_args['post_id'] = (int) $block->context['postId'];
30 }
31
32 if ( get_option( 'thread_comments' ) ) {
33 $comment_args['hierarchical'] = 'threaded';
34 } else {
35 $comment_args['hierarchical'] = false;
36 }
37
38 $inherit = ! empty( $block->context['comments/inherit'] );
39
40 $per_page = (int) get_option( 'comments_per_page' );
41 $query_per_page = (int) get_query_var( 'comments_per_page' );
42 if ( 0 !== $query_per_page ) {
43 $per_page = $query_per_page;
44 }
45 $block_per_page = ! empty( $block->context['comments/perPage'] ) ? (int) $block->context['comments/perPage'] : 0;
46 if ( ! $inherit && 0 !== $block_per_page ) {
47 $per_page = $block_per_page;
48 }
49
50 $default_page = get_option( 'default_comments_page' );
51 if ( ! $inherit && ! empty( $block->context['comments/defaultPage'] ) ) {
52 $default_page = $block->context['comments/defaultPage'];
53 }
54
55 if ( $per_page > 0 ) {
56 $comment_args['number'] = $per_page;
57
58 $page = (int) get_query_var( 'cpage' );
59 if ( $page ) {
60 $comment_args['paged'] = $page;
61 } elseif ( 'oldest' === $default_page ) {
62 $comment_args['paged'] = 1;
63 } elseif ( 'newest' === $default_page ) {
64 $comment_args['paged'] = ( new WP_Comment_Query( $comment_args ) )->max_num_pages;
65 }
66 }
67
68 return $comment_args;
69 }
70 }
71
72 if ( ! function_exists( 'get_comments_pagination_arrow' ) ) {
73 /**
74 * Helper function that returns the proper pagination arrow html for
75 * `CommentsPaginationNext` and `CommentsPaginationPrevious` blocks based
76 * on the provided `paginationArrow` from `CommentsPagination` context.
77 *
78 * It's used in CommentsPaginationNext and CommentsPaginationPrevious blocks.
79 *
80 * @param WP_Block $block Block instance.
81 * @param string $pagination_type Type of the arrow we will be rendering. Default 'next'. Accepts 'next' or 'previous'.
82 *
83 * @return string|null Returns the constructed WP_Query arguments.
84 */
85 function get_comments_pagination_arrow( $block, $pagination_type = 'next' ) {
86 $arrow_map = array(
87 'none' => '',
88 'arrow' => array(
89 'next' => '',
90 'previous' => '',
91 ),
92 'chevron' => array(
93 'next' => '»',
94 'previous' => '«',
95 ),
96 );
97 if ( ! empty( $block->context['comments/paginationArrow'] ) && ! empty( $arrow_map[ $block->context['comments/paginationArrow'] ][ $pagination_type ] ) ) {
98 $arrow_attribute = $block->context['comments/paginationArrow'];
99 $arrow = $arrow_map[ $block->context['comments/paginationArrow'] ][ $pagination_type ];
100 $arrow_classes = "wp-block-comments-pagination-$pagination_type-arrow is-arrow-$arrow_attribute";
101 return "<span class='$arrow_classes'>$arrow</span>";
102 }
103 return null;
104 }
105 }
106
107 if ( ! function_exists( 'extend_block_editor_settings_with_discussion_settings' ) ) {
108 /**
109 * Workaround for getting discussion settings as block editor settings
110 * so any user can access to them without needing to be an admin.
111 *
112 * @param array $settings Default editor settings.
113 *
114 * @return array Filtered editor settings.
115 */
116 function extend_block_editor_settings_with_discussion_settings( $settings ) {
117
118 $settings['__experimentalDiscussionSettings'] = array(
119 'commentOrder' => get_option( 'comment_order' ),
120 'commentsPerPage' => get_option( 'comments_per_page' ),
121 'defaultCommentsPage' => get_option( 'default_comments_page' ),
122 'pageComments' => get_option( 'page_comments' ),
123 'threadComments' => get_option( 'thread_comments' ),
124 'threadCommentsDepth' => get_option( 'thread_comments_depth' ),
125 'avatarURL' => get_avatar_url(
126 '',
127 array(
128 'size' => 96,
129 'force_default' => true,
130 'default' => get_option( 'avatar_default' ),
131 )
132 ),
133 );
134
135 return $settings;
136 }
137 }
138 add_filter( 'block_editor_settings_all', 'extend_block_editor_settings_with_discussion_settings' );
139
140 if ( ! function_exists( 'gutenberg_rest_comment_set_children_as_embeddable' ) ) {
141 /**
142 * Mark the `children` attr of comments as embeddable so they can be included in
143 * REST API responses without additional requests.
144 *
145 * @return void
146 */
147 function gutenberg_rest_comment_set_children_as_embeddable() {
148 add_filter(
149 'rest_prepare_comment',
150 function ( $response ) {
151 $links = $response->get_links();
152 if ( isset( $links['children'] ) ) {
153 $href = $links['children'][0]['href'];
154 $response->remove_link( 'children', $href );
155 $response->add_link( 'children', $href, array( 'embeddable' => true ) );
156 }
157 return $response;
158 }
159 );
160 }
161 }
162 add_action( 'rest_api_init', 'gutenberg_rest_comment_set_children_as_embeddable' );
163