Skip to content

Commit

Permalink
Moved valid screenshot check to utils to fix bug with exporting theme
Browse files Browse the repository at this point in the history
  • Loading branch information
pbking committed Mar 19, 2024
1 parent 001c7d0 commit 9165a1e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
20 changes: 4 additions & 16 deletions admin/class-create-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function create_sibling_theme( $theme, $screenshot ) {
);

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

// Add / replace screenshot.
if ( $this->is_valid_screenshot( $screenshot ) ) {
if ( Theme_Utils::is_valid_screenshot( $screenshot ) ) {
$zip->addFileToTheme(
$screenshot['tmp_name'],
'screenshot.png'
Expand Down Expand Up @@ -291,7 +291,7 @@ function create_child_theme( $theme, $screenshot ) {
);

// Add / replace screenshot.
if ( $this->is_valid_screenshot( $screenshot ) ) {
if ( Theme_Utils::is_valid_screenshot( $screenshot ) ) {
$zip->addFileToTheme(
$screenshot['tmp_name'],
'screenshot.png'
Expand Down Expand Up @@ -384,7 +384,7 @@ function create_blank_theme( $theme, $screenshot ) {
}

// Overwrite default screenshot if one is provided.
if ( $this->is_valid_screenshot( $screenshot ) ) {
if ( Theme_Utils::is_valid_screenshot( $screenshot ) ) {
file_put_contents(
$blank_theme_path . DIRECTORY_SEPARATOR . 'screenshot.png',
file_get_contents( $screenshot['tmp_name'] )
Expand Down Expand Up @@ -489,16 +489,4 @@ function blockbase_save_theme() {
}
}
}

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

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;
}
}
12 changes: 12 additions & 0 deletions admin/create-theme/theme-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,16 @@ public static function relocate_theme( $new_theme_subfolder ) {
}
}

public static function is_valid_screenshot( $file ) {

$allowed_screenshot_types = array(
'png' => 'image/png',
);
$filetype = wp_check_filetype( $file['name'], $allowed_screenshot_types );
if ( is_uploaded_file( $file['tmp_name'] ) && in_array( $filetype['type'], $allowed_screenshot_types, true ) && $file['size'] < 2097152 ) {
return 1;
}
return 0;
}

}
2 changes: 1 addition & 1 deletion includes/class-create-block-theme-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function rest_export_cloned_theme( $request ) {
);

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

0 comments on commit 9165a1e

Please sign in to comment.