Skip to content

Commit

Permalink
Add blank theme creation to the site editor
Browse files Browse the repository at this point in the history
  • Loading branch information
scruffian committed Aug 1, 2022
1 parent 96c0528 commit b1fa460
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 21 deletions.
12 changes: 6 additions & 6 deletions admin/class-create-block-theme-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,17 @@ function create_blank_theme( $theme ) {
if ( ! file_exists( $blank_theme_path ) ) {
mkdir( $blank_theme_path, 0755 );
// Add readme.txt.
file_put_contents(
$blank_theme_path . DIRECTORY_SEPARATOR . 'readme.txt',
file_put_contents(
$blank_theme_path . DIRECTORY_SEPARATOR . 'readme.txt',
$this->build_readme_txt( $theme )
);

// Add new metadata.
$css_contents = $this->build_child_style_css( $theme );

// Add style.css.
file_put_contents(
$blank_theme_path . DIRECTORY_SEPARATOR . 'style.css',
file_put_contents(
$blank_theme_path . DIRECTORY_SEPARATOR . 'style.css',
$css_contents
);

Expand Down Expand Up @@ -344,7 +344,7 @@ function add_theme_json_variation_to_local ( $export_type, $theme ) {
if ( ! file_exists( $variation_path ) ) {
mkdir( $variation_path, 0755, true );
}

if ( file_exists( $variation_path . $variation_slug . '.json' ) ) {
$file_counter++;
while ( file_exists( $variation_path . $variation_slug . '_' . $file_counter . '.json' ) ) {
Expand All @@ -354,7 +354,7 @@ function add_theme_json_variation_to_local ( $export_type, $theme ) {
}

$_GET['theme']['variation_slug'] = $variation_slug;

file_put_contents(
$variation_path . $variation_slug . '.json',
MY_Theme_JSON_Resolver::export_theme_data( $export_type )
Expand Down
2 changes: 1 addition & 1 deletion build/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-api-fetch', 'wp-components', 'wp-data', 'wp-edit-site', 'wp-element', 'wp-i18n', 'wp-notices', 'wp-plugins', 'wp-primitives'), 'version' => 'abc73ef8d192f41a5e6a');
<?php return array('dependencies' => array('wp-api-fetch', 'wp-components', 'wp-data', 'wp-edit-site', 'wp-element', 'wp-i18n', 'wp-notices', 'wp-plugins', 'wp-primitives'), 'version' => 'efb26a3f1a231e36b65f');
86 changes: 82 additions & 4 deletions build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/index.js.map

Large diffs are not rendered by default.

58 changes: 56 additions & 2 deletions create-block-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,17 @@ function create_variation( $data ) {
$plugin_admin->save_variation( 'all', $data );
}
$plugin_admin->clear_user_customizations();

$result = new stdClass();
$result->variation = $data['variation'];
return $result;
$res = new WP_REST_Response( $result );
$res->set_status( 200 );
return [ 'req' => $res ];
}

add_action( 'rest_api_init', function () {
register_rest_route( 'create-block-theme/v1', '/variation/(?P<variation>[a-z0-9-]++)', array(
'methods' => 'GET',
'methods' => 'GET', // TODO - should be POST
'callback' => 'create_variation',
'permission_callback' => function () {
return current_user_can( 'edit_others_posts' );
Expand All @@ -70,6 +73,57 @@ function create_variation( $data ) {
) );
} );

function create_blank_theme( $data ) {
$plugin_admin = new Create_Block_Theme_Admin();
$plugin_admin->create_blank_theme( $data );
$result = new stdClass();
$result->name = $data['name'];
$res = new WP_REST_Response( $result );
$res->set_status( 200 );
return [ 'req' => $res ];

}

add_action( 'rest_api_init', function () {
register_rest_route( 'create-block-theme/v1', '/blank-theme', array(
'methods' => 'POST',
'callback' => 'create_blank_theme',
'permission_callback' => function () {
return current_user_can( 'edit_others_posts' );
},
'args' => array(
'name' => array(
'validate_callback' => function($param, $request, $key) {
return is_string( $param );
}
),
'description' => array(
'validate_callback' => function($param, $request, $key) {
return is_string( $param );
}
),
'uri' => array(
'validate_callback' => function($param, $request, $key) {
return is_string( $param );
}
),
'author' => array(
'validate_callback' => function($param, $request, $key) {
return is_string( $param );
}
),
'author_uri' => array(
'validate_callback' => function($param, $request, $key) {
return is_string( $param );
}
),
),
) );
} );




function create_block_theme_enqueue() {
$asset_file = include( plugin_dir_path( __FILE__ ) . 'build/index.asset.php');

Expand Down
61 changes: 54 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { blockDefault } from '@wordpress/icons';
import { Fragment, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import apiFetch from '@wordpress/api-fetch';
import { Button, MenuGroup, MenuItem, TextControl } from '@wordpress/components';
import { Button, MenuGroup, MenuItem, PanelBody, TextControl } from '@wordpress/components';
import { store as noticesStore } from '@wordpress/notices';
import { useDispatch } from '@wordpress/data';

Expand All @@ -26,8 +26,11 @@ const StyleVariations = () => {

// Reload variations
// We need to invalidate and maybe refetch __experimentalGetCurrentThemeGlobalStylesVariations(),
if ( response.req.status === 200 ) {
createErrorNotice( __( 'Variation created successfully' ), { type: 'snackbar' } );
}

} catch ( errorResponse ) {
} catch ( error ) {
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
Expand All @@ -37,15 +40,58 @@ const StyleVariations = () => {
}

return (
<MenuGroup label={ __( 'Create style variation' ) }>
<MenuItem>
<PanelBody>
<MenuGroup label={ __( 'Create style variation' ) }>
<TextControl onChange={ ( value ) => setVariationName( value ) } placeholder={ __( 'Variation name' ) }></TextControl>
<Button onClick={ () => createVariation() }>{ __( 'Create variation' ) }</Button>
</MenuItem>
</MenuGroup>
<Button variant="secondary" onClick={ () => createVariation() }>{ __( 'Create variation' ) }</Button>
</MenuGroup>
</PanelBody>
);
};

const BlankTheme = () => {
const { createErrorNotice } = useDispatch( noticesStore );
const [ theme, setTheme ] = useState( {
"name": "",
"description": "",
"uri": "",
"author": "",
"author_uri": "",
} );
async function createBlankTheme() {
try {
const response = await apiFetch( {
path: '/create-block-theme/v1/blank-theme',
method: 'POST',
data: theme,
} );

if ( response.req.status === 200 ) {
createErrorNotice( __( 'New theme created' ), { type: 'snackbar' } );
}
} catch ( error ) {
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: __( 'An error occurred while creating the site export.' );
createErrorNotice( errorMessage, { type: 'snackbar' } );
}
}

return (
<PanelBody>
<MenuGroup label={ __( 'Create a blank theme' ) }>
<TextControl label={ __( 'Theme name' ) } onChange={ ( value ) => setTheme( { ...theme, "name": value } ) } placeholder={ __( 'Theme name' ) }></TextControl>
<TextControl label={ __( 'Theme description' ) } onChange={ ( value ) => setTheme( { ...theme, "description": value } ) } placeholder={ __( 'A short description of the theme' ) }></TextControl>
<TextControl label={ __( 'Theme URI' ) } onChange={ ( value ) => setTheme( { ...theme, "uri": value } ) } placeholder={ __( 'https://github.com/wordpress/twentytwentytwo/' ) }></TextControl>
<TextControl label={ __( 'Author' ) } onChange={ ( value ) => setTheme( { ...theme, "author": value } ) } placeholder={ __( 'the WordPress team' ) }></TextControl>
<TextControl label={ __( 'Author URI' ) } onChange={ ( value ) => setTheme( { ...theme, "author_uri": value } ) } placeholder={ __( 'https://wordpress.org/' ) }></TextControl>
<Button variant="secondary" onClick={ () => createBlankTheme() }>{ __( 'Create blank theme' ) }</Button>
</MenuGroup>
</PanelBody>
)
}

const CreateBlockThemePlugin = () => (
<Fragment>
<PluginSidebarMoreMenuItem target="create-block-theme-sidebar" icon={ blockDefault }>
Expand All @@ -54,6 +100,7 @@ const CreateBlockThemePlugin = () => (
<PluginSidebar name="create-block-theme-sidebar" icon={ blockDefault } title={ __( 'Create Block Theme' ) }>
<GlobalStylesProvider>
<StyleVariations />
<BlankTheme />
</GlobalStylesProvider>
</PluginSidebar>
</Fragment>
Expand Down

0 comments on commit b1fa460

Please sign in to comment.