Skip to content

Commit ce22393

Browse files
committed
Introduce Token Map: An optimized static translation class.
This patch introduces a new class: `WP_Token_Map`, designed for efficient lookup and translation of static mappings between string keys or tokens, and string replacements (for example, HTML character references). The Token Map imposes certain restrictions on the byte length of the lookup tokens and their replacements, but is a highly-optimized data structure for mappings with a very high number of tokens. Developed in WordPress/wordpress-develop#5373 Discussed in https://core.trac.wordpress.org/ticket/60698 Fixes #60698. Props: dmsnell, gziolo, jonsurrell, jorbin. Built from https://develop.svn.wordpress.org/trunk@58188 git-svn-id: https://core.svn.wordpress.org/trunk@57651 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent 7f2a0bf commit ce22393

6 files changed

Lines changed: 2259 additions & 1 deletion

File tree

wp-includes/blocks/list.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Adds the wp-block-list class to the rendered list block.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
/**
9+
* Adds the wp-block-list class to the rendered list block.
10+
* Ensures that pre-existing list blocks use the class name on the front.
11+
* For example, <ol> is transformed to <ol class="wp-block-list">.
12+
*
13+
* @since 6.6.0
14+
*
15+
* @see https://github.com/WordPress/gutenberg/issues/12420
16+
*
17+
* @param array $attributes Attributes of the block being rendered.
18+
* @param string $content Content of the block being rendered.
19+
*
20+
* @return string The content of the block being rendered.
21+
*/
22+
function block_core_list_render( $attributes, $content ) {
23+
if ( ! $content ) {
24+
return $content;
25+
}
26+
27+
$processor = new WP_HTML_Tag_Processor( $content );
28+
29+
$list_tags = array( 'OL', 'UL' );
30+
while ( $processor->next_tag() ) {
31+
if ( in_array( $processor->get_tag(), $list_tags, true ) ) {
32+
$processor->add_class( 'wp-block-list' );
33+
break;
34+
}
35+
}
36+
37+
return $processor->get_updated_html();
38+
}
39+
40+
/**
41+
* Registers the `core/list` block on server.
42+
*
43+
* @since 6.6.0
44+
*/
45+
function register_block_core_list() {
46+
register_block_type_from_metadata(
47+
__DIR__ . '/list',
48+
array(
49+
'render_callback' => 'block_core_list_render',
50+
)
51+
);
52+
}
53+
54+
add_action( 'init', 'register_block_core_list' );

wp-includes/blocks/media-text.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Server-side rendering of the `core/media-text` block.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
/**
9+
* Renders the `core/media-text` block on server.
10+
*
11+
* @since 6.6.0
12+
*
13+
* @param array $attributes The block attributes.
14+
* @param string $content The block rendered content.
15+
*
16+
* @return string Returns the Media & Text block markup, if useFeaturedImage is true.
17+
*/
18+
function render_block_core_media_text( $attributes, $content ) {
19+
if ( false === $attributes['useFeaturedImage'] ) {
20+
return $content;
21+
}
22+
23+
if ( in_the_loop() ) {
24+
update_post_thumbnail_cache();
25+
}
26+
27+
$current_featured_image = get_the_post_thumbnail_url();
28+
if ( ! $current_featured_image ) {
29+
return $content;
30+
}
31+
32+
$image_tag = '<figure class="wp-block-media-text__media"><img>';
33+
$content = preg_replace( '/<figure\s+class="wp-block-media-text__media">/', $image_tag, $content );
34+
35+
$processor = new WP_HTML_Tag_Processor( $content );
36+
if ( isset( $attributes['imageFill'] ) && $attributes['imageFill'] ) {
37+
$position = '50% 50%';
38+
if ( isset( $attributes['focalPoint'] ) ) {
39+
$position = round( $attributes['focalPoint']['x'] * 100 ) . '% ' . round( $attributes['focalPoint']['y'] * 100 ) . '%';
40+
}
41+
$processor->next_tag( 'figure' );
42+
$processor->set_attribute( 'style', 'background-image:url(' . esc_url( $current_featured_image ) . ');background-position:' . $position . ';' );
43+
}
44+
$processor->next_tag( 'img' );
45+
$media_size_slug = 'full';
46+
if ( isset( $attributes['mediaSizeSlug'] ) ) {
47+
$media_size_slug = $attributes['mediaSizeSlug'];
48+
}
49+
$processor->set_attribute( 'src', esc_url( $current_featured_image ) );
50+
$processor->set_attribute( 'class', 'wp-image-' . get_post_thumbnail_id() . ' size-' . $media_size_slug );
51+
$processor->set_attribute( 'alt', trim( strip_tags( get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ) ) ) );
52+
53+
$content = $processor->get_updated_html();
54+
55+
return $content;
56+
}
57+
58+
/**
59+
* Registers the `core/media-text` block renderer on server.
60+
*
61+
* @since 6.6.0
62+
*/
63+
function register_block_core_media_text() {
64+
register_block_type_from_metadata(
65+
__DIR__ . '/media-text',
66+
array(
67+
'render_callback' => 'render_block_core_media_text',
68+
)
69+
);
70+
}
71+
add_action( 'init', 'register_block_core_media_text' );

0 commit comments

Comments
 (0)