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
31 changes: 31 additions & 0 deletions admin/create-theme/cbt-zip-file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about naming this file cbt-zip-archive instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, updated!


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 ) {
$this->theme_slug = $theme_slug;
}

function addFromString( string $name, string $content, int $flags = ZipArchive::FL_OVERWRITE ) {
$name = $this->theme_slug . '/' . $name;
return parent::addFromString( $name, $content );
}

function addFile( string $filepath, string $entryname = '', int $start = 0, int $length = 0, int $flags = ZipArchive::FL_OVERWRITE ) {
$entryname = $this->theme_slug . '/' . $entryname;
return parent::addFile( $filepath, $entryname );
}

function addEmptyDir( string $dirname, int $flags = 0 ) {
$dirname = $this->theme_slug . '/' . $dirname;
return parent::addEmptyDir( $dirname );
}

}

}
9 changes: 8 additions & 1 deletion admin/create-theme/theme-zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
require_once( __DIR__ . '/theme-blocks.php' );
require_once( __DIR__ . '/theme-templates.php' );
require_once( __DIR__ . '/theme-patterns.php' );
require_once( __DIR__ . '/cbt-zip-file.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;
}
Expand Down