Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../public_h.../wp-inclu...
File: class-wp-theme-json-schema.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* WP_Theme_JSON_Schema class
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage Theme
[5] Fix | Delete
* @since 5.9.0
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Class that migrates a given theme.json structure to the latest schema.
[10] Fix | Delete
*
[11] Fix | Delete
* This class is for internal core usage and is not supposed to be used by extenders (plugins and/or themes).
[12] Fix | Delete
* This is a low-level API that may need to do breaking changes. Please,
[13] Fix | Delete
* use get_global_settings, get_global_styles, and get_global_stylesheet instead.
[14] Fix | Delete
*
[15] Fix | Delete
* @since 5.9.0
[16] Fix | Delete
* @access private
[17] Fix | Delete
*/
[18] Fix | Delete
#[AllowDynamicProperties]
[19] Fix | Delete
class WP_Theme_JSON_Schema {
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* Maps old properties to their new location within the schema's settings.
[23] Fix | Delete
* This will be applied at both the defaults and individual block levels.
[24] Fix | Delete
*/
[25] Fix | Delete
const V1_TO_V2_RENAMED_PATHS = array(
[26] Fix | Delete
'border.customRadius' => 'border.radius',
[27] Fix | Delete
'spacing.customMargin' => 'spacing.margin',
[28] Fix | Delete
'spacing.customPadding' => 'spacing.padding',
[29] Fix | Delete
'typography.customLineHeight' => 'typography.lineHeight',
[30] Fix | Delete
);
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* Function that migrates a given theme.json structure to the last version.
[34] Fix | Delete
*
[35] Fix | Delete
* @since 5.9.0
[36] Fix | Delete
* @since 6.6.0 Migrate up to v3 and add $origin parameter.
[37] Fix | Delete
*
[38] Fix | Delete
* @param array $theme_json The structure to migrate.
[39] Fix | Delete
* @param string $origin Optional. What source of data this object represents.
[40] Fix | Delete
* One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'.
[41] Fix | Delete
* @return array The structure in the last version.
[42] Fix | Delete
*/
[43] Fix | Delete
public static function migrate( $theme_json, $origin = 'theme' ) {
[44] Fix | Delete
if ( ! isset( $theme_json['version'] ) ) {
[45] Fix | Delete
$theme_json = array(
[46] Fix | Delete
'version' => WP_Theme_JSON::LATEST_SCHEMA,
[47] Fix | Delete
);
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
// Migrate each version in order starting with the current version.
[51] Fix | Delete
switch ( $theme_json['version'] ) {
[52] Fix | Delete
case 1:
[53] Fix | Delete
$theme_json = self::migrate_v1_to_v2( $theme_json );
[54] Fix | Delete
// Deliberate fall through. Once migrated to v2, also migrate to v3.
[55] Fix | Delete
case 2:
[56] Fix | Delete
$theme_json = self::migrate_v2_to_v3( $theme_json, $origin );
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
return $theme_json;
[60] Fix | Delete
}
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* Removes the custom prefixes for a few properties
[64] Fix | Delete
* that were part of v1:
[65] Fix | Delete
*
[66] Fix | Delete
* 'border.customRadius' => 'border.radius',
[67] Fix | Delete
* 'spacing.customMargin' => 'spacing.margin',
[68] Fix | Delete
* 'spacing.customPadding' => 'spacing.padding',
[69] Fix | Delete
* 'typography.customLineHeight' => 'typography.lineHeight',
[70] Fix | Delete
*
[71] Fix | Delete
* @since 5.9.0
[72] Fix | Delete
*
[73] Fix | Delete
* @param array $old Data to migrate.
[74] Fix | Delete
*
[75] Fix | Delete
* @return array Data without the custom prefixes.
[76] Fix | Delete
*/
[77] Fix | Delete
private static function migrate_v1_to_v2( $old ) {
[78] Fix | Delete
// Copy everything.
[79] Fix | Delete
$new = $old;
[80] Fix | Delete
[81] Fix | Delete
// Overwrite the things that changed.
[82] Fix | Delete
if ( isset( $old['settings'] ) ) {
[83] Fix | Delete
$new['settings'] = self::rename_paths( $old['settings'], self::V1_TO_V2_RENAMED_PATHS );
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
// Set the new version.
[87] Fix | Delete
$new['version'] = 2;
[88] Fix | Delete
[89] Fix | Delete
return $new;
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
/**
[93] Fix | Delete
* Migrates from v2 to v3.
[94] Fix | Delete
*
[95] Fix | Delete
* - Sets settings.typography.defaultFontSizes to false if settings.typography.fontSizes are defined.
[96] Fix | Delete
* - Sets settings.spacing.defaultSpacingSizes to false if settings.spacing.spacingSizes are defined.
[97] Fix | Delete
* - Prevents settings.spacing.spacingSizes from merging with settings.spacing.spacingScale by
[98] Fix | Delete
* unsetting spacingScale when spacingSizes are defined.
[99] Fix | Delete
*
[100] Fix | Delete
* @since 6.6.0
[101] Fix | Delete
*
[102] Fix | Delete
* @param array $old Data to migrate.
[103] Fix | Delete
* @param string $origin What source of data this object represents.
[104] Fix | Delete
* One of 'blocks', 'default', 'theme', or 'custom'.
[105] Fix | Delete
* @return array Data with defaultFontSizes set to false.
[106] Fix | Delete
*/
[107] Fix | Delete
private static function migrate_v2_to_v3( $old, $origin ) {
[108] Fix | Delete
// Copy everything.
[109] Fix | Delete
$new = $old;
[110] Fix | Delete
[111] Fix | Delete
// Set the new version.
[112] Fix | Delete
$new['version'] = 3;
[113] Fix | Delete
[114] Fix | Delete
/*
[115] Fix | Delete
* Remaining changes do not need to be applied to the custom origin,
[116] Fix | Delete
* as they should take on the value of the theme origin.
[117] Fix | Delete
*/
[118] Fix | Delete
if ( 'custom' === $origin ) {
[119] Fix | Delete
return $new;
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
/*
[123] Fix | Delete
* Even though defaultFontSizes and defaultSpacingSizes are new
[124] Fix | Delete
* settings, we need to migrate them as they each control
[125] Fix | Delete
* PRESETS_METADATA prevent_override values which were previously
[126] Fix | Delete
* hardcoded to false. This only needs to happen when the theme provides
[127] Fix | Delete
* fontSizes or spacingSizes as they could match the default ones and
[128] Fix | Delete
* affect the generated CSS.
[129] Fix | Delete
*/
[130] Fix | Delete
if ( isset( $old['settings']['typography']['fontSizes'] ) ) {
[131] Fix | Delete
$new['settings']['typography']['defaultFontSizes'] = false;
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
/*
[135] Fix | Delete
* Similarly to defaultFontSizes, we need to migrate defaultSpacingSizes
[136] Fix | Delete
* as it controls the PRESETS_METADATA prevent_override which was
[137] Fix | Delete
* previously hardcoded to false. This only needs to happen when the
[138] Fix | Delete
* theme provided spacing sizes via spacingSizes or spacingScale.
[139] Fix | Delete
*/
[140] Fix | Delete
if (
[141] Fix | Delete
isset( $old['settings']['spacing']['spacingSizes'] ) ||
[142] Fix | Delete
isset( $old['settings']['spacing']['spacingScale'] )
[143] Fix | Delete
) {
[144] Fix | Delete
$new['settings']['spacing']['defaultSpacingSizes'] = false;
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
/*
[148] Fix | Delete
* In v3 spacingSizes is merged with the generated spacingScale sizes
[149] Fix | Delete
* instead of completely replacing them. The v3 behavior is what was
[150] Fix | Delete
* documented for the v2 schema, but the code never actually did work
[151] Fix | Delete
* that way. Instead of surprising users with a behavior change two
[152] Fix | Delete
* years after the fact at the same time as a v3 update is introduced,
[153] Fix | Delete
* we'll continue using the "bugged" behavior for v2 themes. And treat
[154] Fix | Delete
* the "bug fix" as a breaking change for v3.
[155] Fix | Delete
*/
[156] Fix | Delete
if ( isset( $old['settings']['spacing']['spacingSizes'] ) ) {
[157] Fix | Delete
unset( $new['settings']['spacing']['spacingScale'] );
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
return $new;
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
/**
[164] Fix | Delete
* Processes the settings subtree.
[165] Fix | Delete
*
[166] Fix | Delete
* @since 5.9.0
[167] Fix | Delete
*
[168] Fix | Delete
* @param array $settings Array to process.
[169] Fix | Delete
* @param array $paths_to_rename Paths to rename.
[170] Fix | Delete
*
[171] Fix | Delete
* @return array The settings in the new format.
[172] Fix | Delete
*/
[173] Fix | Delete
private static function rename_paths( $settings, $paths_to_rename ) {
[174] Fix | Delete
$new_settings = $settings;
[175] Fix | Delete
[176] Fix | Delete
// Process any renamed/moved paths within default settings.
[177] Fix | Delete
self::rename_settings( $new_settings, $paths_to_rename );
[178] Fix | Delete
[179] Fix | Delete
// Process individual block settings.
[180] Fix | Delete
if ( isset( $new_settings['blocks'] ) && is_array( $new_settings['blocks'] ) ) {
[181] Fix | Delete
foreach ( $new_settings['blocks'] as &$block_settings ) {
[182] Fix | Delete
self::rename_settings( $block_settings, $paths_to_rename );
[183] Fix | Delete
}
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
return $new_settings;
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
/**
[190] Fix | Delete
* Processes a settings array, renaming or moving properties.
[191] Fix | Delete
*
[192] Fix | Delete
* @since 5.9.0
[193] Fix | Delete
*
[194] Fix | Delete
* @param array $settings Reference to settings either defaults or an individual block's.
[195] Fix | Delete
* @param array $paths_to_rename Paths to rename.
[196] Fix | Delete
*/
[197] Fix | Delete
private static function rename_settings( &$settings, $paths_to_rename ) {
[198] Fix | Delete
foreach ( $paths_to_rename as $original => $renamed ) {
[199] Fix | Delete
$original_path = explode( '.', $original );
[200] Fix | Delete
$renamed_path = explode( '.', $renamed );
[201] Fix | Delete
$current_value = _wp_array_get( $settings, $original_path, null );
[202] Fix | Delete
[203] Fix | Delete
if ( null !== $current_value ) {
[204] Fix | Delete
_wp_array_set( $settings, $renamed_path, $current_value );
[205] Fix | Delete
self::unset_setting_by_path( $settings, $original_path );
[206] Fix | Delete
}
[207] Fix | Delete
}
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
/**
[211] Fix | Delete
* Removes a property from within the provided settings by its path.
[212] Fix | Delete
*
[213] Fix | Delete
* @since 5.9.0
[214] Fix | Delete
*
[215] Fix | Delete
* @param array $settings Reference to the current settings array.
[216] Fix | Delete
* @param array $path Path to the property to be removed.
[217] Fix | Delete
*/
[218] Fix | Delete
private static function unset_setting_by_path( &$settings, $path ) {
[219] Fix | Delete
$tmp_settings = &$settings;
[220] Fix | Delete
$last_key = array_pop( $path );
[221] Fix | Delete
foreach ( $path as $key ) {
[222] Fix | Delete
$tmp_settings = &$tmp_settings[ $key ];
[223] Fix | Delete
}
[224] Fix | Delete
[225] Fix | Delete
unset( $tmp_settings[ $last_key ] );
[226] Fix | Delete
}
[227] Fix | Delete
}
[228] Fix | Delete
[229] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function