Skip to content

Commit

Permalink
Merge pull request #205 from wp-media/branch-2.8.21
Browse files Browse the repository at this point in the history
2.8.21
  • Loading branch information
GeekPress authored Oct 17, 2016
2 parents 49fd3a2 + 5c5ed8a commit 4003a3d
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 43 deletions.
24 changes: 20 additions & 4 deletions inc/admin/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ function rocket_display_options() {
'minify_css_key',
'minify_js_key',
'version',
'cloudflare_old_settings'
'cloudflare_old_settings',
'cloudflare_zone_id'
)
);

Expand Down Expand Up @@ -1089,12 +1090,27 @@ function rocket_pre_main_option( $newvalue, $oldvalue ) {
$newvalue['minify_js_key'] = create_rocket_uniqid();
}

// Update CloudFlare zone ID if CloudFlare domain was changed
if ( isset( $newvalue['cloudflare_domain'], $oldvalue['cloudflare_domain'] ) && $newvalue['cloudflare_domain'] != $oldvalue['cloudflare_domain'] && phpversion() >= '5.4' ) {
$cf_instance = get_rocket_cloudflare_api_instance();
if ( ! is_wp_error( $cf_instance ) ) {
try {
$zone_instance = new CloudFlare\Zone( $cf_instance );
$zone = $zone_instance->zones( $newvalue['cloudflare_domain'] );

if ( isset( $zone->result[0]->id ) ) {
$newvalue['cloudflare_zone_id'] = $zone->result[0]->id;
}
} catch ( Exception $e ) {}
}
}

// Save old CloudFlare settings
if ( ( isset( $newvalue['cloudflare_auto_settings'], $oldvalue['cloudflare_auto_settings'] ) && $newvalue['cloudflare_auto_settings'] != $oldvalue['cloudflare_auto_settings'] && $newvalue['cloudflare_auto_settings'] == 1 ) ) {
$cf_settings = get_rocket_cloudflare_settings();
$cf_settings = array_filter( $cf_settings );
$newvalue['cloudflare_old_settings'] = ( isset ( $cf_settings ) ) ? implode( ',' , $cf_settings ) : '';
if ( ( bool ) $cf_settings ) {
$newvalue['cloudflare_old_settings'] = ( isset ( $cf_settings ) ) ? implode( ',' , array_filter( $cf_settings ) ) : '';
}
}

// Checked the SSL option if the whole website is on SSL
Expand Down
1 change: 1 addition & 0 deletions inc/admin/ui/modules/basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
'<a href="https://wordpress.org/plugins/wp-mobilizer/" target="_blank">WP-Mobilizer</a>',
'<a href="https://wordpress.org/plugins/wp-mobile-edition/" target="_blank">WP Mobile Edition</a>',
'<a href="https://wordpress.org/plugins/device-theme-switcher/" target="_blank">Device Theme Switcher</a>',
'<a href="https://wordpress.org/plugins/wp-mobile-detect/" target="_blank">WP Mobile Detect</a>',
'<a href="https://codecanyon.net/item/easy-social-share-buttons-for-wordpress/6394476" target="_blank">Easy Social Share Buttons</a>',
);

Expand Down
20 changes: 20 additions & 0 deletions inc/admin/upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ function rocket_first_install() {
'cloudflare_email' => '',
'cloudflare_api_key' => '',
'cloudflare_domain' => '',
'cloudflare_zone_id' => '',
'cloudflare_devmode' => 0,
'cloudflare_protocol_rewrite' => 0,
'cloudflare_auto_settings' => 0,
Expand Down Expand Up @@ -303,4 +304,23 @@ function rocket_new_upgrade( $wp_rocket_version, $actual_version ) {

update_option( WP_ROCKET_SLUG, $options );
}

// Add a value to the new CF zone_id field if the CF domain is set
if ( version_compare( $actual_version, '2.8.21', '<' ) && phpversion() < '5.4' ) {
$options = get_option( WP_ROCKET_SLUG );
if ( 0 < $options['do_cloudflare'] && $options['cloudflare_domain'] !== '' ) {
$cf_instance = get_rocket_cloudflare_api_instance();
if ( ! is_wp_error( $cf_instance ) ) {
try {
$zone_instance = new CloudFlare\Zone( $cf_instance );
$zone = $zone_instance->zones( $options['cloudflare_domain'] );

if ( isset( $zone->result[0]->id ) ) {
$options['cloudflare_zone_id'] = $zone->result[0]->id;
update_option( WP_ROCKET_SLUG, $options );
}
} catch ( Exception $e ) {}
}
}
}
}
106 changes: 81 additions & 25 deletions inc/functions/cloudflare.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
<?php
defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' );

/**
* Get a CloudFlare\Api instance
*
* @since 2.8.21
* @author Remy Perona
*
* @return Object CloudFlare\Api instance if crendentials are set, WP_Error otherwise
*/
function get_rocket_cloudflare_api_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 ) ) {
return new WP_Error( 'cloudflare_credentials_empty', __( 'CloudFlare Email & API Key are not set', 'rocket' ) );
}
return new Cloudflare\Api( $cf_email, $cf_api_key );
}

/**
* Get a CloudFlare\Api instance & the zone_id corresponding to the domain
*
* @since 2.8.21 Get the zone ID from the options
* @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 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 ) ];

try {
$zone_instance = new CloudFlare\Zone( $cf_instance->auth );
$cf_domain = get_rocket_option( 'cloudflare_domain', null );
$zone = $zone_instance->zones( $cf_domain );
$cf_api_instance = get_rocket_cloudflare_api_instance();
if ( is_wp_error( $cf_api_instance ) ) {
return false;
}

if ( isset( $zone->result[0]->id ) ) {
$cf_instance->zone_id = $zone->result[0]->id;
return $cf_instance;
}
} catch ( Exception $e ) {}
$cf_zone_id = get_rocket_option( 'cloudflare_zone_id', null );

return false;
}
if ( ! isset( $cf_zone_id ) ) {
return false;
}

$cf_instance = ( object ) [ 'auth' => $cf_api_instance, 'zone_id' => $cf_zone_id ];

return false;
return $cf_instance;
}

/**
Expand Down Expand Up @@ -203,19 +215,63 @@ function rocket_purge_cloudflare() {
/**
* Get CloudFlare IPs.
*
* @since 2.8.21 Save IPs in a transient to prevent calling the API everytime
* @since 2.8.16
*
* @return Object Result of API request
* @author Remy Perona
*
* @return mixed Bool|Object Result of API request, false otherwise
*/
function rocket_get_cloudflare_ips() {
if( ! is_object( $GLOBALS['rocket_cloudflare'] ) ) {
$cf_instance = get_rocket_cloudflare_api_instance();

if ( is_wp_error( $cf_instance ) ) {
return false;
}

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

if ( isset( $cf_ips->success ) && $cf_ips->success ) {
set_transient( 'rocket_cloudflare_ips', $cf_ips, 2 * WEEK_IN_SECONDS );
} else {
throw new Exception( 'Error connecting to CloudFlare' );
}
} catch ( Exception $e ) {
$cf_ips = ( object ) [ 'success' => true, 'result' => ( object ) [] ];
$cf_ips->result->ipv4_cidrs = array(
'103.21.244.0/22',
'103.22.200.0/22',
'103.31.4.0/22',
'104.16.0.0/12',
'108.162.192.0/18',
'131.0.72.0/22',
'141.101.64.0/18',
'162.158.0.0/15',
'172.64.0.0/13',
'173.245.48.0/20',
'188.114.96.0/20',
'190.93.240.0/20',
'197.234.240.0/22',
'198.41.128.0/17',
'199.27.128.0/21',
);

$cf_ips->result->ipv6_cidrs = array(
'2400:cb00::/32',
'2405:8100::/32',
'2405:b500::/32',
'2606:4700::/32',
'2803:f800::/32',
'2c0f:f248::/32',
'2a06:98c0::/29',
);

return $cf_ips;
}
}

return $cf_ips;
}
5 changes: 3 additions & 2 deletions inc/vendors/CloudFlare/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,16 @@ protected function request($path, array $data = null, $method = null)
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => false,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYPEER => true,
];

$curl_options = $default_curl_options;
if (isset($this->curl_options) && is_array($this->curl_options)) {
$curl_options = array_replace($default_curl_options, $this->curl_options);
}

$headers = ["X-Auth-Email: {$this->email}", "X-Auth-Key: {$this->auth_key}"];
$wp_rocket_version = WP_ROCKET_VERSION;
$headers = ["X-Auth-Email: {$this->email}", "X-Auth-Key: {$this->auth_key}", "User-Agent: wp-rocket/{$wp_rocket_version}"];

$ch = curl_init();
curl_setopt_array($ch, $curl_options);
Expand Down
1 change: 1 addition & 0 deletions uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
delete_transient( 'wp_rocket_settings' );
delete_transient( 'rocket_check_licence_30' );
delete_transient( 'rocket_check_licence_1' );
delete_transient( 'rocket_cloudflare_ips' );

// Delete WP Rocket options
delete_option( 'wp_rocket_settings' );
Expand Down
22 changes: 10 additions & 12 deletions wp-rocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: WP Rocket
Plugin URI: https://wp-rocket.me
Description: The best WordPress performance plugin.
Version: 2.8.20
Version: 2.8.21
Code Name: Ilum
Author: WP Media
Contributors: Jonathan Buttigieg, Julio Potier, Remy Perona
Expand All @@ -19,7 +19,7 @@
defined( 'ABSPATH' ) or die( 'Cheatin&#8217; uh?' );

// Rocket defines
define( 'WP_ROCKET_VERSION' , '2.8.20' );
define( 'WP_ROCKET_VERSION' , '2.8.21' );
define( 'WP_ROCKET_PRIVATE_KEY' , false );
define( 'WP_ROCKET_SLUG' , 'wp_rocket_settings' );
define( 'WP_ROCKET_WEB_MAIN' , 'http://support.wp-rocket.me/' );
Expand Down Expand Up @@ -95,16 +95,6 @@ function rocket_init()
// Call defines, classes and functions
require( WP_ROCKET_FUNCTIONS_PATH . 'options.php' );

if ( phpversion() >= '5.4' ) {
require( WP_ROCKET_VENDORS_PATH . 'CloudFlare/Exception/AuthenticationException.php' );
require( WP_ROCKET_VENDORS_PATH . 'CloudFlare/Exception/UnauthorizedException.php' );
require( WP_ROCKET_VENDORS_PATH . 'CloudFlare/Api.php' );
require( WP_ROCKET_VENDORS_PATH . 'CloudFlare/IPs.php' );
require( WP_ROCKET_VENDORS_PATH . 'CloudFlare/Zone.php' );
require( WP_ROCKET_VENDORS_PATH . 'CloudFlare/Zone/Cache.php' );
require( WP_ROCKET_VENDORS_PATH . 'CloudFlare/Zone/Settings.php' );
require( WP_ROCKET_FUNCTIONS_PATH . 'cloudflare.php' );
}
// Last constants
define( 'WP_ROCKET_PLUGIN_NAME', get_rocket_option( 'wl_plugin_name', 'WP Rocket' ) );
define( 'WP_ROCKET_PLUGIN_SLUG', sanitize_key( WP_ROCKET_PLUGIN_NAME ) );
Expand Down Expand Up @@ -140,6 +130,14 @@ function rocket_init()
}

if ( 0 < (int) get_rocket_option( 'do_cloudflare' ) && phpversion() >= '5.4' ) {
require( WP_ROCKET_VENDORS_PATH . 'CloudFlare/Exception/AuthenticationException.php' );
require( WP_ROCKET_VENDORS_PATH . 'CloudFlare/Exception/UnauthorizedException.php' );
require( WP_ROCKET_VENDORS_PATH . 'CloudFlare/Api.php' );
require( WP_ROCKET_VENDORS_PATH . 'CloudFlare/IPs.php' );
require( WP_ROCKET_VENDORS_PATH . 'CloudFlare/Zone.php' );
require( WP_ROCKET_VENDORS_PATH . 'CloudFlare/Zone/Cache.php' );
require( WP_ROCKET_VENDORS_PATH . 'CloudFlare/Zone/Settings.php' );
require( WP_ROCKET_FUNCTIONS_PATH . 'cloudflare.php' );
require( WP_ROCKET_VENDORS_PATH . 'ip_in_range.php' );
require( WP_ROCKET_COMMON_PATH . 'cloudflare.php' );
}
Expand Down

0 comments on commit 4003a3d

Please sign in to comment.