Skip to content

Commit

Permalink
Introduce Token Map: An optimized static translation class.
Browse files Browse the repository at this point in the history
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: http://core.svn.wordpress.org/trunk@57651 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
dmsnell committed May 23, 2024
1 parent 1fbf5fa commit cbc1c95
Show file tree
Hide file tree
Showing 6 changed files with 2,259 additions and 1 deletion.
54 changes: 54 additions & 0 deletions wp-includes/blocks/list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Adds the wp-block-list class to the rendered list block.
*
* @package WordPress
*/

/**
* Adds the wp-block-list class to the rendered list block.
* Ensures that pre-existing list blocks use the class name on the front.
* For example, <ol> is transformed to <ol class="wp-block-list">.
*
* @since 6.6.0
*
* @see https://github.com/WordPress/gutenberg/issues/12420
*
* @param array $attributes Attributes of the block being rendered.
* @param string $content Content of the block being rendered.
*
* @return string The content of the block being rendered.
*/
function block_core_list_render( $attributes, $content ) {
if ( ! $content ) {
return $content;
}

$processor = new WP_HTML_Tag_Processor( $content );

$list_tags = array( 'OL', 'UL' );
while ( $processor->next_tag() ) {
if ( in_array( $processor->get_tag(), $list_tags, true ) ) {
$processor->add_class( 'wp-block-list' );
break;
}
}

return $processor->get_updated_html();
}

/**
* Registers the `core/list` block on server.
*
* @since 6.6.0
*/
function register_block_core_list() {
register_block_type_from_metadata(
__DIR__ . '/list',
array(
'render_callback' => 'block_core_list_render',
)
);
}

add_action( 'init', 'register_block_core_list' );
71 changes: 71 additions & 0 deletions wp-includes/blocks/media-text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Server-side rendering of the `core/media-text` block.
*
* @package WordPress
*/

/**
* Renders the `core/media-text` block on server.
*
* @since 6.6.0
*
* @param array $attributes The block attributes.
* @param string $content The block rendered content.
*
* @return string Returns the Media & Text block markup, if useFeaturedImage is true.
*/
function render_block_core_media_text( $attributes, $content ) {
if ( false === $attributes['useFeaturedImage'] ) {
return $content;
}

if ( in_the_loop() ) {
update_post_thumbnail_cache();
}

$current_featured_image = get_the_post_thumbnail_url();
if ( ! $current_featured_image ) {
return $content;
}

$image_tag = '<figure class="wp-block-media-text__media"><img>';
$content = preg_replace( '/<figure\s+class="wp-block-media-text__media">/', $image_tag, $content );

$processor = new WP_HTML_Tag_Processor( $content );
if ( isset( $attributes['imageFill'] ) && $attributes['imageFill'] ) {
$position = '50% 50%';
if ( isset( $attributes['focalPoint'] ) ) {
$position = round( $attributes['focalPoint']['x'] * 100 ) . '% ' . round( $attributes['focalPoint']['y'] * 100 ) . '%';
}
$processor->next_tag( 'figure' );
$processor->set_attribute( 'style', 'background-image:url(' . esc_url( $current_featured_image ) . ');background-position:' . $position . ';' );
}
$processor->next_tag( 'img' );
$media_size_slug = 'full';
if ( isset( $attributes['mediaSizeSlug'] ) ) {
$media_size_slug = $attributes['mediaSizeSlug'];
}
$processor->set_attribute( 'src', esc_url( $current_featured_image ) );
$processor->set_attribute( 'class', 'wp-image-' . get_post_thumbnail_id() . ' size-' . $media_size_slug );
$processor->set_attribute( 'alt', trim( strip_tags( get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ) ) ) );

$content = $processor->get_updated_html();

return $content;
}

/**
* Registers the `core/media-text` block renderer on server.
*
* @since 6.6.0
*/
function register_block_core_media_text() {
register_block_type_from_metadata(
__DIR__ . '/media-text',
array(
'render_callback' => 'render_block_core_media_text',
)
);
}
add_action( 'init', 'register_block_core_media_text' );
Loading

0 comments on commit cbc1c95

Please sign in to comment.