* Get all the current Cloudflare settings for a given domain. * * @return array|WP_Error Array of Cloudflare settings, WP_Error if any error connection to Cloudflare. */ public function get_settings() { $cf_settings = $this->endpoints->get_settings( $this->options->get( 'cloudflare_zone_id', '' ) ); if ( is_wp_error( $cf_settings ) ) { return $cf_settings; } foreach ( $cf_settings as $cloudflare_option ) { switch ( $cloudflare_option->id ) { case 'browser_cache_ttl': $browser_cache_ttl = $cloudflare_option->value; break; case 'cache_level': $cache_level = $cloudflare_option->value; break; case 'rocket_loader': $rocket_loader = $cloudflare_option->value; break; case 'minify': $cf_minify = $cloudflare_option->value; break; } } $cf_minify_value = 'on'; if ( 'off' === $cf_minify->js || 'off' === $cf_minify->css || 'off' === $cf_minify->html ) { $cf_minify_value = 'off'; } $cf_settings_array = [ 'cache_level' => $cache_level, 'minify' => $cf_minify_value, 'rocket_loader' => $rocket_loader, 'browser_cache_ttl' => $browser_cache_ttl, ]; return $cf_settings_array; } /** * Get Cloudflare IPs. No API validation needed, all exceptions returns the default CF IPs array. * * @return object Result of API request if successful, default CF IPs otherwise. */ public function get_cloudflare_ips() { $cf_ips = get_transient( 'rocket_cloudflare_ips' ); if ( false !== $cf_ips ) { return $cf_ips; } $cf_ips = $this->endpoints->get_ips(); if ( is_wp_error( $cf_ips ) || empty( $cf_ips ) ) { // Set default IPs from Cloudflare if call to Cloudflare /ips API does not contain a success. // Prevents from making API calls on each page load. $cf_ips = $this->get_default_ips(); } set_transient( 'rocket_cloudflare_ips', $cf_ips, 2 * WEEK_IN_SECONDS ); return $cf_ips; } /** * Get default Cloudflare IPs. * * @return object Default Cloudflare connecting IPs. */ private function get_default_ips() { $cf_ips = (object) [ 'ipv4_cidrs' => [], 'ipv6_cidrs' => [], ]; $cf_ips->ipv4_cidrs = [ '173.245.48.0/20', '103.21.244.0/22', '103.22.200.0/22', '103.31.4.0/22', '141.101.64.0/18', '108.162.192.0/18', '190.93.240.0/20', '188.114.96.0/20', '197.234.240.0/22', '198.41.128.0/17', '162.158.0.0/15', '104.16.0.0/12', '104.24.0.0/14', '172.64.0.0/13', '131.0.72.0/22', ]; $cf_ips->ipv6_cidrs = [ '2400:cb00::/32', '2606:4700::/32', '2803:f800::/32', '2405:b500::/32', '2405:8100::/32', '2a06:98c0::/29', '2c0f:f248::/32', ]; return $cf_ips; } /** * Sets the Cloudflare IP Rewrite * * @return IpRewrite */ public static function set_ip_rewrite() { static $instance = null; if ( is_null( $instance ) ) { $instance = new IpRewrite(); return $instance; } return $instance; } /** * Fixes Cloudflare Flexible SSL redirect loop */ public static function fix_cf_flexible_ssl() { $ip_rewrite = self::set_ip_rewrite(); if ( $ip_rewrite->isCloudFlare() ) { // Fixes Flexible SSL. if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO'] ) { $_SERVER['HTTPS'] = 'on'; } } } /** * Change client auth. * * @param AuthInterface $auth Client auth. * * @return void */ public function change_auth( AuthInterface $auth ) { $this->endpoints->change_auth( $auth ); } }