Update theme.json classes for WordPress 5.9.
This commit ports to core the changes to the classes that deal with theme.json code. See #54336. Props oandregal, spacedmonkey, noisysocks, hellofromtonya, youknowriad. Built from https://develop.svn.wordpress.org/trunk@52049 git-svn-id: http://core.svn.wordpress.org/trunk@51641 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
25b9998213
commit
065c639a2a
|
@ -303,15 +303,15 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex
|
|||
$custom_settings
|
||||
);
|
||||
|
||||
$theme_json = WP_Theme_JSON_Resolver::get_merged_data( $editor_settings );
|
||||
$theme_json = WP_Theme_JSON_Resolver::get_merged_data();
|
||||
|
||||
if ( WP_Theme_JSON_Resolver::theme_has_support() ) {
|
||||
$editor_settings['styles'][] = array(
|
||||
'css' => $theme_json->get_stylesheet( 'block_styles' ),
|
||||
'css' => $theme_json->get_stylesheet( array( 'styles', 'presets' ) ),
|
||||
'__unstableType' => 'globalStyles',
|
||||
);
|
||||
$editor_settings['styles'][] = array(
|
||||
'css' => $theme_json->get_stylesheet( 'css_variables' ),
|
||||
'css' => $theme_json->get_stylesheet( array( 'variables' ) ),
|
||||
'__experimentalNoWrapper' => true,
|
||||
'__unstableType' => 'globalStyles',
|
||||
);
|
||||
|
@ -358,17 +358,17 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex
|
|||
$editor_settings['disableCustomFontSizes'] = ! $editor_settings['__experimentalFeatures']['typography']['customFontSize'];
|
||||
unset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] );
|
||||
}
|
||||
if ( isset( $editor_settings['__experimentalFeatures']['typography']['customLineHeight'] ) ) {
|
||||
$editor_settings['enableCustomLineHeight'] = $editor_settings['__experimentalFeatures']['typography']['customLineHeight'];
|
||||
unset( $editor_settings['__experimentalFeatures']['typography']['customLineHeight'] );
|
||||
if ( isset( $editor_settings['__experimentalFeatures']['typography']['lineHeight'] ) ) {
|
||||
$editor_settings['enableCustomLineHeight'] = $editor_settings['__experimentalFeatures']['typography']['lineHeight'];
|
||||
unset( $editor_settings['__experimentalFeatures']['typography']['lineHeight'] );
|
||||
}
|
||||
if ( isset( $editor_settings['__experimentalFeatures']['spacing']['units'] ) ) {
|
||||
$editor_settings['enableCustomUnits'] = $editor_settings['__experimentalFeatures']['spacing']['units'];
|
||||
unset( $editor_settings['__experimentalFeatures']['spacing']['units'] );
|
||||
}
|
||||
if ( isset( $editor_settings['__experimentalFeatures']['spacing']['customPadding'] ) ) {
|
||||
$editor_settings['enableCustomSpacing'] = $editor_settings['__experimentalFeatures']['spacing']['customPadding'];
|
||||
unset( $editor_settings['__experimentalFeatures']['spacing']['customPadding'] );
|
||||
if ( isset( $editor_settings['__experimentalFeatures']['spacing']['padding'] ) ) {
|
||||
$editor_settings['enableCustomSpacing'] = $editor_settings['__experimentalFeatures']['spacing']['padding'];
|
||||
unset( $editor_settings['__experimentalFeatures']['spacing']['padding'] );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -69,6 +69,28 @@ function wp_tinycolor_bound01( $n, $max ) {
|
|||
return ( $n % $max ) / (float) $max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Direct port of tinycolor's boundAlpha function to maintain consistency with
|
||||
* how tinycolor works.
|
||||
*
|
||||
* @see https://github.com/bgrins/TinyColor
|
||||
*
|
||||
* @since 5.9.0
|
||||
* @access private
|
||||
*
|
||||
* @param mixed $n Number of unknown type.
|
||||
* @return float Value in the range [0,1].
|
||||
*/
|
||||
function _wp_tinycolor_bound_alpha( $n ) {
|
||||
if ( is_numeric( $n ) ) {
|
||||
$n = (float) $n;
|
||||
if ( $n >= 0 && $n <= 1 ) {
|
||||
return $n;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Round and convert values of an RGB object.
|
||||
*
|
||||
|
@ -170,8 +192,7 @@ function wp_tinycolor_hsl_to_rgb( $hsl_color ) {
|
|||
|
||||
/**
|
||||
* Parses hex, hsl, and rgb CSS strings using the same regex as TinyColor v1.4.2
|
||||
* used in the JavaScript. Only colors output from react-color are implemented
|
||||
* and the alpha value is ignored as it is not used in duotone.
|
||||
* used in the JavaScript. Only colors output from react-color are implemented.
|
||||
*
|
||||
* Direct port of TinyColor's function, lightly simplified to maintain
|
||||
* consistency with TinyColor.
|
||||
|
@ -180,6 +201,7 @@ function wp_tinycolor_hsl_to_rgb( $hsl_color ) {
|
|||
* @see https://github.com/casesandberg/react-color/
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @since 5.9.0 Added alpha processing.
|
||||
* @access private
|
||||
*
|
||||
* @param string $color_str CSS color string.
|
||||
|
@ -199,35 +221,47 @@ function wp_tinycolor_string_to_rgb( $color_str ) {
|
|||
|
||||
$rgb_regexp = '/^rgb' . $permissive_match3 . '$/';
|
||||
if ( preg_match( $rgb_regexp, $color_str, $match ) ) {
|
||||
return wp_tinycolor_rgb_to_rgb(
|
||||
$rgb = wp_tinycolor_rgb_to_rgb(
|
||||
array(
|
||||
'r' => $match[1],
|
||||
'g' => $match[2],
|
||||
'b' => $match[3],
|
||||
)
|
||||
);
|
||||
|
||||
$rgb['a'] = 1;
|
||||
|
||||
return $rgb;
|
||||
}
|
||||
|
||||
$rgba_regexp = '/^rgba' . $permissive_match4 . '$/';
|
||||
if ( preg_match( $rgba_regexp, $color_str, $match ) ) {
|
||||
return wp_tinycolor_rgb_to_rgb(
|
||||
$rgb = wp_tinycolor_rgb_to_rgb(
|
||||
array(
|
||||
'r' => $match[1],
|
||||
'g' => $match[2],
|
||||
'b' => $match[3],
|
||||
)
|
||||
);
|
||||
|
||||
$rgb['a'] = _wp_tinycolor_bound_alpha( $match[4] );
|
||||
|
||||
return $rgb;
|
||||
}
|
||||
|
||||
$hsl_regexp = '/^hsl' . $permissive_match3 . '$/';
|
||||
if ( preg_match( $hsl_regexp, $color_str, $match ) ) {
|
||||
return wp_tinycolor_hsl_to_rgb(
|
||||
$rgb = wp_tinycolor_hsl_to_rgb(
|
||||
array(
|
||||
'h' => $match[1],
|
||||
's' => $match[2],
|
||||
'l' => $match[3],
|
||||
)
|
||||
);
|
||||
|
||||
$rgb['a'] = 1;
|
||||
|
||||
return $rgb;
|
||||
}
|
||||
|
||||
$hsla_regexp = '/^hsla' . $permissive_match4 . '$/';
|
||||
|
@ -239,50 +273,87 @@ function wp_tinycolor_string_to_rgb( $color_str ) {
|
|||
'l' => $match[3],
|
||||
)
|
||||
);
|
||||
|
||||
$rgb['a'] = _wp_tinycolor_bound_alpha( $match[4] );
|
||||
|
||||
return $rgb;
|
||||
}
|
||||
|
||||
$hex8_regexp = '/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/';
|
||||
if ( preg_match( $hex8_regexp, $color_str, $match ) ) {
|
||||
return wp_tinycolor_rgb_to_rgb(
|
||||
$rgb = wp_tinycolor_rgb_to_rgb(
|
||||
array(
|
||||
'r' => base_convert( $match[1], 16, 10 ),
|
||||
'g' => base_convert( $match[2], 16, 10 ),
|
||||
'b' => base_convert( $match[3], 16, 10 ),
|
||||
)
|
||||
);
|
||||
|
||||
$rgb['a'] = _wp_tinycolor_bound_alpha(
|
||||
base_convert( $match[4], 16, 10 ) / 255
|
||||
);
|
||||
|
||||
return $rgb;
|
||||
}
|
||||
|
||||
$hex6_regexp = '/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/';
|
||||
if ( preg_match( $hex6_regexp, $color_str, $match ) ) {
|
||||
return wp_tinycolor_rgb_to_rgb(
|
||||
$rgb = wp_tinycolor_rgb_to_rgb(
|
||||
array(
|
||||
'r' => base_convert( $match[1], 16, 10 ),
|
||||
'g' => base_convert( $match[2], 16, 10 ),
|
||||
'b' => base_convert( $match[3], 16, 10 ),
|
||||
)
|
||||
);
|
||||
|
||||
$rgb['a'] = 1;
|
||||
|
||||
return $rgb;
|
||||
}
|
||||
|
||||
$hex4_regexp = '/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/';
|
||||
if ( preg_match( $hex4_regexp, $color_str, $match ) ) {
|
||||
return wp_tinycolor_rgb_to_rgb(
|
||||
$rgb = wp_tinycolor_rgb_to_rgb(
|
||||
array(
|
||||
'r' => base_convert( $match[1] . $match[1], 16, 10 ),
|
||||
'g' => base_convert( $match[2] . $match[2], 16, 10 ),
|
||||
'b' => base_convert( $match[3] . $match[3], 16, 10 ),
|
||||
)
|
||||
);
|
||||
|
||||
$rgb['a'] = _wp_tinycolor_bound_alpha(
|
||||
base_convert( $match[4] . $match[4], 16, 10 ) / 255
|
||||
);
|
||||
|
||||
return $rgb;
|
||||
}
|
||||
|
||||
$hex3_regexp = '/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/';
|
||||
if ( preg_match( $hex3_regexp, $color_str, $match ) ) {
|
||||
return wp_tinycolor_rgb_to_rgb(
|
||||
$rgb = wp_tinycolor_rgb_to_rgb(
|
||||
array(
|
||||
'r' => base_convert( $match[1] . $match[1], 16, 10 ),
|
||||
'g' => base_convert( $match[2] . $match[2], 16, 10 ),
|
||||
'b' => base_convert( $match[3] . $match[3], 16, 10 ),
|
||||
)
|
||||
);
|
||||
|
||||
$rgb['a'] = 1;
|
||||
|
||||
return $rgb;
|
||||
}
|
||||
|
||||
/*
|
||||
* The JS color picker considers the string "transparent" to be a hex value,
|
||||
* so we need to handle it here as a special case.
|
||||
*/
|
||||
if ( 'transparent' === $color_str ) {
|
||||
return array(
|
||||
'r' => 0,
|
||||
'g' => 0,
|
||||
'b' => 0,
|
||||
'a' => 0,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -313,6 +384,95 @@ function wp_register_duotone_support( $block_type ) {
|
|||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Renders the duotone filter SVG and returns the CSS filter property to
|
||||
* reference the rendered SVG.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param array $preset Duotone preset value as seen in theme.json.
|
||||
* @return string Duotone CSS filter property.
|
||||
*/
|
||||
function wp_render_duotone_filter_preset( $preset ) {
|
||||
$duotone_id = $preset['slug'];
|
||||
$duotone_colors = $preset['colors'];
|
||||
$filter_id = 'wp-duotone-' . $duotone_id;
|
||||
$duotone_values = array(
|
||||
'r' => array(),
|
||||
'g' => array(),
|
||||
'b' => array(),
|
||||
'a' => array(),
|
||||
);
|
||||
foreach ( $duotone_colors as $color_str ) {
|
||||
$color = wp_tinycolor_string_to_rgb( $color_str );
|
||||
|
||||
$duotone_values['r'][] = $color['r'] / 255;
|
||||
$duotone_values['g'][] = $color['g'] / 255;
|
||||
$duotone_values['b'][] = $color['b'] / 255;
|
||||
$duotone_values['a'][] = $color['a'];
|
||||
}
|
||||
|
||||
ob_start();
|
||||
|
||||
?>
|
||||
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 0 0"
|
||||
width="0"
|
||||
height="0"
|
||||
focusable="false"
|
||||
role="none"
|
||||
style="visibility: hidden; position: absolute; left: -9999px; overflow: hidden;"
|
||||
>
|
||||
<defs>
|
||||
<filter id="<?php echo esc_attr( $filter_id ); ?>">
|
||||
<feColorMatrix
|
||||
color-interpolation-filters="sRGB"
|
||||
type="matrix"
|
||||
values="
|
||||
.299 .587 .114 0 0
|
||||
.299 .587 .114 0 0
|
||||
.299 .587 .114 0 0
|
||||
.299 .587 .114 0 0
|
||||
"
|
||||
/>
|
||||
<feComponentTransfer color-interpolation-filters="sRGB" >
|
||||
<feFuncR type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['r'] ) ); ?>" />
|
||||
<feFuncG type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['g'] ) ); ?>" />
|
||||
<feFuncB type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['b'] ) ); ?>" />
|
||||
<feFuncA type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['a'] ) ); ?>" />
|
||||
</feComponentTransfer>
|
||||
<feComposite in2="SourceGraphic" operator="in" />
|
||||
</filter>
|
||||
</defs>
|
||||
</svg>
|
||||
|
||||
<?php
|
||||
|
||||
$svg = ob_get_clean();
|
||||
|
||||
if ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) {
|
||||
// Clean up the whitespace.
|
||||
$svg = preg_replace( "/[\r\n\t ]+/", ' ', $svg );
|
||||
$svg = preg_replace( '/> </', '><', $svg );
|
||||
$svg = trim( $svg );
|
||||
}
|
||||
|
||||
add_action(
|
||||
/*
|
||||
* Safari doesn't render SVG filters defined in data URIs,
|
||||
* and SVG filters won't render in the head of a document,
|
||||
* so the next best place to put the SVG is in the footer.
|
||||
*/
|
||||
is_admin() ? 'admin_footer' : 'wp_footer',
|
||||
static function () use ( $svg ) {
|
||||
echo $svg;
|
||||
}
|
||||
);
|
||||
|
||||
return "url('#" . $filter_id . "')";
|
||||
}
|
||||
|
||||
/**
|
||||
* Render out the duotone stylesheet and SVG.
|
||||
|
|
|
@ -11,6 +11,10 @@
|
|||
* Class that abstracts the processing of the different data sources
|
||||
* for site-level config and offers an API to work with them.
|
||||
*
|
||||
* This class is for internal core usage and is not supposed to be used by extenders (plugins and/or themes).
|
||||
* This is a low-level API that may need to do breaking changes. Please,
|
||||
* use get_global_settings, get_global_styles, and get_global_stylesheet instead.
|
||||
*
|
||||
* @access private
|
||||
*/
|
||||
class WP_Theme_JSON_Resolver {
|
||||
|
@ -40,9 +44,27 @@ class WP_Theme_JSON_Resolver {
|
|||
private static $theme_has_support = null;
|
||||
|
||||
/**
|
||||
* Container to keep loaded i18n schema for `theme.json`.
|
||||
* Container for data coming from the user.
|
||||
*
|
||||
* @since 5.9.0
|
||||
* @var WP_Theme_JSON
|
||||
*/
|
||||
private static $user = null;
|
||||
|
||||
/**
|
||||
* Stores the ID of the custom post type
|
||||
* that holds the user data.
|
||||
*
|
||||
* @since 5.9.0
|
||||
* @var integer
|
||||
*/
|
||||
private static $user_custom_post_type_id = null;
|
||||
|
||||
/**
|
||||
* Container to keep loaded i18n schema for `theme.json`.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @since 5.9.0 Renamed from $theme_json_i18n
|
||||
* @var array
|
||||
*/
|
||||
private static $i18n_schema = null;
|
||||
|
@ -122,34 +144,45 @@ class WP_Theme_JSON_Resolver {
|
|||
/**
|
||||
* Returns the theme's data.
|
||||
*
|
||||
* Data from theme.json can be augmented via the $theme_support_data variable.
|
||||
* This is useful, for example, to backfill the gaps in theme.json that a theme
|
||||
* has declared via add_theme_supports.
|
||||
*
|
||||
* Note that if the same data is present in theme.json and in $theme_support_data,
|
||||
* the theme.json's is not overwritten.
|
||||
* Data from theme.json will be backfilled from existing
|
||||
* theme supports, if any. Note that if the same data
|
||||
* is present in theme.json and in theme supports,
|
||||
* the theme.json takes precendence.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @since 5.9.0 Theme supports have been inlined and the argument removed.
|
||||
*
|
||||
* @param array $theme_support_data Optional. Theme support data in theme.json format.
|
||||
* Default empty array.
|
||||
* @return WP_Theme_JSON Entity that holds theme data.
|
||||
*/
|
||||
public static function get_theme_data( $theme_support_data = array() ) {
|
||||
public static function get_theme_data( $deprecated = array() ) {
|
||||
if ( ! empty( $deprecated ) ) {
|
||||
_deprecated_argument( __METHOD__, '5.9' );
|
||||
}
|
||||
if ( null === self::$theme ) {
|
||||
$theme_json_data = self::read_json_file( self::get_file_path_from_theme( 'theme.json' ) );
|
||||
$theme_json_data = self::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
|
||||
self::$theme = new WP_Theme_JSON( $theme_json_data );
|
||||
}
|
||||
|
||||
if ( empty( $theme_support_data ) ) {
|
||||
return self::$theme;
|
||||
if ( wp_get_theme()->parent() ) {
|
||||
// Get parent theme.json.
|
||||
$parent_theme_json_data = self::read_json_file( self::get_file_path_from_theme( 'theme.json', true ) );
|
||||
$parent_theme_json_data = self::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) );
|
||||
$parent_theme = new WP_Theme_JSON( $parent_theme_json_data );
|
||||
|
||||
// Merge the child theme.json into the parent theme.json.
|
||||
// The child theme takes precedence over the parent.
|
||||
$parent_theme->merge( self::$theme );
|
||||
self::$theme = $parent_theme;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* We want the presets and settings declared in theme.json
|
||||
* to override the ones declared via add_theme_support.
|
||||
*/
|
||||
* We want the presets and settings declared in theme.json
|
||||
* to override the ones declared via theme supports.
|
||||
* So we take theme supports, transform it to theme.json shape
|
||||
* and merge the self::$theme upon that.
|
||||
*/
|
||||
$theme_support_data = WP_Theme_JSON::get_from_editor_settings( get_default_block_editor_settings() );
|
||||
$with_theme_supports = new WP_Theme_JSON( $theme_support_data );
|
||||
$with_theme_supports->merge( self::$theme );
|
||||
|
||||
|
@ -157,40 +190,180 @@ class WP_Theme_JSON_Resolver {
|
|||
}
|
||||
|
||||
/**
|
||||
* There are different sources of data for a site: core and theme.
|
||||
* Returns the CPT that contains the user's origin config
|
||||
* for the current theme or a void array if none found.
|
||||
*
|
||||
* While the getters {@link get_core_data}, {@link get_theme_data} return the raw data
|
||||
* from the respective origins, this method merges them all together.
|
||||
* It can also create and return a new draft CPT.
|
||||
*
|
||||
* If the same piece of data is declared in different origins (core and theme),
|
||||
* the last origin overrides the previous. For example, if core disables custom colors
|
||||
* but a theme enables them, the theme config wins.
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param bool $should_create_cpt Optional. Whether a new CPT should be created if no one was found.
|
||||
* False by default.
|
||||
* @param array $post_status_filter Filter Optional. CPT by post status.
|
||||
* ['publish'] by default, so it only fetches published posts.
|
||||
*
|
||||
* @return array Custom Post Type for the user's origin config.
|
||||
*/
|
||||
private static function get_user_data_from_custom_post_type( $should_create_cpt = false, $post_status_filter = array( 'publish' ) ) {
|
||||
$user_cpt = array();
|
||||
$post_type_filter = 'wp_global_styles';
|
||||
$query = new WP_Query(
|
||||
array(
|
||||
'posts_per_page' => 1,
|
||||
'orderby' => 'date',
|
||||
'order' => 'desc',
|
||||
'post_type' => $post_type_filter,
|
||||
'post_status' => $post_status_filter,
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => 'wp_theme',
|
||||
'field' => 'name',
|
||||
'terms' => wp_get_theme()->get_stylesheet(),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
if ( is_array( $query->posts ) && ( 1 === $query->post_count ) ) {
|
||||
$user_cpt = $query->posts[0]->to_array();
|
||||
} elseif ( $should_create_cpt ) {
|
||||
$cpt_post_id = wp_insert_post(
|
||||
array(
|
||||
'post_content' => '{"version": ' . WP_Theme_JSON::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }',
|
||||
'post_status' => 'publish',
|
||||
'post_title' => __( 'Custom Styles', 'default' ),
|
||||
'post_type' => $post_type_filter,
|
||||
'post_name' => 'wp-global-styles-' . urlencode( wp_get_theme()->get_stylesheet() ),
|
||||
'tax_input' => array(
|
||||
'wp_theme' => array( wp_get_theme()->get_stylesheet() ),
|
||||
),
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
if ( is_wp_error( $cpt_post_id ) ) {
|
||||
$user_cpt = array();
|
||||
} else {
|
||||
$user_cpt = get_post( $cpt_post_id, ARRAY_A );
|
||||
}
|
||||
}
|
||||
|
||||
return $user_cpt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the user's origin config.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @return WP_Theme_JSON Entity that holds user data.
|
||||
*/
|
||||
public static function get_user_data() {
|
||||
if ( null !== self::$user ) {
|
||||
return self::$user;
|
||||
}
|
||||
|
||||
$config = array();
|
||||
$user_cpt = self::get_user_data_from_custom_post_type();
|
||||
|
||||
if ( array_key_exists( 'post_content', $user_cpt ) ) {
|
||||
$decoded_data = json_decode( $user_cpt['post_content'], true );
|
||||
|
||||
$json_decoding_error = json_last_error();
|
||||
if ( JSON_ERROR_NONE !== $json_decoding_error ) {
|
||||
trigger_error( 'Error when decoding a theme.json schema for user data. ' . json_last_error_msg() );
|
||||
return new WP_Theme_JSON( $config, 'user' );
|
||||
}
|
||||
|
||||
// Very important to verify if the flag isGlobalStylesUserThemeJSON is true.
|
||||
// If is not true the content was not escaped and is not safe.
|
||||
if (
|
||||
is_array( $decoded_data ) &&
|
||||
isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) &&
|
||||
$decoded_data['isGlobalStylesUserThemeJSON']
|
||||
) {
|
||||
unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
|
||||
$config = $decoded_data;
|
||||
}
|
||||
}
|
||||
self::$user = new WP_Theme_JSON( $config, 'user' );
|
||||
|
||||
return self::$user;
|
||||
}
|
||||
|
||||
/**
|
||||
* There are three sources of data (origins) for a site:
|
||||
* core, theme, and user. The user's has higher priority
|
||||
* than the theme's, and the theme's higher than core's.
|
||||
*
|
||||
* Unlike the getters {@link get_core_data},
|
||||
* {@link get_theme_data}, and {@link get_user_data},
|
||||
* this method returns data after it has been merged
|
||||
* with the previous origins. This means that if the same piece of data
|
||||
* is declared in different origins (user, theme, and core),
|
||||
* the last origin overrides the previous.
|
||||
*
|
||||
* For example, if the user has set a background color
|
||||
* for the paragraph block, and the theme has done it as well,
|
||||
* the user preference wins.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @since 5.9.0 Add user data and change the arguments.
|
||||
*
|
||||
* @param array $settings Optional. Existing block editor settings. Default empty array.
|
||||
* @param string $origin Optional. To what level should we merge data.
|
||||
* Valid values are 'theme' or 'user'.
|
||||
* Default is 'user'.
|
||||
* @return WP_Theme_JSON
|
||||
*/
|
||||
public static function get_merged_data( $settings = array() ) {
|
||||
$theme_support_data = WP_Theme_JSON::get_from_editor_settings( $settings );
|
||||
public static function get_merged_data( $origin = 'user' ) {
|
||||
if ( is_array( $origin ) ) {
|
||||
_deprecated_argument( __FUNCTION__, '5.9' );
|
||||
}
|
||||
|
||||
$result = new WP_Theme_JSON();
|
||||
$result->merge( self::get_core_data() );
|
||||
$result->merge( self::get_theme_data( $theme_support_data ) );
|
||||
$result->merge( self::get_theme_data() );
|
||||
|
||||
if ( 'user' === $origin ) {
|
||||
$result->merge( self::get_user_data() );
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ID of the custom post type
|
||||
* that stores user data.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @return integer|null
|
||||
*/
|
||||
public static function get_user_custom_post_type_id() {
|
||||
if ( null !== self::$user_custom_post_type_id ) {
|
||||
return self::$user_custom_post_type_id;
|
||||
}
|
||||
|
||||
$user_cpt = self::get_user_data_from_custom_post_type( true );
|
||||
|
||||
if ( array_key_exists( 'ID', $user_cpt ) ) {
|
||||
self::$user_custom_post_type_id = $user_cpt['ID'];
|
||||
}
|
||||
|
||||
return self::$user_custom_post_type_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the current theme has a theme.json file.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @since 5.9.0 Also check in the parent theme.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function theme_has_support() {
|
||||
if ( ! isset( self::$theme_has_support ) ) {
|
||||
self::$theme_has_support = (bool) self::get_file_path_from_theme( 'theme.json' );
|
||||
self::$theme_has_support = is_readable( get_theme_file_path( 'theme.json' ) );
|
||||
}
|
||||
|
||||
return self::$theme_has_support;
|
||||
|
@ -202,35 +375,32 @@ class WP_Theme_JSON_Resolver {
|
|||
* If it isn't, returns an empty string, otherwise returns the whole file path.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @since 5.9.0 Adapt to work with child themes.
|
||||
*
|
||||
* @param string $file_name Name of the file.
|
||||
* @param bool $template Optional. Use template theme directory. Default false.
|
||||
* @return string The whole file path or empty if the file doesn't exist.
|
||||
*/
|
||||
private static function get_file_path_from_theme( $file_name ) {
|
||||
/*
|
||||
* This used to be a locate_template call. However, that method proved problematic
|
||||
* due to its use of constants (STYLESHEETPATH) that threw errors in some scenarios.
|
||||
*
|
||||
* When the theme.json merge algorithm properly supports child themes,
|
||||
* this should also fall back to the template path, as locate_template did.
|
||||
*/
|
||||
$located = '';
|
||||
$candidate = get_stylesheet_directory() . '/' . $file_name;
|
||||
if ( is_readable( $candidate ) ) {
|
||||
$located = $candidate;
|
||||
}
|
||||
return $located;
|
||||
private static function get_file_path_from_theme( $file_name, $template = false ) {
|
||||
$path = $template ? get_template_directory() : get_stylesheet_directory();
|
||||
$candidate = $path . '/' . $file_name;
|
||||
|
||||
return is_readable( $candidate ) ? $candidate : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans the cached data so it can be recalculated.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @since 5.9.0 Added new variables to reset.
|
||||
*/
|
||||
public static function clean_cached_data() {
|
||||
self::$core = null;
|
||||
self::$theme = null;
|
||||
self::$theme_has_support = null;
|
||||
self::$core = null;
|
||||
self::$theme = null;
|
||||
self::$user = null;
|
||||
self::$user_custom_post_type_id = null;
|
||||
self::$theme_has_support = null;
|
||||
self::$i18n_schema = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,141 @@
|
|||
<?php
|
||||
/**
|
||||
* WP_Theme_JSON_Schema class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Theme
|
||||
* @since 5.9.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class that migrates a given theme.json structure to the latest schema.
|
||||
*
|
||||
* This class is for internal core usage and is not supposed to be used by extenders (plugins and/or themes).
|
||||
* This is a low-level API that may need to do breaking changes. Please,
|
||||
* use get_global_settings, get_global_styles, and get_global_stylesheet instead.
|
||||
*
|
||||
* @access private
|
||||
*/
|
||||
class WP_Theme_JSON_Schema {
|
||||
|
||||
/**
|
||||
* Maps old properties to their new location within the schema's settings.
|
||||
* This will be applied at both the defaults and individual block levels.
|
||||
*/
|
||||
const V1_TO_V2_RENAMED_PATHS = array(
|
||||
'border.customRadius' => 'border.radius',
|
||||
'spacing.customMargin' => 'spacing.margin',
|
||||
'spacing.customPadding' => 'spacing.padding',
|
||||
'typography.customLineHeight' => 'typography.lineHeight',
|
||||
);
|
||||
|
||||
/**
|
||||
* Function that migrates a given theme.json structure to the last version.
|
||||
*
|
||||
* @param array $theme_json The structure to migrate.
|
||||
*
|
||||
* @return array The structure in the last version.
|
||||
*/
|
||||
public static function migrate( $theme_json ) {
|
||||
if ( ! isset( $theme_json['version'] ) ) {
|
||||
$theme_json = array(
|
||||
'version' => WP_Theme_JSON::LATEST_SCHEMA,
|
||||
);
|
||||
}
|
||||
|
||||
if ( 1 === $theme_json['version'] ) {
|
||||
$theme_json = self::migrate_v1_to_v2( $theme_json );
|
||||
}
|
||||
|
||||
return $theme_json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the custom prefixes for a few properties
|
||||
* that were part of v1:
|
||||
*
|
||||
* 'border.customRadius' => 'border.radius',
|
||||
* 'spacing.customMargin' => 'spacing.margin',
|
||||
* 'spacing.customPadding' => 'spacing.padding',
|
||||
* 'typography.customLineHeight' => 'typography.lineHeight',
|
||||
*
|
||||
* @param array $old Data to migrate.
|
||||
*
|
||||
* @return array Data without the custom prefixes.
|
||||
*/
|
||||
private static function migrate_v1_to_v2( $old ) {
|
||||
// Copy everything.
|
||||
$new = $old;
|
||||
|
||||
// Overwrite the things that changed.
|
||||
if ( isset( $old['settings'] ) ) {
|
||||
$new['settings'] = self::rename_paths( $old['settings'], self::V1_TO_V2_RENAMED_PATHS );
|
||||
}
|
||||
|
||||
// Set the new version.
|
||||
$new['version'] = 2;
|
||||
|
||||
return $new;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the settings subtree.
|
||||
*
|
||||
* @param array $settings Array to process.
|
||||
* @param array $paths_to_rename Paths to rename.
|
||||
*
|
||||
* @return array The settings in the new format.
|
||||
*/
|
||||
private static function rename_paths( $settings, $paths_to_rename ) {
|
||||
$new_settings = $settings;
|
||||
|
||||
// Process any renamed/moved paths within default settings.
|
||||
self::rename_settings( $new_settings, $paths_to_rename );
|
||||
|
||||
// Process individual block settings.
|
||||
if ( isset( $new_settings['blocks'] ) && is_array( $new_settings['blocks'] ) ) {
|
||||
foreach ( $new_settings['blocks'] as &$block_settings ) {
|
||||
self::rename_settings( $block_settings, $paths_to_rename );
|
||||
}
|
||||
}
|
||||
|
||||
return $new_settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes a settings array, renaming or moving properties.
|
||||
*
|
||||
* @param array $settings Reference to settings either defaults or an individual block's.
|
||||
* @param arary $paths_to_rename Paths to rename.
|
||||
*/
|
||||
private static function rename_settings( &$settings, $paths_to_rename ) {
|
||||
foreach ( $paths_to_rename as $original => $renamed ) {
|
||||
$original_path = explode( '.', $original );
|
||||
$renamed_path = explode( '.', $renamed );
|
||||
$current_value = _wp_array_get( $settings, $original_path, null );
|
||||
|
||||
if ( null !== $current_value ) {
|
||||
_wp_array_set( $settings, $renamed_path, $current_value );
|
||||
self::unset_setting_by_path( $settings, $original_path );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a property from within the provided settings by its path.
|
||||
*
|
||||
* @param array $settings Reference to the current settings array.
|
||||
* @param array $path Path to the property to be removed.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private static function unset_setting_by_path( &$settings, $path ) {
|
||||
$tmp_settings = &$settings; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
|
||||
$last_key = array_pop( $path );
|
||||
foreach ( $path as $key ) {
|
||||
$tmp_settings = &$tmp_settings[ $key ];
|
||||
}
|
||||
|
||||
unset( $tmp_settings[ $last_key ] );
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -336,6 +336,7 @@ add_action( 'current_screen', '_load_remote_block_patterns' );
|
|||
add_action( 'init', 'check_theme_switched', 99 );
|
||||
add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 );
|
||||
add_action( 'switch_theme', array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) );
|
||||
add_action( 'start_previewing_theme', array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) );
|
||||
add_action( 'after_switch_theme', '_wp_menus_changed' );
|
||||
add_action( 'after_switch_theme', '_wp_sidebars_changed' );
|
||||
add_action( 'wp_print_styles', 'print_emoji_styles' );
|
||||
|
|
|
@ -2260,6 +2260,8 @@ function safecss_filter_attr( $css, $deprecated = '' ) {
|
|||
'border-bottom-color',
|
||||
'border-bottom-style',
|
||||
'border-bottom-width',
|
||||
'border-bottom-right-radius',
|
||||
'border-bottom-left-radius',
|
||||
'border-left',
|
||||
'border-left-color',
|
||||
'border-left-style',
|
||||
|
@ -2268,6 +2270,8 @@ function safecss_filter_attr( $css, $deprecated = '' ) {
|
|||
'border-top-color',
|
||||
'border-top-style',
|
||||
'border-top-width',
|
||||
'border-top-left-radius',
|
||||
'border-top-right-radius',
|
||||
|
||||
'border-spacing',
|
||||
'border-collapse',
|
||||
|
@ -2282,6 +2286,7 @@ function safecss_filter_attr( $css, $deprecated = '' ) {
|
|||
'column-width',
|
||||
|
||||
'color',
|
||||
'filter',
|
||||
'font',
|
||||
'font-family',
|
||||
'font-size',
|
||||
|
|
|
@ -2321,8 +2321,7 @@ function wp_enqueue_global_styles() {
|
|||
}
|
||||
|
||||
if ( null === $stylesheet ) {
|
||||
$settings = get_default_block_editor_settings();
|
||||
$theme_json = WP_Theme_JSON_Resolver::get_merged_data( $settings );
|
||||
$theme_json = WP_Theme_JSON_Resolver::get_merged_data();
|
||||
$stylesheet = $theme_json->get_stylesheet();
|
||||
|
||||
if ( $can_use_cache ) {
|
||||
|
|
|
@ -5,6 +5,11 @@
|
|||
{
|
||||
"name": "Font size name"
|
||||
}
|
||||
],
|
||||
"fontFamilies": [
|
||||
{
|
||||
"name": "Font family name"
|
||||
}
|
||||
]
|
||||
},
|
||||
"color": {
|
||||
|
@ -31,6 +36,11 @@
|
|||
{
|
||||
"name": "Font size name"
|
||||
}
|
||||
],
|
||||
"fontFamilies": [
|
||||
{
|
||||
"name": "Font family name"
|
||||
}
|
||||
]
|
||||
},
|
||||
"color": {
|
||||
|
@ -47,5 +57,15 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"customTemplates": [
|
||||
{
|
||||
"title": "Custom template name"
|
||||
}
|
||||
],
|
||||
"templateParts": [
|
||||
{
|
||||
"title": "Template part name"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -1,14 +1,19 @@
|
|||
{
|
||||
"version": 1,
|
||||
"version": 2,
|
||||
"settings": {
|
||||
"border": {
|
||||
"customRadius": false
|
||||
"color": false,
|
||||
"radius": false,
|
||||
"style": false,
|
||||
"width": false
|
||||
},
|
||||
"color": {
|
||||
"custom": true,
|
||||
"customDuotone": true,
|
||||
"customGradient": true,
|
||||
"link": false,
|
||||
"background": true,
|
||||
"text": true,
|
||||
"duotone": [
|
||||
{
|
||||
"name": "Dark grayscale" ,
|
||||
|
@ -177,14 +182,20 @@
|
|||
]
|
||||
},
|
||||
"spacing": {
|
||||
"customMargin": false,
|
||||
"customPadding": false,
|
||||
"blockGap": null,
|
||||
"margin": false,
|
||||
"padding": false,
|
||||
"units": [ "px", "em", "rem", "vh", "vw", "%" ]
|
||||
},
|
||||
"typography": {
|
||||
"customFontSize": true,
|
||||
"customLineHeight": false,
|
||||
"dropCap": true,
|
||||
"fontStyle": true,
|
||||
"fontWeight": true,
|
||||
"letterSpacing": true,
|
||||
"lineHeight": false,
|
||||
"textDecoration": true,
|
||||
"textTransform": true,
|
||||
"fontSizes": [
|
||||
{
|
||||
"name": "Small",
|
||||
|
@ -216,9 +227,20 @@
|
|||
"blocks": {
|
||||
"core/button": {
|
||||
"border": {
|
||||
"customRadius": true
|
||||
"radius": true
|
||||
}
|
||||
},
|
||||
"core/pullquote": {
|
||||
"border": {
|
||||
"color": true,
|
||||
"radius": true,
|
||||
"style": true,
|
||||
"width": true
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"styles": {
|
||||
"spacing": { "blockGap": "24px" }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '5.9-alpha-52048';
|
||||
$wp_version = '5.9-alpha-52049';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
|
@ -170,6 +170,7 @@ require ABSPATH . WPINC . '/query.php';
|
|||
require ABSPATH . WPINC . '/class-wp-date-query.php';
|
||||
require ABSPATH . WPINC . '/theme.php';
|
||||
require ABSPATH . WPINC . '/class-wp-theme.php';
|
||||
require ABSPATH . WPINC . '/class-wp-theme-json-schema.php';
|
||||
require ABSPATH . WPINC . '/class-wp-theme-json.php';
|
||||
require ABSPATH . WPINC . '/class-wp-theme-json-resolver.php';
|
||||
require ABSPATH . WPINC . '/class-wp-block-template.php';
|
||||
|
|
Loading…
Reference in New Issue