Make WordPress Core

Changeset 62671


Ignore:
Timestamp:
07/09/2026 01:46:32 AM (4 months ago)
Author:
isabel_brison
Message:

Editor: allow configuring viewport values in theme.json.

Enables a viewport object with tablet and mobile in theme.json settings and applies its values to style state and block visibility media queries.

Props isabel_brison, ramonopoly.
Fixes #65596.

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-supports/block-style-variations.php

    r61564 r62671  
    155155        );
    156156
     157        // Ensure variation state styles know about any custom viewport breakpoints.
     158        if ( isset( $theme_json['settings']['viewport'] ) ) {
     159                $config['settings']['viewport'] = $theme_json['settings']['viewport'];
     160        }
     161
    157162        // Turn off filter that excludes block nodes. They are needed here for the variation's inner block types.
    158163        if ( ! is_admin() ) {
  • trunk/src/wp-includes/block-supports/block-visibility.php

    r62586 r62671  
    4545                        return $block_content;
    4646                }
    47                 /*
    48                  * Viewport size definitions are in several places in WordPress packages.
    49                  * The following are taken from: https://github.com/WordPress/gutenberg/blob/trunk/packages/base-styles/_breakpoints.scss
    50                  * The array is in a future, potential JSON format, and will be centralized
    51                  * as the feature is developed.
    52                  *
    53                  * Viewport sizes as array items are defined sequentially. The first item's size is the max value.
    54                  * Each subsequent item starts after the previous size (using > operator), and its size is the max.
    55                  * The last item starts after the previous size (using > operator), and it has no max.
    56                  */
    57                 $viewport_sizes = array(
     47                $viewport_settings      = wp_get_global_settings( array( 'viewport' ) );
     48                $viewport_media_queries = WP_Theme_JSON::get_viewport_media_queries(
     49                        $viewport_settings,
    5850                        array(
    59                                 'name' => 'Mobile',
    60                                 'slug' => 'mobile',
    61                                 'size' => '480px',
    62                         ),
    63                         array(
    64                                 'name' => 'Tablet',
    65                                 'slug' => 'tablet',
    66                                 'size' => '782px',
    67                         ),
    68                         array(
    69                                 'name' => 'Desktop',
    70                                 'slug' => 'desktop',
    71                                 /*
    72                                  * Note: the last item in the $viewport_sizes array does not technically require a 'size' key,
    73                                  * as the last item's media query is calculated using `width > previous size`.
    74                                  * The last item is present for validating the attribute values, and in order to indicate
    75                                  * that this is the final viewport size, and to calculate the previous media query accordingly.
    76                                  */
    77                         ),
     51                                'include_desktop' => true,
     52                        )
    7853                );
    7954
    8055                /*
    81                  * Build media queries from viewport size definitions using the CSS range syntax.
    82                  * Could be absorbed into the style engine,
    83                  * as well as classname building, and declaration of the display property, if required.
     56                 * Viewport media queries are keyed by style-state names (`@mobile`,
     57                 * `@tablet`, and `@desktop`). Block visibility metadata and generated
     58                 * classes use plain viewport names, so map the keys at this boundary.
    8459                 */
    85                 $viewport_media_queries = array();
    86                 $previous_size          = null;
    87                 foreach ( $viewport_sizes as $index => $viewport_size ) {
    88                         // First item: width <= size.
    89                         if ( 0 === $index ) {
    90                                 $viewport_media_queries[ $viewport_size['slug'] ] = "@media (width <= {$viewport_size['size']})";
    91                         } elseif ( count( $viewport_sizes ) - 1 === $index && $previous_size ) {
    92                                 // Last item: width > previous size.
    93                                 $viewport_media_queries[ $viewport_size['slug'] ] = "@media (width > $previous_size)";
    94                         } else {
    95                                 // Middle items: previous size < width <= size.
    96                                 $viewport_media_queries[ $viewport_size['slug'] ] = "@media ({$previous_size} < width <= {$viewport_size['size']})";
    97                         }
    98 
    99                         $previous_size = $viewport_size['size'] ?? null;
     60                $block_visibility_media_queries = array();
     61                foreach ( $viewport_media_queries as $viewport_state => $media_query ) {
     62                        $block_visibility_media_queries[ ltrim( $viewport_state, '@' ) ] = $media_query;
    10063                }
     64                $viewport_media_queries = $block_visibility_media_queries;
    10165
    10266                $hidden_on = array();
  • trunk/src/wp-includes/block-supports/layout.php

    r62579 r62671  
    956956        static $global_styles = null;
    957957
    958         $block_type            = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
    959         $block_supports_layout = block_has_support( $block_type, 'layout', false ) || block_has_support( $block_type, '__experimentalLayout', false );
    960         $style_attr            = $block['attrs']['style'] ?? array();
    961         $child_layout          = $style_attr['layout'] ?? null;
     958        $block_type               = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
     959        $block_supports_layout    = block_has_support( $block_type, 'layout', false ) || block_has_support( $block_type, '__experimentalLayout', false );
     960        $style_attr               = $block['attrs']['style'] ?? array();
     961        $global_settings          = wp_get_global_settings();
     962        $viewport_settings        = $global_settings['viewport'] ?? null;
     963        $responsive_media_queries = WP_Theme_JSON::get_viewport_media_queries( $viewport_settings );
     964        $child_layout             = $style_attr['layout'] ?? null;
    962965
    963966        /*
    … …  
    966969         */
    967970        $viewport_child_layouts = array();
    968         foreach ( WP_Theme_JSON::RESPONSIVE_BREAKPOINTS as $breakpoint => $media_query ) {
     971        foreach ( $responsive_media_queries as $breakpoint => $media_query ) {
    969972                $viewport_child = wp_get_layout_child_values( $style_attr[ $breakpoint ]['layout'] ?? null );
    970973
    … …  
    10761079        }
    10771080
    1078         $global_settings = wp_get_global_settings();
    10791081        $fallback_layout = $block_type->supports['layout']['default'] ?? array();
    10801082        if ( empty( $fallback_layout ) ) {
    … …  
    11841186                );
    11851187
    1186                 foreach ( array_keys( WP_Theme_JSON::RESPONSIVE_BREAKPOINTS ) as $breakpoint ) {
     1188                foreach ( array_keys( $responsive_media_queries ) as $breakpoint ) {
    11871189                        $viewport_style = $style_attr[ $breakpoint ] ?? null;
    11881190                        if ( ! is_array( $viewport_style ) ) {
    … …  
    12321234                 * selector as the base layout so they target the inner block wrapper.
    12331235                 */
    1234                 foreach ( WP_Theme_JSON::RESPONSIVE_BREAKPOINTS as $breakpoint => $media_query ) {
     1236                foreach ( $responsive_media_queries as $breakpoint => $media_query ) {
    12351237                        $viewport_style = $style_attr[ $breakpoint ] ?? null;
    12361238                        if ( ! is_array( $viewport_style ) ) {
  • trunk/src/wp-includes/block-supports/states.php

    r62559 r62671  
    542542        }
    543543
    544         $supported_pseudo_states = WP_Theme_JSON::VALID_BLOCK_PSEUDO_SELECTORS[ $block_name ] ?? array();
    545         $style                   = $block['attrs']['style'] ?? array();
    546         $css_rules               = array();
     544        $supported_pseudo_states  = WP_Theme_JSON::VALID_BLOCK_PSEUDO_SELECTORS[ $block_name ] ?? array();
     545        $style                    = $block['attrs']['style'] ?? array();
     546        $css_rules                = array();
     547        $viewport_settings        = wp_get_global_settings( array( 'viewport' ) );
     548        $responsive_media_queries = WP_Theme_JSON::get_viewport_media_queries( $viewport_settings );
    547549
    548550        foreach ( $supported_pseudo_states as $pseudo_state ) {
    … …  
    560562        }
    561563
    562         foreach ( WP_Theme_JSON::RESPONSIVE_BREAKPOINTS as $breakpoint => $media_query ) {
     564        foreach ( $responsive_media_queries as $breakpoint => $media_query ) {
    563565                if ( empty( $style[ $breakpoint ] ) || ! is_array( $style[ $breakpoint ] ) ) {
    564566                        continue;
  • trunk/src/wp-includes/class-wp-theme-json.php

    r62650 r62671  
    413413         *              Added support for `dimensions.width` and `dimensions.height`.
    414414         *              Added support for `typography.textIndent`.
     415         * @since 7.1.0 Added `viewport` property.
    415416         * @var array
    416417         */
    … …  
    502503                        'writingMode'      => null,
    503504                ),
     505                'viewport'                      => array(
     506                        'mobile' => null,
     507                        'tablet' => null,
     508                ),
    504509        );
    505510
    … …  
    669674
    670675        /**
    671          * Responsive breakpoint state keys and their corresponding CSS media queries.
    672          * These are available for all blocks and wrap their styles in the given media query.
    673          * Keep in sync with RESPONSIVE_BREAKPOINTS in packages/global-styles-engine/src/core/render.tsx.
     676         * Default viewport breakpoint sizes.
    674677         *
    675678         * @since 7.1.0
    676679         * @var array
    677680         */
    678         const RESPONSIVE_BREAKPOINTS = array(
    679                 '@mobile' => '@media (width <= 480px)',
    680                 '@tablet' => '@media (480px < width <= 782px)',
     681        const DEFAULT_VIEWPORT_BREAKPOINTS = array(
     682                'mobile' => '480px',
     683                'tablet' => '782px',
    681684        );
     685
     686        /**
     687         * Returns CSS media queries for responsive viewport style states.
     688         *
     689         * Breakpoint values are read from `settings.viewport`, sanitized, and
     690         * normalized before the media query strings are generated. By default, the
     691         * returned keys are the theme.json style-state names (`@mobile`, `@tablet`).
     692         * When `$options['include_desktop']` is truthy, `@desktop` is included.
     693         *
     694         * @since 7.1.0
     695         *
     696         * @param mixed $viewport_settings Viewport settings from theme.json.
     697         * @param array $options           {
     698         *     Optional. Options for generating media queries.
     699         *
     700         *     @type bool $include_desktop Whether to include the desktop media query. Default false.
     701         * }
     702         * @return array Responsive media queries.
     703         */
     704        public static function get_viewport_media_queries( $viewport_settings = null, $options = array() ) {
     705                $breakpoints = static::sanitize_viewport_settings( $viewport_settings );
     706
     707                $responsive_media_queries = array();
     708
     709                if ( isset( $breakpoints['mobile'] ) ) {
     710                        $responsive_media_queries['@mobile'] = "@media (width <= {$breakpoints['mobile']})";
     711                }
     712
     713                if ( isset( $breakpoints['tablet'] ) ) {
     714                        $responsive_media_queries['@tablet'] = isset( $breakpoints['mobile'] )
     715                                ? sprintf(
     716                                        '@media (%s < width <= %s)',
     717                                        $breakpoints['mobile'],
     718                                        $breakpoints['tablet']
     719                                )
     720                                : "@media (width <= {$breakpoints['tablet']})";
     721                }
     722
     723                if ( ! empty( $options['include_desktop'] ) ) {
     724                        if ( isset( $breakpoints['tablet'] ) ) {
     725                                $desktop_breakpoint = $breakpoints['tablet'];
     726                        } else {
     727                                $desktop_breakpoint = $breakpoints['mobile'];
     728                        }
     729
     730                        $responsive_media_queries['@desktop'] =
     731                                "@media (width > {$desktop_breakpoint})";
     732                }
     733
     734                return $responsive_media_queries;
     735        }
     736
     737        /**
     738         * Checks whether a viewport breakpoint value is a safe CSS length.
     739         *
     740         * Viewport breakpoints are limited to numeric `px`, `em`, and `rem` lengths.
     741         * CSS functions, percentages, and other units are rejected because breakpoint
     742         * values are interpolated into generated media queries.
     743         *
     744         * @since 7.1.0
     745         *
     746         * @param mixed $value Value to check.
     747         * @return bool Whether the value is valid.
     748         */
     749        private static function is_valid_viewport_breakpoint_size( $value ) {
     750                if ( ! is_string( $value ) ) {
     751                        return false;
     752                }
     753
     754                $value = trim( $value );
     755                if ( '' === $value ) {
     756                        return false;
     757                }
     758
     759                return 1 === preg_match( '/^(?:\d+|\d*\.\d+)(?:px|em|rem)$/', $value );
     760        }
     761
     762        /**
     763         * Converts a valid viewport breakpoint size to pixels for ordering checks.
     764         *
     765         * Generated media queries keep the original units. This method only
     766         * normalizes values so `mobile` and `tablet` can be compared safely. `em`
     767         * and `rem` lengths use a 16px base for comparison.
     768         *
     769         * @since 7.1.0
     770         *
     771         * @param mixed $value Viewport breakpoint size.
     772         * @return float|null Viewport breakpoint size in pixels, or null when invalid.
     773         */
     774        private static function get_viewport_breakpoint_value_in_pixels( $value ) {
     775                if ( ! static::is_valid_viewport_breakpoint_size( $value ) ) {
     776                        return null;
     777                }
     778
     779                $value = trim( $value );
     780                $unit  = substr( $value, -3 );
     781                if ( 'rem' === $unit ) {
     782                        $number = (float) substr( $value, 0, -3 );
     783                } else {
     784                        $unit   = substr( $value, -2 );
     785                        $number = (float) substr( $value, 0, -2 );
     786                }
     787
     788                /*
     789                 * Use the most common browser default font size as the base for em/rem
     790                 * media query conversions. This pixel value is only used to compare
     791                 * breakpoint order; generated media queries keep the original units.
     792                 */
     793                return 'px' === $unit ? $number : $number * 16;
     794        }
     795
     796        /**
     797         * Sanitizes and normalizes viewport breakpoint settings.
     798         *
     799         * Keeps only supported breakpoint keys, trims valid CSS lengths, and returns
     800         * the default breakpoints when no valid custom breakpoint is provided. When
     801         * only one breakpoint is valid, it remains keyed by its configured state and
     802         * uses a single max-width media query. When `tablet` is not larger than
     803         * `mobile`, it is removed.
     804         *
     805         * @since 7.1.0
     806         *
     807         * @param mixed $viewport_settings Viewport settings from theme.json.
     808         * @return array Sanitized viewport breakpoint settings.
     809         */
     810        private static function sanitize_viewport_settings( $viewport_settings ) {
     811                if ( ! is_array( $viewport_settings ) ) {
     812                        return static::DEFAULT_VIEWPORT_BREAKPOINTS;
     813                }
     814
     815                $breakpoints = array();
     816                foreach ( array_keys( static::DEFAULT_VIEWPORT_BREAKPOINTS ) as $breakpoint ) {
     817                        $value = $viewport_settings[ $breakpoint ] ?? null;
     818                        $px    = static::get_viewport_breakpoint_value_in_pixels( $value );
     819                        if ( null !== $px ) {
     820                                $breakpoints[ $breakpoint ] = array(
     821                                        'value' => trim( $value ),
     822                                        'px'    => $px,
     823                                );
     824                        }
     825                }
     826
     827                if ( empty( $breakpoints ) ) {
     828                        return static::DEFAULT_VIEWPORT_BREAKPOINTS;
     829                }
     830
     831                if ( 1 === count( $breakpoints ) ) {
     832                        $breakpoint = key( $breakpoints );
     833                        return array( $breakpoint => $breakpoints[ $breakpoint ]['value'] );
     834                }
     835
     836                $sanitized = array( 'mobile' => $breakpoints['mobile']['value'] );
     837
     838                if ( isset( $breakpoints['tablet'] ) && $breakpoints['mobile']['px'] < $breakpoints['tablet']['px'] ) {
     839                        $sanitized['tablet'] = $breakpoints['tablet']['value'];
     840                }
     841
     842                return $sanitized;
     843        }
    682844
    683845        /**
    … …  
    11191281
    11201282                // Build the schema based on valid block & element names.
    1121                 $schema                 = array();
    1122                 $schema_styles_elements = array();
     1283                $schema                   = array();
     1284                $schema_styles_elements   = array();
     1285                $responsive_media_queries = static::get_viewport_media_queries( $input['settings']['viewport'] ?? null );
    11231286
    11241287                /*
    … …  
    11401303
    11411304                        // Add responsive breakpoint states for elements.
    1142                         foreach ( array_keys( static::RESPONSIVE_BREAKPOINTS ) as $breakpoint_state ) {
     1305                        foreach ( array_keys( $responsive_media_queries ) as $breakpoint_state ) {
    11431306                                $schema_styles_elements[ $element ][ $breakpoint_state ] = $styles_non_top_level;
    11441307                        }
    … …  
    11591322                 */
    11601323                foreach ( $valid_block_names as $block ) {
    1161                         $schema_settings_blocks[ $block ]           = static::VALID_SETTINGS;
     1324                        $schema_settings_blocks[ $block ] = static::VALID_SETTINGS;
     1325                        unset( $schema_settings_blocks[ $block ]['viewport'] );
    11621326                        $schema_styles_blocks[ $block ]             = $styles_non_top_level;
    11631327                        $schema_styles_blocks[ $block ]['elements'] = $schema_styles_elements;
    11641328
    11651329                        // Add responsive breakpoint states for all blocks.
    1166                         foreach ( array_keys( static::RESPONSIVE_BREAKPOINTS ) as $breakpoint_state ) {
     1330                        foreach ( array_keys( $responsive_media_queries ) as $breakpoint_state ) {
    11671331                                $schema_styles_blocks[ $block ][ $breakpoint_state ]             = $styles_non_top_level;
    11681332                                $schema_styles_blocks[ $block ][ $breakpoint_state ]['elements'] = $schema_styles_elements;
    … …  
    12241388
    12251389                                        // Add responsive breakpoint states to block style variations.
    1226                                         foreach ( array_keys( static::RESPONSIVE_BREAKPOINTS ) as $breakpoint_state ) {
     1390                                        foreach ( array_keys( $responsive_media_queries ) as $breakpoint_state ) {
    12271391                                                $variation_schema[ $breakpoint_state ]             = $styles_non_top_level;
    12281392                                                $variation_schema[ $breakpoint_state ]['elements'] = $schema_styles_elements;
    … …  
    12691433
    12701434                        $result = static::remove_keys_not_in_schema( $input[ $subtree ], $schema[ $subtree ] );
     1435
     1436                        if ( 'settings' === $subtree && array_key_exists( 'viewport', $input[ $subtree ] ) ) {
     1437                                $result['viewport'] = static::sanitize_viewport_settings( $input[ $subtree ]['viewport'] );
     1438                        }
    12711439
    12721440                        if ( empty( $result ) ) {
    … …  
    31633331                }
    31643332
    3165                 $include_variations      = $options['include_block_style_variations'] ?? false;
    3166                 $include_node_paths_only = $options['include_node_paths_only'] ?? false;
     3333                $include_variations       = $options['include_block_style_variations'] ?? false;
     3334                $include_node_paths_only  = $options['include_node_paths_only'] ?? false;
     3335                $responsive_media_queries = static::get_viewport_media_queries( $theme_json['settings']['viewport'] ?? null );
    31673336
    31683337                // If only node paths are to be returned, skip selector assignment.
    … …  
    32313400                                // These are rendered immediately after the base block node so that
    32323401                                // the cascade order is: .block{} → @media{.block{}}
    3233                                 foreach ( array_keys( static::RESPONSIVE_BREAKPOINTS ) as $breakpoint ) {
     3402                                foreach ( array_keys( $responsive_media_queries ) as $breakpoint ) {
    32343403                                        if ( isset( $theme_json['styles']['blocks'][ $name ][ $breakpoint ] ) ) {
    32353404                                                $nodes[] = array(
    32363405                                                        'name'        => $name,
    32373406                                                        'path'        => array( 'styles', 'blocks', $name, $breakpoint ),
    3238                                                         'media_query' => static::RESPONSIVE_BREAKPOINTS[ $breakpoint ],
     3407                                                        'media_query' => $responsive_media_queries[ $breakpoint ],
    32393408                                                        'selector'    => $selector,
    32403409                                                        'selectors'   => $feature_selectors,
    … …  
    32513420                                                $has_pseudo            = isset( $theme_json['styles']['blocks'][ $name ][ $pseudo_selector ] );
    32523421                                                $has_responsive_pseudo = false;
    3253                                                 foreach ( array_keys( static::RESPONSIVE_BREAKPOINTS ) as $breakpoint ) {
     3422                                                foreach ( array_keys( $responsive_media_queries ) as $breakpoint ) {
    32543423                                                        if ( isset( $theme_json['styles']['blocks'][ $name ][ $breakpoint ][ $pseudo_selector ] ) ) {
    32553424                                                                $has_responsive_pseudo = true;
    … …  
    32963465                                                // this pseudo state, immediately after the default pseudo node.
    32973466                                                // Cascade order: .block:hover{} → @media{.block:hover{}}
    3298                                                 foreach ( array_keys( static::RESPONSIVE_BREAKPOINTS ) as $breakpoint ) {
     3467                                                foreach ( array_keys( $responsive_media_queries ) as $breakpoint ) {
    32993468                                                        if ( isset( $theme_json['styles']['blocks'][ $name ][ $breakpoint ][ $pseudo_selector ] ) ) {
    33003469                                                                $nodes[] = array(
    33013470                                                                        'name'        => $name,
    33023471                                                                        'path'        => array( 'styles', 'blocks', $name, $breakpoint, $pseudo_selector ),
    3303                                                                         'media_query' => static::RESPONSIVE_BREAKPOINTS[ $breakpoint ],
     3472                                                                        'media_query' => $responsive_media_queries[ $breakpoint ],
    33043473                                                                        'selector'    => static::append_to_selector( $selector, $pseudo_selector ),
    33053474                                                                        'selectors'   => $pseudo_feature_selectors,
    … …  
    33733542                                        // Responsive element nodes: one node per breakpoint that has
    33743543                                        // styles for this element. Cascade: a{} → @media{a{}}
    3375                                         foreach ( array_keys( static::RESPONSIVE_BREAKPOINTS ) as $breakpoint ) {
     3544                                        foreach ( array_keys( $responsive_media_queries ) as $breakpoint ) {
    33763545                                                if ( isset( $theme_json['styles']['blocks'][ $name ][ $breakpoint ]['elements'][ $element ] ) ) {
    33773546                                                        $nodes[] = array(
    33783547                                                                'path'        => array( 'styles', 'blocks', $name, $breakpoint, 'elements', $element ),
    33793548                                                                'selector'    => $element_selector,
    3380                                                                 'media_query' => static::RESPONSIVE_BREAKPOINTS[ $breakpoint ],
     3549                                                                'media_query' => $responsive_media_queries[ $breakpoint ],
    33813550                                                        );
    33823551                                                }
    … …  
    33893558                                                        $has_element_pseudo = isset( $theme_json['styles']['blocks'][ $name ]['elements'][ $element ][ $pseudo_selector ] );
    33903559                                                        if ( ! $has_element_pseudo ) {
    3391                                                                 foreach ( array_keys( static::RESPONSIVE_BREAKPOINTS ) as $bp ) {
     3560                                                                foreach ( array_keys( $responsive_media_queries ) as $bp ) {
    33923561                                                                        if ( isset( $theme_json['styles']['blocks'][ $name ][ $bp ]['elements'][ $element ][ $pseudo_selector ] ) ) {
    33933562                                                                                $has_element_pseudo = true;
    … …  
    34143583                                                                // that has this pseudo state for this element.
    34153584                                                                // Cascade: a:hover{} → @media{a:hover{}}
    3416                                                                 foreach ( array_keys( static::RESPONSIVE_BREAKPOINTS ) as $breakpoint ) {
     3585                                                                foreach ( array_keys( $responsive_media_queries ) as $breakpoint ) {
    34173586                                                                        if ( isset( $theme_json['styles']['blocks'][ $name ][ $breakpoint ]['elements'][ $element ][ $pseudo_selector ] ) ) {
    34183587                                                                                $nodes[] = array(
    34193588                                                                                        'path'        => array( 'styles', 'blocks', $name, $breakpoint, 'elements', $element ),
    34203589                                                                                        'selector'    => static::append_to_selector( $element_selector, $pseudo_selector ),
    3421                                                                                         'media_query' => static::RESPONSIVE_BREAKPOINTS[ $breakpoint ],
     3590                                                                                        'media_query' => $responsive_media_queries[ $breakpoint ],
    34223591                                                                                );
    34233592                                                                        }
    … …  
    34453614         */
    34463615        public function get_styles_for_block( $block_metadata ) {
    3447                 $node                 = _wp_array_get( $this->theme_json, $block_metadata['path'], array() );
    3448                 $use_root_padding     = isset( $this->theme_json['settings']['useRootPaddingAwareAlignments'] ) && true === $this->theme_json['settings']['useRootPaddingAwareAlignments'];
    3449                 $selector             = $block_metadata['selector'];
    3450                 $settings             = $this->theme_json['settings'] ?? array();
    3451                 $feature_declarations = static::get_feature_declarations_for_node( $block_metadata, $node );
    3452                 $is_root_selector     = static::ROOT_BLOCK_SELECTOR === $selector;
    3453                 $media_query          = $block_metadata['media_query'] ?? null;
     3616                $node                     = _wp_array_get( $this->theme_json, $block_metadata['path'], array() );
     3617                $use_root_padding         = isset( $this->theme_json['settings']['useRootPaddingAwareAlignments'] ) && true === $this->theme_json['settings']['useRootPaddingAwareAlignments'];
     3618                $selector                 = $block_metadata['selector'];
     3619                $settings                 = $this->theme_json['settings'] ?? array();
     3620                $feature_declarations     = static::get_feature_declarations_for_node( $block_metadata, $node );
     3621                $is_root_selector         = static::ROOT_BLOCK_SELECTOR === $selector;
     3622                $media_query              = $block_metadata['media_query'] ?? null;
     3623                $responsive_media_queries = static::get_viewport_media_queries( $settings['viewport'] ?? null );
    34543624
    34553625                // Update text indent selector for paragraph blocks based on the textIndent setting.
    … …  
    35183688                                $variation_responsive_pseudo_css = '';
    35193689
    3520                                 foreach ( array_keys( static::RESPONSIVE_BREAKPOINTS ) as $breakpoint ) {
     3690                                foreach ( array_keys( $responsive_media_queries ) as $breakpoint ) {
    35213691                                        if ( ! isset( $style_variation_node[ $breakpoint ] ) ) {
    35223692                                                continue;
    … …  
    35243694
    35253695                                        $breakpoint_node  = $style_variation_node[ $breakpoint ];
    3526                                         $breakpoint_media = static::RESPONSIVE_BREAKPOINTS[ $breakpoint ];
     3696                                        $breakpoint_media = $responsive_media_queries[ $breakpoint ];
    35273697                                        // Process feature-level declarations for this breakpoint.
    35283698                                        $breakpoint_feature_declarations = static::get_feature_declarations_for_node( $block_metadata, $breakpoint_node );
    … …  
    42884458                $theme_json = static::sanitize( $theme_json, $valid_block_names, $valid_element_names, $valid_variations );
    42894459
    4290                 $blocks_metadata = static::get_blocks_metadata();
    4291                 $style_options   = array( 'include_block_style_variations' => true ); // Allow variations data.
    4292                 $style_nodes     = static::get_style_nodes( $theme_json, $blocks_metadata, $style_options );
     4460                $blocks_metadata          = static::get_blocks_metadata();
     4461                $style_options            = array( 'include_block_style_variations' => true ); // Allow variations data.
     4462                $style_nodes              = static::get_style_nodes( $theme_json, $blocks_metadata, $style_options );
     4463                $responsive_media_queries = static::get_viewport_media_queries( $theme_json['settings']['viewport'] ?? null );
    42934464
    42944465                foreach ( $style_nodes as $metadata ) {
    … …  
    43284499
    43294500                        // Re-add and process responsive breakpoint styles.
    4330                         foreach ( array_keys( static::RESPONSIVE_BREAKPOINTS ) as $breakpoint ) {
     4501                        foreach ( array_keys( $responsive_media_queries ) as $breakpoint ) {
    43314502                                if ( isset( $input[ $breakpoint ] ) ) {
    43324503                                        $output[ $breakpoint ] = static::remove_insecure_styles( $input[ $breakpoint ] );
    43334504
    43344505                                        if ( isset( $input[ $breakpoint ]['elements'] ) ) {
    4335                                                 $output[ $breakpoint ]['elements'] = static::remove_insecure_element_styles( $input[ $breakpoint ]['elements'] );
     4506                                                $output[ $breakpoint ]['elements'] = static::remove_insecure_element_styles( $input[ $breakpoint ]['elements'], $responsive_media_queries );
    43364507                                        }
    43374508
    43384509                                        if ( isset( $input[ $breakpoint ]['blocks'] ) ) {
    4339                                                 $output[ $breakpoint ]['blocks'] = static::remove_insecure_inner_block_styles( $input[ $breakpoint ]['blocks'] );
     4510                                                $output[ $breakpoint ]['blocks'] = static::remove_insecure_inner_block_styles( $input[ $breakpoint ]['blocks'], $responsive_media_queries );
    43404511                                        }
    43414512
    … …  
    43694540
    43704541                                        if ( isset( $variation_input['blocks'] ) ) {
    4371                                                 $variation_output['blocks'] = static::remove_insecure_inner_block_styles( $variation_input['blocks'] );
     4542                                                $variation_output['blocks'] = static::remove_insecure_inner_block_styles( $variation_input['blocks'], $responsive_media_queries );
    43724543                                        }
    43734544
    43744545                                        if ( isset( $variation_input['elements'] ) ) {
    4375                                                 $variation_output['elements'] = static::remove_insecure_element_styles( $variation_input['elements'] );
     4546                                                $variation_output['elements'] = static::remove_insecure_element_styles( $variation_input['elements'], $responsive_media_queries );
    43764547                                        }
    43774548
    43784549                                        // Re-add and process responsive breakpoint styles for variations.
    4379                                         foreach ( array_keys( static::RESPONSIVE_BREAKPOINTS ) as $breakpoint ) {
     4550                                        foreach ( array_keys( $responsive_media_queries ) as $breakpoint ) {
    43804551                                                if ( isset( $variation_input[ $breakpoint ] ) ) {
    43814552                                                        $variation_output[ $breakpoint ] = static::remove_insecure_styles( $variation_input[ $breakpoint ] );
    43824553
    43834554                                                        if ( isset( $variation_input[ $breakpoint ]['elements'] ) ) {
    4384                                                                 $variation_output[ $breakpoint ]['elements'] = static::remove_insecure_element_styles( $variation_input[ $breakpoint ]['elements'] );
     4555                                                                $variation_output[ $breakpoint ]['elements'] = static::remove_insecure_element_styles( $variation_input[ $breakpoint ]['elements'], $responsive_media_queries );
    43854556                                                        }
    43864557
    43874558                                                        if ( isset( $variation_input[ $breakpoint ]['blocks'] ) ) {
    4388                                                                 $variation_output[ $breakpoint ]['blocks'] = static::remove_insecure_inner_block_styles( $variation_input[ $breakpoint ]['blocks'] );
     4559                                                                $variation_output[ $breakpoint ]['blocks'] = static::remove_insecure_inner_block_styles( $variation_input[ $breakpoint ]['blocks'], $responsive_media_queries );
    43894560                                                        }
    43904561
    … …  
    44184589                        }
    44194590
    4420                         $output = static::remove_insecure_settings( $input );
     4591                        $output = static::remove_insecure_settings( $input, array( 'settings' ) === $metadata['path'] );
    44214592                        if ( ! empty( $output ) ) {
    44224593                                _wp_array_set( $sanitized, $metadata['path'], $output );
    … …  
    44424613         * Remove insecure element styles within a variation or block.
    44434614         *
     4615         *  * When responsive media queries are provided, nested responsive state styles
     4616         * matching those viewport state keys are re-added after the base sanitization pass.
     4617         *
    44444618         * @since 6.8.0
    4445          *
    4446          * @param array $elements The elements to process.
     4619         * @since 7.1.0 Added the `$responsive_media_queries` parameter.
     4620         *
     4621         * @param array      $elements                 The elements to process.
     4622         * @param array|null $responsive_media_queries Optional. Media queries whose keys define allowed
     4623         *                                             viewport states. Default null.
    44474624         * @return array The sanitized elements styles.
    44484625         */
    4449         protected static function remove_insecure_element_styles( $elements ) {
     4626        protected static function remove_insecure_element_styles( $elements, $responsive_media_queries = null ) {
    44504627                $sanitized           = array();
    44514628                $valid_element_names = array_keys( static::ELEMENTS );
    … …  
    44644641                                }
    44654642
    4466                                 // Re-add and process responsive breakpoint styles for elements.
    4467                                 foreach ( array_keys( static::RESPONSIVE_BREAKPOINTS ) as $breakpoint ) {
    4468                                         if ( isset( $element_input[ $breakpoint ] ) ) {
    4469                                                 $element_output[ $breakpoint ] = static::remove_insecure_styles( $element_input[ $breakpoint ] );
    4470 
    4471                                                 if ( isset( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element_name ] ) ) {
    4472                                                         foreach ( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element_name ] as $pseudo_selector ) {
    4473                                                                 if ( isset( $element_input[ $breakpoint ][ $pseudo_selector ] ) ) {
    4474                                                                         $element_output[ $breakpoint ][ $pseudo_selector ] = static::remove_insecure_styles( $element_input[ $breakpoint ][ $pseudo_selector ] );
     4643                                if ( null !== $responsive_media_queries ) {
     4644                                        // Re-add and process responsive breakpoint styles for elements.
     4645                                        foreach ( array_keys( $responsive_media_queries ) as $breakpoint ) {
     4646                                                if ( isset( $element_input[ $breakpoint ] ) ) {
     4647                                                        $element_output[ $breakpoint ] = static::remove_insecure_styles( $element_input[ $breakpoint ] );
     4648
     4649                                                        if ( isset( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element_name ] ) ) {
     4650                                                                foreach ( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element_name ] as $pseudo_selector ) {
     4651                                                                        if ( isset( $element_input[ $breakpoint ][ $pseudo_selector ] ) ) {
     4652                                                                                $element_output[ $breakpoint ][ $pseudo_selector ] = static::remove_insecure_styles( $element_input[ $breakpoint ][ $pseudo_selector ] );
     4653                                                                        }
    44754654                                                                }
    44764655                                                        }
    … …  
    44884667         * Remove insecure styles from inner blocks and their elements.
    44894668         *
     4669         * When responsive media queries are provided, nested responsive state styles
     4670         * for those media-query keys are re-added after the base sanitization pass.
     4671         *
    44904672         * @since 6.8.0
    4491          *
    4492          * @param array $blocks The block styles to process.
     4673         * @since 7.1.0 Added the `$responsive_media_queries` parameter.
     4674         *
     4675         * @param array      $blocks                   The block styles to process.
     4676         * @param array|null $responsive_media_queries Optional. Media queries whose keys define allowed
     4677         *                                             viewport states. Default null.
    44934678         * @return array Sanitized block type styles.
    44944679         */
    4495         protected static function remove_insecure_inner_block_styles( $blocks ) {
     4680        protected static function remove_insecure_inner_block_styles( $blocks, $responsive_media_queries = null ) {
    44964681                $sanitized = array();
    44974682                foreach ( $blocks as $block_type => $block_input ) {
    … …  
    44994684
    45004685                        if ( isset( $block_input['elements'] ) ) {
    4501                                 $block_output['elements'] = static::remove_insecure_element_styles( $block_input['elements'] );
    4502                         }
    4503 
    4504                         // Re-add and process responsive breakpoint styles for inner blocks.
    4505                         foreach ( array_keys( static::RESPONSIVE_BREAKPOINTS ) as $breakpoint ) {
    4506                                 if ( isset( $block_input[ $breakpoint ] ) ) {
    4507                                         $block_output[ $breakpoint ] = static::remove_insecure_styles( $block_input[ $breakpoint ] );
    4508 
    4509                                         if ( isset( static::VALID_BLOCK_PSEUDO_SELECTORS[ $block_type ] ) ) {
    4510                                                 foreach ( static::VALID_BLOCK_PSEUDO_SELECTORS[ $block_type ] as $pseudo_selector ) {
    4511                                                         if ( isset( $block_input[ $breakpoint ][ $pseudo_selector ] ) ) {
    4512                                                                 $block_output[ $breakpoint ][ $pseudo_selector ] = static::remove_insecure_styles( $block_input[ $breakpoint ][ $pseudo_selector ] );
     4686                                $block_output['elements'] = static::remove_insecure_element_styles( $block_input['elements'], $responsive_media_queries );
     4687                        }
     4688
     4689                        if ( null !== $responsive_media_queries ) {
     4690                                // Re-add and process responsive breakpoint styles for inner blocks.
     4691                                foreach ( array_keys( $responsive_media_queries ) as $breakpoint ) {
     4692                                        if ( isset( $block_input[ $breakpoint ] ) ) {
     4693                                                $block_output[ $breakpoint ] = static::remove_insecure_styles( $block_input[ $breakpoint ] );
     4694
     4695                                                if ( isset( static::VALID_BLOCK_PSEUDO_SELECTORS[ $block_type ] ) ) {
     4696                                                        foreach ( static::VALID_BLOCK_PSEUDO_SELECTORS[ $block_type ] as $pseudo_selector ) {
     4697                                                                if ( isset( $block_input[ $breakpoint ][ $pseudo_selector ] ) ) {
     4698                                                                        $block_output[ $breakpoint ][ $pseudo_selector ] = static::remove_insecure_styles( $block_input[ $breakpoint ][ $pseudo_selector ] );
     4699                                                                }
    45134700                                                        }
    45144701                                                }
    … …  
    45564743         *
    45574744         * @since 5.9.0
    4558          *
    4559          * @param array $input Node to process.
     4745         * @since 7.1.0 Added the `$is_root` parameter.
     4746         *
     4747         * @param array $input   Node to process.
     4748         * @param bool  $is_root Optional. Whether the node is the root settings node. Default false.
    45604749         * @return array
    45614750         */
    4562         protected static function remove_insecure_settings( $input ) {
     4751        protected static function remove_insecure_settings( $input, $is_root = false ) {
    45634752                $output = array();
    45644753                foreach ( static::PRESETS_METADATA as $preset_metadata ) {
    … …  
    46124801                // Preserve all valid settings that have type markers in VALID_SETTINGS.
    46134802                self::preserve_valid_typed_settings( $input, $output, static::VALID_SETTINGS );
     4803
     4804                if ( $is_root && array_key_exists( 'viewport', $input ) ) {
     4805                        $output['viewport'] = static::sanitize_viewport_settings( $input['viewport'] );
     4806                }
    46144807
    46154808                return $output;
  • trunk/tests/phpunit/tests/block-supports/block-style-variations.php

    r58691 r62671  
    99 * @group block-supports
    1010 */
    11 class WP_Block_Supports_Block_Style_Variations_Test extends WP_UnitTestCase {
     11class Tests_Block_Supports_BlockStyleVariations extends WP_UnitTestCase {
    1212        /**
    1313         * Theme root directory.
    … …  
    162162
    163163        /**
     164         * Tests that state styles in block style variations use custom viewport breakpoints.
     165         *
     166         * @ticket 65596
     167         */
     168        public function test_block_style_variation_state_styles_use_custom_viewport_breakpoints() {
     169                switch_theme( 'block-theme' );
     170
     171                register_block_style(
     172                        'core/group',
     173                        array(
     174                                'name'       => 'custom-breakpoint-variation',
     175                                'style_data' => array(
     176                                        'color'   => array(
     177                                                'text' => 'blue',
     178                                        ),
     179                                        '@mobile' => array(
     180                                                'color' => array(
     181                                                        'text' => 'red',
     182                                                ),
     183                                        ),
     184                                        '@tablet' => array(
     185                                                'color' => array(
     186                                                        'text' => 'green',
     187                                                ),
     188                                        ),
     189                                ),
     190                        )
     191                );
     192
     193                $filter = static function ( $theme_json ) {
     194                        return $theme_json->update_with(
     195                                array(
     196                                        'version'  => WP_Theme_JSON::LATEST_SCHEMA,
     197                                        'settings' => array(
     198                                                'viewport' => array(
     199                                                        'mobile' => '640px',
     200                                                        'tablet' => '960px',
     201                                                ),
     202                                        ),
     203                                )
     204                        );
     205                };
     206
     207                add_filter( 'wp_theme_json_data_theme', $filter );
     208                WP_Theme_JSON_Resolver::clean_cached_data();
     209
     210                try {
     211                        $parsed_block = array(
     212                                'blockName' => 'core/group',
     213                                'attrs'     => array(
     214                                        'className' => 'wp-block-group is-style-custom-breakpoint-variation',
     215                                ),
     216                        );
     217
     218                        wp_render_block_style_variation_support_styles( $parsed_block );
     219                        $inline_styles     = wp_styles()->get_data( 'block-style-variation-styles', 'after' );
     220                        $actual_stylesheet = is_array( $inline_styles ) ? implode( '', $inline_styles ) : '';
     221
     222                        $this->assertStringContainsString(
     223                                '@media (width <= 640px)',
     224                                $actual_stylesheet,
     225                                'CSS should contain the custom mobile viewport media query.'
     226                        );
     227                        $this->assertStringContainsString(
     228                                '@media (640px < width <= 960px)',
     229                                $actual_stylesheet,
     230                                'CSS should contain the custom tablet viewport media query.'
     231                        );
     232                        $this->assertStringNotContainsString(
     233                                '@media (width <= 480px)',
     234                                $actual_stylesheet,
     235                                'CSS should not use the default mobile viewport media query.'
     236                        );
     237                        $this->assertStringNotContainsString(
     238                                '@media (480px < width <= 782px)',
     239                                $actual_stylesheet,
     240                                'CSS should not use the default tablet viewport media query.'
     241                        );
     242                } finally {
     243                        remove_filter( 'wp_theme_json_data_theme', $filter );
     244                        unregister_block_style( 'core/group', 'custom-breakpoint-variation' );
     245                        wp_deregister_style( 'block-style-variation-styles' );
     246                        WP_Theme_JSON_Resolver::clean_cached_data();
     247                }
     248        }
     249
     250        /**
    164251         * Tests that block style variations resolve any `ref` values when generating styles.
    165252         *
  • trunk/tests/phpunit/tests/block-supports/block-visibility.php

    r62586 r62671  
    1111 * @covers ::wp_render_block_visibility_support
    1212 */
    13 class Tests_Block_Supports_Block_Visibility extends WP_UnitTestCase {
     13class Tests_Block_Supports_BlockVisibility extends WP_UnitTestCase {
    1414
    1515        private ?string $test_block_name;
    … …  
    415415
    416416        /**
     417         * @ticket 65596
     418         */
     419        public function test_block_visibility_support_uses_custom_viewport_breakpoints(): void {
     420                $this->register_visibility_block_with_support(
     421                        'test/viewport-custom-breakpoints',
     422                        array( 'visibility' => true )
     423                );
     424
     425                $filter = static function ( $theme_json ) {
     426                        return $theme_json->update_with(
     427                                array(
     428                                        'version'  => WP_Theme_JSON::LATEST_SCHEMA,
     429                                        'settings' => array(
     430                                                'viewport' => array(
     431                                                        'mobile' => '640px',
     432                                                        'tablet' => '960px',
     433                                                ),
     434                                        ),
     435                                )
     436                        );
     437                };
     438
     439                add_filter( 'wp_theme_json_data_theme', $filter );
     440                WP_Theme_JSON_Resolver::clean_cached_data();
     441
     442                try {
     443                        $block = array(
     444                                'blockName' => 'test/viewport-custom-breakpoints',
     445                                'attrs'     => array(
     446                                        'metadata' => array(
     447                                                'blockVisibility' => array(
     448                                                        'viewport' => array(
     449                                                                'mobile'  => false,
     450                                                                'tablet'  => false,
     451                                                                'desktop' => false,
     452                                                        ),
     453                                                ),
     454                                        ),
     455                                ),
     456                        );
     457
     458                        wp_render_block_visibility_support( '<div>Test content</div>', $block );
     459                        $actual_stylesheet = wp_style_engine_get_stylesheet_from_context( 'block-supports', array( 'prettify' => false ) );
     460
     461                        $this->assertStringContainsString(
     462                                '@media (width <= 640px){.wp-block-hidden-mobile{display:none !important;}}',
     463                                $actual_stylesheet,
     464                                'CSS should contain custom mobile visibility rule'
     465                        );
     466                        $this->assertStringContainsString(
     467                                '@media (640px < width <= 960px){.wp-block-hidden-tablet{display:none !important;}}',
     468                                $actual_stylesheet,
     469                                'CSS should contain custom tablet visibility rule'
     470                        );
     471                        $this->assertStringContainsString(
     472                                '@media (width > 960px){.wp-block-hidden-desktop{display:none !important;}}',
     473                                $actual_stylesheet,
     474                                'CSS should contain custom desktop visibility rule'
     475                        );
     476                } finally {
     477                        remove_filter( 'wp_theme_json_data_theme', $filter );
     478                        WP_Theme_JSON_Resolver::clean_cached_data();
     479                }
     480        }
     481
     482        /**
     483         * @ticket 65596
     484         */
     485        public function test_block_visibility_support_uses_single_max_width_tablet_query_for_single_breakpoint(): void {
     486                $this->register_visibility_block_with_support(
     487                        'test/viewport-tablet-only-breakpoint',
     488                        array( 'visibility' => true )
     489                );
     490
     491                $filter = static function ( $theme_json ) {
     492                        return $theme_json->update_with(
     493                                array(
     494                                        'version'  => WP_Theme_JSON::LATEST_SCHEMA,
     495                                        'settings' => array(
     496                                                'viewport' => array(
     497                                                        'tablet' => '64rem',
     498                                                ),
     499                                        ),
     500                                )
     501                        );
     502                };
     503
     504                add_filter( 'wp_theme_json_data_theme', $filter );
     505                WP_Theme_JSON_Resolver::clean_cached_data();
     506
     507                try {
     508                        $block = array(
     509                                'blockName' => 'test/viewport-tablet-only-breakpoint',
     510                                'attrs'     => array(
     511                                        'metadata' => array(
     512                                                'blockVisibility' => array(
     513                                                        'viewport' => array(
     514                                                                'mobile' => false,
     515                                                                'tablet' => false,
     516                                                        ),
     517                                                ),
     518                                        ),
     519                                ),
     520                        );
     521
     522                        $result            = wp_render_block_visibility_support( '<div>Test content</div>', $block );
     523                        $actual_stylesheet = wp_style_engine_get_stylesheet_from_context( 'block-supports', array( 'prettify' => false ) );
     524
     525                        $this->assertStringContainsString(
     526                                'class="wp-block-hidden-tablet"',
     527                                $result,
     528                                'Block should have the visibility class for the tablet viewport size.'
     529                        );
     530                        $this->assertStringContainsString(
     531                                '@media (width <= 64rem){.wp-block-hidden-tablet{display:none !important;}}',
     532                                $actual_stylesheet,
     533                                'CSS should contain a single max-width tablet visibility rule.'
     534                        );
     535                        $this->assertStringNotContainsString(
     536                                'wp-block-hidden-mobile',
     537                                $result,
     538                                'Block should not have the mobile visibility class when no mobile breakpoint is configured.'
     539                        );
     540                } finally {
     541                        remove_filter( 'wp_theme_json_data_theme', $filter );
     542                        WP_Theme_JSON_Resolver::clean_cached_data();
     543                }
     544        }
     545
     546        /**
    417547         * @ticket 64414
    418548         */
  • trunk/tests/phpunit/tests/theme/wpThemeJson.php

    r62650 r62671  
    10001000                        '@media (width <= 480px){:root :where(.wp-block-test-responsive-feature){color: red;}}',
    10011001                        $actual_styles
     1002                );
     1003        }
     1004
     1005        /**
     1006         * @ticket 65596
     1007         */
     1008        public function test_get_viewport_media_queries_uses_valid_custom_breakpoint_without_merging_defaults() {
     1009                $this->assertSame(
     1010                        array(
     1011                                '@mobile'  => '@media (width <= 640px)',
     1012                                '@desktop' => '@media (width > 640px)',
     1013                        ),
     1014                        WP_Theme_JSON::get_viewport_media_queries(
     1015                                array(
     1016                                        'mobile'  => ' 640px ',
     1017                                        'tablet'  => 'calc(100% - 1rem)',
     1018                                        'desktop' => '1200px',
     1019                                ),
     1020                                array(
     1021                                        'include_desktop' => true,
     1022                                )
     1023                        )
     1024                );
     1025        }
     1026
     1027        /**
     1028         * @ticket 65596
     1029         */
     1030        public function test_get_viewport_media_queries_uses_defaults_when_no_custom_breakpoints_are_valid() {
     1031                $this->assertSame(
     1032                        array(
     1033                                '@mobile'  => '@media (width <= 480px)',
     1034                                '@tablet'  => '@media (480px < width <= 782px)',
     1035                                '@desktop' => '@media (width > 782px)',
     1036                        ),
     1037                        WP_Theme_JSON::get_viewport_media_queries(
     1038                                array(
     1039                                        'mobile' => '100%',
     1040                                        'tablet' => 'auto',
     1041                                ),
     1042                                array(
     1043                                        'include_desktop' => true,
     1044                                )
     1045                        )
     1046                );
     1047        }
     1048
     1049        /**
     1050         * @ticket 65596
     1051         */
     1052        public function test_get_viewport_media_queries_uses_valid_tablet_breakpoint_with_single_max_width_query_when_mobile_is_invalid() {
     1053                $this->assertSame(
     1054                        array(
     1055                                '@tablet'  => '@media (width <= 64rem)',
     1056                                '@desktop' => '@media (width > 64rem)',
     1057                        ),
     1058                        WP_Theme_JSON::get_viewport_media_queries(
     1059                                array(
     1060                                        'mobile' => '100%',
     1061                                        'tablet' => '64rem',
     1062                                ),
     1063                                array(
     1064                                        'include_desktop' => true,
     1065                                )
     1066                        )
     1067                );
     1068        }
     1069
     1070        /**
     1071         * @ticket 65596
     1072         */
     1073        public function test_viewport_settings_preserve_valid_tablet_breakpoint_when_mobile_is_invalid() {
     1074                $theme_json = new WP_Theme_JSON(
     1075                        array(
     1076                                'version'  => WP_Theme_JSON::LATEST_SCHEMA,
     1077                                'settings' => array(
     1078                                        'viewport' => array(
     1079                                                'mobile' => '100%',
     1080                                                'tablet' => '64rem',
     1081                                        ),
     1082                                ),
     1083                        )
     1084                );
     1085
     1086                $this->assertSame(
     1087                        array(
     1088                                'tablet' => '64rem',
     1089                        ),
     1090                        $theme_json->get_raw_data()['settings']['viewport']
     1091                );
     1092        }
     1093
     1094        /**
     1095         * @ticket 65596
     1096         */
     1097        public function test_viewport_settings_use_defaults_when_no_custom_breakpoints_are_valid() {
     1098                $theme_json = new WP_Theme_JSON(
     1099                        array(
     1100                                'version'  => WP_Theme_JSON::LATEST_SCHEMA,
     1101                                'settings' => array(
     1102                                        'viewport' => array(
     1103                                                'mobile'  => '100%',
     1104                                                'tablet'  => 'auto',
     1105                                                'desktop' => '1200px',
     1106                                        ),
     1107                                ),
     1108                        )
     1109                );
     1110
     1111                $this->assertSame(
     1112                        array(
     1113                                'mobile' => '480px',
     1114                                'tablet' => '782px',
     1115                        ),
     1116                        $theme_json->get_raw_data()['settings']['viewport']
     1117                );
     1118        }
     1119
     1120        /**
     1121         * @ticket 65596
     1122         */
     1123        public function test_get_viewport_media_queries_omits_tablet_when_its_breakpoint_is_not_larger_than_mobile() {
     1124                $this->assertSame(
     1125                        array(
     1126                                '@mobile'  => '@media (width <= 64rem)',
     1127                                '@desktop' => '@media (width > 64rem)',
     1128                        ),
     1129                        WP_Theme_JSON::get_viewport_media_queries(
     1130                                array(
     1131                                        'mobile' => '64rem',
     1132                                        'tablet' => '40rem',
     1133                                ),
     1134                                array(
     1135                                        'include_desktop' => true,
     1136                                )
     1137                        )
     1138                );
     1139        }
     1140
     1141        /**
     1142         * @ticket 65596
     1143         */
     1144        public function test_get_stylesheet_uses_custom_viewport_breakpoints_for_responsive_block_styles() {
     1145                $theme_json = new WP_Theme_JSON(
     1146                        array(
     1147                                'version'  => WP_Theme_JSON::LATEST_SCHEMA,
     1148                                'settings' => array(
     1149                                        'viewport' => array(
     1150                                                'mobile' => '640px',
     1151                                                'tablet' => '960px',
     1152                                        ),
     1153                                ),
     1154                                'styles'   => array(
     1155                                        'blocks' => array(
     1156                                                'core/group' => array(
     1157                                                        '@mobile' => array(
     1158                                                                'color' => array(
     1159                                                                        'text' => 'red',
     1160                                                                ),
     1161                                                        ),
     1162                                                        '@tablet' => array(
     1163                                                                'color' => array(
     1164                                                                        'text' => 'blue',
     1165                                                                ),
     1166                                                        ),
     1167                                                ),
     1168                                        ),
     1169                                ),
     1170                        )
     1171                );
     1172
     1173                $stylesheet = $theme_json->get_stylesheet( array( 'styles' ), null, array( 'skip_root_layout_styles' => true ) );
     1174
     1175                $this->assertStringContainsString(
     1176                        '@media (width <= 640px){:root :where(.wp-block-group){color: red;}}',
     1177                        $stylesheet
     1178                );
     1179                $this->assertStringContainsString(
     1180                        '@media (640px < width <= 960px){:root :where(.wp-block-group){color: blue;}}',
     1181                        $stylesheet
     1182                );
     1183        }
     1184
     1185        /**
     1186         * @ticket 65596
     1187         */
     1188        public function test_get_stylesheet_omits_tablet_styles_when_its_breakpoint_is_not_larger_than_mobile() {
     1189                $theme_json = new WP_Theme_JSON(
     1190                        array(
     1191                                'version'  => WP_Theme_JSON::LATEST_SCHEMA,
     1192                                'settings' => array(
     1193                                        'viewport' => array(
     1194                                                'mobile' => '960px',
     1195                                                'tablet' => '640px',
     1196                                        ),
     1197                                ),
     1198                                'styles'   => array(
     1199                                        'blocks' => array(
     1200                                                'core/group' => array(
     1201                                                        '@mobile' => array(
     1202                                                                'color' => array(
     1203                                                                        'text' => 'red',
     1204                                                                ),
     1205                                                        ),
     1206                                                        '@tablet' => array(
     1207                                                                'color' => array(
     1208                                                                        'text' => 'blue',
     1209                                                                ),
     1210                                                        ),
     1211                                                ),
     1212                                        ),
     1213                                ),
     1214                        )
     1215                );
     1216
     1217                $stylesheet = $theme_json->get_stylesheet( array( 'styles' ), null, array( 'skip_root_layout_styles' => true ) );
     1218
     1219                $this->assertSame(
     1220                        array(
     1221                                'mobile' => '960px',
     1222                        ),
     1223                        $theme_json->get_raw_data()['settings']['viewport']
     1224                );
     1225                $this->assertStringContainsString(
     1226                        '@media (width <= 960px){:root :where(.wp-block-group){color: red;}}',
     1227                        $stylesheet
     1228                );
     1229                $this->assertStringNotContainsString(
     1230                        'color: blue',
     1231                        $stylesheet
     1232                );
     1233        }
     1234
     1235        /**
     1236         * @ticket 65596
     1237         */
     1238        public function test_get_stylesheet_uses_single_max_width_tablet_query_when_mobile_is_invalid() {
     1239                $theme_json = new WP_Theme_JSON(
     1240                        array(
     1241                                'version'  => WP_Theme_JSON::LATEST_SCHEMA,
     1242                                'settings' => array(
     1243                                        'viewport' => array(
     1244                                                'mobile' => '100%',
     1245                                                'tablet' => '64rem',
     1246                                        ),
     1247                                ),
     1248                                'styles'   => array(
     1249                                        'blocks' => array(
     1250                                                'core/group' => array(
     1251                                                        '@mobile' => array(
     1252                                                                'color' => array(
     1253                                                                        'text' => 'red',
     1254                                                                ),
     1255                                                        ),
     1256                                                        '@tablet' => array(
     1257                                                                'color' => array(
     1258                                                                        'text' => 'blue',
     1259                                                                ),
     1260                                                        ),
     1261                                                ),
     1262                                        ),
     1263                                ),
     1264                        )
     1265                );
     1266
     1267                $stylesheet = $theme_json->get_stylesheet( array( 'styles' ), null, array( 'skip_root_layout_styles' => true ) );
     1268
     1269                $this->assertSame(
     1270                        array(
     1271                                'tablet' => '64rem',
     1272                        ),
     1273                        $theme_json->get_raw_data()['settings']['viewport']
     1274                );
     1275                $this->assertStringContainsString(
     1276                        '@media (width <= 64rem){:root :where(.wp-block-group){color: blue;}}',
     1277                        $stylesheet
     1278                );
     1279                $this->assertStringNotContainsString(
     1280                        'color: red',
     1281                        $stylesheet
    10021282                );
    10031283        }
Note: See TracChangeset for help on using the changeset viewer.