From 89dbb9fb77190976d163f380d5b60d8a3b192b26 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Fri, 24 May 2024 10:26:53 -0500 Subject: [PATCH 1/3] Add backward compat shim for WP_Theme_JSON_Data::get_theme_json --- .../class-wp-theme-json-resolver.php | 67 ++++++++++++++++--- 1 file changed, 59 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index 59c5f54b96e45..212f6c1c441e6 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -172,8 +172,18 @@ public static function get_core_data() { * * @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data. */ - $theme_json = apply_filters( 'wp_theme_json_data_default', new WP_Theme_JSON_Data( $config, 'default' ) ); - static::$core = $theme_json->get_theme_json(); + $theme_json = apply_filters( 'wp_theme_json_data_default', new WP_Theme_JSON_Data( $config, 'default' ) ); + + /* + * Backward compatibility for extenders returning a WP_Theme_JSON_Data + * compatible class that has not implemented the get_theme_json method. + */ + if ( method_exists( $theme_json, 'get_theme_json' ) ) { + static::$core = $theme_json->get_theme_json(); + } else { + $config = $theme_json->get_data(); + static::$core = new WP_Theme_JSON( $config, 'default' ); + } return static::$core; } @@ -253,8 +263,18 @@ public static function get_theme_data( $deprecated = array(), $options = array() * * @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data. */ - $theme_json = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) ); - static::$theme = $theme_json->get_theme_json(); + $theme_json = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) ); + + /* + * Backward compatibility for extenders returning a WP_Theme_JSON_Data + * compatible class that has not implemented the get_theme_json method. + */ + if ( method_exists( $theme_json, 'get_theme_json' ) ) { + static::$theme = $theme_json->get_theme_json(); + } else { + $config = $theme_json->get_data(); + static::$theme = new WP_Theme_JSON( $config ); + } if ( $wp_theme->parent() ) { // Get parent theme.json. @@ -385,7 +405,18 @@ public static function get_block_data() { * @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data. */ $theme_json = apply_filters( 'wp_theme_json_data_blocks', new WP_Theme_JSON_Data( $config, 'blocks' ) ); - static::$blocks = $theme_json->get_theme_json(); + + /* + * Backward compatibility for extenders returning a WP_Theme_JSON_Data + * compatible class that has not implemented the get_theme_json method. + */ + if ( method_exists( $theme_json, 'get_theme_json' ) ) { + static::$blocks = $theme_json->get_theme_json(); + } else { + $config = $theme_json->get_data(); + static::$blocks = new WP_Theme_JSON( $config, 'blocks' ); + } + return static::$blocks; } @@ -519,7 +550,17 @@ public static function get_user_data() { * @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data. */ $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) ); - return $theme_json->get_theme_json(); + + /* + * Backward compatibility for extenders returning a WP_Theme_JSON_Data + * compatible class that has not implemented the get_theme_json method. + */ + if ( method_exists( $theme_json, 'get_theme_json' ) ) { + return $theme_json->get_theme_json(); + } else { + $config = $theme_json->get_data(); + return new WP_Theme_JSON( $config, 'custom' ); + } } /* @@ -537,8 +578,18 @@ public static function get_user_data() { } /** This filter is documented in wp-includes/class-wp-theme-json-resolver.php */ - $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) ); - static::$user = $theme_json->get_theme_json(); + $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) ); + + /* + * Backward compatibility for extenders returning a WP_Theme_JSON_Data + * compatible class that has not implemented the get_theme_json method. + */ + if ( method_exists( $theme_json, 'get_theme_json' ) ) { + static::$user = $theme_json->get_theme_json(); + } else { + $config = $theme_json->get_data(); + static::$user = new WP_Theme_JSON( $config, 'custom' ); + } return static::$user; } From 327bb487045e3dfcd5ae542bd43fe7a2834175de Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Fri, 7 Jun 2024 14:34:21 -0500 Subject: [PATCH 2/3] Ensure objects returned from filters are validated by `WP_Theme_JSON` --- .../class-wp-theme-json-resolver.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index 6a3ab0913d3dc..fe6b6e922daf6 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -176,9 +176,9 @@ public static function get_core_data() { /* * Backward compatibility for extenders returning a WP_Theme_JSON_Data - * compatible class that has not implemented the get_theme_json method. + * compatible class that is not a WP_Theme_JSON_Data object. */ - if ( method_exists( $theme_json, 'get_theme_json' ) ) { + if ( is_a( $theme_json, 'WP_Theme_JSON_Data' ) ) { static::$core = $theme_json->get_theme_json(); } else { $config = $theme_json->get_data(); @@ -267,9 +267,9 @@ public static function get_theme_data( $deprecated = array(), $options = array() /* * Backward compatibility for extenders returning a WP_Theme_JSON_Data - * compatible class that has not implemented the get_theme_json method. + * compatible class that is not a WP_Theme_JSON_Data object. */ - if ( method_exists( $theme_json, 'get_theme_json' ) ) { + if ( is_a( $theme_json, 'WP_Theme_JSON_Data' ) ) { static::$theme = $theme_json->get_theme_json(); } else { $config = $theme_json->get_data(); @@ -408,9 +408,9 @@ public static function get_block_data() { /* * Backward compatibility for extenders returning a WP_Theme_JSON_Data - * compatible class that has not implemented the get_theme_json method. + * compatible class that is not a WP_Theme_JSON_Data object. */ - if ( method_exists( $theme_json, 'get_theme_json' ) ) { + if ( is_a( $theme_json, 'WP_Theme_JSON_Data' ) ) { static::$blocks = $theme_json->get_theme_json(); } else { $config = $theme_json->get_data(); @@ -553,9 +553,9 @@ public static function get_user_data() { /* * Backward compatibility for extenders returning a WP_Theme_JSON_Data - * compatible class that has not implemented the get_theme_json method. + * compatible class that is not a WP_Theme_JSON_Data object. */ - if ( method_exists( $theme_json, 'get_theme_json' ) ) { + if ( is_a( $theme_json, 'WP_Theme_JSON_Data' ) ) { return $theme_json->get_theme_json(); } else { $config = $theme_json->get_data(); @@ -582,9 +582,9 @@ public static function get_user_data() { /* * Backward compatibility for extenders returning a WP_Theme_JSON_Data - * compatible class that has not implemented the get_theme_json method. + * compatible class that is not a WP_Theme_JSON_Data object. */ - if ( method_exists( $theme_json, 'get_theme_json' ) ) { + if ( is_a( $theme_json, 'WP_Theme_JSON_Data' ) ) { static::$user = $theme_json->get_theme_json(); } else { $config = $theme_json->get_data(); From 544d7628992d4bfd53f8176ef6ee1a979b15139a Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Tue, 18 Jun 2024 16:28:34 -0500 Subject: [PATCH 3/3] Prefer instanceof to is_a() --- src/wp-includes/class-wp-theme-json-resolver.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index fe6b6e922daf6..760a8ce80a4ef 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -178,7 +178,7 @@ public static function get_core_data() { * Backward compatibility for extenders returning a WP_Theme_JSON_Data * compatible class that is not a WP_Theme_JSON_Data object. */ - if ( is_a( $theme_json, 'WP_Theme_JSON_Data' ) ) { + if ( $theme_json instanceof WP_Theme_JSON_Data ) { static::$core = $theme_json->get_theme_json(); } else { $config = $theme_json->get_data(); @@ -269,7 +269,7 @@ public static function get_theme_data( $deprecated = array(), $options = array() * Backward compatibility for extenders returning a WP_Theme_JSON_Data * compatible class that is not a WP_Theme_JSON_Data object. */ - if ( is_a( $theme_json, 'WP_Theme_JSON_Data' ) ) { + if ( $theme_json instanceof WP_Theme_JSON_Data ) { static::$theme = $theme_json->get_theme_json(); } else { $config = $theme_json->get_data(); @@ -410,7 +410,7 @@ public static function get_block_data() { * Backward compatibility for extenders returning a WP_Theme_JSON_Data * compatible class that is not a WP_Theme_JSON_Data object. */ - if ( is_a( $theme_json, 'WP_Theme_JSON_Data' ) ) { + if ( $theme_json instanceof WP_Theme_JSON_Data ) { static::$blocks = $theme_json->get_theme_json(); } else { $config = $theme_json->get_data(); @@ -555,7 +555,7 @@ public static function get_user_data() { * Backward compatibility for extenders returning a WP_Theme_JSON_Data * compatible class that is not a WP_Theme_JSON_Data object. */ - if ( is_a( $theme_json, 'WP_Theme_JSON_Data' ) ) { + if ( $theme_json instanceof WP_Theme_JSON_Data ) { return $theme_json->get_theme_json(); } else { $config = $theme_json->get_data(); @@ -584,7 +584,7 @@ public static function get_user_data() { * Backward compatibility for extenders returning a WP_Theme_JSON_Data * compatible class that is not a WP_Theme_JSON_Data object. */ - if ( is_a( $theme_json, 'WP_Theme_JSON_Data' ) ) { + if ( $theme_json instanceof WP_Theme_JSON_Data ) { static::$user = $theme_json->get_theme_json(); } else { $config = $theme_json->get_data();