Skip to content

Commit

Permalink
Add try / catch each time we use the CF API to avoid Fatal error: Unc…
Browse files Browse the repository at this point in the history
…aught Cloudflare\Exception\UnauthorizedException
  • Loading branch information
GeekPress committed Oct 13, 2016
1 parent 9e88edf commit 60c0af4
Showing 1 changed file with 60 additions and 40 deletions.
100 changes: 60 additions & 40 deletions inc/functions/cloudflare.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
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 ) ];

try {
$zone_instance = new CloudFlare\Zone( $cf_instance->auth );
$cf_domain = get_rocket_option( 'cloudflare_domain', null );
$zone = $zone_instance->zones( $cf_domain );

if ( isset( $zone->result[0]->id ) ) {
$cf_instance->zone_id = $zone->result[0]->id;
$cf_instance->zone_id = $zone->result[0]->id;
return $cf_instance;
}
} catch ( Exception $e ) {}
Expand All @@ -45,30 +45,34 @@ function get_rocket_cloudflare_instance() {
* @since 2.8.16 Update to CloudFlare API v4
* @since 2.5
*
* @return Array
* @return Array
*/
function get_rocket_cloudflare_settings() {
if( ! is_object( $GLOBALS['rocket_cloudflare'] ) ) {
return false;
}

$cf_settings_instance = new CloudFlare\Zone\Settings( $GLOBALS['rocket_cloudflare']->auth );
$cf_settings = $cf_settings_instance->settings( $GLOBALS['rocket_cloudflare']->zone_id );
$cf_minify = $cf_settings->result[16]->value;
$cf_minify_value = 'on';

if ( $cf_minify->js === 'off' || $cf_minify->css === 'off' || $cf_minify->html === 'off' ) {
$cf_minify_value = 'off';

try {
$cf_settings_instance = new CloudFlare\Zone\Settings( $GLOBALS['rocket_cloudflare']->auth );
$cf_settings = $cf_settings_instance->settings( $GLOBALS['rocket_cloudflare']->zone_id );
$cf_minify = $cf_settings->result[16]->value;
$cf_minify_value = 'on';

if ( $cf_minify->js === 'off' || $cf_minify->css === 'off' || $cf_minify->html === 'off' ) {
$cf_minify_value = 'off';
}

$cf_settings_array = array(
'cache_level' => $cf_settings->result[5]->value,
'minify' => $cf_minify_value,
'rocket_loader' => $cf_settings->result[25]->value,
'browser_cache_ttl' => $cf_settings->result[3]->value
);

return $cf_settings_array;
} catch ( Exception $e ) {
return false;
}

$cf_settings_array = array(
'cache_level' => $cf_settings->result[5]->value,
'minify' => $cf_minify_value,
'rocket_loader' => $cf_settings->result[25]->value,
'browser_cache_ttl' => $cf_settings->result[3]->value
);

return $cf_settings_array;
}

/**
Expand All @@ -90,8 +94,10 @@ function set_rocket_cloudflare_devmode( $mode ) {
$value = 'on';
}

$cf_settings = new CloudFlare\Zone\Settings( $GLOBALS['rocket_cloudflare']->auth );
$cf_settings->change_development_mode( $GLOBALS['rocket_cloudflare']->zone_id, $value );
try {
$cf_settings = new CloudFlare\Zone\Settings( $GLOBALS['rocket_cloudflare']->auth );
$cf_settings->change_development_mode( $GLOBALS['rocket_cloudflare']->zone_id, $value );
} catch ( Exception $e ) {}
}

/**
Expand All @@ -106,9 +112,11 @@ function set_rocket_cloudflare_cache_level( $mode ) {
if( ! is_object( $GLOBALS['rocket_cloudflare'] ) ) {
return false;
}

$cf_settings = new CloudFlare\Zone\Settings( $GLOBALS['rocket_cloudflare']->auth );
$cf_settings->change_cache_level( $GLOBALS['rocket_cloudflare']->zone_id, $mode );

try {
$cf_settings = new CloudFlare\Zone\Settings( $GLOBALS['rocket_cloudflare']->auth );
$cf_settings->change_cache_level( $GLOBALS['rocket_cloudflare']->zone_id, $mode );
} catch ( Exception $e ) {}
}

/**
Expand All @@ -129,9 +137,11 @@ function set_rocket_cloudflare_minify( $mode ) {
'html' => $mode,
'js' => $mode
);

$cf_settings = new CloudFlare\Zone\Settings( $GLOBALS['rocket_cloudflare']->auth );
$cf_settings->change_minify( $GLOBALS['rocket_cloudflare']->zone_id, $cf_minify_settings );

try {
$cf_settings = new CloudFlare\Zone\Settings( $GLOBALS['rocket_cloudflare']->auth );
$cf_settings->change_minify( $GLOBALS['rocket_cloudflare']->zone_id, $cf_minify_settings );
} catch ( Exception $e ) {}
}

/**
Expand All @@ -146,9 +156,11 @@ function set_rocket_cloudflare_rocket_loader( $mode ) {
if( ! is_object( $GLOBALS['rocket_cloudflare'] ) ) {
return false;
}

$cf_settings = new CloudFlare\Zone\Settings( $GLOBALS['rocket_cloudflare']->auth );
$cf_settings->change_rocket_loader( $GLOBALS['rocket_cloudflare']->zone_id, $mode );

try {
$cf_settings = new CloudFlare\Zone\Settings( $GLOBALS['rocket_cloudflare']->auth );
$cf_settings->change_rocket_loader( $GLOBALS['rocket_cloudflare']->zone_id, $mode );
} catch ( Exception $e ) {}
}

/**
Expand All @@ -162,9 +174,11 @@ function set_rocket_cloudflare_browser_cache_ttl( $mode ) {
if( ! is_object( $GLOBALS['rocket_cloudflare'] ) ) {
return false;
}

$cf_settings = new CloudFlare\Zone\Settings( $GLOBALS['rocket_cloudflare']->auth );
$cf_settings->change_browser_cache_ttl( $GLOBALS['rocket_cloudflare']->zone_id, $mode );

try {
$cf_settings = new CloudFlare\Zone\Settings( $GLOBALS['rocket_cloudflare']->auth );
$cf_settings->change_browser_cache_ttl( $GLOBALS['rocket_cloudflare']->zone_id, $mode );
} catch ( Exception $e ) {}
}

/**
Expand All @@ -179,9 +193,11 @@ function rocket_purge_cloudflare() {
if( ! is_object( $GLOBALS['rocket_cloudflare'] ) ) {
return false;
}

$cf_cache = new CloudFlare\Zone\Cache( $GLOBALS['rocket_cloudflare']->auth );
$cf_cache->purge( $GLOBALS['rocket_cloudflare']->zone_id, true );

try {
$cf_cache = new CloudFlare\Zone\Cache( $GLOBALS['rocket_cloudflare']->auth );
$cf_cache->purge( $GLOBALS['rocket_cloudflare']->zone_id, true );
} catch ( Exception $e ) {}
}

/**
Expand All @@ -196,6 +212,10 @@ function rocket_get_cloudflare_ips() {
return false;
}

$cf_ips_instance = new CloudFlare\IPs( $GLOBALS['rocket_cloudflare']->auth );
return $cf_ips_instance->ips();
try {
$cf_ips_instance = new CloudFlare\IPs( $GLOBALS['rocket_cloudflare']->auth );
return $cf_ips_instance->ips();
} catch ( Exception $e ) {
return false;
}
}

0 comments on commit 60c0af4

Please sign in to comment.