PluginProbe
Gutenberg / 22.9.0
Gutenberg v22.9.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 7.4.0 All 402 releases
gutenberg / build / scripts / block-library / footnotes.php

footnotes.php in Gutenberg 22.9.0, at build/scripts/block-library/footnotes.php

144 lines 3.8 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/footnotes` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Renders the `core/footnotes` block on the server.
10 *
11 * @since 6.3.0
12 *
13 * @param array $attributes Block attributes.
14 * @param string $content Block default content.
15 * @param WP_Block $block Block instance.
16 *
17 * @return string Returns the HTML representing the footnotes.
18 */
19 function gutenberg_render_block_core_footnotes( $attributes, $content, $block ) {
20 // Bail out early if the post ID is not set for some reason.
21 if ( empty( $block->context['postId'] ) ) {
22 return '';
23 }
24
25 if ( post_password_required( $block->context['postId'] ) ) {
26 return '';
27 }
28
29 $footnotes = get_post_meta( $block->context['postId'], 'footnotes', true );
30
31 if ( ! $footnotes ) {
32 return '';
33 }
34
35 $footnotes = json_decode( $footnotes, true );
36
37 if ( ! is_array( $footnotes ) || count( $footnotes ) === 0 ) {
38 return '';
39 }
40
41 $wrapper_attributes = get_block_wrapper_attributes();
42 $footnote_index = 1;
43
44 $block_content = '';
45
46 foreach ( $footnotes as $footnote ) {
47 // Translators: %d: Integer representing the number of return links on the page.
48 $aria_label = sprintf( __( 'Jump to footnote reference %1$d' ), $footnote_index );
49 $block_content .= sprintf(
50 '<li id="%1$s">%2$s <a href="#%1$s-link" aria-label="%3$s">↩︎</a></li>',
51 esc_attr( $footnote['id'] ),
52 wp_kses_post( $footnote['content'] ),
53 esc_attr( $aria_label )
54 );
55 ++$footnote_index;
56 }
57
58 return sprintf(
59 '<ol %1$s>%2$s</ol>',
60 $wrapper_attributes,
61 $block_content
62 );
63 }
64
65 /**
66 * Registers the `core/footnotes` block on the server.
67 *
68 * @since 6.3.0
69 */
70 function gutenberg_register_block_core_footnotes() {
71 register_block_type_from_metadata(
72 __DIR__ . '/footnotes',
73 array(
74 'render_callback' => 'gutenberg_render_block_core_footnotes',
75 )
76 );
77 }
78 add_action( 'init', 'gutenberg_register_block_core_footnotes', 20 );
79
80
81 /**
82 * Registers the footnotes meta field required for footnotes to work.
83 *
84 * @since 6.5.0
85 */
86 function gutenberg_register_block_core_footnotes_post_meta() {
87 $post_types = get_post_types( array( 'show_in_rest' => true ) );
88 foreach ( $post_types as $post_type ) {
89 // Only register the meta field if the post type supports the editor, custom fields, and revisions.
90 if (
91 post_type_supports( $post_type, 'editor' ) &&
92 post_type_supports( $post_type, 'custom-fields' ) &&
93 post_type_supports( $post_type, 'revisions' )
94 ) {
95 register_post_meta(
96 $post_type,
97 'footnotes',
98 array(
99 'show_in_rest' => true,
100 'single' => true,
101 'type' => 'string',
102 'revisions_enabled' => true,
103 )
104 );
105 }
106 }
107 }
108
109 /*
110 * Most post types are registered at priority 10, so use priority 20 here in
111 * order to catch them.
112 */
113 add_action( 'init', 'gutenberg_register_block_core_footnotes_post_meta', 20 );
114
115 /**
116 * Adds the footnotes field to the revisions display.
117 *
118 * @since 6.3.0
119 *
120 * @param array $fields The revision fields.
121 * @return array The revision fields.
122 */
123 function gutenberg_add_footnotes_to_revision( $fields ) {
124 $fields['footnotes'] = __( 'Footnotes' );
125 return $fields;
126 }
127 add_filter( '_wp_post_revision_fields', 'gutenberg_add_footnotes_to_revision' );
128
129 /**
130 * Gets the footnotes field from the revision for the revisions screen.
131 *
132 * @since 6.3.0
133 *
134 * @param string $revision_field The field value, but $revision->$field
135 * (footnotes) does not exist.
136 * @param string $field The field name, in this case "footnotes".
137 * @param object $revision The revision object to compare against.
138 * @return string The field value.
139 */
140 function gutenberg_get_footnotes_from_revision( $revision_field, $field, $revision ) {
141 return get_metadata( 'post', $revision->ID, $field, true );
142 }
143 add_filter( '_wp_post_revision_field_footnotes', 'gutenberg_get_footnotes_from_revision', 10, 3 );
144