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

Try/refactor editor UI #563

Merged
merged 20 commits into from
Apr 18, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion admin/create-theme/theme-create.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ public static function clone_current_theme( $theme ) {
wp_mkdir_p( $new_theme_path );

// Copy theme files.
$template_options = array(
'localizeText' => false,
'removeNavRefs' => false,
'localizeImages' => false,
);
Theme_Utils::clone_theme_to_folder( $new_theme_path, $theme['slug'], $theme['name'] );
Theme_Templates::add_templates_to_local( 'all', $new_theme_path, $theme['slug'] );
Theme_Templates::add_templates_to_local( 'all', $new_theme_path, $theme['slug'], $template_options );
file_put_contents( $new_theme_path . DIRECTORY_SEPARATOR . 'theme.json', MY_Theme_JSON_Resolver::export_theme_data( 'all' ) );

if ( $theme['subfolder'] ) {
Expand Down
43 changes: 30 additions & 13 deletions admin/create-theme/theme-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,26 @@ public static function paternize_template( $template ) {
* @param string $slug The slug of the theme.
* @return object The prepared template.
*/
public static function prepare_template_for_export( $template, $slug = null ) {
public static function prepare_template_for_export( $template, $slug = null, $options = null ) {

if ( ! $options ) {
$options = array(
'localizeText' => false,
'removeNavRefs' => true,
'localizeImages' => true,
);
}

$template = self::eliminate_environment_specific_content( $template, $options );

if ( array_key_exists( 'localizeText', $options ) && $options['localizeText'] ) {
$template = self::escape_text_in_template( $template );
}

if ( array_key_exists( 'localizeImages', $options ) && $options['localizeImages'] ) {
$template = Theme_Media::make_template_images_local( $template );
}

$template = self::eliminate_environment_specific_content( $template );
$template = self::escape_text_in_template( $template );
$template = Theme_Media::make_template_images_local( $template );
$template = self::paternize_template( $template );

if ( $slug ) {
Expand All @@ -176,7 +191,7 @@ public static function prepare_template_for_export( $template, $slug = null ) {
* @param string $path The path to the theme folder. If null it is assumed to be the current theme.
* @param string $slug The slug of the theme. If null it is assumed to be the current theme.
*/
public static function add_templates_to_local( $export_type, $path = null, $slug = null ) {
public static function add_templates_to_local( $export_type, $path = null, $slug = null, $options = null ) {

$theme_templates = self::get_theme_templates( $export_type );
$template_folders = get_block_theme_folders();
Expand All @@ -198,7 +213,7 @@ public static function add_templates_to_local( $export_type, $path = null, $slug

foreach ( $theme_templates->templates as $template ) {

$template = self::prepare_template_for_export( $template, $slug );
$template = self::prepare_template_for_export( $template, $slug, $options );

// Write the template content
file_put_contents(
Expand Down Expand Up @@ -226,7 +241,7 @@ public static function add_templates_to_local( $export_type, $path = null, $slug

foreach ( $theme_templates->parts as $template ) {

$template = self::prepare_template_for_export( $template, $slug );
$template = self::prepare_template_for_export( $template, $slug, $options );

// Write the template content
file_put_contents(
Expand Down Expand Up @@ -352,16 +367,18 @@ public static function escape_text( $text ) {
return "<?php echo __('" . $text . "', '" . wp_get_theme()->get( 'TextDomain' ) . "');?>";
}

private static function eliminate_environment_specific_content_from_block( $block ) {
private static function eliminate_environment_specific_content_from_block( $block, $options = null ) {

// remove theme attribute from template parts
if ( 'core/template-part' === $block['blockName'] && isset( $block['attrs']['theme'] ) ) {
unset( $block['attrs']['theme'] );
}

// remove ref attribute from blocks
// (optionally) remove ref attribute from nav blocks
if ( 'core/navigation' === $block['blockName'] && isset( $block['attrs']['ref'] ) ) {
unset( $block['attrs']['ref'] );
if ( ! $options || ( array_key_exists( 'removeNavRefs', $options ) && $options['removeNavRefs'] ) ) {
unset( $block['attrs']['ref'] );
}
}

// remove id attributes and classes from image and cover blocks
Expand All @@ -387,20 +404,20 @@ private static function eliminate_environment_specific_content_from_block( $bloc
// process any inner blocks
if ( ! empty( $block['innerBlocks'] ) ) {
foreach ( $block['innerBlocks'] as $inner_block_key => $inner_block ) {
$block['innerBlocks'][ $inner_block_key ] = static::eliminate_environment_specific_content_from_block( $inner_block );
$block['innerBlocks'][ $inner_block_key ] = static::eliminate_environment_specific_content_from_block( $inner_block, $options );
}
}

return $block;
}

public static function eliminate_environment_specific_content( $template ) {
public static function eliminate_environment_specific_content( $template, $options = null ) {

$template_blocks = parse_blocks( $template->content );
$parsed_content = '';

foreach ( $template_blocks as $block ) {
$parsed_block = static::eliminate_environment_specific_content_from_block( $block );
$parsed_block = static::eliminate_environment_specific_content_from_block( $block, $options );
$parsed_content .= serialize_block( $parsed_block );
}

Expand Down
30 changes: 21 additions & 9 deletions includes/class-create-block-theme-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,17 +420,29 @@ function rest_update_theme( $request ) {
*/
function rest_save_theme( $request ) {

Theme_Fonts::persist_font_settings();
$options = $request->get_params();

if ( is_child_theme() ) {
Theme_Templates::add_templates_to_local( 'current' );
Theme_Json::add_theme_json_to_local( 'current' );
} else {
Theme_Templates::add_templates_to_local( 'all' );
Theme_Json::add_theme_json_to_local( 'all' );
if ( isset( $options['saveFonts'] ) && true === $options['saveFonts'] ) {
Theme_Fonts::persist_font_settings();
}

if ( isset( $options['saveTemplates'] ) && true === $options['saveTemplates'] ) {
if ( is_child_theme() ) {
Theme_Templates::add_templates_to_local( 'current', null, null, $options );
} else {
Theme_Templates::add_templates_to_local( 'all', null, null, $options );
}
Theme_Templates::clear_user_templates_customizations();
}

if ( isset( $options['saveStyle'] ) && true === $options['saveStyle'] ) {
if ( is_child_theme() ) {
Theme_Json::add_theme_json_to_local( 'current', null, null, $options );
} else {
Theme_Json::add_theme_json_to_local( 'all', null, null, $options );
}
Theme_Styles::clear_user_styles_customizations();
}
Theme_Styles::clear_user_styles_customizations();
Theme_Templates::clear_user_templates_customizations();

return new WP_REST_Response(
array(
Expand Down
Loading
Loading