Skip to content

Commit

Permalink
Extracted any logic that may need to be tested from the api class
Browse files Browse the repository at this point in the history
  • Loading branch information
pbking committed Mar 19, 2024
1 parent 1545d48 commit b772013
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 210 deletions.
2 changes: 1 addition & 1 deletion admin/class-create-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
require_once __DIR__ . '/create-theme/theme-readme.php';
require_once __DIR__ . '/create-theme/theme-form.php';
require_once __DIR__ . '/create-theme/form-messages.php';

require_once __DIR__ . '/create-theme/theme-create.php';
/**
* The admin-specific functionality of the plugin.
*
Expand Down
111 changes: 111 additions & 0 deletions admin/create-theme/theme-create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

class Theme_Create {

const ALLOWED_SCREENSHOT_TYPES = array(
'png' => 'image/png',
);

public static function clone_current_theme( $theme ) {

// Create theme directory.
$new_theme_path = get_theme_root() . DIRECTORY_SEPARATOR . $theme['slug'];

if ( $theme['subfolder'] ) {
$new_theme_path = get_theme_root() . DIRECTORY_SEPARATOR . $theme['subfolder'] . DIRECTORY_SEPARATOR . $theme['slug'];
}

if ( file_exists( $new_theme_path ) ) {
return new WP_Error( 'theme_already_exists', __( 'Theme already exists.', 'create-block-theme' ) );
}

wp_mkdir_p( $new_theme_path );

// Copy theme files.
Theme_Utils::clone_theme_to_folder( $new_theme_path, $theme['slug'], $theme['name'] );
Theme_Utils::add_templates_to_folder( $new_theme_path, 'all', $theme['slug'] );

file_put_contents( $new_theme_path . DIRECTORY_SEPARATOR . 'theme.json', MY_Theme_JSON_Resolver::export_theme_data( 'all' ) );

if ( $theme['subfolder'] ) {
switch_theme( $theme['subfolder'] . '/' . $theme['slug'] );
} else {
switch_theme( $theme['slug'] );
}
}

public static function create_blank_theme( $theme, $screenshot ) {

// Create theme directory.
$source = plugin_dir_path( __DIR__ ) . 'assets/boilerplate';
$blank_theme_path = get_theme_root() . DIRECTORY_SEPARATOR . $theme['subfolder'] . DIRECTORY_SEPARATOR . $theme['slug'];

if ( file_exists( $blank_theme_path ) ) {
return new WP_Error( 'theme_already_exists', __( 'Theme already exists.', 'create-block-theme' ) );
}

wp_mkdir_p( $blank_theme_path );

// Add readme.txt.
file_put_contents(
$blank_theme_path . DIRECTORY_SEPARATOR . 'readme.txt',
Theme_Readme::build_readme_txt( $theme )
);

// Add new metadata.
$css_contents = Theme_Styles::build_child_style_css( $theme );

// Add style.css.
file_put_contents(
$blank_theme_path . DIRECTORY_SEPARATOR . 'style.css',
$css_contents
);

$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator( $source, \RecursiveDirectoryIterator::SKIP_DOTS ),
\RecursiveIteratorIterator::SELF_FIRST
);

foreach (
$iterator as $item
) {
if ( $item->isDir() ) {
wp_mkdir_p( $blank_theme_path . DIRECTORY_SEPARATOR . $iterator->getSubPathname() );
} else {
copy( $item, $blank_theme_path . DIRECTORY_SEPARATOR . $iterator->getSubPathname() );
}
}

// Overwrite default screenshot if one is provided.
if ( static::is_valid_screenshot( $screenshot ) ) {
file_put_contents(
$blank_theme_path . DIRECTORY_SEPARATOR . 'screenshot.png',
file_get_contents( $screenshot['tmp_name'] )
);
}

if ( ! defined( 'IS_GUTENBERG_PLUGIN' ) ) {
global $wp_version;
$theme_json_version = 'wp/' . substr( $wp_version, 0, 3 );
$schema = '"$schema": "https://schemas.wp.org/' . $theme_json_version . '/theme.json"';
$theme_json_path = $blank_theme_path . DIRECTORY_SEPARATOR . 'theme.json';
$theme_json_string = file_get_contents( $theme_json_path );
$theme_json_string = str_replace( '"$schema": "https://schemas.wp.org/trunk/theme.json"', $schema, $theme_json_string );
file_put_contents( $theme_json_path, $theme_json_string );
}

if ( $theme['subfolder'] ) {
switch_theme( $theme['subfolder'] . '/' . $theme['slug'] );
} else {
switch_theme( $theme['slug'] );
}
}

private static function is_valid_screenshot( $file ) {
$filetype = wp_check_filetype( $file['name'], self::ALLOWED_SCREENSHOT_TYPES );
if ( is_uploaded_file( $file['tmp_name'] ) && in_array( $filetype['type'], self::ALLOWED_SCREENSHOT_TYPES, true ) && $file['size'] < 2097152 ) {
return 1;
}
return 0;
}
}
47 changes: 47 additions & 0 deletions admin/create-theme/theme-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,51 @@ public static function get_readme_data() {
return $readme_file_details;
}


/**
* Relocate the theme to a new folder and activate the newly relocated theme.
*/
public static function relocate_theme( $new_theme_subfolder ) {

$current_theme_subfolder = '';
$theme_dir = get_stylesheet();

$source = get_theme_root() . DIRECTORY_SEPARATOR . $theme_dir;
$destination = get_theme_root() . DIRECTORY_SEPARATOR . $theme_dir;

if ( str_contains( get_stylesheet(), '/' ) ) {
$current_theme_subfolder = substr( get_stylesheet(), 0, strrpos( get_stylesheet(), '/' ) );
$theme_dir = substr( get_stylesheet(), strrpos( get_stylesheet(), '/' ) + 1 );
$source = get_theme_root() . DIRECTORY_SEPARATOR . $current_theme_subfolder . DIRECTORY_SEPARATOR . $theme_dir;
$destination = get_theme_root() . DIRECTORY_SEPARATOR . $theme_dir;
}

if ( $new_theme_subfolder ) {
$destination = get_theme_root() . DIRECTORY_SEPARATOR . $new_theme_subfolder . DIRECTORY_SEPARATOR . $theme_dir;
wp_mkdir_p( get_theme_root() . DIRECTORY_SEPARATOR . $new_theme_subfolder );
}

if ( $source === $destination ) {
return;
}

global $wp_filesystem;
if ( ! $wp_filesystem ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
WP_Filesystem();
}

$success = move_dir( $source, $destination, false );

if ( ! $success ) {
return new \WP_Error( 'problem_moving', __( 'There was a problem moving the theme', 'create-block-theme' ) );
}

if ( $new_theme_subfolder ) {
switch_theme( $new_theme_subfolder . '/' . $theme_dir );
} else {
switch_theme( $theme_dir );
}
}

}
Loading

0 comments on commit b772013

Please sign in to comment.