Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds error handling for themes.svn.wordpress.org request #469

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
Loading