| @@ -14,26 +14,18 @@ | ||
| 14 | 14 | */ |
| 15 | 15 | function gutenberg_render_block_visibility_support( $block_content, $block ) { |
| 16 | 16 | $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); |
| 17 | 17 | |
| 18 | - if ( ! $block_type ) { | |
| 18 | + if ( ! $block_type || ! block_has_support( $block_type, 'visibility', true ) ) { | |
| 19 | 19 | return $block_content; |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | $block_visibility = $block['attrs']['metadata']['blockVisibility'] ?? null; |
| 23 | 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 | 24 | if ( false === $block_visibility ) { |
| 29 | 25 | return ''; |
| 30 | 26 | } |
| 31 | 27 | |
| 32 | - if ( ! block_has_support( $block_type, 'visibility', true ) ) { | |
| 33 | - return $block_content; | |
| 34 | - } | |
| 35 | - | |
| 36 | 28 | if ( is_array( $block_visibility ) && ! empty( $block_visibility ) ) { |
| 37 | 29 | // Get viewport configuration from nested structure. |
| 38 | 30 | $viewport_config = $block_visibility['viewport'] ?? null; |
| 39 | 31 | |
| @@ -41,26 +33,62 @@ | ||
| 41 | 33 | if ( ! is_array( $viewport_config ) || empty( $viewport_config ) ) { |
| 42 | 34 | return $block_content; |
| 43 | 35 | } |
| 44 | 36 | |
| 45 | - $viewport_settings = gutenberg_get_global_settings( array( 'viewport' ) ); | |
| 46 | - $viewport_media_queries = WP_Theme_JSON_Gutenberg::get_viewport_media_queries( | |
| 47 | - $viewport_settings, | |
| 37 | + /* | |
| 38 | + * Viewport size definitions are in several places in WordPress packages. | |
| 39 | + * The following are taken from: https://github.com/WordPress/gutenberg/blob/trunk/packages/base-styles/_breakpoints.scss | |
| 40 | + * The array is in a future, potential JSON format, and will be centralized | |
| 41 | + * as the feature is developed. | |
| 42 | + * | |
| 43 | + * Viewport sizes as array items are defined sequentially. The first item's size is the max value. | |
| 44 | + * Each subsequent item starts after the previous size (using > operator), and its size is the max. | |
| 45 | + * The last item starts after the previous size (using > operator), and it has no max. | |
| 46 | + */ | |
| 47 | + $viewport_sizes = array( | |
| 48 | 48 | array( |
| 49 | - 'include_desktop' => true, | |
| 50 | - ) | |
| 49 | + 'name' => 'Mobile', | |
| 50 | + 'slug' => 'mobile', | |
| 51 | + 'size' => '480px', | |
| 52 | + ), | |
| 53 | + array( | |
| 54 | + 'name' => 'Tablet', | |
| 55 | + 'slug' => 'tablet', | |
| 56 | + 'size' => '782px', | |
| 57 | + ), | |
| 58 | + array( | |
| 59 | + 'name' => 'Desktop', | |
| 60 | + 'slug' => 'desktop', | |
| 61 | + /* | |
| 62 | + * Note: the last item in the $viewport_sizes array does not technically require a 'size' key, | |
| 63 | + * as the last item's media query is calculated using `width > previous size`. | |
| 64 | + * The last item is present for validating the attribute values, and in order to indicate | |
| 65 | + * that this is the final viewport size, and to calculate the previous media query accordingly. | |
| 66 | + */ | |
| 67 | + ), | |
| 51 | 68 | ); |
| 52 | 69 | |
| 53 | 70 | /* |
| 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. | |
| 71 | + * Build media queries from viewport size definitions using the CSS range syntax. | |
| 72 | + * Could be absorbed into the style engine, | |
| 73 | + * as well as classname building, and declaration of the display property, if required. | |
| 57 | 74 | */ |
| 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; | |
| 75 | + $viewport_media_queries = array(); | |
| 76 | + $previous_size = null; | |
| 77 | + foreach ( $viewport_sizes as $index => $viewport_size ) { | |
| 78 | + // First item: width <= size. | |
| 79 | + if ( 0 === $index ) { | |
| 80 | + $viewport_media_queries[ $viewport_size['slug'] ] = "@media (width <= {$viewport_size['size']})"; | |
| 81 | + } elseif ( count( $viewport_sizes ) - 1 === $index && $previous_size ) { | |
| 82 | + // Last item: width > previous size. | |
| 83 | + $viewport_media_queries[ $viewport_size['slug'] ] = "@media (width > $previous_size)"; | |
| 84 | + } else { | |
| 85 | + // Middle items: previous size < width <= size. | |
| 86 | + $viewport_media_queries[ $viewport_size['slug'] ] = "@media ({$previous_size} < width <= {$viewport_size['size']})"; | |
| 87 | + } | |
| 88 | + | |
| 89 | + $previous_size = $viewport_size['size'] ?? null; | |
| 61 | 90 | } |
| 62 | - $viewport_media_queries = $block_visibility_media_queries; | |
| 63 | 91 | |
| 64 | 92 | $hidden_on = array(); |
| 65 | 93 | |
| 66 | 94 | // Collect which viewport the block is hidden on (only known viewport sizes). |
| @@ -108,18 +136,8 @@ | ||
| 108 | 136 | if ( ! empty( $block_content ) ) { |
| 109 | 137 | $processor = new WP_HTML_Tag_Processor( $block_content ); |
| 110 | 138 | if ( $processor->next_tag() ) { |
| 111 | 139 | $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 | 140 | $block_content = $processor->get_updated_html(); |
| 123 | 141 | } |
| 124 | 142 | } |
| 125 | 143 | } |