Make WordPress Core

Changeset 58443


Ignore:
Timestamp:
06/19/2024 04:39:07 PM (2 years ago)
Author:
joemcgill
Message:

Editor: Improve compatibility for WP_Theme_JSON_Data.

This checks that objects returned from any of the wp_theme_json_data_ filters are WP_Theme_JSON_Data objects in order to avoid incompatibilities. Otherwise, reprocess the theme.json data as new WP_Theme_JSON objects to ensure the data matches the expectations of code consuming that data.

Follow-up to [58185].

Props joemcgill, adamsilverstein, oandregal, ryelle, ocean90, pbearne.
See #61112.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json-resolver.php

    r58429 r58443  
    173173                 * @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
    174174                 */
    175                 $theme_json   = apply_filters( 'wp_theme_json_data_default', new WP_Theme_JSON_Data( $config, 'default' ) );
    176                 static::$core = $theme_json->get_theme_json();
     175                $theme_json = apply_filters( 'wp_theme_json_data_default', new WP_Theme_JSON_Data( $config, 'default' ) );
     176
     177                /*
     178                 * Backward compatibility for extenders returning a WP_Theme_JSON_Data
     179                 * compatible class that is not a WP_Theme_JSON_Data object.
     180                 */
     181                if ( $theme_json instanceof WP_Theme_JSON_Data ) {
     182                        static::$core = $theme_json->get_theme_json();
     183                } else {
     184                        $config       = $theme_json->get_data();
     185                        static::$core = new WP_Theme_JSON( $config, 'default' );
     186                }
    177187
    178188                return static::$core;
     
    264274                         * @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
    265275                         */
    266                         $theme_json    = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) );
    267                         static::$theme = $theme_json->get_theme_json();
     276                        $theme_json = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) );
     277
     278                        /*
     279                         * Backward compatibility for extenders returning a WP_Theme_JSON_Data
     280                         * compatible class that is not a WP_Theme_JSON_Data object.
     281                         */
     282                        if ( $theme_json instanceof WP_Theme_JSON_Data ) {
     283                                static::$theme = $theme_json->get_theme_json();
     284                        } else {
     285                                $config        = $theme_json->get_data();
     286                                static::$theme = new WP_Theme_JSON( $config );
     287                        }
    268288
    269289                        if ( $wp_theme->parent() ) {
     
    387407                 * @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
    388408                 */
    389                 $theme_json     = apply_filters( 'wp_theme_json_data_blocks', new WP_Theme_JSON_Data( $config, 'blocks' ) );
    390                 static::$blocks = $theme_json->get_theme_json();
     409                $theme_json = apply_filters( 'wp_theme_json_data_blocks', new WP_Theme_JSON_Data( $config, 'blocks' ) );
     410
     411                /*
     412                 * Backward compatibility for extenders returning a WP_Theme_JSON_Data
     413                 * compatible class that is not a WP_Theme_JSON_Data object.
     414                 */
     415                if ( $theme_json instanceof WP_Theme_JSON_Data ) {
     416                        static::$blocks = $theme_json->get_theme_json();
     417                } else {
     418                        $config         = $theme_json->get_data();
     419                        static::$blocks = new WP_Theme_JSON( $config, 'blocks' );
     420                }
     421
    391422                return static::$blocks;
    392423        }
     
    524555                                 */
    525556                                $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
    526                                 return $theme_json->get_theme_json();
     557
     558                                /*
     559                                 * Backward compatibility for extenders returning a WP_Theme_JSON_Data
     560                                 * compatible class that is not a WP_Theme_JSON_Data object.
     561                                 */
     562                                if ( $theme_json instanceof WP_Theme_JSON_Data ) {
     563                                        return $theme_json->get_theme_json();
     564                                } else {
     565                                        $config = $theme_json->get_data();
     566                                        return new WP_Theme_JSON( $config, 'custom' );
     567                                }
    527568                        }
    528569
     
    546587
    547588                /** This filter is documented in wp-includes/class-wp-theme-json-resolver.php */
    548                 $theme_json   = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
    549                 static::$user = $theme_json->get_theme_json();
     589                $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
     590
     591                /*
     592                 * Backward compatibility for extenders returning a WP_Theme_JSON_Data
     593                 * compatible class that is not a WP_Theme_JSON_Data object.
     594                 */
     595                if ( $theme_json instanceof WP_Theme_JSON_Data ) {
     596                        static::$user = $theme_json->get_theme_json();
     597                } else {
     598                        $config       = $theme_json->get_data();
     599                        static::$user = new WP_Theme_JSON( $config, 'custom' );
     600                }
    550601
    551602                return static::$user;
Note: See TracChangeset for help on using the changeset viewer.