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 / media-text.php

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

132 lines 4.2 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/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 gutenberg_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 $has_media_on_right = 'right' === ( $attributes['mediaPosition'] ?? null );
33 $image_fill = (bool) ( $attributes['imageFill'] ?? false );
34 $focal_point = isset( $attributes['focalPoint'] ) ? round( $attributes['focalPoint']['x'] * 100 ) . '% ' . round( $attributes['focalPoint']['y'] * 100 ) . '%' : '50% 50%';
35 $unique_id = 'wp-block-media-text__media-' . wp_unique_id();
36
37 $block_tag_processor = new WP_HTML_Tag_Processor( $content );
38 $block_query = array(
39 'tag_name' => 'div',
40 'class_name' => 'wp-block-media-text',
41 );
42
43 while ( $block_tag_processor->next_tag( $block_query ) ) {
44 if ( $image_fill ) {
45 // The markup below does not work with the deprecated `is-image-fill` class.
46 $block_tag_processor->remove_class( 'is-image-fill' );
47 $block_tag_processor->add_class( 'is-image-fill-element' );
48 }
49 }
50
51 $content = $block_tag_processor->get_updated_html();
52
53 $media_tag_processor = new WP_HTML_Tag_Processor( $content );
54 $wrapping_figure_query = array(
55 'tag_name' => 'figure',
56 'class_name' => 'wp-block-media-text__media',
57 );
58
59 if ( $has_media_on_right ) {
60 // Loop through all the figure tags and set a bookmark on the last figure tag.
61 while ( $media_tag_processor->next_tag( $wrapping_figure_query ) ) {
62 $media_tag_processor->set_bookmark( 'last_figure' );
63 }
64 if ( $media_tag_processor->has_bookmark( 'last_figure' ) ) {
65 $media_tag_processor->seek( 'last_figure' );
66 // Insert a unique ID to identify the figure tag.
67 $media_tag_processor->set_attribute( 'id', $unique_id );
68 }
69 } else {
70 if ( $media_tag_processor->next_tag( $wrapping_figure_query ) ) {
71 // Insert a unique ID to identify the figure tag.
72 $media_tag_processor->set_attribute( 'id', $unique_id );
73 }
74 }
75
76 $content = $media_tag_processor->get_updated_html();
77
78 // Add the image tag inside the figure tag, and update the image attributes
79 // in order to display the featured image.
80 $media_size_slug = $attributes['mediaSizeSlug'] ?? 'full';
81 $image_tag = '<img class="wp-block-media-text__featured_image">';
82 $content = preg_replace(
83 '/(<figure\s+id="' . preg_quote( $unique_id, '/' ) . '"\s+class="wp-block-media-text__media"\s*>)/',
84 '$1' . $image_tag,
85 $content
86 );
87
88 $image_tag_processor = new WP_HTML_Tag_Processor( $content );
89 if ( $image_tag_processor->next_tag(
90 array(
91 'tag_name' => 'figure',
92 'id' => $unique_id,
93 )
94 ) ) {
95 // The ID is only used to ensure that the correct figure tag is selected,
96 // and can now be removed.
97 $image_tag_processor->remove_attribute( 'id' );
98 if ( $image_tag_processor->next_tag(
99 array(
100 'tag_name' => 'img',
101 'class_name' => 'wp-block-media-text__featured_image',
102 )
103 ) ) {
104 $image_tag_processor->set_attribute( 'src', esc_url( $current_featured_image ) );
105 $image_tag_processor->set_attribute( 'class', 'wp-image-' . get_post_thumbnail_id() . ' size-' . $media_size_slug );
106 $image_tag_processor->set_attribute( 'alt', trim( strip_tags( get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ) ) ) );
107 if ( $image_fill ) {
108 $image_tag_processor->set_attribute( 'style', 'object-position:' . $focal_point . ';' );
109 }
110
111 $content = $image_tag_processor->get_updated_html();
112 }
113 }
114
115 return $content;
116 }
117
118 /**
119 * Registers the `core/media-text` block renderer on server.
120 *
121 * @since 6.6.0
122 */
123 function gutenberg_register_block_core_media_text() {
124 register_block_type_from_metadata(
125 __DIR__ . '/media-text',
126 array(
127 'render_callback' => 'gutenberg_render_block_core_media_text',
128 )
129 );
130 }
131 add_action( 'init', 'gutenberg_register_block_core_media_text', 20 );
132