diff --git a/inc/functions/cloudflare.php b/inc/functions/cloudflare.php index 7f8a843233..90cfbfeb20 100644 --- a/inc/functions/cloudflare.php +++ b/inc/functions/cloudflare.php @@ -4,25 +4,33 @@ /** * Get a CloudFlare\Api instance & the zone_id corresponding to the domain * + * @since 2.8.18 Add try/catch to prevent fatal error Uncaugh Exception * @since 2.8.16 Update to CloudFlare API v4 * @since 2.5 * - * @return obj CloudFlare instance & zone_id + * @return mixed bool|object CloudFlare instance & zone_id if credentials are correct, false otherwise */ function get_rocket_cloudflare_instance() { $cf_email = get_rocket_option( 'cloudflare_email', null ); $cf_api_key = ( defined( 'WP_ROCKET_CF_API_KEY' ) ) ? WP_ROCKET_CF_API_KEY : get_rocket_option( 'cloudflare_api_key', null ); if ( isset( $cf_email, $cf_api_key ) ) { - $cf_instance = ( object ) [ 'auth' => new Cloudflare\Api( $cf_email, $cf_api_key ) ]; - $zone_instance = new CloudFlare\Zone( $cf_instance->auth ); - if ( $cf_domain = get_rocket_option( 'cloudflare_domain' ) ) { - $zone = $zone_instance->zones( get_rocket_option( 'cloudflare_domain' ) ); - $cf_instance->zone_id = $zone->result[0]->id; - } - - return $cf_instance; + $cf_instance = ( object ) [ 'auth' => new Cloudflare\Api( $cf_email, $cf_api_key ) ]; + + try { + $zone_instance = new CloudFlare\Zone( $cf_instance->auth ); + + if ( $cf_domain = get_rocket_option( 'cloudflare_domain' ) ) { + $zone = $zone_instance->zones( $cf_domain ); + $cf_instance->zone_id = $zone->result[0]->id; + + return $cf_instance; + } + } catch( Exception $e ) {} + + return false; } + return false; }