Skip to content

Commit

Permalink
Add try/catch to prevent fatal error Uncaught Exception when credenti…
Browse files Browse the repository at this point in the history
…als are not correct
  • Loading branch information
Tabrisrp committed Oct 12, 2016
1 parent d5838d9 commit 4bb4d0e
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions inc/functions/cloudflare.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 4bb4d0e

Please sign in to comment.