PluginProbe
Gutenberg / 23.7.2
Gutenberg v23.7.2
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 / lib / block-supports / block-visibility.php

block-visibility.php in Gutenberg 23.7.2, at lib/block-supports/block-visibility.php

134 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 * Block visibility block support flag.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Render nothing if the block is hidden, or add viewport visibility styles.
10 *
11 * @param string $block_content Rendered block content.
12 * @param array $block Block object.
13 * @return string Filtered block content.
14 */
15 function gutenberg_render_block_visibility_support( $block_content, $block ) {
16 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
17
18 if ( ! $block_type ) {
19 return $block_content;
20 }
21
22 $block_visibility = $block['attrs']['metadata']['blockVisibility'] ?? null;
23
24 // Hide the block whenever the value is boolean false, regardless of the
25 // block's current visibility support. This prevents blocks that previously
26 // supported visibility from unintentionally appearing on the front end
27 // after their support was disabled.
28 if ( false === $block_visibility ) {
29 return '';
30 }
31
32 if ( ! block_has_support( $block_type, 'visibility', true ) ) {
33 return $block_content;
34 }
35
36 if ( is_array( $block_visibility ) && ! empty( $block_visibility ) ) {
37 // Get viewport configuration from nested structure.
38 $viewport_config = $block_visibility['viewport'] ?? null;
39
40 // If no viewport config, return unchanged.
41 if ( ! is_array( $viewport_config ) || empty( $viewport_config ) ) {
42 return $block_content;
43 }
44
45 $viewport_settings = gutenberg_get_global_settings( array( 'viewport' ) );
46 $viewport_media_queries = WP_Theme_JSON_Gutenberg::get_viewport_media_queries(
47 $viewport_settings,
48 array(
49 'include_desktop' => true,
50 )
51 );
52
53 /*
54 * Viewport media queries are keyed by style-state names (`@mobile`,
55 * `@tablet`, and `@desktop`). Block visibility metadata and generated
56 * classes use plain viewport names, so map the keys at this boundary.
57 */
58 $block_visibility_media_queries = array();
59 foreach ( $viewport_media_queries as $viewport_state => $media_query ) {
60 $block_visibility_media_queries[ ltrim( $viewport_state, '@' ) ] = $media_query;
61 }
62 $viewport_media_queries = $block_visibility_media_queries;
63
64 $hidden_on = array();
65
66 // Collect which viewport the block is hidden on (only known viewport sizes).
67 foreach ( $viewport_config as $viewport_config_size => $is_visible ) {
68 if ( false === $is_visible && isset( $viewport_media_queries[ $viewport_config_size ] ) ) {
69 $hidden_on[] = $viewport_config_size;
70 }
71 }
72
73 // If no viewport sizes have visibility set to false, return unchanged.
74 if ( empty( $hidden_on ) ) {
75 return $block_content;
76 }
77
78 // Maintain consistent order of viewport sizes for class name generation.
79 sort( $hidden_on );
80
81 $css_rules = array();
82 $class_names = array();
83
84 foreach ( $hidden_on as $hidden_viewport_size ) {
85 /*
86 * If these values ever become user-defined,
87 * they should be sanitized and kebab-cased.
88 */
89 $visibility_class = 'wp-block-hidden-' . $hidden_viewport_size;
90 $class_names[] = $visibility_class;
91 $css_rules[] = array(
92 'selector' => '.' . $visibility_class,
93 'declarations' => array(
94 'display' => 'none !important',
95 ),
96 'rules_group' => $viewport_media_queries[ $hidden_viewport_size ],
97 );
98 }
99
100 gutenberg_style_engine_get_stylesheet_from_css_rules(
101 $css_rules,
102 array(
103 'context' => 'block-supports',
104 'prettify' => false,
105 )
106 );
107
108 if ( ! empty( $block_content ) ) {
109 $processor = new WP_HTML_Tag_Processor( $block_content );
110 if ( $processor->next_tag() ) {
111 $processor->add_class( implode( ' ', $class_names ) );
112
113 /*
114 * Set all IMG tags to be `fetchpriority=auto` so that wp_get_loading_optimization_attributes() won't add
115 * `fetchpriority=high` or increment the media count to affect whether subsequent IMG tags get `loading=lazy`.
116 */
117 do {
118 if ( 'IMG' === $processor->get_tag() ) {
119 $processor->set_attribute( 'fetchpriority', 'auto' );
120 }
121 } while ( $processor->next_tag() );
122 $block_content = $processor->get_updated_html();
123 }
124 }
125 }
126
127 return $block_content;
128 }
129
130 if ( function_exists( 'wp_render_block_visibility_support' ) ) {
131 remove_filter( 'render_block', 'wp_render_block_visibility_support' );
132 }
133 add_filter( 'render_block', 'gutenberg_render_block_visibility_support', 10, 2 );
134