=== Full Site Cache Enabler for KeyCDN ===
Contributors: ze3kr
Donate link: https://www.tlo.xyz/donate/
Tags: keycdn, cache, optimize, performance, speed, pagespeed, html, cdn, keycdn
Requires at least: 4.4
Tested up to: 4.4.2
Stable tag: 2.0.0
License: The MIT License
License URI: https://github.com/ZE3kr/Full-Site-Cache-Enabler-for-KeyCDN/blob/master/LICENSE

Make all HTML page on CDN cache, much faster than other cache plugins (WP Super Cache, W3 Total Cache…). FREE SSL, HTTP/2, GZIP and Let’s Encrypt support.

== Description ==

This plugin can help you to use KeyCDN on your WordPress, not only your Media and CSS, but also all HTML page. It is much faster than other cache plugins (WP Super Cache, W3 Total Cache…). This plugins can automatically purge the page (And also homepage, archive page, tag page, category page, feed, sitemap) when you publish a page or post.

Use this plugin can also let your WordPress support SSL (HTTPS), HTTP/2, GZIP and Let’s Encrypt, **even if your server doesn’t support it**. This plugin is much faster than other cache plugin because this plugin use Layer 7 Cache (CDN), which is fastest.

Require:

+ PHP 5.4+
+ Wordpress 4.4+

You can find KeyCDN support [here](https://www.keycdn.com/support/).

== Installation ==

Before use this plugin, you need to have a KeyCDN account. You can [sign up by this link](https://www.keycdn.com/?a=7126), and you can get $10 free credit, that is included 250GB web traffic, it's enough for your test, and can use for a long time (if you don't have too much web traffic).

Put the folder `full-site-cache-kc` in your server, to `wp-content/plugins/full-site-cache-kc/`, you can [download it at here](https://wordpress.org/plugins/full-site-cache-kc/).

Or if you can add plugin online, you can search `full-site-cache-kc` and install it.

After that, goto the settings page of this plugin, which is called “KeyCDN”, and click “Setup Online” button, and following the introduction of the installation.

== Manual Setup ==

You can set it up manually, and use an existing Zone.

Note: This plugin doesn’t support root domain like `example.com`, you have to change settings before use it.

= 1. Setup this plugin =

Add the configuration code to your `wp-config.php` file in `wp-config.php` **above** the line reading `/* That’s all, stop editing! Happy blogging. */`.

You can get those configuration in the settings page of this plugin by click “Generate Configuration (for Manual Setup)” button.

Example configuration:

	$fsckeycdn_useHTTPS = false; // Change it to true if your server support HTTPS.
	$fsckeycdn_x_pull_key = 'KeyCDN'; // By default is `KeyCDN`, to make your server more secure, change it to a random 15 alphameric key, and update KeyCDN Zone settings.
	$fsckeycdn_apikey = 'vHlnpHcE6GPEWyTWWisr4hE9e80Xvr4a'; // You can find your key at here https://app.keycdn.com/users/authSettings
	$fsckeycdn_id = [
		1 => 10001,
		2 => 10002,
		3 => 10003,
	]; // The key (1, 2, 3) is blog id. the value (10001, 10002, 10003) is KeyCDN Zone ID.
	// $fsckeycdn_id = "10001"; // Use this line instead of above if you doesn't use multisite.

Then add a `require_once` function just below the variables just add, that need to run before evey plugin, to identify the server is KeyCDN or not.

	require_once(ABSPATH . 'wp-content/plugins/full-site-cache-kc/include.php'); // This plugin need run some scripts before everything, so you need to add this, if you use a different location for plugins, change it.

After that, you can enable this plugin.

= 2. Setup DNS and Change Siteurl =

You need to use a domain like `www.example.com` or `blog.example.com` but not root domain like `example.com`, because you need to create a CNAME on that domain.

If you are using domain like `www.example.com`, you need to create a DNS at `wp-admin.example.com` that point to your server.

If you are using domain like `blog.example.com`, you need to create a DNS at `wp-admin-blog.example.com` that point to your server.

Example BIND DNS file for root domain:

	example.com 300 IN A <origin-server>
	www.example.com 300 IN CNAME <your-zone-name>-<userid-hex>.kxcdn.com
	wp-admin.example.com 300 IN A <origin-server>

After you setted up, you need to go to the new host to visit WordPress dashboard.

And you need to change Siteurl to the new domain, keep Home URL not change. Example: Siteurl is `wp-admin.example.com` and Homeurl is `www.example.com`. Or Siteurl is `wp-admin-blog.example.com` and Homeurl is `blog.example.com`.

And if you need to edit your post, or go to the dashboard, you need to goto `wp-admin.example.com/wp-admin/` or `wp-admin-blog.example.com/wp-admin/`

= 3. Setup KeyCDN =

You need to add a `Pull Zone` in KeyCDN, and set `Origin URL` to `http(s)://[The IP of your server]`, set SSL to `Letsencrypt` (You can get free SSL support!), enable `Cache Cookies`, `Strip Cookies` and `Forward Host Header`. Set `Expire (in minutes)` to 0 and set `Max Expire (in minutes)` to 1440 or bigger, and make sure to disable `CORS`.

== Advance Options ==

= Default Zone Options =

You can add `$fsckeycdn_default_settings` variable as a array in `wp-config.php` to replace the default value of KeyCDN Zone.

Default value:

	$fsckeycdn_default_settings = [
		'status' => 'active',
		'type' => 'pull',
		'forcedownload' => 'disabled',
		'cors' => 'disabled',
		'gzip' => 'enabled',
		'expire' => '0',
		'http2' => 'enabled',
		'securetoken' => 'disabled',
		'cacheignorecachecontrol' => 'enabled',
		'cacheignorequerystring' => 'disabled',
		'cachestripcookies' => 'enabled',
		'cachecanonical' => 'disabled',
		'cacherobots' => 'disabled',
		'cachehostheader' => 'enabled',
		'cachecookies' => 'enabled',
		'cachestripcookies' => 'enabled',
	];

You can see [all the parameters](https://www.keycdn.com/api#add-zone) that can set.

Example:

	$fsckeycdn_default_settings = [
		'cachemaxexpire' => '525949'.
		'sslcert' => 'letsencrypt',
	]

== Advance Feature ==

= Different API Key for Each Site =

KeyCDN has Zone Limit, so if your has too many site in your multisite, you have to use it.

This plugin can use different API Key for each site:

	$fsckeycdn_apikey = [
		1 => 'vHlnpHcE6GPEWyTWWisr4hE9e80Xvr4a',
		2 => 'w752ikcfTv0srgLhwNHayBpxAss8VT3X',
		3 => 'BI0lFsGBKOF5S6Y0Kc9zaQkq8BQXVJsv',
	];

And you can use different API Key for every 10 site like:

	$fsckeycdn_apikey = [
		'0X' => 'vHlnpHcE6GPEWyTWWisr4hE9e80Xvr4a',
		'1X' => 'w752ikcfTv0srgLhwNHayBpxAss8VT3X',
		'2X' => 'BI0lFsGBKOF5S6Y0Kc9zaQkq8BQXVJsv',
	];

0X for site 1~9, 1X for site 10~19.

= IP White List =

You can enable IP White List feature, to enable this feature add the configuration code to your `wp-config.php` file in `wp-config.php` **above** the line reading `/* That’s all, stop editing! Happy blogging. */`.

Example, if you are using CloudFlare (or other proxy server) and KeyCDN, you might set your DNS like that, [CF] means this domain enabled CloudFlare. Note: You need to disable CloudFlare on KeyCDN domain to ensure make HTML cache available:

	example.com 300 IN A <origin-server> [CF]
	www.example.com 300 IN CNAME <your-zone-name>-<userid-hex>.kxcdn.com
	wp-admin.example.com 300 IN A <origin-server> [CF]

And the configuration code. Note: KeyCDN doesn’t support IPv6 yet, but if your server both support IPv6 and IPv4, it’s still necessary to add `$fsckeycdn_ipv6_white_list`:

	$fsckeycdn_ip_white_list =[
		'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',
	]; // This is CloudFlare's IP.
	$fsckeycdn_ipv6_white_list =[
		'2400:cb00::/32',
		'2405:8100::/32',
		'2405:b500::/32',
		'2606:4700::/32',
		'2803:f800::/32'
	]; // This is CloudFlare's IPv6.
	$fsckeycdn_client_ip = $_SERVER['HTTP_X_CLIENT_IP']; // The IP for CloudFlare
	$fsckeycdn_client_real_ip = $_SERVER['HTTP_CF_CONNECTING_IP']; // The IP for real client (Optional), this is for CloudFlare

Why need to use IP White List? Use this feature can make sure even if others has your origin IP, the request direct to your origin server will reject by this plugin.

= Variable X-Pull Key =

By default online setup will enable this feature, this make X-Pull Key more secure, especially for multisite.

This feature can generate different X-Pull Key for each domain, even if you enable Forward Host Header, it can prevent visitor visit your site using another domain.

Algorithm:

	substr(fsckeycdn_convert('f'.md5($fsckeycdn_x_pull_key.$_SERVER['HTTP_HOST'])),-15);

“fsckeycdn_convert” is a function that can convert hexadecimal to alphameric.

	function fsckeycdn_convert($s, $to=62) {
		$dict = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$res = '';
		$b = '';
		if($to > 0) {
			$to = ceil(log($to, 2));
			for($i=0; $i<strlen($s); $i++) $b .= sprintf('%04b', hexdec($s{$i}));
			while(strlen($b) >= $to) {
				$res = $dict{bindec(substr($b, -$to))} . $res;
				$b = substr($b, 0, -$to);
			}
			$res = $dict{bindec($b)} . $res;
			return $res;
		}
		$to = ceil(log(-$to, 2));
		for($i=0; $i<strlen($s); $i++) $b .= sprintf("%0{$to}b", strpos($dict, $s{$i}));
		while(strlen($b) > 4) {
			$res = $dict{bindec(substr($b, -4))} . $res;
			$b = substr($b, 0, -4);
		}
		if(bindec($b)) $res = $dict{bindec($b)} . $res;
		return $res;
	}

== About Purge ==

This plugin will purge all page that need to be purge, but when your customized your theme or changed your theme, you need to purge your site manually. go to the settings page and you can find purge button.

You can set `$fsckeycdn_purge` to change purge type.

+ `1`. Purge that page and every archive page when a post/page published.
+ `2`. Purge whole blog (not include css, js and image) when a post/page published.
+ `3`. Purge all page that need to be purge (Recommend).
+ `4`. Only purge that page when a post/page published.
+ `5`. Do nothing when a post/page published.

Example:

	$fsckeycdn_purge = 3;

== Extra Settings For Root Domain ==

You will get an error if you trying to enable it for root domain, whatever you setup manually or not.

To solve this problem, you need to change your domain beginning with `www.`, for example, if your blog domain is `example.com`, you have to change it to `www.example.com`. 

= For NOT Multisite Installed =

Go to general settings page, change WordPress Address and Site Address to the new domain.

= For Multisite Installed =

If you NOT use subdomain install, pleace [see this page](http://codex.wordpress.org/Moving_WordPress#Moving_WordPress_Multisite) to find out how to move WordPress multisite to a new URL.

If you use subdomain install, I suggest you create a new blog and that blog’s domain is beginning with `www.`, for example, your have a network blog which use domain `example.com`, and you create a another blog which use domain `www.example.com`. Then input the content from the old blog which use domain `example.com`, and enable this plugin for the new blog.

After you add `require_once` function in `wp-config.php`, it will automatically redirect the old blog to new bog, and not effect with admin page.

== Known Issues ==

+ Customize page can't set widgets and preview, if you want to set, goto `/wp-admin/widgets.php`.
+ Have problems with WP-Cron, suggest [use cron on your server](https://www.lucasrolff.com/wordpress/why-wp-cron-sucks/). Actually, other cache plugin may have same problems.

== Frequently Asked Questions ==

= It takes a long time to publish a long time to publish a post or page? =

That is because this plugin will purge the cache immediately, and that need to send a request to KeyCDN server. It very depend on your network and KeyCDN network

== Changelog ==

= 2.0.0 =

+ Add setup online feature, you don’t need to create a KeyCDN Zone by you self.
+ Add a admin page that can purge page.
+ Add Variable X-Pull Key feature.
+ Add Different API Key for Each Site feature.

= 1.0.0 =

Improve performance, some variable changed in this version, you need to do something before update, see “Upgrade Notice”.

= 0.4.3 =

IPv6 White list supported!

= 0.4.2 =

Never redirect when user is logged in.

= 0.4.1 =

Just update readme.txt

= 0.4.0 =

+ Disallow Direct File Access to plugin files.
+ Use Unique function (and/or define) names.

== Upgrade Notice ==

= 2.0.0 =

This is a big update, you can setup online very easily now.

+ Add setup online feature, you don’t need to create a KeyCDN Zone by you self.
+ Add a admin page that can purge page.
+ Add Variable X-Pull Key feature.
+ Add Different API Key for Each Site feature.

= 1.0.0 =

Improve performance.

You need to do something before update this plugin to v1.0.0.

First, you need to change `$fsckeycdn_id` variable, like this:

	$fsckeycdn_id = [
		1 => 10001,
		2 => 10002,
		3 => 10003,
	]; // The key (1, 2, 3) is blog id. the value (10001, 10002, 10003) is KeyCDN Zone ID.

Then, you need to change KeyCDN Zone settings, change `Expire (in minutes)` to 0.

= 0.4.3 =

IPv6 White list supported!

= 0.4.2 =

You can see admin bar when you enter admin page, can show post/page preview.

= 0.4.1 =

Just update readme.txt

= 0.4.0 =

This update changed some function name and variable name, you need to edit `wp-config.php`.

== Screenshots ==

1. Before setup.
2. Online setup.
3. Settings page.
