Skip to content

Commit

Permalink
Removed all of the code relating to 'folder management' of a theme.
Browse files Browse the repository at this point in the history
  • Loading branch information
pbking committed Sep 18, 2024
1 parent 8993439 commit 4ef60f0
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 105 deletions.
8 changes: 0 additions & 8 deletions includes/class-create-block-theme-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,6 @@ function rest_update_theme( $request ) {
CBT_Theme_Utils::replace_screenshot( $theme['screenshot'] );
}

// Relocate the theme to a new folder
$response = CBT_Theme_Utils::relocate_theme( $theme['subfolder'] );

if ( is_wp_error( $response ) ) {
return $response;
}

wp_cache_flush();

return new WP_REST_Response(
Expand Down Expand Up @@ -463,7 +456,6 @@ private function sanitize_theme_data( $theme ) {
$sanitized_theme['author'] = sanitize_text_field( $theme['author'] ?? '' );
$sanitized_theme['author_uri'] = sanitize_text_field( $theme['author_uri'] ?? '' );
$sanitized_theme['tags_custom'] = sanitize_text_field( $theme['tags_custom'] ?? '' );
$sanitized_theme['subfolder'] = sanitize_text_field( $theme['subfolder'] ?? '' );
$sanitized_theme['version'] = sanitize_text_field( $theme['version'] ?? '' );
$sanitized_theme['screenshot'] = sanitize_text_field( $theme['screenshot'] ?? '' );
$sanitized_theme['recommended_plugins'] = sanitize_textarea_field( $theme['recommended_plugins'] ?? '' );
Expand Down
1 change: 0 additions & 1 deletion includes/create-theme/cbt-zip-archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// This Class extends the ZipArchive class to add the theme slug as a base folder for all the files
class CBT_Zip_Archive extends ZipArchive {

private string $theme_slug;
private string $theme_folder;

function __construct( $theme_slug ) {
Expand Down
28 changes: 4 additions & 24 deletions includes/create-theme/theme-create.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ 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' ) );
}
Expand All @@ -40,22 +36,14 @@ public static function clone_current_theme( $theme ) {
file_put_contents( path_join( $new_theme_path, 'readme.txt' ), CBT_Theme_Readme::create( $theme ) );
file_put_contents( path_join( $new_theme_path, 'style.css' ), CBT_Theme_Styles::update_style_css( file_get_contents( path_join( $new_theme_path, 'style.css' ) ), $theme ) );

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

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

// 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' ) );
}
Expand Down Expand Up @@ -91,18 +79,14 @@ public static function create_child_theme( $theme, $screenshot ) {
copy( $source, $new_theme_path . DIRECTORY_SEPARATOR . 'screenshot.png' );
}

if ( $theme['subfolder'] ) {
switch_theme( $theme['subfolder'] . '/' . $theme['slug'] );
} else {
switch_theme( $theme['slug'] );
}
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'];
$blank_theme_path = get_theme_root() . DIRECTORY_SEPARATOR . $theme['slug'];

if ( file_exists( $blank_theme_path ) ) {
return new WP_Error( 'theme_already_exists', __( 'Theme already exists.', 'create-block-theme' ) );
Expand Down Expand Up @@ -158,11 +142,7 @@ public static function create_blank_theme( $theme, $screenshot ) {
file_put_contents( $theme_json_path, $theme_json_string );
}

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

private static function is_valid_screenshot( $file ) {
Expand Down
46 changes: 0 additions & 46 deletions includes/create-theme/theme-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,52 +91,6 @@ public static function clone_theme_to_folder( $location, $new_slug, $new_name )
}
}

/**
* 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 );
}
}

public static function is_valid_screenshot( $file ) {

$allowed_screenshot_types = array(
Expand Down
11 changes: 1 addition & 10 deletions src/editor-sidebar/create-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
import {
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
Expand Down Expand Up @@ -32,22 +32,13 @@ import {
export const CreateThemePanel = ( { createType } ) => {
const { createErrorNotice } = useDispatch( noticesStore );

const subfolder = useSelect( ( select ) => {
const stylesheet = select( 'core' ).getCurrentTheme().stylesheet;
if ( stylesheet.lastIndexOf( '/' ) > 1 ) {
return stylesheet.substring( 0, stylesheet.lastIndexOf( '/' ) );
}
return '';
}, [] );

const [ theme, setTheme ] = useState( {
name: '',
description: '',
uri: '',
author: '',
author_uri: '',
tags_custom: '',
subfolder,
} );

const cloneTheme = () => {
Expand Down
15 changes: 0 additions & 15 deletions src/editor-sidebar/metadata-editor-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export const ThemeMetadataEditorModal = ( { onRequestClose } ) => {
recommended_plugins: '',
font_credits: '',
image_credits: '',
subfolder: '',
} );

const { createErrorNotice } = useDispatch( noticesStore );
Expand All @@ -72,13 +71,6 @@ export const ThemeMetadataEditorModal = ( { onRequestClose } ) => {
recommended_plugins: readmeData.recommended_plugins,
font_credits: readmeData.fonts,
image_credits: readmeData.images,
subfolder:
themeData.stylesheet.lastIndexOf( '/' ) > 1
? themeData.stylesheet.substring(
0,
themeData.stylesheet.lastIndexOf( '/' )
)
: '',
} );
}, [] );

Expand Down Expand Up @@ -387,13 +379,6 @@ Image license`,
/>
</MediaUploadCheck>
</BaseControl>
<TextControl
label={ __( 'Theme Subfolder', 'create-block-theme' ) }
value={ theme.subfolder }
onChange={ ( value ) =>
setTheme( { ...theme, subfolder: value } )
}
/>
</VStack>
<Spacer />
<HStack
Expand Down
1 change: 0 additions & 1 deletion tests/test-theme-fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ private function create_blank_theme() {
$request->set_param( 'author', '' );
$request->set_param( 'author_uri', '' );
$request->set_param( 'tags_custom', '' );
$request->set_param( 'subfolder', '' );
$request->set_param( 'recommended_plugins', '' );

rest_do_request( $request );
Expand Down

0 comments on commit 4ef60f0

Please sign in to comment.