Skip to content

Commit

Permalink
Making images local for the templates and parts saved to local files
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbenedetto committed Feb 8, 2023
1 parent 5d06cff commit 71b4a2b
Showing 1 changed file with 57 additions and 12 deletions.
69 changes: 57 additions & 12 deletions admin/class-create-block-theme-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,41 +726,86 @@ function add_templates_to_local( $export_type ) {
$template_folders = get_block_theme_folders();

// If there is no templates folder, create it.
if ( ! is_dir( get_stylesheet_directory() . '/' . $template_folders['wp_template'] ) ) {
wp_mkdir_p( get_stylesheet_directory() . '/' . $template_folders['wp_template'] );
if ( ! is_dir( get_stylesheet_directory() . DIRECTORY_SEPARATOR . $template_folders['wp_template'] ) ) {
wp_mkdir_p( get_stylesheet_directory() . DIRECTORY_SEPARATOR . $template_folders['wp_template'] );
}

if ( ! is_dir( get_stylesheet_directory() . '/assets/images' ) ) {
wp_mkdir_p( get_stylesheet_directory() . '/assets/images' );
}

foreach ( $theme_templates->templates as $template ) {
$template_data = $this->make_template_images_local( $template->content );
$template_data = $this->make_template_images_local( $template );

// If there are images in the template, add it as a pattern
if ( ! empty ( $template_data->media ) ) {
// If there is no templates folder, create it.
if ( ! is_dir( get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'patterns' ) ) {
wp_mkdir_p( get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'patterns' );
}

// If there are external images, add it as a pattern
$pattern = $this->pattern_from_template( $template_data );
$template_data->content = '<!-- wp:pattern {"slug":"'. $pattern[ 'slug' ] .'"} /-->';

// Write the pattern
file_put_contents(
get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'patterns' . DIRECTORY_SEPARATOR . $template_data->slug . '.php',
$pattern[ 'content' ]
);
}

// Write the template content
file_put_contents(
get_stylesheet_directory() . '/' . $template_folders['wp_template'] . '/' . $template->slug . '.html',
$template_data[ 'content' ]
get_stylesheet_directory() . DIRECTORY_SEPARATOR . $template_folders['wp_template'] . DIRECTORY_SEPARATOR . $template->slug . '.html',
$template_data->content
);
foreach ( $template_data[ 'media' ] as $media ) {

// Write the image assets
foreach ( $template_data->media as $media ) {
$download_file = file_get_contents( $media );
file_put_contents(
get_stylesheet_directory() . DIRECTORY_SEPARATOR .'assets' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . basename( $media ),
$download_file
);
}

}

// If there is no parts folder, create it.
if ( ! is_dir( get_stylesheet_directory() . '/' . $template_folders['wp_template_part'] ) ) {
wp_mkdir_p( get_stylesheet_directory() . '/' . $template_folders['wp_template_part'] );
if ( ! is_dir( get_stylesheet_directory() . DIRECTORY_SEPARATOR . $template_folders['wp_template_part'] ) ) {
wp_mkdir_p( get_stylesheet_directory() . DIRECTORY_SEPARATOR . $template_folders['wp_template_part'] );
}

foreach ( $theme_templates->parts as $template_part ) {
$template_data = $this->make_template_images_local( $template_part->content );
$template_data = $this->make_template_images_local( $template_part );

// If there are images in the template, add it as a pattern
if ( ! empty ( $template_data->media ) ) {
// If there is no templates folder, create it.
if ( ! is_dir( get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'patterns' ) ) {
wp_mkdir_p( get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'patterns' );
}

// If there are external images, add it as a pattern
$pattern = $this->pattern_from_template( $template_data );
$template_data->content = '<!-- wp:pattern {"slug":"'. $pattern[ 'slug' ] .'"} /-->';

// Write the pattern
file_put_contents(
get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'patterns' . DIRECTORY_SEPARATOR . $template_data->slug . '.php',
$pattern[ 'content' ]
);
}

// Write the template content
file_put_contents(
get_stylesheet_directory() . '/' . $template_folders['wp_template_part'] . '/' . $template_part->slug . '.html',
$template_data[ 'content' ]
get_stylesheet_directory() . DIRECTORY_SEPARATOR . $template_folders['wp_template_part'] . DIRECTORY_SEPARATOR . $template_data->slug . '.html',
$template_data->content
);
foreach ( $template_data[ 'media' ] as $media ) {

// Write the image assets
foreach ( $template_data->media as $media ) {
$download_file = file_get_contents( $media );
file_put_contents(
get_stylesheet_directory() . DIRECTORY_SEPARATOR .'assets' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . basename( $media ),
Expand Down

0 comments on commit 71b4a2b

Please sign in to comment.