Skip to content

Commit

Permalink
Adds error handling for themes.svn.wordpress.org request (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
creativecoder authored Nov 7, 2023
1 parent d30fcae commit ed24804
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion admin/js/form-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function isThemeNameValid( themeName ) {
// Check if the theme name is available
const isNameAvailable = () => {
// default to empty array if the unavailable theme names are not loaded yet from the API
const notAvailableSlugs = wpOrgThemeDirectory.themeSlugs || [];
const notAvailableSlugs = window.wpOrgThemeDirectory.themeSlugs || [];

// Compare the theme name to the list of unavailable theme names using several different slug formats
return ! notAvailableSlugs.some(
Expand Down
8 changes: 5 additions & 3 deletions admin/wp-org-theme-directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public static function register_theme_names_endpoint() {
}

public static function get_theme_names() {
$html = wp_remote_get( self::THEME_NAMES_ENDPOINT );
$html = wp_safe_remote_get( self::THEME_NAMES_ENDPOINT );

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

// parse the html response extracting all the a inside li elements
$pattern = '/<li><a href=".*?">(.*?)<\/a><\/li>/';
Expand Down Expand Up @@ -71,5 +75,3 @@ function assets_enqueue() {
}

}


10 changes: 8 additions & 2 deletions src/wp-org-theme-directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ async function loadUnavailableThemeNames() {
const requestOptions = {
path: '/create-block-theme/v1/wp-org-theme-names',
};
const request = await apiFetch( requestOptions );
wpOrgThemeDirectory.themeSlugs = request.names;

try {
const request = await apiFetch( requestOptions );
window.wpOrgThemeDirectory.themeSlugs = request.names;
} catch ( error ) {
// eslint-disable-next-line no-console
console.error( error );
}
}

window.addEventListener( 'load', loadUnavailableThemeNames );

0 comments on commit ed24804

Please sign in to comment.