Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 61 additions & 26 deletions includes/create-theme/resolver_additions.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ public static function export_theme_data( $content, $extra_theme_data = null ) {
$parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) );
$parent_theme_json_data = static::translate( $parent_theme_json_data, $current_theme->parent()->get( 'TextDomain' ) );

// Get the schema from the parent JSON.
$schema = $parent_theme_json_data['$schema'];
if ( array_key_exists( 'schema', $parent_theme_json_data ) ) {
$schema = $parent_theme_json_data['$schema'];
}

if ( class_exists( 'WP_Theme_JSON_Gutenberg' ) ) {
$parent_theme = new WP_Theme_JSON_Gutenberg( $parent_theme_json_data );
} else {
Expand All @@ -51,11 +45,6 @@ public static function export_theme_data( $content, $extra_theme_data = null ) {
$theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) );
$theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );

// Get the schema from the parent JSON.
if ( array_key_exists( 'schema', $theme_json_data ) ) {
$schema = $theme_json_data['$schema'];
}

if ( class_exists( 'WP_Theme_JSON_Gutenberg' ) ) {
$theme_theme = new WP_Theme_JSON_Gutenberg( $theme_json_data );
} else {
Expand All @@ -79,27 +68,73 @@ public static function export_theme_data( $content, $extra_theme_data = null ) {

$data = $theme->get_data();

// Add the schema.
if ( empty( $schema ) ) {
global $wp_version;
$theme_json_version = 'wp/' . substr( $wp_version, 0, 3 );
if ( defined( 'IS_GUTENBERG_PLUGIN' ) ) {
$theme_json_version = 'trunk';
// Ensure `$schema` is first and `version` second in the JSON output.
return static::stringify( static::add_schema_to_data( $data ) );
}

/**
* Resolve the `$schema` URL for the active theme's theme.json.
*
* Prefers the schema declared by the theme (or by its parent, for child
* themes) so the exported data matches the theme it was created from.
* Falls back to the schema for the running WordPress/Gutenberg version.
*
* @return string The theme.json schema URL.
*/
public static function get_schema() {
$current_theme = wp_get_theme();

if ( $current_theme->parent() ) {
$parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) );
if ( ! empty( $parent_theme_json_data['$schema'] ) ) {
return $parent_theme_json_data['$schema'];
}
$schema = 'https://schemas.wp.org/' . $theme_json_version . '/theme.json';
}
// Ensure $schema is the first property and version is second in the JSON output.

$theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) );
if ( ! empty( $theme_json_data['$schema'] ) ) {
return $theme_json_data['$schema'];
}

global $wp_version;
$theme_json_version = 'wp/' . substr( $wp_version, 0, 3 );
if ( defined( 'IS_GUTENBERG_PLUGIN' ) ) {
$theme_json_version = 'trunk';
}
return 'https://schemas.wp.org/' . $theme_json_version . '/theme.json';
}

/**
* Ensure `$schema` is first, `version` second, and `title` third.
*
* Centralises the ordering used when writing theme.json and its style
* variations so both files stay consistent and IDE-friendly.
*
* @param array $data Theme JSON data.
* @param string|null $schema Optional schema URL. Resolved from the theme when omitted.
* @return array The reordered data.
*/
public static function add_schema_to_data( $data, $schema = null ) {
// Preserve a schema already present in the data when none was passed.
if ( empty( $schema ) && ! empty( $data['$schema'] ) ) {
$schema = $data['$schema'];
}
if ( empty( $schema ) ) {
$schema = static::get_schema();
}
unset( $data['$schema'] );

$ordered_data = array( '$schema' => $schema );

// Add version as the second property if it exists.
if ( isset( $data['version'] ) ) {
$ordered_data['version'] = $data['version'];
unset( $data['version'] );
// Add `version` and `title` next so the file leads with metadata.
foreach ( array( 'version', 'title' ) as $key ) {
if ( isset( $data[ $key ] ) ) {
$ordered_data[ $key ] = $data[ $key ];
unset( $data[ $key ] );
}
}

// Merge the remaining data.
$data = array_merge( $ordered_data, $data );
return static::stringify( $data );
return array_merge( $ordered_data, $data );
}

/**
Expand Down
3 changes: 3 additions & 0 deletions includes/create-theme/theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public static function add_theme_json_variation_to_local( $export_type, $theme,
$variation['settings']['typography']['fontFamilies'] = $copied_font_families;
}

// Match theme.json by leading the variation with `$schema` and `version`.
$variation = CBT_Theme_JSON_Resolver::add_schema_to_data( $variation );

file_put_contents(
$variation_path . $theme['slug'] . '.json',
CBT_Theme_JSON_Resolver::stringify( $variation )
Expand Down
102 changes: 102 additions & 0 deletions tests/test-theme-json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* Tests for CBT_Theme_JSON and its schema handling.
*
* @package Create_Block_Theme
*/
class Test_Create_Block_Theme_JSON extends WP_UnitTestCase {

protected static $admin_id;

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$admin_id = $factory->user->create(
array(
'role' => 'administrator',
)
);
}

public function test_add_schema_to_data_orders_metadata_first() {
$data = array(
'version' => 3,
'settings' => array( 'color' => array() ),
'title' => 'Test',
);

$result = CBT_Theme_JSON_Resolver::add_schema_to_data( $data, 'https://schemas.wp.org/trunk/theme.json' );

$this->assertSame(
array( '$schema', 'version', 'title', 'settings' ),
array_keys( $result )
);
$this->assertSame( 'https://schemas.wp.org/trunk/theme.json', $result['$schema'] );
}

public function test_add_schema_to_data_without_version_or_title() {
$result = CBT_Theme_JSON_Resolver::add_schema_to_data(
array( 'settings' => array() ),
'https://schemas.wp.org/trunk/theme.json'
);

$this->assertSame( array( '$schema', 'settings' ), array_keys( $result ) );
}

public function test_style_variation_includes_schema_and_version() {
wp_set_current_user( self::$admin_id );

$theme_slug = $this->create_blank_theme();
$slug = 'cbt-schema-test';

$variation = CBT_Theme_JSON::add_theme_json_variation_to_local(
'variation',
array(
'name' => 'Schema Test',
'slug' => $slug,
)
);

$this->assertNotWPError( $variation );

$variation_file = get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'styles' . DIRECTORY_SEPARATOR . $slug . '.json';
$this->assertFileExists( $variation_file );

$decoded = json_decode( file_get_contents( $variation_file ), true );

// `$schema` must lead the file, followed by `version` and `title`.
$this->assertSame( array( '$schema', 'version', 'title' ), array_slice( array_keys( $decoded ), 0, 3 ) );
$this->assertNotEmpty( $decoded['$schema'] );

// The variation must declare the same schema as theme.json.
$theme_json = json_decode( CBT_Theme_JSON_Resolver::export_theme_data( 'current' ), true );
$this->assertSame( $theme_json['$schema'], $decoded['$schema'] );

$this->uninstall_theme( $theme_slug );
}

private function create_blank_theme() {

$test_theme_slug = 'cbttesttheme';

delete_theme( $test_theme_slug );

$request = new WP_REST_Request( 'POST', '/create-block-theme/v1/create-blank' );
$request->set_param( 'name', $test_theme_slug );
$request->set_param( 'description', '' );
$request->set_param( 'uri', '' );
$request->set_param( 'author', '' );
$request->set_param( 'author_uri', '' );
$request->set_param( 'tags_custom', '' );
$request->set_param( 'recommended_plugins', '' );

rest_do_request( $request );

CBT_Theme_JSON_Resolver::clean_cached_data();

return $test_theme_slug;
}

private function uninstall_theme( $theme_slug ) {
CBT_Theme_JSON_Resolver::write_user_settings( array() );
delete_theme( $theme_slug );
}
}
Loading