Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/create theme variation to site editor interface #532

Merged
merged 1 commit into from Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 5 additions & 11 deletions admin/create-theme/theme-json.php
Expand Up @@ -10,33 +10,27 @@ public static function add_theme_json_to_local( $export_type ) {
}

public static function add_theme_json_variation_to_local( $export_type, $theme ) {
$variation_slug = sanitize_title( $theme['variation'] );
$variation_path = get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'styles' . DIRECTORY_SEPARATOR;
$file_counter = 0;

if ( ! file_exists( $variation_path ) ) {
wp_mkdir_p( $variation_path );
}

if ( file_exists( $variation_path . $variation_slug . '.json' ) ) {
$file_counter++;
while ( file_exists( $variation_path . $variation_slug . '_' . $file_counter . '.json' ) ) {
$file_counter++;
}
$variation_slug = $variation_slug . '_' . $file_counter;
if ( file_exists( $variation_path . $theme['slug'] . '.json' ) ) {
return new WP_Error( 'variation_already_exists', __( 'Variation already exists.', 'create-block-theme' ) );
}

$_POST['theme']['variation_slug'] = $variation_slug;
$_POST['theme']['variation_slug'] = $theme['slug'];

$extra_theme_data = array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'title' => $theme['variation'],
'title' => $theme['name'],
);

$variation_theme_json = MY_Theme_JSON_Resolver::export_theme_data( $export_type, $extra_theme_data );

file_put_contents(
$variation_path . $variation_slug . '.json',
$variation_path . $theme['slug'] . '.json',
$variation_theme_json
);
}
Expand Down
27 changes: 27 additions & 0 deletions includes/class-create-block-theme-api.php
Expand Up @@ -64,6 +64,17 @@ public function register_rest_routes() {
},
)
);
register_rest_route(
'create-block-theme/v1',
'/create-variation',
array(
'methods' => 'POST',
'callback' => array( $this, 'rest_create_variation' ),
'permission_callback' => function () {
return current_user_can( 'edit_theme_options' );
},
)
);
register_rest_route(
'create-block-theme/v1',
'/create-blank',
Expand Down Expand Up @@ -177,6 +188,22 @@ function rest_create_child_theme( $request ) {
);
}

function rest_create_variation( $request ) {

$response = Theme_Json::add_theme_json_variation_to_local( 'variation', $this->sanitize_theme_data( $request->get_params() ) );

if ( is_wp_error( $response ) ) {
return $response;
}

return new WP_REST_Response(
array(
'status' => 'SUCCESS',
'message' => __( 'Theme Variation Created.', 'create-block-theme' ),
)
);
}

function rest_create_blank_theme( $request ) {

$theme = $this->sanitize_theme_data( $request->get_params() );
Expand Down
47 changes: 47 additions & 0 deletions src/editor-sidebar/create-panel.js
Expand Up @@ -202,6 +202,36 @@ export const CreateThemePanel = () => {
} );
};

const handleCreateVariationClick = () => {
apiFetch( {
path: '/create-block-theme/v1/create-variation',
method: 'POST',
data: theme,
headers: {
'Content-Type': 'application/json',
},
} )
.then( () => {
// eslint-disable-next-line
alert(
__(
'Theme variation created successfully. The editor will now reload.',
'create-block-theme'
)
);
window.location.reload();
} )
.catch( ( error ) => {
const errorMessage =
error.message ||
__(
'An error occurred while attempting to create the theme variation.',
'create-block-theme'
);
createErrorNotice( errorMessage, { type: 'snackbar' } );
} );
};

return (
<PanelBody>
<Heading>
Expand Down Expand Up @@ -311,6 +341,23 @@ export const CreateThemePanel = () => {
) }
</Text>

<hr></hr>
<Spacer />
<Button
icon={ copy }
variant="secondary"
onClick={ handleCreateVariationClick }
>
{ __( 'Create Theme Variation', 'create-block-theme' ) }
</Button>
<Spacer />
<Text variant="muted">
{ __(
'Save the Global Styles changes as a theme variation.',
'create-block-theme'
) }
</Text>

<hr></hr>
<Spacer />
<Button
Expand Down