wp/wp-includes/blocks/footnotes.php
changeset 21 48c4eec2b7e6
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
       
     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 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 			$footnote['id'],
       
    52 			$footnote['content'],
       
    53 			$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 register_block_core_footnotes() {
       
    71 	register_block_type_from_metadata(
       
    72 		__DIR__ . '/footnotes',
       
    73 		array(
       
    74 			'render_callback' => 'render_block_core_footnotes',
       
    75 		)
       
    76 	);
       
    77 }
       
    78 add_action( 'init', 'register_block_core_footnotes' );
       
    79 
       
    80 
       
    81 /**
       
    82  * Registers the footnotes meta field required for footnotes to work.
       
    83  *
       
    84  * @since 6.5.0
       
    85  */
       
    86 function 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  * Most post types are registered at priority 10, so use priority 20 here in
       
   110  * order to catch them.
       
   111 */
       
   112 add_action( 'init', 'register_block_core_footnotes_post_meta', 20 );
       
   113 
       
   114 /**
       
   115  * Adds the footnotes field to the revisions display.
       
   116  *
       
   117  * @since 6.3.0
       
   118  *
       
   119  * @param array $fields The revision fields.
       
   120  * @return array The revision fields.
       
   121  */
       
   122 function wp_add_footnotes_to_revision( $fields ) {
       
   123 	$fields['footnotes'] = __( 'Footnotes' );
       
   124 	return $fields;
       
   125 }
       
   126 add_filter( '_wp_post_revision_fields', 'wp_add_footnotes_to_revision' );
       
   127 
       
   128 /**
       
   129  * Gets the footnotes field from the revision for the revisions screen.
       
   130  *
       
   131  * @since 6.3.0
       
   132  *
       
   133  * @param string $revision_field The field value, but $revision->$field
       
   134  *                               (footnotes) does not exist.
       
   135  * @param string $field          The field name, in this case "footnotes".
       
   136  * @param object $revision       The revision object to compare against.
       
   137  * @return string The field value.
       
   138  */
       
   139 function wp_get_footnotes_from_revision( $revision_field, $field, $revision ) {
       
   140 	return get_metadata( 'post', $revision->ID, $field, true );
       
   141 }
       
   142 add_filter( '_wp_post_revision_field_footnotes', 'wp_get_footnotes_from_revision', 10, 3 );