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

Adding files to zip subfolder called as theme slug #357

Merged
merged 7 commits into from
Jun 19, 2023
18 changes: 9 additions & 9 deletions admin/class-create-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function create_sibling_theme( $theme, $screenshot ) {
$zip = Theme_Zip::add_theme_json_to_zip( $zip, 'current' );

// Add readme.txt.
$zip->addFromString(
$zip->addFromStringToTheme(
'readme.txt',
Theme_Readme::build_readme_txt( $theme )
);
Expand All @@ -134,14 +134,14 @@ function create_sibling_theme( $theme, $screenshot ) {
$css_contents = trim( substr( $css_contents, strpos( $css_contents, '*/' ) + 2 ) );
// Add new metadata
$css_contents = Theme_Styles::build_child_style_css( $theme ) . $css_contents;
$zip->addFromString(
$zip->addFromStringToTheme(
'style.css',
$css_contents
);

// Add / replace screenshot.
if ( $this->is_valid_screenshot( $screenshot ) ) {
$zip->addFile(
$zip->addFileToTheme(
$screenshot['tmp_name'],
'screenshot.png'
);
Expand Down Expand Up @@ -191,7 +191,7 @@ function clone_theme( $theme, $screenshot ) {
$zip = Theme_Zip::add_theme_json_to_zip( $zip, 'all' );

// Add readme.txt.
$zip->addFromString(
$zip->addFromStringToTheme(
'readme.txt',
Theme_Readme::build_readme_txt( $theme )
);
Expand All @@ -202,14 +202,14 @@ function clone_theme( $theme, $screenshot ) {
$css_contents = trim( substr( $css_contents, strpos( $css_contents, '*/' ) + 2 ) );
// Add new metadata
$css_contents = Theme_Styles::build_child_style_css( $theme ) . $css_contents;
$zip->addFromString(
$zip->addFromStringToTheme(
'style.css',
$css_contents
);

// Add / replace screenshot.
if ( $this->is_valid_screenshot( $screenshot ) ) {
$zip->addFile(
$zip->addFileToTheme(
$screenshot['tmp_name'],
'screenshot.png'
);
Expand Down Expand Up @@ -255,20 +255,20 @@ function create_child_theme( $theme, $screenshot ) {
$zip = Theme_Zip::add_theme_json_to_zip( $zip, 'user' );

// Add readme.txt.
$zip->addFromString(
$zip->addFromStringToTheme(
'readme.txt',
Theme_Readme::build_readme_txt( $theme )
);

// Add style.css.
$zip->addFromString(
$zip->addFromStringToTheme(
'style.css',
Theme_Styles::build_child_style_css( $theme )
);

// Add / replace screenshot.
if ( $this->is_valid_screenshot( $screenshot ) ) {
$zip->addFile(
$zip->addFileToTheme(
$screenshot['tmp_name'],
'screenshot.png'
);
Expand Down
34 changes: 34 additions & 0 deletions admin/create-theme/cbt-zip-archive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

if ( class_exists( 'ZipArchive' ) ) {

// This Class extends the ZipArchive class to add the theme slug as a base folder for all the files
class CbtZipArchive extends ZipArchive {

private string $theme_slug;

function __construct( $theme_slug ) {
// If the original theme is in a subfolder the theme slug will be the last part of the path
$complete_slug = explode( DIRECTORY_SEPARATOR, $theme_slug );
$folder = end( $complete_slug );
$this->theme_folder = $folder;
}

function addFromStringToTheme( $name, $content ) {
$name = $this->theme_folder . '/' . $name;
return parent::addFromString( $name, $content );
}

function addFileToTheme( $filepath, $entryname ) {
$entryname = $this->theme_folder . '/' . $entryname;
return parent::addFile( $filepath, $entryname );
}

function addThemeDir( $dirname ) {
$dirname = $this->theme_folder . '/' . $dirname;
return parent::addEmptyDir( $dirname );
}

}

}
30 changes: 18 additions & 12 deletions admin/create-theme/theme-zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@
require_once( __DIR__ . '/theme-blocks.php' );
require_once( __DIR__ . '/theme-templates.php' );
require_once( __DIR__ . '/theme-patterns.php' );
require_once( __DIR__ . '/theme-utils.php' );
require_once( __DIR__ . '/cbt-zip-archive.php' );

class Theme_Zip {

public static function create_zip( $filename ) {
if ( ! class_exists( 'ZipArchive' ) ) {
return new WP_Error( 'Zip Export not supported.' );
}
$zip = new ZipArchive();

$theme_slug = get_stylesheet();
if ( ! empty( $_POST['theme']['name'] ) ) {
$theme_slug = Theme_Utils::get_theme_slug( $_POST['theme']['name'] );
}

$zip = new CbtZipArchive( $theme_slug );
$zip->open( $filename, ZipArchive::CREATE | ZipArchive::OVERWRITE );
return $zip;
}

public static function add_theme_json_to_zip( $zip, $export_type ) {
$zip->addFromString(
$zip->addFromStringToTheme(
'theme.json',
MY_Theme_JSON_Resolver::export_theme_data( $export_type )
);
Expand Down Expand Up @@ -62,7 +68,7 @@ public static function copy_theme_to_zip( $zip, $new_slug, $new_name ) {
$valid_extensions = array( 'php', 'css', 'scss', 'js', 'txt', 'html' );
$valid_extensions_regex = implode( '|', $valid_extensions );
if ( ! preg_match( "/\.({$valid_extensions_regex})$/", $relative_path ) ) {
$zip->addFile( $file_path, $relative_path );
$zip->addFileToTheme( $file_path, $relative_path );
} else {
$contents = file_get_contents( $file_path );

Expand All @@ -72,7 +78,7 @@ public static function copy_theme_to_zip( $zip, $new_slug, $new_name ) {
}

// Add current file to archive
$zip->addFromString( $relative_path, $contents );
$zip->addFromStringToTheme( $relative_path, $contents );
}
}
}
Expand All @@ -94,11 +100,11 @@ public static function add_templates_to_zip( $zip, $export_type, $new_slug ) {
$theme_templates = Theme_Templates::get_theme_templates( $export_type );

if ( $theme_templates->templates ) {
$zip->addEmptyDir( 'templates' );
$zip->addThemeDir( 'templates' );
}

if ( $theme_templates->parts ) {
$zip->addEmptyDir( 'parts' );
$zip->addThemeDir( 'parts' );
}

foreach ( $theme_templates->templates as $template ) {
Expand All @@ -114,7 +120,7 @@ public static function add_templates_to_zip( $zip, $export_type, $new_slug ) {
$template_data->content = Theme_Patterns::create_pattern_link( $pattern_link_attributes );

// Add pattern to zip
$zip->addFromString(
$zip->addFromStringToTheme(
'patterns/' . $template_data->slug . '.php',
$pattern['content']
);
Expand All @@ -124,7 +130,7 @@ public static function add_templates_to_zip( $zip, $export_type, $new_slug ) {
}

// Add template to zip
$zip->addFromString(
$zip->addFromStringToTheme(
'templates/' . $template_data->slug . '.html',
$template_data->content
);
Expand All @@ -144,7 +150,7 @@ public static function add_templates_to_zip( $zip, $export_type, $new_slug ) {
$template_data->content = Theme_Patterns::create_pattern_link( $pattern_link_attributes );

// Add pattern to zip
$zip->addFromString(
$zip->addFromStringToTheme(
'patterns/' . $template_data->slug . '.php',
$pattern['content']
);
Expand All @@ -154,7 +160,7 @@ public static function add_templates_to_zip( $zip, $export_type, $new_slug ) {
}

// Add template to zip
$zip->addFromString(
$zip->addFromStringToTheme(
'parts/' . $template_data->slug . '.html',
$template_data->content
);
Expand All @@ -173,7 +179,7 @@ static function add_media_to_zip( $zip, $media ) {
if ( ! is_wp_error( $download_file ) ) {
$content_array = file( $download_file );
$file_as_string = implode( '', $content_array );
$zip->addFromString( $folder_path . basename( $url ), $file_as_string );
$zip->addFromStringToTheme( $folder_path . basename( $url ), $file_as_string );
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions includes/class-create-block-theme-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ function rest_export_cloned_theme( $request ) {
$zip = Theme_Zip::add_theme_json_to_zip( $zip, 'all' );

// Add readme.txt.
$zip->addFromString(
$zip->addFromStringToTheme(
'readme.txt',
Theme_Readme::build_readme_txt( $theme )
);
Expand All @@ -273,14 +273,14 @@ function rest_export_cloned_theme( $request ) {
$css_contents = file_get_contents( get_stylesheet_directory() . '/style.css' );
$css_contents = trim( substr( $css_contents, strpos( $css_contents, '*/' ) + 2 ) );
$css_contents = Theme_Styles::build_child_style_css( $theme ) . $css_contents;
$zip->addFromString(
$zip->addFromStringToTheme(
'style.css',
$css_contents
);

// Add / replace screenshot.
if ( $this->is_valid_screenshot( $screenshot ) ) {
$zip->addFile(
$zip->addFileToTheme(
$screenshot['tmp_name'],
'screenshot.png'
);
Expand Down
Loading