PluginProbe
Gutenberg / 18.9.0
Gutenberg v18.9.0
24.0.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 All 403 releases
gutenberg / build / block-library / blocks / media-text.php

media-text.php in Gutenberg 18.9.0, at build/block-library/blocks/media-text.php

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