mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 12:20:22 +00:00
Editor: Add functionality required for theme export in the site editor
This bring across changes to theme export functionality, and related code, and tests. Relates issue in Gutenberg: https://github.com/WordPress/gutenberg/issues/39889. Props scruffian, timothyblynjacobs, oandregal, ajlende, zieleadam. See #55505. git-svn-id: https://develop.svn.wordpress.org/trunk@53129 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -357,10 +357,10 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase {
|
||||
// Open ZIP file and make sure the directories exist.
|
||||
$zip = new ZipArchive();
|
||||
$zip->open( $filename );
|
||||
$has_theme_dir = $zip->locateName( 'theme/' ) !== false;
|
||||
$has_block_templates_dir = $zip->locateName( 'theme/templates/' ) !== false;
|
||||
$has_block_template_parts_dir = $zip->locateName( 'theme/parts/' ) !== false;
|
||||
$this->assertTrue( $has_theme_dir, 'theme directory exists' );
|
||||
$has_theme_json = $zip->locateName( 'theme.json' ) !== false;
|
||||
$has_block_templates_dir = $zip->locateName( 'templates/' ) !== false;
|
||||
$has_block_template_parts_dir = $zip->locateName( 'parts/' ) !== false;
|
||||
$this->assertTrue( $has_theme_json, 'theme.json exists' );
|
||||
$this->assertTrue( $has_block_templates_dir, 'theme/templates directory exists' );
|
||||
$this->assertTrue( $has_block_template_parts_dir, 'theme/parts directory exists' );
|
||||
|
||||
|
||||
@@ -2173,4 +2173,91 @@ class Tests_Functions extends WP_UnitTestCase {
|
||||
|
||||
$this->assertEquals( 111, wp_filesize( $file ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 55505
|
||||
* @covers ::wp_recursive_ksort
|
||||
*/
|
||||
function test_wp_recursive_ksort() {
|
||||
// Create an array to test.
|
||||
$theme_json = array(
|
||||
'version' => 1,
|
||||
'settings' => array(
|
||||
'typography' => array(
|
||||
'fontFamilies' => array(
|
||||
'fontFamily' => 'DM Sans, sans-serif',
|
||||
'slug' => 'dm-sans',
|
||||
'name' => 'DM Sans',
|
||||
),
|
||||
),
|
||||
'color' => array(
|
||||
'palette' => array(
|
||||
array(
|
||||
'slug' => 'foreground',
|
||||
'color' => '#242321',
|
||||
'name' => 'Foreground',
|
||||
),
|
||||
array(
|
||||
'slug' => 'background',
|
||||
'color' => '#FCFBF8',
|
||||
'name' => 'Background',
|
||||
),
|
||||
array(
|
||||
'slug' => 'primary',
|
||||
'color' => '#71706E',
|
||||
'name' => 'Primary',
|
||||
),
|
||||
array(
|
||||
'slug' => 'tertiary',
|
||||
'color' => '#CFCFCF',
|
||||
'name' => 'Tertiary',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Sort the array.
|
||||
wp_recursive_ksort( $theme_json );
|
||||
|
||||
// Expected result.
|
||||
$expected_theme_json = array(
|
||||
'settings' => array(
|
||||
'color' => array(
|
||||
'palette' => array(
|
||||
array(
|
||||
'color' => '#242321',
|
||||
'name' => 'Foreground',
|
||||
'slug' => 'foreground',
|
||||
),
|
||||
array(
|
||||
'color' => '#FCFBF8',
|
||||
'name' => 'Background',
|
||||
'slug' => 'background',
|
||||
),
|
||||
array(
|
||||
'color' => '#71706E',
|
||||
'name' => 'Primary',
|
||||
'slug' => 'primary',
|
||||
),
|
||||
array(
|
||||
'color' => '#CFCFCF',
|
||||
'name' => 'Tertiary',
|
||||
'slug' => 'tertiary',
|
||||
),
|
||||
),
|
||||
),
|
||||
'typography' => array(
|
||||
'fontFamilies' => array(
|
||||
'fontFamily' => 'DM Sans, sans-serif',
|
||||
'name' => 'DM Sans',
|
||||
'slug' => 'dm-sans',
|
||||
),
|
||||
),
|
||||
),
|
||||
'version' => 1,
|
||||
);
|
||||
$this->assertEquals( $theme_json, $expected_theme_json );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -423,9 +423,9 @@ class Tests_Theme_wpThemeJson extends WP_UnitTestCase {
|
||||
)
|
||||
);
|
||||
|
||||
$expected = 'body { margin: 0; }body{--wp--style--block-gap: 1em;}.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }.wp-site-blocks > * { margin-top: 0; margin-bottom: 0; }.wp-site-blocks > * + * { margin-top: var( --wp--style--block-gap ); }';
|
||||
$this->assertSame( $expected, $theme_json->get_stylesheet() );
|
||||
$this->assertSame( $expected, $theme_json->get_stylesheet( array( 'styles' ) ) );
|
||||
$expected = 'body { margin: 0; }body{--wp--style--block-gap: 1em;}.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }.wp-site-blocks > * { margin-block-start: 0; margin-block-end: 0; }.wp-site-blocks > * + * { margin-block-start: var( --wp--style--block-gap ); }';
|
||||
$this->assertEquals( $expected, $theme_json->get_stylesheet() );
|
||||
$this->assertEquals( $expected, $theme_json->get_stylesheet( array( 'styles' ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -553,13 +553,13 @@ class Tests_Theme_wpThemeJson extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
$variables = 'body{--wp--preset--color--grey: grey;--wp--preset--font-family--small: 14px;--wp--preset--font-family--big: 41px;}.wp-block-group{--wp--custom--base-font: 16;--wp--custom--line-height--small: 1.2;--wp--custom--line-height--medium: 1.4;--wp--custom--line-height--large: 1.8;}';
|
||||
$styles = 'body { margin: 0; }body{color: var(--wp--preset--color--grey);}.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }.wp-site-blocks > * { margin-top: 0; margin-bottom: 0; }.wp-site-blocks > * + * { margin-top: var( --wp--style--block-gap ); }a{background-color: #333;color: #111;}.wp-block-group{border-radius: 10px;padding: 24px;}.wp-block-group a{color: #111;}h1,h2,h3,h4,h5,h6{color: #123456;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{background-color: #333;color: #111;font-size: 60px;}.wp-block-post-date{color: #123456;}.wp-block-post-date a{background-color: #777;color: #555;}.wp-block-image{border-top-left-radius: 10px;border-bottom-right-radius: 1em;margin-bottom: 30px;}';
|
||||
$styles = 'body { margin: 0; }body{color: var(--wp--preset--color--grey);}.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }.wp-site-blocks > * { margin-block-start: 0; margin-block-end: 0; }.wp-site-blocks > * + * { margin-block-start: var( --wp--style--block-gap ); }a{background-color: #333;color: #111;}.wp-block-group{border-radius: 10px;padding: 24px;}.wp-block-group a{color: #111;}h1,h2,h3,h4,h5,h6{color: #123456;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{background-color: #333;color: #111;font-size: 60px;}.wp-block-post-date{color: #123456;}.wp-block-post-date a{background-color: #777;color: #555;}.wp-block-image{border-top-left-radius: 10px;border-bottom-right-radius: 1em;margin-bottom: 30px;}';
|
||||
$presets = '.has-grey-color{color: var(--wp--preset--color--grey) !important;}.has-grey-background-color{background-color: var(--wp--preset--color--grey) !important;}.has-grey-border-color{border-color: var(--wp--preset--color--grey) !important;}.has-small-font-family{font-family: var(--wp--preset--font-family--small) !important;}.has-big-font-family{font-family: var(--wp--preset--font-family--big) !important;}';
|
||||
$all = $variables . $styles . $presets;
|
||||
$this->assertSame( $all, $theme_json->get_stylesheet() );
|
||||
$this->assertSame( $styles, $theme_json->get_stylesheet( array( 'styles' ) ) );
|
||||
$this->assertSame( $presets, $theme_json->get_stylesheet( array( 'presets' ) ) );
|
||||
$this->assertSame( $variables, $theme_json->get_stylesheet( array( 'variables' ) ) );
|
||||
$this->assertEquals( $all, $theme_json->get_stylesheet() );
|
||||
$this->assertEquals( $styles, $theme_json->get_stylesheet( array( 'styles' ) ) );
|
||||
$this->assertEquals( $presets, $theme_json->get_stylesheet( array( 'presets' ) ) );
|
||||
$this->assertEquals( $variables, $theme_json->get_stylesheet( array( 'variables' ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2384,4 +2384,241 @@ class Tests_Theme_wpThemeJson extends WP_UnitTestCase {
|
||||
$this->assertEqualSetsWithIndex( $expected, $actual );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 55505
|
||||
*/
|
||||
function test_export_data() {
|
||||
$theme = new WP_Theme_JSON(
|
||||
array(
|
||||
'version' => 2,
|
||||
'settings' => array(
|
||||
'color' => array(
|
||||
'palette' => array(
|
||||
array(
|
||||
'slug' => 'white',
|
||||
'color' => 'white',
|
||||
'label' => 'White',
|
||||
),
|
||||
array(
|
||||
'slug' => 'black',
|
||||
'color' => 'black',
|
||||
'label' => 'Black',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$user = new WP_Theme_JSON(
|
||||
array(
|
||||
'version' => 2,
|
||||
'settings' => array(
|
||||
'color' => array(
|
||||
'palette' => array(
|
||||
array(
|
||||
'slug' => 'white',
|
||||
'color' => '#fff',
|
||||
'label' => 'User White',
|
||||
),
|
||||
array(
|
||||
'slug' => 'hotpink',
|
||||
'color' => 'hotpink',
|
||||
'label' => 'hotpink',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
'custom'
|
||||
);
|
||||
|
||||
$theme->merge( $user );
|
||||
$actual = $theme->get_data();
|
||||
$expected = array(
|
||||
'version' => 2,
|
||||
'settings' => array(
|
||||
'color' => array(
|
||||
'palette' => array(
|
||||
array(
|
||||
'slug' => 'white',
|
||||
'color' => '#fff',
|
||||
'label' => 'User White',
|
||||
),
|
||||
array(
|
||||
'slug' => 'black',
|
||||
'color' => 'black',
|
||||
'label' => 'Black',
|
||||
),
|
||||
array(
|
||||
'slug' => 'hotpink',
|
||||
'color' => 'hotpink',
|
||||
'label' => 'hotpink',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEqualSetsWithIndex( $expected, $actual );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 55505
|
||||
*/
|
||||
function test_export_data_deals_with_empty_user_data() {
|
||||
$theme = new WP_Theme_JSON(
|
||||
array(
|
||||
'version' => 2,
|
||||
'settings' => array(
|
||||
'color' => array(
|
||||
'palette' => array(
|
||||
array(
|
||||
'slug' => 'white',
|
||||
'color' => 'white',
|
||||
'label' => 'White',
|
||||
),
|
||||
array(
|
||||
'slug' => 'black',
|
||||
'color' => 'black',
|
||||
'label' => 'Black',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$actual = $theme->get_data();
|
||||
$expected = array(
|
||||
'version' => 2,
|
||||
'settings' => array(
|
||||
'color' => array(
|
||||
'palette' => array(
|
||||
array(
|
||||
'slug' => 'white',
|
||||
'color' => 'white',
|
||||
'label' => 'White',
|
||||
),
|
||||
array(
|
||||
'slug' => 'black',
|
||||
'color' => 'black',
|
||||
'label' => 'Black',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEqualSetsWithIndex( $expected, $actual );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 55505
|
||||
*/
|
||||
function test_export_data_deals_with_empty_theme_data() {
|
||||
$user = new WP_Theme_JSON(
|
||||
array(
|
||||
'version' => 2,
|
||||
'settings' => array(
|
||||
'color' => array(
|
||||
'palette' => array(
|
||||
array(
|
||||
'slug' => 'white',
|
||||
'color' => '#fff',
|
||||
'label' => 'User White',
|
||||
),
|
||||
array(
|
||||
'slug' => 'hotpink',
|
||||
'color' => 'hotpink',
|
||||
'label' => 'hotpink',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
'custom'
|
||||
);
|
||||
|
||||
$actual = $user->get_data();
|
||||
$expected = array(
|
||||
'version' => 2,
|
||||
'settings' => array(
|
||||
'color' => array(
|
||||
'palette' => array(
|
||||
array(
|
||||
'slug' => 'white',
|
||||
'color' => '#fff',
|
||||
'label' => 'User White',
|
||||
),
|
||||
array(
|
||||
'slug' => 'hotpink',
|
||||
'color' => 'hotpink',
|
||||
'label' => 'hotpink',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEqualSetsWithIndex( $expected, $actual );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 55505
|
||||
*/
|
||||
function test_export_data_deals_with_empty_data() {
|
||||
$theme_v2 = new WP_Theme_JSON(
|
||||
array(
|
||||
'version' => 2,
|
||||
),
|
||||
'theme'
|
||||
);
|
||||
$actual_v2 = $theme_v2->get_data();
|
||||
$expected_v2 = array( 'version' => 2 );
|
||||
$this->assertEqualSetsWithIndex( $expected_v2, $actual_v2 );
|
||||
|
||||
$theme_v1 = new WP_Theme_JSON(
|
||||
array(
|
||||
'version' => 1,
|
||||
),
|
||||
'theme'
|
||||
);
|
||||
$actual_v1 = $theme_v1->get_data();
|
||||
$expected_v1 = array( 'version' => 2 );
|
||||
$this->assertEqualSetsWithIndex( $expected_v1, $actual_v1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 55505
|
||||
*/
|
||||
function test_export_data_sets_appearance_tools() {
|
||||
$theme = new WP_Theme_JSON(
|
||||
array(
|
||||
'version' => 2,
|
||||
'settings' => array(
|
||||
'appearanceTools' => true,
|
||||
'blocks' => array(
|
||||
'core/paragraph' => array(
|
||||
'appearanceTools' => true,
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$actual = $theme->get_data();
|
||||
$expected = array(
|
||||
'version' => 2,
|
||||
'settings' => array(
|
||||
'appearanceTools' => true,
|
||||
'blocks' => array(
|
||||
'core/paragraph' => array(
|
||||
'appearanceTools' => true,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEqualSetsWithIndex( $expected, $actual );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user