| @@ -14,18 +14,26 @@ | ||
| 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 || ! block_has_support( $block_type, 'visibility', true ) ) { | |
| 18 | + if ( ! $block_type ) { | |
| 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. | |
| 24 | 28 | if ( false === $block_visibility ) { |
| 25 | 29 | return ''; |
| 26 | 30 | } |
| 27 | 31 | |
| 32 | + if ( ! block_has_support( $block_type, 'visibility', true ) ) { | |
| 33 | + return $block_content; | |
| 34 | + } | |
| 35 | + | |
| 28 | 36 | if ( is_array( $block_visibility ) && ! empty( $block_visibility ) ) { |
| 29 | 37 | // Get viewport configuration from nested structure. |
| 30 | 38 | $viewport_config = $block_visibility['viewport'] ?? null; |
| 31 | 39 | |
| @@ -33,62 +41,26 @@ | ||
| 33 | 41 | if ( ! is_array( $viewport_config ) || empty( $viewport_config ) ) { |
| 34 | 42 | return $block_content; |
| 35 | 43 | } |
| 36 | 44 | |
| 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( | |
| 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 | 48 | array( |
| 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 | - ), | |
| 49 | + 'include_desktop' => true, | |
| 50 | + ) | |
| 68 | 51 | ); |
| 69 | 52 | |
| 70 | 53 | /* |
| 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. | |
| 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. | |
| 74 | 57 | */ |
| 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; | |
| 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; | |
| 90 | 61 | } |
| 62 | + $viewport_media_queries = $block_visibility_media_queries; | |
| 91 | 63 | |
| 92 | 64 | $hidden_on = array(); |
| 93 | 65 | |
| 94 | 66 | // Collect which viewport the block is hidden on (only known viewport sizes). |