hardcoded on/off VCL Generator, filters to readme
hardcoded on/off VCL Generator, filters to readme

file:a/readme.txt -> file:b/readme.txt
=== Varnish Caching === === Varnish Caching ===
Donate link: www.paypal.com/use/email/razvan_stanga@yahoo.com Donate link: http://git.razvi.ro/
Contributors: razvanstanga Contributors: razvanstanga
Tags: varnish, purge, cache, caching, optimization, performance, traffic Tags: varnish, purge, cache, caching, optimization, performance, traffic
Requires at least: 4.0 Requires at least: 4.0
Tested up to: 4.6 Tested up to: 4.7
Stable tag: 1.5.2 Stable tag: 1.5.3
License: GPLv2 or later License: GPLv2 or later
   
Wordpress Varnish Cache 3.x/4.x integration Wordpress Varnish Cache 3.x/4.x integration
   
== Description == == Description ==
Complete Wordpress Varnish Cache 3.x/4.x integration. Complete Wordpress Varnish Cache 3.x/4.x integration.
   
This plugin handles all integration with Varnish Cache. It was designed for high traffic websites. This plugin handles all integration with Varnish Cache. It was designed for high traffic websites.
   
Main features Main features
   
* admin interface, see screenshots * admin interface, see screenshots
* console for manual purges, supports regular expressions so you can purge an entire folder or just a single file * console for manual purges, supports regular expressions so you can purge an entire folder or just a single file
* supports every type of Varnish Cache implementation, see screenshots for examples * supports every type of Varnish Cache implementation, see screenshots for examples
* unlimited number of Varnish Cache servers * unlimited number of Varnish Cache servers
* use of custom headers when communicating with Varnish Cache does not interfere with other caching plugins, cloudflare, etc * use of custom headers when communicating with Varnish Cache does not interfere with other caching plugins, cloudflare, etc
* Varnish Cache configuration generator * Varnish Cache configuration generator
* purge key method so you don't need to setup ACLs * purge key method so you don't need to setup ACLs
* debugging * debugging
* actively maintained * actively maintained
   
You can control the following from the Varnish Caching admin panel : You can control the following from the Varnish Caching admin panel :
   
* Enable/Disable caching * Enable/Disable caching
* Homepage cache TTL * Homepage cache TTL
* Cache TTL (for every other page) * Cache TTL (for every other page)
* IPs/Hosts to clear cache to support every type of Varnish Cache implementation * IPs/Hosts to clear cache to support every type of Varnish Cache implementation
* Override default TTL in posts/pages * Override default TTL in posts/pages
* Purge key based PURGE * Purge key based PURGE
* Logged in cookie * Logged in cookie
* Debugging option * Debugging option
* console for precise manual purges * console for precise manual purges
   
This plugin also auto purges Varnish Cache when your site is modified. This plugin also auto purges Varnish Cache when your site is modified.
   
Varnish Caching sends a PURGE request to Varnish Cache when a page or post is modified. This occurs when editing, publishing, commenting or deleting an item, and when changing themes. Varnish Caching sends a PURGE request to Varnish Cache when a page or post is modified. This occurs when editing, publishing, commenting or deleting an item, and when changing themes.
Not all pages are purged every time, depending on your Varnish configuration. When a post, page, or custom post type is edited, or a new comment is added, <em>only</em> the following pages will purge: Not all pages are purged every time, depending on your Varnish configuration. When a post, page, or custom post type is edited, or a new comment is added, <em>only</em> the following pages will purge:
   
* The front page * The front page
* The post/page edited * The post/page edited
* Any categories or tags associated with the page * Any categories or tags associated with the page
   
<a href="https://www.varnish-cache.org/">Varnish Cache</a> is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents. This plugin <em>does not</em> install Varnish for you, nor does it configure Varnish for WordPress. It's expected you already did that on your own using the provided config files. <a href="https://www.varnish-cache.org/">Varnish Cache</a> is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents. This plugin <em>does not</em> install Varnish for you, nor does it configure Varnish for WordPress. It's expected you already did that on your own using the provided config files.
   
Inspired from the following : Inspired from the following :
   
* https://wordpress.org/plugins/varnish-http-purge/ * https://wordpress.org/plugins/varnish-http-purge/
* https://github.com/dreamhost/varnish-vcl-collection/ * https://github.com/dreamhost/varnish-vcl-collection/
   
== Installation == == Installation ==
   
* You must install Varnish Cache on your server(s) * You must install Varnish Cache on your server(s)
* Go to the configuration generator. Fill in the backends/ACLs then download the configuration files * Go to the configuration generator. Fill in the backends/ACLs then download the configuration files
* Use these configuration files to configure Varnish Cache server(s). Usualy the configuration files are in /etc/varnish. In most cases you must put the downloaded configuration files in /etc/varnish and restart Varnish Cache * Use these configuration files to configure Varnish Cache server(s). Usualy the configuration files are in /etc/varnish. In most cases you must put the downloaded configuration files in /etc/varnish and restart Varnish Cache
   
Or use the provided Varnish Cache configuration files located in /wp-content/plugins/vcaching/varnish-conf folder. Or use the provided Varnish Cache configuration files located in /wp-content/plugins/vcaching/varnish-conf folder.
   
You can also use the purge key method if you can't setup ACLs. You must fill in lib/purge.vcl the purge key. You can also use the purge key method if you can't setup ACLs. You must fill in lib/purge.vcl the purge key.
   
== Frequently Asked Questions == == Frequently Asked Questions ==
   
= What version of Varnish is supported? = = What version of Varnish is supported? =
   
This was built and tested on Varnish 3.x/4.x. This was built and tested on Varnish 3.x/4.x.
   
= Why doesn't every page flush when I make a new post? = = Why doesn't every page flush when I make a new post? =
   
The only pages that should purge are the post's page, the front page, categories, and tags. The only pages that should purge are the post's page, the front page, categories, and tags.
   
= How do I manually purge the whole cache? = = How do I manually purge the whole cache? =
   
Click the 'Purge ALL Varnish Cache' button on the "Right Now" Dashboard. Click the 'Purge ALL Varnish Cache' button on the "Right Now" Dashboard.
   
= How do I manually purge cache? = = How do I manually purge cache? =
   
Use the console. For example you can purge the whole uploads folder with the URL /wp-content/uploads/.* Use the console. For example you can purge the whole uploads folder with the URL /wp-content/uploads/.*
   
= Does this work with W3 Total Cache? = = Does this work with W3 Total Cache? =
   
Yes it does. This plugin uses its own custom headers to communicate with Varnish and does not interfere with the heders sent by W3 Total Cache or any other caching plugin. Yes it does. This plugin uses its own custom headers to communicate with Varnish and does not interfere with the heders sent by W3 Total Cache or any other caching plugin.
   
= Varnish Statistics = = Varnish Statistics =
   
Statistics need a special setup. More info on the Statistics tab on your Wordpress environment. Statistics need a special setup. More info on the Statistics tab on your Wordpress environment.
   
= How do I configure my Varnish Cache VCL? = = How do I configure my Varnish Cache VCL? =
   
Use the Varnish Cache configuration generator. Fill in the backends/ACLs then download your configuration files. Use the Varnish Cache configuration generator. Fill in the backends/ACLs then download your configuration files.
Or use the provided Varnish Cache configuration files located in /wp-content/plugins/vcaching/varnish-conf folder. Or use the provided Varnish Cache configuration files located in /wp-content/plugins/vcaching/varnish-conf folder.
   
= Can I use this with a proxy service like CloudFlare? = = Can I use this with a proxy service like CloudFlare? =
   
Yes. Yes.
   
= What is logged in cookie? = = What is logged in cookie? =
   
Logged in cookie is a special cookie this plugin sets upon user login. Varnish Cache uses this cookie to bypass caching for logged in users. Logged in cookie is a special cookie this plugin sets upon user login. Varnish Cache uses this cookie to bypass caching for logged in users.
   
This is a small step towards securing your site for denial of service attacks. Denial of service attacks can happen if the attacker bypasses Varnish Cache and hits the backend directly. This is a small step towards securing your site for denial of service attacks. Denial of service attacks can happen if the attacker bypasses Varnish Cache and hits the backend directly.
With the current configuration and the way Wordpress works, this can still happen with POST/AJAX requests. With the current configuration and the way Wordpress works, this can still happen with POST/AJAX requests.
   
  = Available filters =
   
  * `vcaching_varnish_ips` - change the IPs set in Settings
  * `vcaching_varnish_hosts` - change the Hosts set in Settings
  * `vcaching_events` - add events to trigger the purge
  * `vcaching_schema` - change the schema (default is http://)
  * `vcaching_purge_urls` - add additional URLs to purge
   
== Changelog == == Changelog ==
   
= 1.5.2 = = 1.5.2 =
* added AMP URL purge * added AMP URL purge
   
= 1.5.1 = = 1.5.1 =
* fixed PHP notices * fixed PHP notices
* tested with 4.6 * tested with 4.6
   
= 1.5 = = 1.5 =
* `Purge from Varnish` post/page action link * `Purge from Varnish` post/page action link
* removed 10 chars logged in cookie restriction * removed 10 chars logged in cookie restriction
* code cleanup/some wp coding standards * code cleanup/some wp coding standards
* vcaching_varnish_ips filter * vcaching_varnish_ips filter
* vcaching_varnish_hosts filter * vcaching_varnish_hosts filter
   
= 1.4.3 = = 1.4.3 =
* Truncate option added for too many 'trying to purge' messages. Added check for ZipArchive class to download VCLs. * Truncate option added for too many 'trying to purge' messages. Added check for ZipArchive class to download VCLs.
   
= 1.4.2 = = 1.4.2 =
* Bugfix release. Replaced home_url with plugins_url to show VCaching image * Bugfix release. Replaced home_url with plugins_url to show VCaching image
   
= 1.4.1 = = 1.4.1 =
* Do not cache static files on admin domain * Do not cache static files on admin domain
   
= 1.4 = = 1.4 =
* Varnish Cache configuration generator * Varnish Cache configuration generator
* added `logged in cookie`. This replaces the logged in admin/user based on Wordpress standard cookies to bypass caching * added `logged in cookie`. This replaces the logged in admin/user based on Wordpress standard cookies to bypass caching
* moved backends to conf/backend.vcl * moved backends to conf/backend.vcl
* moved ACLs to conf/acl.vcl * moved ACLs to conf/acl.vcl
* updated VCLs. If you are using 1.3 VCLs should upgrade to 1.4 * updated VCLs. If you are using 1.3 VCLs should upgrade to 1.4
   
= 1.3.3 = = 1.3.3 =
* support for Varnish 4 * support for Varnish 4
   
= 1.3.2 = = 1.3.2 =
* bugfix displaying a single server stats * bugfix displaying a single server stats
   
= 1.3.1 = = 1.3.1 =
* better varnish statistics, generated by varnishstat * better varnish statistics, generated by varnishstat
   
= 1.3 = = 1.3 =
* varnish statistics * varnish statistics
   
= 1.2.3 = = 1.2.3 =
* wordpress 4.4 compatibility * wordpress 4.4 compatibility
* Romanian language file * Romanian language file
   
= 1.2.1 = = 1.2.1 =
* wp cli * wp cli
   
= 1.2 = = 1.2 =
* console for precise manual purge * console for precise manual purge
   
= 1.1 = = 1.1 =
* Play nice with W3 Total Cache * Play nice with W3 Total Cache
   
= 1.0 = = 1.0 =
* Initial commit * Initial commit
   
== Upgrade Notice == == Upgrade Notice ==
   
= 1.4 = = 1.4 =
Users using 1.3 Varnish Cache VCLs should upgrade to 1.4 Users using 1.3 Varnish Cache VCLs should upgrade to 1.4
   
== Screenshots == == Screenshots ==
   
1. Settings admin panel 1. Settings admin panel
2. Console/manual purge admin panel 2. Console/manual purge admin panel
3. Varnish Cache Statistics admin panel 3. Varnish Cache Statistics admin panel
4. Varnish Cache configuration generator admin panel 4. Varnish Cache configuration generator admin panel
5. override default TTL in posts/pages 5. override default TTL in posts/pages
6. integration example 6. integration example
   
<?php <?php
/* /*
Plugin Name: Varnish Caching Plugin Name: Varnish Caching
Plugin URI: http://wordpress.org/extend/plugins/vcaching/ Plugin URI: http://wordpress.org/extend/plugins/vcaching/
Description: WordPress Varnish Cache integration. Description: WordPress Varnish Cache integration.
Version: 1.5.2 Version: 1.5.3
Author: Razvan Stanga Author: Razvan Stanga
Author URI: http://git.razvi.ro/ Author URI: http://git.razvi.ro/
License: http://www.apache.org/licenses/LICENSE-2.0 License: http://www.apache.org/licenses/LICENSE-2.0
Text Domain: vcaching Text Domain: vcaching
Network: true Network: true
   
Copyright 2016: Razvan Stanga (email: varnish-caching@razvi.ro) Copyright 2016: Razvan Stanga (email: varnish-caching@razvi.ro)
*/ */
   
class VCaching { class VCaching {
protected $blogId; protected $blogId;
protected $plugin = 'vcaching'; protected $plugin = 'vcaching';
protected $prefix = 'varnish_caching_'; protected $prefix = 'varnish_caching_';
protected $purgeUrls = array(); protected $purgeUrls = array();
protected $varnishIp = null; protected $varnishIp = null;
protected $varnishHost = null; protected $varnishHost = null;
protected $dynamicHost = null; protected $dynamicHost = null;
protected $ipsToHosts = array(); protected $ipsToHosts = array();
protected $statsJsons = array(); protected $statsJsons = array();
protected $purgeKey = null; protected $purgeKey = null;
protected $getParam = 'purge_varnish_cache'; protected $getParam = 'purge_varnish_cache';
protected $postTypes = array('page', 'post'); protected $postTypes = array('page', 'post');
protected $override = 0; protected $override = 0;
protected $customFields = array(); protected $customFields = array();
protected $noticeMessage = ''; protected $noticeMessage = '';
protected $truncateNotice = false; protected $truncateNotice = false;
protected $truncateCount = 0; protected $truncateCount = 0;
protected $debug = 0; protected $debug = 0;
  protected $vclGeneratorTab = true;
   
public function __construct() public function __construct()
{ {
global $blog_id; global $blog_id;
defined($this->plugin) || define($this->plugin, true); defined($this->plugin) || define($this->plugin, true);
   
$this->blogId = $blog_id; $this->blogId = $blog_id;
add_action('init', array(&$this, 'init')); add_action('init', array(&$this, 'init'));
add_action('activity_box_end', array($this, 'varnish_glance'), 100); add_action('activity_box_end', array($this, 'varnish_glance'), 100);
} }
   
public function init() public function init()
{ {
load_plugin_textdomain($this->plugin, false, plugin_basename( dirname( __FILE__ ) ) . '/languages' ); load_plugin_textdomain($this->plugin, false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
   
$this->customFields = array( $this->customFields = array(
array( array(
'name' => 'ttl', 'name' => 'ttl',
'title' => 'TTL', 'title' => 'TTL',
'description' => __('Not required. If filled in overrides default TTL of %s seconds. 0 means no caching.', $this->plugin), 'description' => __('Not required. If filled in overrides default TTL of %s seconds. 0 means no caching.', $this->plugin),
'type' => 'text', 'type' => 'text',
'scope' => array('post', 'page'), 'scope' => array('post', 'page'),
'capability' => 'manage_options' 'capability' => 'manage_options'
) )
); );
   
$this->setup_ips_to_hosts(); $this->setup_ips_to_hosts();
$this->purgeKey = ($purgeKey = trim(get_option($this->prefix . 'purge_key'))) ? $purgeKey : null; $this->purgeKey = ($purgeKey = trim(get_option($this->prefix . 'purge_key'))) ? $purgeKey : null;
$this->admin_menu(); $this->admin_menu();
   
add_action('wp', array($this, 'buffer_start'), 1000000); add_action('wp', array($this, 'buffer_start'), 1000000);
add_action('shutdown', array($this, 'buffer_end'), 1000000); add_action('shutdown', array($this, 'buffer_end'), 1000000);
   
$this->truncateNotice = get_option($this->prefix . 'truncate_notice'); $this->truncateNotice = get_option($this->prefix . 'truncate_notice');
$this->debug = get_option($this->prefix . 'debug'); $this->debug = get_option($this->prefix . 'debug');
   
// send headers to varnish // send headers to varnish
add_action('send_headers', array($this, 'send_headers'), 1000000); add_action('send_headers', array($this, 'send_headers'), 1000000);
   
// logged in cookie // logged in cookie
add_action('wp_login', array($this, 'wp_login'), 1000000); add_action('wp_login', array($this, 'wp_login'), 1000000);
add_action('wp_logout', array($this, 'wp_logout'), 1000000); add_action('wp_logout', array($this, 'wp_logout'), 1000000);
   
// register events to purge post // register events to purge post
foreach ($this->get_register_events() as $event) { foreach ($this->get_register_events() as $event) {
add_action($event, array($this, 'purge_post'), 10, 2); add_action($event, array($this, 'purge_post'), 10, 2);
} }
   
// purge all cache from admin bar // purge all cache from admin bar
if ($this->check_if_purgeable()) { if ($this->check_if_purgeable()) {
add_action('admin_bar_menu', array($this, 'purge_varnish_cache_all_adminbar'), 100); add_action('admin_bar_menu', array($this, 'purge_varnish_cache_all_adminbar'), 100);
if (isset($_GET[$this->getParam]) && check_admin_referer($this->plugin)) { if (isset($_GET[$this->getParam]) && check_admin_referer($this->plugin)) {
if (get_option('permalink_structure') == '' && current_user_can('manage_options')) { if (get_option('permalink_structure') == '' && current_user_can('manage_options')) {
add_action('admin_notices' , array($this, 'pretty_permalinks_message')); add_action('admin_notices' , array($this, 'pretty_permalinks_message'));
} }
if ($this->varnishIp == null) { if ($this->varnishIp == null) {
add_action('admin_notices' , array($this, 'purge_message_no_ips')); add_action('admin_notices' , array($this, 'purge_message_no_ips'));
} else { } else {
$this->purge_cache(); $this->purge_cache();
} }
} }
} }
   
// purge post/page cache from post/page actions // purge post/page cache from post/page actions
if ($this->check_if_purgeable()) { if ($this->check_if_purgeable()) {
if(!session_id()) { if(!session_id()) {
session_start(); session_start();
} }
add_filter('post_row_actions', array( add_filter('post_row_actions', array(
&$this, &$this,
'post_row_actions' 'post_row_actions'
), 0, 2); ), 0, 2);
add_filter('page_row_actions', array( add_filter('page_row_actions', array(
&$this, &$this,
'page_row_actions' 'page_row_actions'
), 0, 2); ), 0, 2);
if (isset($_GET['action']) && isset($_GET['post_id']) && ($_GET['action'] == 'purge_post' || $_GET['action'] == 'purge_page') && check_admin_referer($this->plugin)) { if (isset($_GET['action']) && isset($_GET['post_id']) && ($_GET['action'] == 'purge_post' || $_GET['action'] == 'purge_page') && check_admin_referer($this->plugin)) {
$this->purge_post($_GET['post_id']); $this->purge_post($_GET['post_id']);
$_SESSION['vcaching_note'] = $this->noticeMessage; $_SESSION['vcaching_note'] = $this->noticeMessage;
$referer = str_replace('purge_varnish_cache=1', '', wp_get_referer()); $referer = str_replace('purge_varnish_cache=1', '', wp_get_referer());
wp_redirect($referer . (strpos($referer, '?') ? '&' : '?') . 'vcaching_note=' . $_GET['action']); wp_redirect($referer . (strpos($referer, '?') ? '&' : '?') . 'vcaching_note=' . $_GET['action']);
} }
if (isset($_GET['vcaching_note']) && ($_GET['vcaching_note'] == 'purge_post' || $_GET['vcaching_note'] == 'purge_page')) { if (isset($_GET['vcaching_note']) && ($_GET['vcaching_note'] == 'purge_post' || $_GET['vcaching_note'] == 'purge_page')) {
add_action('admin_notices' , array($this, 'purge_post_page')); add_action('admin_notices' , array($this, 'purge_post_page'));
} }
} }
   
if ($this->override = get_option($this->prefix . 'override')) { if ($this->override = get_option($this->prefix . 'override')) {
add_action('admin_menu', array($this, 'create_custom_fields')); add_action('admin_menu', array($this, 'create_custom_fields'));
add_action('save_post', array($this, 'save_custom_fields' ), 1, 2); add_action('save_post', array($this, 'save_custom_fields' ), 1, 2);
add_action('wp_enqueue_scripts', array($this, 'override_ttl'), 1000); add_action('wp_enqueue_scripts', array($this, 'override_ttl'), 1000);
} }
add_action('wp_enqueue_scripts', array($this, 'override_homepage_ttl'), 1000); add_action('wp_enqueue_scripts', array($this, 'override_homepage_ttl'), 1000);
   
// console purge // console purge
if ($this->check_if_purgeable() && isset($_POST['varnish_caching_purge_url'])) { if ($this->check_if_purgeable() && isset($_POST['varnish_caching_purge_url'])) {
$this->purge_url(home_url() . $_POST['varnish_caching_purge_url']); $this->purge_url(home_url() . $_POST['varnish_caching_purge_url']);
add_action('admin_notices' , array($this, 'purge_message')); add_action('admin_notices' , array($this, 'purge_message'));
} }
} }
   
public function override_ttl($post) public function override_ttl($post)
{ {
$postId = isset($GLOBALS['wp_the_query']->post->ID) ? $GLOBALS['wp_the_query']->post->ID : 0; $postId = isset($GLOBALS['wp_the_query']->post->ID) ? $GLOBALS['wp_the_query']->post->ID : 0;
if ($postId && (is_page() || is_single())) { if ($postId && (is_page() || is_single())) {
$ttl = get_post_meta($postId, $this->prefix . 'ttl', true); $ttl = get_post_meta($postId, $this->prefix . 'ttl', true);
if (trim($ttl) != '') { if (trim($ttl) != '') {
Header('X-VC-TTL: ' . intval($ttl), true); Header('X-VC-TTL: ' . intval($ttl), true);
} }
} }
} }
   
public function override_homepage_ttl() public function override_homepage_ttl()
{ {
if (is_home() || is_front_page()) { if (is_home() || is_front_page()) {
$this->homepage_ttl = get_option($this->prefix . 'homepage_ttl'); $this->homepage_ttl = get_option($this->prefix . 'homepage_ttl');
Header('X-VC-TTL: ' . intval($this->homepage_ttl), true); Header('X-VC-TTL: ' . intval($this->homepage_ttl), true);
} }
} }
   
public function buffer_callback($buffer) public function buffer_callback($buffer)
{ {
return $buffer; return $buffer;
} }
   
public function buffer_start() public function buffer_start()
{ {
ob_start(array($this, "buffer_callback")); ob_start(array($this, "buffer_callback"));
} }
   
public function buffer_end() public function buffer_end()
{ {
ob_end_flush(); ob_end_flush();
} }
   
protected function setup_ips_to_hosts() protected function setup_ips_to_hosts()
{ {
$this->varnishIp = get_option($this->prefix . 'ips'); $this->varnishIp = get_option($this->prefix . 'ips');
$this->varnishHost = get_option($this->prefix . 'hosts'); $this->varnishHost = get_option($this->prefix . 'hosts');
$this->dynamicHost = get_option($this->prefix . 'dynamic_host'); $this->dynamicHost = get_option($this->prefix . 'dynamic_host');
$this->statsJsons = get_option($this->prefix . 'stats_json_file'); $this->statsJsons = get_option($this->prefix . 'stats_json_file');
$varnishIp = explode(',', $this->varnishIp); $varnishIp = explode(',', $this->varnishIp);
$varnishIp = apply_filters('vcaching_varnish_ips', $varnishIp); $varnishIp = apply_filters('vcaching_varnish_ips', $varnishIp);
$varnishHost = explode(',', $this->varnishHost); $varnishHost = explode(',', $this->varnishHost);
$varnishHost = apply_filters('vcaching_varnish_hosts', $varnishHost); $varnishHost = apply_filters('vcaching_varnish_hosts', $varnishHost);
$statsJsons = explode(',', $this->statsJsons); $statsJsons = explode(',', $this->statsJsons);
foreach ($varnishIp as $key => $ip) { foreach ($varnishIp as $key => $ip) {
$this->ipsToHosts[] = array( $this->ipsToHosts[] = array(
'ip' => $ip, 'ip' => $ip,
'host' => $this->dynamicHost ? $_SERVER['HTTP_HOST'] : $varnishHost[$key], 'host' => $this->dynamicHost ? $_SERVER['HTTP_HOST'] : $varnishHost[$key],
'statsJson' => isset($statsJsons[$key]) ? $statsJsons[$key] : null 'statsJson' => isset($statsJsons[$key]) ? $statsJsons[$key] : null
); );
} }
} }
   
public function create_custom_fields() public function create_custom_fields()
{ {
if (function_exists('add_meta_box')) { if (function_exists('add_meta_box')) {
foreach ($this->postTypes as $postType) { foreach ($this->postTypes as $postType) {
add_meta_box($this->plugin, __('Varnish Caching', $this->plugin), array($this, 'display_custom_fields'), $postType, 'side', 'high'); add_meta_box($this->plugin, __('Varnish Caching', $this->plugin), array($this, 'display_custom_fields'), $postType, 'side', 'high');
} }
} }
} }
   
public function save_custom_fields($post_id, $post) public function save_custom_fields($post_id, $post)
{ {
if (!isset($_POST['vc-custom-fields_wpnonce']) || !wp_verify_nonce($_POST['vc-custom-fields_wpnonce'], 'vc-custom-fields')) if (!isset($_POST['vc-custom-fields_wpnonce']) || !wp_verify_nonce($_POST['vc-custom-fields_wpnonce'], 'vc-custom-fields'))
return; return;
if (!current_user_can('edit_post', $post_id)) if (!current_user_can('edit_post', $post_id))
return; return;
if (!in_array($post->post_type, $this->postTypes)) if (!in_array($post->post_type, $this->postTypes))
return; return;
foreach ($this->customFields as $customField) { foreach ($this->customFields as $customField) {
if (current_user_can($customField['capability'], $post_id)) { if (current_user_can($customField['capability'], $post_id)) {
if (isset($_POST[$this->prefix . $customField['name']]) && trim($_POST[$this->prefix . $customField['name']]) != '') { if (isset($_POST[$this->prefix . $customField['name']]) && trim($_POST[$this->prefix . $customField['name']]) != '') {
update_post_meta($post_id, $this->prefix . $customField['name'], $_POST[$this->prefix . $customField['name']]); update_post_meta($post_id, $this->prefix . $customField['name'], $_POST[$this->prefix . $customField['name']]);
} else { } else {
delete_post_meta($post_id, $this->prefix . $customField['name']); delete_post_meta($post_id, $this->prefix . $customField['name']);
} }
} }
} }
} }
   
public function display_custom_fields() public function display_custom_fields()
{ {
global $post; global $post;
wp_nonce_field('vc-custom-fields', 'vc-custom-fields_wpnonce', false, true); wp_nonce_field('vc-custom-fields', 'vc-custom-fields_wpnonce', false, true);
foreach ($this->customFields as $customField) { foreach ($this->customFields as $customField) {
// Check scope // Check scope
$scope = $customField['scope']; $scope = $customField['scope'];
$output = false; $output = false;
foreach ($scope as $scopeItem) { foreach ($scope as $scopeItem) {
switch ($scopeItem) { switch ($scopeItem) {
default: { default: {
if ($post->post_type == $scopeItem) if ($post->post_type == $scopeItem)
$output = true; $output = true;
break; break;
} }
} }
if ($output) break; if ($output) break;
} }
// Check capability // Check capability
if (!current_user_can($customField['capability'], $post->ID)) if (!current_user_can($customField['capability'], $post->ID))
$output = false; $output = false;
// Output if allowed // Output if allowed
if ($output) { if ($output) {
switch ($customField['type']) { switch ($customField['type']) {
case "checkbox": { case "checkbox": {
// Checkbox // Checkbox
echo '<p><strong>' . $customField['title'] . '</strong></p>'; echo '<p><strong>' . $customField['title'] . '</strong></p>';
echo '<label class="screen-reader-text" for="' . $this->prefix . $customField['name'] . '">' . $customField['title'] . '</label>'; echo '<label class="screen-reader-text" for="' . $this->prefix . $customField['name'] . '">' . $customField['title'] . '</label>';
echo '<p><input type="checkbox" name="' . $this->prefix . $customField['name'] . '" id="' . $this->prefix . $customField['name'] . '" value="yes"'; echo '<p><input type="checkbox" name="' . $this->prefix . $customField['name'] . '" id="' . $this->prefix . $customField['name'] . '" value="yes"';
if (get_post_meta($post->ID, $this->prefix . $customField['name'], true ) == "yes") if (get_post_meta($post->ID, $this->prefix . $customField['name'], true ) == "yes")
echo ' checked="checked"'; echo ' checked="checked"';
echo '" style="width: auto;" /></p>'; echo '" style="width: auto;" /></p>';
break; break;
} }
default: { default: {
// Plain text field // Plain text field
echo '<p><strong>' . $customField['title'] . '</strong></p>'; echo '<p><strong>' . $customField['title'] . '</strong></p>';
$value = get_post_meta($post->ID, $this->prefix . $customField[ 'name' ], true); $value = get_post_meta($post->ID, $this->prefix . $customField[ 'name' ], true);
echo '<p><input type="text" name="' . $this->prefix . $customField['name'] . '" id="' . $this->prefix . $customField['name'] . '" value="' . $value . '" /></p>'; echo '<p><input type="text" name="' . $this->prefix . $customField['name'] . '" id="' . $this->prefix . $customField['name'] . '" value="' . $value . '" /></p>';
break; break;
} }
} }
} else { } else {
echo '<p><strong>' . $customField['title'] . '</strong></p>'; echo '<p><strong>' . $customField['title'] . '</strong></p>';
$value = get_post_meta($post->ID, $this->prefix . $customField[ 'name' ], true); $value = get_post_meta($post->ID, $this->prefix . $customField[ 'name' ], true);
echo '<p><input type="text" name="' . $this->prefix . $customField['name'] . '" id="' . $this->prefix . $customField['name'] . '" value="' . $value . '" disabled /></p>'; echo '<p><input type="text" name="' . $this->prefix . $customField['name'] . '" id="' . $this->prefix . $customField['name'] . '" value="' . $value . '" disabled /></p>';
} }
$default_ttl = get_option($this->prefix . 'ttl'); $default_ttl = get_option($this->prefix . 'ttl');
if ($customField['description']) echo '<p>' . sprintf($customField['description'], $default_ttl) . '</p>'; if ($customField['description']) echo '<p>' . sprintf($customField['description'], $default_ttl) . '</p>';
} }
} }
   
public function check_if_purgeable() public function check_if_purgeable()
{ {
return (!is_multisite() && current_user_can('activate_plugins')) || current_user_can('manage_network') || (is_multisite() && !current_user_can('manage_network') && (SUBDOMAIN_INSTALL || (!SUBDOMAIN_INSTALL && (BLOG_ID_CURRENT_SITE != $this->blogId)))); return (!is_multisite() && current_user_can('activate_plugins')) || current_user_can('manage_network') || (is_multisite() && !current_user_can('manage_network') && (SUBDOMAIN_INSTALL || (!SUBDOMAIN_INSTALL && (BLOG_ID_CURRENT_SITE != $this->blogId))));
} }
   
public function purge_message() public function purge_message()
{ {
echo '<div id="message" class="updated fade"><p><strong>' . __('Varnish Caching', $this->plugin) . '</strong><br /><br />' . $this->noticeMessage . '</p></div>'; echo '<div id="message" class="updated fade"><p><strong>' . __('Varnish Caching', $this->plugin) . '</strong><br /><br />' . $this->noticeMessage . '</p></div>';
} }
   
public function purge_message_no_ips() public function purge_message_no_ips()
{ {
echo '<div id="message" class="error fade"><p><strong>' . __('Please set the IPs for Varnish!', $this->plugin) . '</strong></p></div>'; echo '<div id="message" class="error fade"><p><strong>' . __('Please set the IPs for Varnish!', $this->plugin) . '</strong></p></div>';
} }
   
public function purge_post_page() public function purge_post_page()
{ {
if (isset($_SESSION['vcaching_note'])) { if (isset($_SESSION['vcaching_note'])) {
echo '<div id="message" class="updated fade"><p><strong>' . __('Varnish Caching', $this->plugin) . '</strong><br /><br />' . $_SESSION['vcaching_note'] . '</p></div>'; echo '<div id="message" class="updated fade"><p><strong>' . __('Varnish Caching', $this->plugin) . '</strong><br /><br />' . $_SESSION['vcaching_note'] . '</p></div>';
unset ($_SESSION['vcaching_note']); unset ($_SESSION['vcaching_note']);
} }
} }
   
public function pretty_permalinks_message() public function pretty_permalinks_message()
{ {
echo '<div id="message" class="error"><p>' . __('Varnish Caching requires you to use custom permalinks. Please go to the <a href="options-permalink.php">Permalinks Options Page</a> to configure them.', $this->plugin) . '</p></div>'; echo '<div id="message" class="error"><p>' . __('Varnish Caching requires you to use custom permalinks. Please go to the <a href="options-permalink.php">Permalinks Options Page</a> to configure them.', $this->plugin) . '</p></div>';
} }
   
public function purge_varnish_cache_all_adminbar($admin_bar) public function purge_varnish_cache_all_adminbar($admin_bar)
{ {
$admin_bar->add_menu(array( $admin_bar->add_menu(array(
'id' => 'purge-all-varnish-cache', 'id' => 'purge-all-varnish-cache',
'title' => __('Purge ALL Varnish Cache', $this->plugin), 'title' => __('Purge ALL Varnish Cache', $this->plugin),
'href' => wp_nonce_url(add_query_arg($this->getParam, 1), $this->plugin), 'href' => wp_nonce_url(add_query_arg($this->getParam, 1), $this->plugin),
'meta' => array( 'meta' => array(
'title' => __('Purge ALL Varnish Cache', $this->plugin), 'title' => __('Purge ALL Varnish Cache', $this->plugin),
) )
)); ));
} }
   
public function varnish_glance() public function varnish_glance()
{ {
$url = wp_nonce_url(admin_url('?' . $this->getParam), $this->plugin); $url = wp_nonce_url(admin_url('?' . $this->getParam), $this->plugin);
$button = ''; $button = '';
$nopermission = ''; $nopermission = '';
if ($this->varnishIp == null) { if ($this->varnishIp == null) {
$intro .= sprintf(__('Please setup Varnish IPs to be able to use <a href="%1$s">Varnish Caching</a>.', $this->plugin), 'http://wordpress.org/plugins/varnish-caching/'); $intro .= sprintf(__('Please setup Varnish IPs to be able to use <a href="%1$s">Varnish Caching</a>.', $this->plugin), 'http://wordpress.org/plugins/varnish-caching/');
} else { } else {
$intro .= sprintf(__('<a href="%1$s">Varnish Caching</a> automatically purges your posts when published or updated. Sometimes you need a manual flush.', $this->plugin), 'http://wordpress.org/plugins/varnish-caching/'); $intro .= sprintf(__('<a href="%1$s">Varnish Caching</a> automatically purges your posts when published or updated. Sometimes you need a manual flush.', $this->plugin), 'http://wordpress.org/plugins/varnish-caching/');
$button .= __('Press the button below to force it to purge your entire cache.', $this->plugin); $button .= __('Press the button below to force it to purge your entire cache.', $this->plugin);
$button .= '</p><p><span class="button"><a href="' . $url . '"><strong>'; $button .= '</p><p><span class="button"><a href="' . $url . '"><strong>';
$button .= __('Purge ALL Varnish Cache', $this->plugin); $button .= __('Purge ALL Varnish Cache', $this->plugin);
$button .= '</strong></a></span>'; $button .= '</strong></a></span>';
$nopermission .= __('You do not have permission to purge the cache for the whole site. Please contact your adminstrator.', $this->plugin); $nopermission .= __('You do not have permission to purge the cache for the whole site. Please contact your adminstrator.', $this->plugin);
} }
if ($this->check_if_purgeable()) { if ($this->check_if_purgeable()) {
$text = $intro . ' ' . $button; $text = $intro . ' ' . $button;
} else { } else {
$text = $intro . ' ' . $nopermission; $text = $intro . ' ' . $nopermission;
} }
echo '<p class="varnish-galce">' . $text . '</p>'; echo '<p class="varnish-glance">' . $text . '</p>';
} }
   
protected function get_register_events() protected function get_register_events()
{ {
$actions = array( $actions = array(
'save_post', 'save_post',
'deleted_post', 'deleted_post',
'trashed_post', 'trashed_post',
'edit_post', 'edit_post',
'delete_attachment', 'delete_attachment',
'switch_theme', 'switch_theme',
); );
return apply_filters('vcaching_events', $actions); return apply_filters('vcaching_events', $actions);
} }
   
public function purge_cache() public function purge_cache()
{ {
$purgeUrls = array_unique($this->purgeUrls); $purgeUrls = array_unique($this->purgeUrls);
   
if (empty($purgeUrls)) { if (empty($purgeUrls)) {
if (isset($_GET[$this->getParam]) && $this->check_if_purgeable() && check_admin_referer($this->plugin)) { if (isset($_GET[$this->getParam]) && $this->check_if_purgeable() && check_admin_referer($this->plugin)) {
$this->purge_url(home_url() .'/?vc-regex'); $this->purge_url(home_url() .'/?vc-regex');
} }
} else { } else {
foreach($purgeUrls as $url) { foreach($purgeUrls as $url) {
$this->purge_url($url); $this->purge_url($url);
} }
} }
if ($this->truncateNotice) { if ($this->truncateNotice) {
$this->noticeMessage .= '<br />' . __('Truncate message activated. Showing only first 3 messages.', $this->plugin); $this->noticeMessage .= '<br />' . __('Truncate message activated. Showing only first 3 messages.', $this->plugin);
} }
add_action('admin_notices' , array($this, 'purge_message')); add_action('admin_notices' , array($this, 'purge_message'));
} }
   
protected function purge_url($url) protected function purge_url($url)
{ {
$p = parse_url($url); $p = parse_url($url);
   
if (isset($p['query']) && ($p['query'] == 'vc-regex')) { if (isset($p['query']) && ($p['query'] == 'vc-regex')) {
$pregex = '.*'; $pregex = '.*';
$purgemethod = 'regex'; $purgemethod = 'regex';
} else { } else {
$pregex = ''; $pregex = '';
$purgemethod = 'default'; $purgemethod = 'default';
} }
   
if (isset($p['path'])) { if (isset($p['path'])) {
$path = $p['path']; $path = $p['path'];
} else { } else {
$path = ''; $path = '';
} }
   
$schema = apply_filters('vcaching_schema', 'http://'); $schema = apply_filters('vcaching_schema', 'http://');
   
foreach ($this->ipsToHosts as $key => $ipToHost) { foreach ($this->ipsToHosts as $key => $ipToHost) {
$purgeme = $schema . $ipToHost['ip'] . $path . $pregex; $purgeme = $schema . $ipToHost['ip'] . $path . $pregex;
$headers = array('host' => $ipToHost['host'], 'X-VC-Purge-Method' => $purgemethod, 'X-VC-Purge-Host' => $ipToHost['host']); $headers = array('host' => $ipToHost['host'], 'X-VC-Purge-Method' => $purgemethod, 'X-VC-Purge-Host' => $ipToHost['host']);
if (!is_null($this->purgeKey)) { if (!is_null($this->purgeKey)) {
$headers['X-VC-Purge-Key'] = $this->purgeKey; $headers['X-VC-Purge-Key'] = $this->purgeKey;
} }
$response = wp_remote_request($purgeme, array('method' => 'PURGE', 'headers' => $headers)); $response = wp_remote_request($purgeme, array('method' => 'PURGE', 'headers' => $headers));
if ($response instanceof WP_Error) { if ($response instanceof WP_Error) {
foreach ($response->errors as $error => $errors) { foreach ($response->errors as $error => $errors) {
$this->noticeMessage .= '<br />Error ' . $error . '<br />'; $this->noticeMessage .= '<br />Error ' . $error . '<br />';
foreach ($errors as $error => $description) { foreach ($errors as $error => $description) {
$this->noticeMessage .= ' - ' . $description . '<br />'; $this->noticeMessage .= ' - ' . $description . '<br />';
} }
} }
} else { } else {
if ($this->truncateNotice && $this->truncateCount <= 2 || $this->truncateNotice == false) { if ($this->truncateNotice && $this->truncateCount <= 2 || $this->truncateNotice == false) {
$this->noticeMessage .= '' . __('Trying to purge URL :', $this->plugin) . $purgeme; $this->noticeMessage .= '' . __('Trying to purge URL :', $this->plugin) . $purgeme;
preg_match("/<title>(.*)<\/title>/i", $response['body'], $matches); preg_match("/<title>(.*)<\/title>/i", $response['body'], $matches);
$this->noticeMessage .= ' => <br /> ' . isset($matches[1]) ? " => " . $matches[1] : $response['body']; $this->noticeMessage .= ' => <br /> ' . isset($matches[1]) ? " => " . $matches[1] : $response['body'];
$this->noticeMessage .= '<br />'; $this->noticeMessage .= '<br />';
if ($this->debug) { if ($this->debug) {
$this->noticeMessage .= $response['body'] . "<br />"; $this->noticeMessage .= $response['body'] . "<br />";
} }
} }
$this->truncateCount++; $this->truncateCount++;
} }
} }
   
do_action('vcaching_after_purge_url', $url, $purgeme); do_action('vcaching_after_purge_url', $url, $purgeme);
} }
   
public function purge_post($postId) public function purge_post($postId)
{ {
// If this is a valid post we want to purge the post, the home page and any associated tags & cats // If this is a valid post we want to purge the post, the home page and any associated tags & cats
// If not, purge everything on the site. // If not, purge everything on the site.
   
$validPostStatus = array('publish', 'trash'); $validPostStatus = array('publish', 'trash');
$thisPostStatus = get_post_status($postId); $thisPostStatus = get_post_status($postId);
   
// If this is a revision, stop. // If this is a revision, stop.
if(get_permalink($postId) !== true && !in_array($thisPostStatus, $validPostStatus)) { if(get_permalink($postId) !== true && !in_array($thisPostStatus, $validPostStatus)) {
return; return;
} else { } else {
// array to collect all our URLs // array to collect all our URLs
$listofurls = array(); $listofurls = array();
   
// Category purge based on Donnacha's work in WP Super Cache // Category purge based on Donnacha's work in WP Super Cache
$categories = get_the_category($postId); $categories = get_the_category($postId);
if ($categories) { if ($categories) {
foreach ($categories as $cat) { foreach ($categories as $cat) {
array_push($listofurls, get_category_link($cat->term_id)); array_push($listofurls, get_category_link($cat->term_id));
} }
} }
// Tag purge based on Donnacha's work in WP Super Cache // Tag purge based on Donnacha's work in WP Super Cache
$tags = get_the_tags($postId); $tags = get_the_tags($postId);
if ($tags) { if ($tags) {
foreach ($tags as $tag) { foreach ($tags as $tag) {
array_push($listofurls, get_tag_link($tag->term_id)); array_push($listofurls, get_tag_link($tag->term_id));
} }
} }
   
// Author URL // Author URL
array_push($listofurls, array_push($listofurls,
get_author_posts_url(get_post_field('post_author', $postId)), get_author_posts_url(get_post_field('post_author', $postId)),
get_author_feed_link(get_post_field('post_author', $postId)) get_author_feed_link(get_post_field('post_author', $postId))
); );
   
// Archives and their feeds // Archives and their feeds
$archiveurls = array(); $archiveurls = array();
if (get_post_type_archive_link(get_post_type($postId)) == true) { if (get_post_type_archive_link(get_post_type($postId)) == true) {
array_push($listofurls, array_push($listofurls,
get_post_type_archive_link( get_post_type($postId)), get_post_type_archive_link( get_post_type($postId)),
get_post_type_archive_feed_link( get_post_type($postId)) get_post_type_archive_feed_link( get_post_type($postId))
); );
} }
   
// Post URL // Post URL
array_push($listofurls, get_permalink($postId)); array_push($listofurls, get_permalink($postId));
   
// Feeds // Feeds
array_push($listofurls, array_push($listofurls,
get_bloginfo_rss('rdf_url') , get_bloginfo_rss('rdf_url') ,
get_bloginfo_rss('rss_url') , get_bloginfo_rss('rss_url') ,
get_bloginfo_rss('rss2_url'), get_bloginfo_rss('rss2_url'),
get_bloginfo_rss('atom_url'), get_bloginfo_rss('atom_url'),
get_bloginfo_rss('comments_rss2_url'), get_bloginfo_rss('comments_rss2_url'),
get_post_comments_feed_link($postId) get_post_comments_feed_link($postId)
); );
   
// Home Page and (if used) posts page // Home Page and (if used) posts page
array_push($listofurls, home_url('/')); array_push($listofurls, home_url('/'));
if (get_option('show_on_front') == 'page') { if (get_option('show_on_front') == 'page') {
array_push($listofurls, get_permalink(get_option('page_for_posts'))); array_push($listofurls, get_permalink(get_option('page_for_posts')));
} }
   
// If Automattic's AMP is installed, add AMP permalink // If Automattic's AMP is installed, add AMP permalink
if (function_exists('amp_get_permalink')) { if (function_exists('amp_get_permalink')) {
array_push($listofurls, amp_get_permalink($postId)); array_push($listofurls, amp_get_permalink($postId));
} }
   
// Now flush all the URLs we've collected // Now flush all the URLs we've collected
foreach ($listofurls as $url) { foreach ($listofurls as $url) {
array_push($this->purgeUrls, $url) ; array_push($this->purgeUrls, $url) ;
} }
} }
// Filter to add or remove urls to the array of purged urls // Filter to add or remove urls to the array of purged urls
// @param array $purgeUrls the urls (paths) to be purged // @param array $purgeUrls the urls (paths) to be purged
// @param int $postId the id of the new/edited post // @param int $postId the id of the new/edited post
$this->purgeUrls = apply_filters('vcaching_purge_urls', $this->purgeUrls, $postId); $this->purgeUrls = apply_filters('vcaching_purge_urls', $this->purgeUrls, $postId);
$this->purge_cache(); $this->purge_cache();
} }
   
public function send_headers() public function send_headers()
{ {
$enable = get_option($this->prefix . 'enable'); $enable = get_option($this->prefix . 'enable');
if ($enable) { if ($enable) {
Header('X-VC-Enabled: true', true); Header('X-VC-Enabled: true', true);
if (is_user_logged_in()) { if (is_user_logged_in()) {
Header('X-VC-Cacheable: NO:User is logged in', true); Header('X-VC-Cacheable: NO:User is logged in', true);
$ttl = 0; $ttl = 0;
} else { } else {
$ttl = get_option($this->prefix . 'ttl'); $ttl = get_option($this->prefix . 'ttl');
} }
Header('X-VC-TTL: ' . $ttl, true); Header('X-VC-TTL: ' . $ttl, true);
if ($this->debug) { if ($this->debug) {
Header('X-VC-Debug: true', true); Header('X-VC-Debug: true', true);
} }
} else { } else {
Header('X-VC-Enabled: false', true); Header('X-VC-Enabled: false', true);
} }
} }
   
public function wp_login() public function wp_login()
{ {
$cookie = get_option($this->prefix . 'cookie'); $cookie = get_option($this->prefix . 'cookie');
setcookie($cookie, 1, time()+3600*24*100, COOKIEPATH, COOKIE_DOMAIN, false, true); setcookie($cookie, 1, time()+3600*24*100, COOKIEPATH, COOKIE_DOMAIN, false, true);
} }
   
public function wp_logout() public function wp_logout()
{ {
$cookie = get_option($this->prefix . 'cookie'); $cookie = get_option($this->prefix . 'cookie');
setcookie($cookie, null, time()-3600*24*100, COOKIEPATH, COOKIE_DOMAIN, false, true); setcookie($cookie, null, time()-3600*24*100, COOKIEPATH, COOKIE_DOMAIN, false, true);
} }
   
public function admin_menu() public function admin_menu()
{ {
add_action('admin_menu', array($this, 'add_menu_item')); add_action('admin_menu', array($this, 'add_menu_item'));
add_action('admin_init', array($this, 'options_page_fields')); add_action('admin_init', array($this, 'options_page_fields'));
add_action('admin_init', array($this, 'console_page_fields')); add_action('admin_init', array($this, 'console_page_fields'));
add_action('admin_init', array($this, 'conf_page_fields')); if ($this->vclGeneratorTab) {
  add_action('admin_init', array($this, 'conf_page_fields'));
  }
} }
   
public function add_menu_item() public function add_menu_item()
{ {
if ($this->check_if_purgeable()) { if ($this->check_if_purgeable()) {
add_menu_page(__('Varnish Caching', $this->plugin), __('Varnish Caching', $this->plugin), 'manage_options', $this->plugin . '-plugin', array($this, 'settings_page'), plugins_url() . '/' . $this->plugin . '/icon.png', 99); add_menu_page(__('Varnish Caching', $this->plugin), __('Varnish Caching', $this->plugin), 'manage_options', $this->plugin . '-plugin', array($this, 'settings_page'), plugins_url() . '/' . $this->plugin . '/icon.png', 99);
} }
} }
   
public function settings_page() public function settings_page()
{ {
?> ?>
<div class="wrap"> <div class="wrap">
<h1><?=__('Varnish Caching', $this->plugin)?></h1> <h1><?=__('Varnish Caching', $this->plugin)?></h1>
   
<h2 class="nav-tab-wrapper"> <h2 class="nav-tab-wrapper">
<a class="nav-tab <?php if(!isset($_GET['tab']) || $_GET['tab'] == 'options'): ?>nav-tab-active<?php endif; ?>" href="<?php echo admin_url() ?>index.php?page=<?=$this->plugin?>-plugin&amp;tab=options"><?=__('Settings', $this->plugin)?></a> <a class="nav-tab <?php if(!isset($_GET['tab']) || $_GET['tab'] == 'options'): ?>nav-tab-active<?php endif; ?>" href="<?php echo admin_url() ?>index.php?page=<?=$this->plugin?>-plugin&amp;tab=options"><?=__('Settings', $this->plugin)?></a>
<?php if ($this->check_if_purgeable()): ?> <?php if ($this->check_if_purgeable()): ?>
<a class="nav-tab <?php if($_GET['tab'] == 'console'): ?>nav-tab-active<?php endif; ?>" href="<?php echo admin_url() ?>index.php?page=<?=$this->plugin?>-plugin&amp;tab=console"><?=__('Console', $this->plugin)?></a> <a class="nav-tab <?php if($_GET['tab'] == 'console'): ?>nav-tab-active<?php endif; ?>" href="<?php echo admin_url() ?>index.php?page=<?=$this->plugin?>-plugin&amp;tab=console"><?=__('Console', $this->plugin)?></a>
<?php endif; ?> <?php endif; ?>
<a class="nav-tab <?php if($_GET['tab'] == 'stats'): ?>nav-tab-active<?php endif; ?>" href="<?php echo admin_url() ?>index.php?page=<?=$this->plugin?>-plugin&amp;tab=stats"><?=__('Statistics', $this->plugin)?></a> <a class="nav-tab <?php if($_GET['tab'] == 'stats'): ?>nav-tab-active<?php endif; ?>" href="<?php echo admin_url() ?>index.php?page=<?=$this->plugin?>-plugin&amp;tab=stats"><?=__('Statistics', $this->plugin)?></a>
<a class="nav-tab <?php if($_GET['tab'] == 'conf'): ?>nav-tab-active<?php endif; ?>" href="<?php echo admin_url() ?>index.php?page=<?=$this->plugin?>-plugin&amp;tab=conf"><?=__('VCLs Generator', $this->plugin)?></a> <?php if ($this->vclGeneratorTab): ?>
  <a class="nav-tab <?php if($_GET['tab'] == 'conf'): ?>nav-tab-active<?php endif; ?>" href="<?php echo admin_url() ?>index.php?page=<?=$this->plugin?>-plugin&amp;tab=conf"><?=__('VCLs Generator', $this->plugin)?></a>
  <?php endif; ?>
</h2> </h2>
   
<?php if(!isset($_GET['tab']) || $_GET['tab'] == 'options'): ?> <?php if(!isset($_GET['tab']) || $_GET['tab'] == 'options'): ?>
<form method="post" action="options.php"> <form method="post" action="options.php">
<?php <?php
settings_fields($this->prefix . 'options'); settings_fields($this->prefix . 'options');
do_settings_sections($this->prefix . 'options'); do_settings_sections($this->prefix . 'options');
submit_button(); submit_button();
?> ?>
</form> </form>
<script type="text/javascript"> <script type="text/javascript">
function generateHash(length, bits, id) { function generateHash(length, bits, id) {
bits = bits || 36; bits = bits || 36;
var outStr = "", newStr; var outStr = "", newStr;
while (outStr.length < length) while (outStr.length < length)
{ {
newStr = Math.random().toString(bits).slice(2); newStr = Math.random().toString(bits).slice(2);
outStr += newStr.slice(0, Math.min(newStr.length, (length - outStr.length))); outStr += newStr.slice(0, Math.min(newStr.length, (length - outStr.length)));
} }
jQuery('#' + id).val(outStr); jQuery('#' + id).val(outStr);
} }
</script> </script>
<?php elseif($_GET['tab'] == 'console'): ?> <?php elseif($_GET['tab'] == 'console'): ?>
<form method="post" action="index.php?page=<?=$this->plugin?>-plugin&amp;tab=console"> <form method="post" action="index.php?page=<?=$this->plugin?>-plugin&amp;tab=console">
<?php <?php
settings_fields($this->prefix . 'console'); settings_fields($this->prefix . 'console');
do_settings_sections($this->prefix . 'console'); do_settings_sections($this->prefix . 'console');
submit_button(__('Purge', $this->plugin)); submit_button(__('Purge', $this->plugin));
?> ?>
</form> </form>
<?php elseif($_GET['tab'] == 'stats'): ?> <?php elseif($_GET['tab'] == 'stats'): ?>
<h2><?= __('Statistics', $this->plugin) ?></h2> <h2><?= __('Statistics', $this->plugin) ?></h2>
   
<div class="wrap"> <div class="wrap">
<?php if ($_GET['info'] == 1 || trim($this->statsJsons) == ""): ?> <?php if ($_GET['info'] == 1 || trim($this->statsJsons) == ""): ?>
<div class="fade"> <div class="fade">
<h4><?=__('Setup information', $this->plugin)?></h4> <h4><?=__('Setup information', $this->plugin)?></h4>
<?= __('<strong>Short story</strong><br />You must generate by cronjob the JSON stats file. The generated files must be copied on the backend servers in the wordpress root folder.', $this->plugin) ?> <?= __('<strong>Short story</strong><br />You must generate by cronjob the JSON stats file. The generated files must be copied on the backend servers in the wordpress root folder.', $this->plugin) ?>
<br /><br /> <br /><br />
<?=sprintf(__('<strong>Long story</strong><br />On every Varnish Cache server setup a cronjob that generates the JSON stats file :<br /> %1$s /path/to/be/set/filename.json # every 3 minutes.', $this->plugin), '*/3 * * * * root /usr/bin/varnishstat -1j >')?> <?=sprintf(__('<strong>Long story</strong><br />On every Varnish Cache server setup a cronjob that generates the JSON stats file :<br /> %1$s /path/to/be/set/filename.json # every 3 minutes.', $this->plugin), '*/3 * * * * root /usr/bin/varnishstat -1j >')?>
<br /> <br />
<?= __('The generated files must be copied on the backend servers in the wordpress root folder.', $this->plugin) ?> <?= __('The generated files must be copied on the backend servers in the wordpress root folder.', $this->plugin) ?>
<br /> <br />
<?=__("Use a different filename for each Varnish Cache server. After this is done, fill in the relative path to the files in Statistics JSONs on the Settings tab.", $this->plugin)?> <?=__("Use a different filename for each Varnish Cache server. After this is done, fill in the relative path to the files in Statistics JSONs on the Settings tab.", $this->plugin)?>
<br /><br /> <br /><br />
<?= __('Example 1 <br />If you have a single server, both Varnish Cache and the backend on it, use the folowing cronjob:', $this->plugin) ?> <?= __('Example 1 <br />If you have a single server, both Varnish Cache and the backend on it, use the folowing cronjob:', $this->plugin) ?>
<br /> <br />
<?=sprintf(__('%1$s /path/to/the/wordpress/root/varnishstat.json # every 3 minutes.', $this->plugin), '*/3 * * * * root /usr/bin/varnishstat -1j >')?> <?=sprintf(__('%1$s /path/to/the/wordpress/root/varnishstat.json # every 3 minutes.', $this->plugin), '*/3 * * * * root /usr/bin/varnishstat -1j >')?>
<br /> <br />
<?=__("Then fill in the relative path to the files in Statistics JSONs on the Settings tab :", $this->plugin)?> <?=__("Then fill in the relative path to the files in Statistics JSONs on the Settings tab :", $this->plugin)?>
<br /> <br />
<input type="text" size="100" value="<?=__("/varnishstat.json", $this->plugin)?>" /> <input type="text" size="100" value="<?=__("/varnishstat.json", $this->plugin)?>" />
   
<br /><br /> <br /><br />
<?=__("Example 2 <br />You have 2 Varnish Cache Servers, and 3 backend servers. Setup the cronjob :", $this->plugin)?> <?=__("Example 2 <br />You have 2 Varnish Cache Servers, and 3 backend servers. Setup the cronjob :", $this->plugin)?>
<br /> <br />
<?=sprintf(__('VC Server 1 : %1$s # every 3 minutes.', $this->plugin), '*/3 * * * * root /usr/bin/varnishstat -1j > /root/varnishstat/server1_3389398cd359cfa443f85ca040da069a.json')?> <?=sprintf(__('VC Server 1 : %1$s # every 3 minutes.', $this->plugin), '*/3 * * * * root /usr/bin/varnishstat -1j > /root/varnishstat/server1_3389398cd359cfa443f85ca040da069a.json')?>
<br /> <br />
<?=sprintf(__('VC Server 2 : %1$s # every 3 minutes.', $this->plugin), '*/3 * * * * root /usr/bin/varnishstat -1j > /root/varnishstat/server2_3389398cd359cfa443f85ca040da069a.json')?> <?=sprintf(__('VC Server 2 : %1$s # every 3 minutes.', $this->plugin), '*/3 * * * * root /usr/bin/varnishstat -1j > /root/varnishstat/server2_3389398cd359cfa443f85ca040da069a.json')?>
<br /> <br />
<?=__("Copy the files on the backend servers in /path/to/wordpress/root/varnishstat/ folder. Then fill in the relative path to the files in Statistics JSONs on the Settings tab :", $this->plugin)?> <?=__("Copy the files on the backend servers in /path/to/wordpress/root/varnishstat/ folder. Then fill in the relative path to the files in Statistics JSONs on the Settings tab :", $this->plugin)?>
<br /> <br />
   
<input type="text" size="100" value="<?=__("/varnishstat/server1_3389398cd359cfa443f85ca040da069a.json,/varnishstat/server2_3389398cd359cfa443f85ca040da069a.json", $this->plugin)?>" /> <input type="text" size="100" value="<?=__("/varnishstat/server1_3389398cd359cfa443f85ca040da069a.json,/varnishstat/server2_3389398cd359cfa443f85ca040da069a.json", $this->plugin)?>" />
</div> </div>
<?php endif; ?> <?php endif; ?>
   
<?php if(trim($this->statsJsons)): ?> <?php if(trim($this->statsJsons)): ?>
<h2 class="nav-tab-wrapper"> <h2 class="nav-tab-wrapper">
<?php foreach ($this->ipsToHosts as $server => $ipToHost): ?> <?php foreach ($this->ipsToHosts as $server => $ipToHost): ?>
<a class="server nav-tab <?php if($server == 0) echo "nav-tab-active"; ?>" href="#" server="<?=$server?>"><?= sprintf(__('Server %1$s', $this->plugin), $ipToHost['ip'])?></a> <a class="server nav-tab <?php if($server == 0) echo "nav-tab-active"; ?>" href="#" server="<?=$server?>"><?= sprintf(__('Server %1$s', $this->plugin), $ipToHost['ip'])?></a>
<?php endforeach; ?> <?php endforeach; ?>
</h2> </h2>
   
<?php foreach ($this->ipsToHosts as $server => $ipToHost): ?> <?php foreach ($this->ipsToHosts as $server => $ipToHost): ?>
<div id="server_<?=$server?>" class="servers" style="display:<?php if($server == 0) {echo 'block';} else {echo 'none';} ?>"> <div id="server_<?=$server?>" class="servers" style="display:<?php if($server == 0) {echo 'block';} else {echo 'none';} ?>">
<?= sprintf(__('Fetching stats for server %1$s', $this->plugin), $ipToHost['ip']) ?> <?= sprintf(__('Fetching stats for server %1$s', $this->plugin), $ipToHost['ip']) ?>
</div> </div>
<script type="text/javascript"> <script type="text/javascript">
jQuery.getJSON("<?=$ipToHost['statsJson']?>", function(data) { jQuery.getJSON("<?=$ipToHost['statsJson']?>", function(data) {
var server = '#server_<?=$server?>'; var server = '#server_<?=$server?>';
jQuery(server).html(''); jQuery(server).html('');
jQuery(server).append('<p><?= __('Stats generated on', $this->plugin) ?> ' + data.timestamp + '</p>'); jQuery(server).append('<p><?= __('Stats generated on', $this->plugin) ?> ' + data.timestamp + '</p>');
jQuery(server).append('<table class="wp-list-table widefat fixed striped server_<?=$server?>"><thead><tr><td class="manage-column"><strong><?= __('Description', $this->plugin) ?></strong></td><td class="manage-column"><strong><?= __('Value', $this->plugin) ?></strong></td><td class="manage-column"><strong><?= __('Key', $this->plugin) ?></strong></td></tr></thead><tbody id="varnishstats_<?=$server?>"></tbody></table>'); jQuery(server).append('<table class="wp-list-table widefat fixed striped server_<?=$server?>"><thead><tr><td class="manage-column"><strong><?= __('Description', $this->plugin) ?></strong></td><td class="manage-column"><strong><?= __('Value', $this->plugin) ?></strong></td><td class="manage-column"><strong><?= __('Key', $this->plugin) ?></strong></td></tr></thead><tbody id="varnishstats_<?=$server?>"></tbody></table>');
delete data.timestamp; delete data.timestamp;
jQuery.each(data, function(key, val) { jQuery.each(data, function(key, val) {
jQuery('#varnishstats_<?=$server?>').append('<tr><td>'+val.description+'</td><td>'+val.value+'</td><td>'+key+'</td></tr>'); jQuery('#varnishstats_<?=$server?>').append('<tr><td>'+val.description+'</td><td>'+val.value+'</td><td>'+key+'</td></tr>');
}); });
}); });
</script> </script>
<?php endforeach; ?> <?php endforeach; ?>
<script type="text/javascript"> <script type="text/javascript">
jQuery('.nav-tab-wrapper > a.server').click(function(e){ jQuery('.nav-tab-wrapper > a.server').click(function(e){
e.preventDefault(); e.preventDefault();
jQuery('.nav-tab-wrapper > a.server').removeClass('nav-tab-active'); jQuery('.nav-tab-wrapper > a.server').removeClass('nav-tab-active');
jQuery(this).addClass('nav-tab-active'); jQuery(this).addClass('nav-tab-active');
jQuery(".servers").hide(); jQuery(".servers").hide();
jQuery("#server_" + jQuery(this).attr('server')).show(); jQuery("#server_" + jQuery(this).attr('server')).show();
}); });
</script> </script>
<?php endif; ?> <?php endif; ?>
</div> </div>
<?php elseif($_GET['tab'] == 'conf'): ?> <?php elseif($_GET['tab'] == 'conf'): ?>
<form method="post" action="options.php"> <form method="post" action="options.php">
<?php <?php
settings_fields($this->prefix . 'conf'); settings_fields($this->prefix . 'conf');
do_settings_sections($this->prefix . 'conf'); do_settings_sections($this->prefix . 'conf');
submit_button(); submit_button();
?> ?>
</form> </form>
<?php if (class_exists('ZipArchive')): ?> <?php if (class_exists('ZipArchive')): ?>
<form method="post" action="index.php?page=<?=$this->plugin?>-plugin&amp;tab=conf"> <form method="post" action="index.php?page=<?=$this->plugin?>-plugin&amp;tab=conf">
<?php <?php
settings_fields($this->prefix . 'download'); settings_fields($this->prefix . 'download');
do_settings_sections($this->prefix . 'download'); do_settings_sections($this->prefix . 'download');
submit_button(__('Download')); submit_button(__('Download'));
?> ?>
</form> </form>
<?php else: ?> <?php else: ?>
<?php <?php
do_settings_sections($this->prefix . 'download_error'); do_settings_sections($this->prefix . 'download_error');
echo sprintf(__('You server\'s PHP configuration is missing the ZIP extension (ZipArchive class is used by VCaching). Please enable the ZIP extension. For more information click <a href="%1$s" target="_blank">here</a>.', $this->plugin), 'http://www.php.net/manual/en/book.zip.php'); echo sprintf(__('You server\'s PHP configuration is missing the ZIP extension (ZipArchive class is used by VCaching). Please enable the ZIP extension. For more information click <a href="%1$s" target="_blank">here</a>.', $this->plugin), 'http://www.php.net/manual/en/book.zip.php');
echo "<br />"; echo "<br />";
echo __('If you cannot enable the ZIP extension, please use the provided Varnish Cache configuration files located in /wp-content/plugins/vcaching/varnish-conf folder', $this->plugin); echo __('If you cannot enable the ZIP extension, please use the provided Varnish Cache configuration files located in /wp-content/plugins/vcaching/varnish-conf folder', $this->plugin);
?> ?>
<?php endif; ?> <?php endif; ?>
<?php endif; ?> <?php endif; ?>
</div> </div>
<?php <?php
} }
   
public function options_page_fields() public function options_page_fields()
{ {
add_settings_section($this->prefix . 'options', __('Settings', $this->plugin), null, $this->prefix . 'options'); add_settings_section($this->prefix . 'options', __('Settings', $this->plugin), null, $this->prefix . 'options');
   
add_settings_field($this->prefix . "enable", __("Enable" , $this->plugin), array($this, $this->prefix . "enable"), $this->prefix . 'options', $this->prefix . 'options'); add_settings_field($this->prefix . "enable", __("Enable" , $this->plugin), array($this, $this->prefix . "enable"), $this->prefix . 'options', $this->prefix . 'options');
add_settings_field($this->prefix . "homepage_ttl", __("Homepage cache TTL", $this->plugin), array($this, $this->prefix . "homepage_ttl"), $this->prefix . 'options', $this->prefix . 'options'); add_settings_field($this->prefix . "homepage_ttl", __("Homepage cache TTL", $this->plugin), array($this, $this->prefix . "homepage_ttl"), $this->prefix . 'options', $this->prefix . 'options');
add_settings_field($this->prefix . "ttl", __("Cache TTL", $this->plugin), array($this, $this->prefix . "ttl"), $this->prefix . 'options', $this->prefix . 'options'); add_settings_field($this->prefix . "ttl", __("Cache TTL", $this->plugin), array($this, $this->prefix . "ttl"), $this->prefix . 'options', $this->prefix . 'options');
add_settings_field($this->prefix . "ips", __("IPs", $this->plugin), array($this, $this->prefix . "ips"), $this->prefix . 'options', $this->prefix . 'options'); add_settings_field($this->prefix . "ips", __("IPs", $this->plugin), array($this, $this->prefix . "ips"), $this->prefix . 'options', $this->prefix . 'options');
add_settings_field($this->prefix . "dynamic_host", __("Dynamic host", $this->plugin), array($this, $this->prefix . "dynamic_host"), $this->prefix . 'options', $this->prefix . 'options'); add_settings_field($this->prefix . "dynamic_host", __("Dynamic host", $this->plugin), array($this, $this->prefix . "dynamic_host"), $this->prefix . 'options', $this->prefix . 'options');
if (!get_option($this->prefix . 'dynamic_host')) { if (!get_option($this->prefix . 'dynamic_host')) {
add_settings_field($this->prefix . "hosts", __("Hosts", $this->plugin), array($this, $this->prefix . "hosts"), $this->prefix . 'options', $this->prefix . 'options'); add_settings_field($this->prefix . "hosts", __("Hosts", $this->plugin), array($this, $this->prefix . "hosts"), $this->prefix . 'options', $this->prefix . 'options');
} }
add_settings_field($this->prefix . "override", __("Override default TTL", $this->plugin), array($this, $this->prefix . "override"), $this->prefix . 'options', $this->prefix . 'options'); add_settings_field($this->prefix . "override", __("Override default TTL", $this->plugin), array($this, $this->prefix . "override"), $this->prefix . 'options', $this->prefix . 'options');
add_settings_field($this->prefix . "purge_key", __("Purge key", $this->plugin), array($this, $this->prefix . "purge_key"), $this->prefix . 'options', $this->prefix . 'options'); add_settings_field($this->prefix . "purge_key", __("Purge key", $this->plugin), array($this, $this->prefix . "purge_key"), $this->prefix . 'options', $this->prefix . 'options');
add_settings_field($this->prefix . "cookie", __("Logged in cookie", $this->plugin), array($this, $this->prefix . "cookie"), $this->prefix . 'options', $this->prefix . 'options'); add_settings_field($this->prefix . "cookie", __("Logged in cookie", $this->plugin), array($this, $this->prefix . "cookie"), $this->prefix . 'options', $this->prefix . 'options');
add_settings_field($this->prefix . "stats_json_file", __("Statistics JSONs", $this->plugin), array($this, $this->prefix . "stats_json_file"), $this->prefix . 'options', $this->prefix . 'options'); add_settings_field($this->prefix . "stats_json_file", __("Statistics JSONs", $this->plugin), array($this, $this->prefix . "stats_json_file"), $this->prefix . 'options', $this->prefix . 'options');
add_settings_field($this->prefix . "truncate_notice", __("Truncate notice message", $this->plugin), array($this, $this->prefix . "truncate_notice"), $this->prefix . 'options', $this->prefix . 'options'); add_settings_field($this->prefix . "truncate_notice", __("Truncate notice message", $this->plugin), array($this, $this->prefix . "truncate_notice"), $this->prefix . 'options', $this->prefix . 'options');
add_settings_field($this->prefix . "debug", __("Enable debug", $this->plugin), array($this, $this->prefix . "debug"), $this->prefix . 'options', $this->prefix . 'options'); add_settings_field($this->prefix . "debug", __("Enable debug", $this->plugin), array($this, $this->prefix . "debug"), $this->prefix . 'options', $this->prefix . 'options');
   
if(isset($_POST['option_page']) && $_POST['option_page'] == $this->prefix . 'options') { if(isset($_POST['option_page']) && $_POST['option_page'] == $this->prefix . 'options') {
register_setting($this->prefix . 'options', $this->prefix . "enable"); register_setting($this->prefix . 'options', $this->prefix . "enable");
register_setting($this->prefix . 'options', $this->prefix . "ttl"); register_setting($this->prefix . 'options', $this->prefix . "ttl");
register_setting($this->prefix . 'options', $this->prefix . "homepage_ttl"); register_setting($this->prefix . 'options', $this->prefix . "homepage_ttl");
register_setting($this->prefix . 'options', $this->prefix . "ips"); register_setting($this->prefix . 'options', $this->prefix . "ips");
register_setting($this->prefix . 'options', $this->prefix . "dynamic_host"); register_setting($this->prefix . 'options', $this->prefix . "dynamic_host");
register_setting($this->prefix . 'options', $this->prefix . "hosts"); register_setting($this->prefix . 'options', $this->prefix . "hosts");
register_setting($this->prefix . 'options', $this->prefix . "override"); register_setting($this->prefix . 'options', $this->prefix . "override");
register_setting($this->prefix . 'options', $this->prefix . "purge_key"); register_setting($this->prefix . 'options', $this->prefix . "purge_key");
register_setting($this->prefix . 'options', $this->prefix . "cookie"); register_setting($this->prefix . 'options', $this->prefix . "cookie");
register_setting($this->prefix . 'options', $this->prefix . "stats_json_file"); register_setting($this->prefix . 'options', $this->prefix . "stats_json_file");
register_setting($this->prefix . 'options', $this->prefix . "truncate_notice"); register_setting($this->prefix . 'options', $this->prefix . "truncate_notice");
register_setting($this->prefix . 'options', $this->prefix . "debug"); register_setting($this->prefix . 'options', $this->prefix . "debug");
} }
} }
   
public function varnish_caching_enable() public function varnish_caching_enable()
{ {
?> ?>
<input type="checkbox" name="varnish_caching_enable" value="1" <?php checked(1, get_option($this->prefix . 'enable'), true); ?> /> <input type="checkbox" name="varnish_caching_enable" value="1" <?php checked(1, get_option($this->prefix . 'enable'), true); ?> />
<p class="description"><?=__('Enable Varnish Caching', $this->plugin)?></p> <p class="description"><?=__('Enable Varnish Caching', $this->plugin)?></p>
<?php <?php
} }
   
public function varnish_caching_homepage_ttl() public function varnish_caching_homepage_ttl()
{ {
?> ?>
<input type="text" name="varnish_caching_homepage_ttl" id="varnish_caching_homepage_ttl" value="<?php echo get_option($this->prefix . 'homepage_ttl'); ?>" /> <input type="text" name="varnish_caching_homepage_ttl" id="varnish_caching_homepage_ttl" value="<?php echo get_option($this->prefix . 'homepage_ttl'); ?>" />
<p class="description"><?=__('Time to live in seconds in Varnish cache for homepage', $this->plugin)?></p> <p class="description"><?=__('Time to live in seconds in Varnish cache for homepage', $this->plugin)?></p>
<?php <?php
} }
   
public function varnish_caching_ttl() public function varnish_caching_ttl()
{ {
?> ?>
<input type="text" name="varnish_caching_ttl" id="varnish_caching_ttl" value="<?php echo get_option($this->prefix . 'ttl'); ?>" /> <input type="text" name="varnish_caching_ttl" id="varnish_caching_ttl" value="<?php echo get_option($this->prefix . 'ttl'); ?>" />
<p class="description"><?=__('Time to live in seconds in Varnish cache', $this->plugin)?></p> <p class="description"><?=__('Time to live in seconds in Varnish cache', $this->plugin)?></p>
<?php <?php
} }
   
public function varnish_caching_ips() public function varnish_caching_ips()
{ {
?> ?>
<input type="text" name="varnish_caching_ips" id="varnish_caching_ips" size="100" value="<?php echo get_option($this->prefix . 'ips'); ?>" /> <input type="text" name="varnish_caching_ips" id="varnish_caching_ips" size="100" value="<?php echo get_option($this->prefix . 'ips'); ?>" />
<p class="description"><?=__('Comma separated ip/ip:port. Example : 192.168.0.2,192.168.0.3:8080', $this->plugin)?></p> <p class="description"><?=__('Comma separated ip/ip:port. Example : 192.168.0.2,192.168.0.3:8080', $this->plugin)?></p>
<?php <?php
} }
   
public function varnish_caching_dynamic_host() public function varnish_caching_dynamic_host()
{ {
?> ?>
<input type="checkbox" name="varnish_caching_dynamic_host" value="1" <?php checked(1, get_option($this->prefix . 'dynamic_host'), true); ?> /> <input type="checkbox" name="varnish_caching_dynamic_host" value="1" <?php checked(1, get_option($this->prefix . 'dynamic_host'), true); ?> />
<p class="description"> <p class="description">
<?=__('Uses the $_SERVER[\'HTTP_HOST\'] as hash for Varnish. This means the purge cache action will work on the domain you\'re on.<br />Use this option if you use only one domain.', $this->plugin)?> <?=__('Uses the $_SERVER[\'HTTP_HOST\'] as hash for Varnish. This means the purge cache action will work on the domain you\'re on.<br />Use this option if you use only one domain.', $this->plugin)?>
</p> </p>
<?php <?php
} }
   
public function varnish_caching_hosts() public function varnish_caching_hosts()
{ {
?> ?>
<input type="text" name="varnish_caching_hosts" id="varnish_caching_hosts" size="100" value="<?php echo get_option($this->prefix . 'hosts'); ?>" /> <input type="text" name="varnish_caching_hosts" id="varnish_caching_hosts" size="100" value="<?php echo get_option($this->prefix . 'hosts'); ?>" />
<p class="description"> <p class="description">
<?=__('Comma separated hostnames. Varnish uses the hostname to create the cache hash. For each IP, you must set a hostname.<br />Use this option if you use multiple domains.', $this->plugin)?> <?=__('Comma separated hostnames. Varnish uses the hostname to create the cache hash. For each IP, you must set a hostname.<br />Use this option if you use multiple domains.', $this->plugin)?>
</p> </p>
<?php <?php
} }
   
public function varnish_caching_override() public function varnish_caching_override()
{ {
?> ?>
<input type="checkbox" name="varnish_caching_override" value="1" <?php checked(1, get_option($this->prefix . 'override'), true); ?> /> <input type="checkbox" name="varnish_caching_override" value="1" <?php checked(1, get_option($this->prefix . 'override'), true); ?> />
<p class="description"><?=__('Override default TTL on each post/page.', $this->plugin)?></p> <p class="description"><?=__('Override default TTL on each post/page.', $this->plugin)?></p>
<?php <?php
} }
   
public function varnish_caching_purge_key() public function varnish_caching_purge_key()
{ {
?> ?>
<input type="text" name="varnish_caching_purge_key" id="varnish_caching_purge_key" size="100" maxlength="64" value="<?php echo get_option($this->prefix . 'purge_key'); ?>" /> <input type="text" name="varnish_caching_purge_key" id="varnish_caching_purge_key" size="100" maxlength="64" value="<?php echo get_option($this->prefix . 'purge_key'); ?>" />
<span onclick="generateHash(64, 0, 'varnish_caching_purge_key'); return false;" class="dashicons dashicons-image-rotate" title="<?=__('Generate')?>"></span> <span onclick="generateHash(64, 0, 'varnish_caching_purge_key'); return false;" class="dashicons dashicons-image-rotate" title="<?=__('Generate')?>"></span>
<p class="description"> <p class="description">
<?=__('Key used to purge Varnish cache. It is sent to Varnish as X-VC-Purge-Key header. Use a SHA-256 hash.<br />If you can\'t use ACL\'s, use this option. You can set the `purge key` in lib/purge.vcl.<br />Search the default value ff93c3cb929cee86901c7eefc8088e9511c005492c6502a930360c02221cf8f4 to find where to replace it.', $this->plugin)?> <?=__('Key used to purge Varnish cache. It is sent to Varnish as X-VC-Purge-Key header. Use a SHA-256 hash.<br />If you can\'t use ACL\'s, use this option. You can set the `purge key` in lib/purge.vcl.<br />Search the default value ff93c3cb929cee86901c7eefc8088e9511c005492c6502a930360c02221cf8f4 to find where to replace it.', $this->plugin)?>
</p> </p>
<?php <?php
} }
   
public function varnish_caching_cookie() public function varnish_caching_cookie()
{ {
?> ?>
<input type="text" name="varnish_caching_cookie" id="varnish_caching_cookie" size="100" maxlength="64" value="<?php echo get_option($this->prefix . 'cookie'); ?>" /> <input type="text" name="varnish_caching_cookie" id="varnish_caching_cookie" size="100" maxlength="64" value="<?php echo get_option($this->prefix . 'cookie'); ?>" />
<span onclick="generateHash(64, 0, 'varnish_caching_cookie'); return false;" class="dashicons dashicons-image-rotate" title="<?=__('Generate')?>"></span> <span onclick="generateHash(64, 0, 'varnish_caching_cookie'); return false;" class="dashicons dashicons-image-rotate" title="<?=__('Generate')?>"></span>
<p class="description"> <p class="description">
<?=__('This module sets a special cookie to tell Varnish that the user is logged in. Use a SHA-256 hash. You can set the `logged in cookie` in default.vcl.<br />Search the default value <i>flxn34napje9kwbwr4bjwz5miiv9dhgj87dct4ep0x3arr7ldif73ovpxcgm88vs</i> to find where to replace it.', $this->plugin)?> <?=__('This module sets a special cookie to tell Varnish that the user is logged in. Use a SHA-256 hash. You can set the `logged in cookie` in default.vcl.<br />Search the default value <i>flxn34napje9kwbwr4bjwz5miiv9dhgj87dct4ep0x3arr7ldif73ovpxcgm88vs</i> to find where to replace it.', $this->plugin)?>
</p> </p>
<?php <?php
} }
   
public function varnish_caching_stats_json_file() public function varnish_caching_stats_json_file()
{ {
?> ?>
<input type="text" name="varnish_caching_stats_json_file" id="varnish_caching_stats_json_file" size="100" value="<?php echo get_option($this->prefix . 'stats_json_file'); ?>" /> <input type="text" name="varnish_caching_stats_json_file" id="varnish_caching_stats_json_file" size="100" value="<?php echo get_option($this->prefix . 'stats_json_file'); ?>" />
<p class="description"> <p class="description">
<?=sprintf(__('Comma separated relative URLs. One for each IP. <a href="%1$s/wp-admin/index.php?page=vcaching-plugin&tab=stats&info=1">Click here</a> for more info on how to set this up.', $this->plugin), home_url())?> <?=sprintf(__('Comma separated relative URLs. One for each IP. <a href="%1$s/wp-admin/index.php?page=vcaching-plugin&tab=stats&info=1">Click here</a> for more info on how to set this up.', $this->plugin), home_url())?>
</p> </p>
<?php <?php
} }
   
public function varnish_caching_truncate_notice() public function varnish_caching_truncate_notice()
{ {
?> ?>
<input type="checkbox" name="varnish_caching_truncate_notice" value="1" <?php checked(1, get_option($this->prefix . 'truncate_notice'), true); ?> /> <input type="checkbox" name="varnish_caching_truncate_notice" value="1" <?php checked(1, get_option($this->prefix . 'truncate_notice'), true); ?> />
<p class="description"> <p class="description">
<?=__('When using multiple Varnish Cache servers, VCaching shows too many `Trying to purge URL` messages. Check this option to truncate that message.', $this->plugin)?> <?=__('When using multiple Varnish Cache servers, VCaching shows too many `Trying to purge URL` messages. Check this option to truncate that message.', $this->plugin)?>
</p> </p>
<?php <?php
} }
   
public function varnish_caching_debug() public function varnish_caching_debug()
{ {
?> ?>
<input type="checkbox" name="varnish_caching_debug" value="1" <?php checked(1, get_option($this->prefix . 'debug'), true); ?> /> <input type="checkbox" name="varnish_caching_debug" value="1" <?php checked(1, get_option($this->prefix . 'debug'), true); ?> />
<p class="description"> <p class="description">
<?=__('Send all debugging headers to the client. Also shows complete response from Varnish on purge all.', $this->plugin)?> <?=__('Send all debugging headers to the client. Also shows complete response from Varnish on purge all.', $this->plugin)?>
</p> </p>
<?php <?php
} }
   
public function console_page_fields() public function console_page_fields()
{ {
add_settings_section('console', __("Console", $this->plugin), null, $this->prefix . 'console'); add_settings_section('console', __("Console", $this->plugin), null, $this->prefix . 'console');
   
add_settings_field($this->prefix . "purge_url", __("URL", $this->plugin), array($this, $this->prefix . "purge_url"), $this->prefix . 'console', "console"); add_settings_field($this->prefix . "purge_url", __("URL", $this->plugin), array($this, $this->prefix . "purge_url"), $this->prefix . 'console', "console");
} }
   
public function varnish_caching_purge_url() public function varnish_caching_purge_url()
{ {
?> ?>
<input type="text" name="varnish_caching_purge_url" size="100" id="varnish_caching_purge_url" value="" /> <input type="text" name="varnish_caching_purge_url" size="100" id="varnish_caching_purge_url" value="" />
<p class="description"><?=__('Relative URL to purge. Example : /wp-content/uploads/.*', $this->plugin)?></p> <p class="description"><?=__('Relative URL to purge. Example : /wp-content/uploads/.*', $this->plugin)?></p>
<?php <?php
} }
   
public function conf_page_fields() public function conf_page_fields()
{ {
add_settings_section('conf', __("Varnish configuration", $this->plugin), null, $this->prefix . 'conf'); add_settings_section('conf', __("Varnish configuration", $this->plugin), null, $this->prefix . 'conf');
   
add_settings_field($this->prefix . "varnish_backends", __("Backends", $this->plugin), array($this, $this->prefix . "varnish_backends"), $this->prefix . 'conf', "conf"); add_settings_field($this->prefix . "varnish_backends", __("Backends", $this->plugin), array($this, $this->prefix . "varnish_backends"), $this->prefix . 'conf', "conf");
add_settings_field($this->prefix . "varnish_acls", __("ACLs", $this->plugin), array($this, $this->prefix . "varnish_acls"), $this->prefix . 'conf', "conf"); add_settings_field($this->prefix . "varnish_acls", __("ACLs", $this->plugin), array($this, $this->prefix . "varnish_acls"), $this->prefix . 'conf', "conf");
   
if(isset($_POST['option_page']) && $_POST['option_page'] == $this->prefix . 'conf') { if(isset($_POST['option_page']) && $_POST['option_page'] == $this->prefix . 'conf') {
register_setting($this->prefix . 'conf', $this->prefix . "varnish_backends"); register_setting($this->prefix . 'conf', $this->prefix . "varnish_backends");
register_setting($this->prefix . 'conf', $this->prefix . "varnish_acls"); register_setting($this->prefix . 'conf', $this->prefix . "varnish_acls");
} }
   
add_settings_section('download', __("Get configuration files", $this->plugin), null, $this->prefix . 'download'); add_settings_section('download', __("Get configuration files", $this->plugin), null, $this->prefix . 'download');
if (!class_exists('ZipArchive')) { if (!class_exists('ZipArchive')) {
add_settings_section('download_error', __("You cannot download the configuration files", $this->plugin), null, $this->prefix . 'download_error'); add_settings_section('download_error', __("You cannot download the configuration files", $this->plugin), null, $this->prefix . 'download_error');
} }
   
add_settings_field($this->prefix . "varnish_version", __("Version", $this->plugin), array($this, $this->prefix . "varnish_version"), $this->prefix . 'download', "download"); add_settings_field($this->prefix . "varnish_version", __("Version", $this->plugin), array($this, $this->prefix . "varnish_version"), $this->prefix . 'download', "download");
   
if(isset($_POST['option_page']) && $_POST['option_page'] == $this->prefix . 'download') { if(isset($_POST['option_page']) && $_POST['option_page'] == $this->prefix . 'download') {
$version = in_array($_POST['varnish_caching_varnish_version'], array(3,4)) ? $_POST['varnish_caching_varnish_version'] : 3; $version = in_array($_POST['varnish_caching_varnish_version'], array(3,4)) ? $_POST['varnish_caching_varnish_version'] : 3;
$tmpfile = tempnam("tmp", "zip"); $tmpfile = tempnam("tmp", "zip");
$zip = new ZipArchive(); $zip = new ZipArchive();
$zip->open($tmpfile, ZipArchive::OVERWRITE); $zip->open($tmpfile, ZipArchive::OVERWRITE);
$files = array( $files = array(
'default.vcl' => true, 'default.vcl' => true,
'LICENSE' => false, 'LICENSE' => false,
'README.rst' => false, 'README.rst' => false,
'conf/acl.vcl' => true, 'conf/acl.vcl' => true,
'conf/backend.vcl' => true, 'conf/backend.vcl' => true,
'lib/bigfiles.vcl' => false, 'lib/bigfiles.vcl' => false,
'lib/bigfiles_pipe.vcl' => false, 'lib/bigfiles_pipe.vcl' => false,
'lib/cloudflare.vcl' => false, 'lib/cloudflare.vcl' => false,
'lib/mobile_cache.vcl' => false, 'lib/mobile_cache.vcl' => false,
'lib/mobile_pass.vcl' => false, 'lib/mobile_pass.vcl' => false,
'lib/purge.vcl' => true, 'lib/purge.vcl' => true,
'lib/static.vcl' => false, 'lib/static.vcl' => false,
'lib/xforward.vcl' => false, 'lib/xforward.vcl' => false,
); );
foreach ($files as $file => $parse) { foreach ($files as $file => $parse) {
$filepath = __DIR__ . '/varnish-conf/v' . $version . '/' . $file; $filepath = __DIR__ . '/varnish-conf/v' . $version . '/' . $file;
if ($parse) { if ($parse) {
$content = $this->_parse_conf_file($version, $file, file_get_contents($filepath)); $content = $this->_parse_conf_file($version, $file, file_get_contents($filepath));
} else { } else {
$content = file_get_contents($filepath); $content = file_get_contents($filepath);
} }
$zip->addFromString($file, $content); $zip->addFromString($file, $content);
} }
$zip->close(); $zip->close();
header('Content-Type: application/zip'); header('Content-Type: application/zip');
header('Content-Length: ' . filesize($tmpfile)); header('Content-Length: ' . filesize($tmpfile));
header('Content-Disposition: attachment; filename="varnish_v' . $version . '_conf.zip"'); header('Content-Disposition: attachment; filename="varnish_v' . $version . '_conf.zip"');
readfile($tmpfile); readfile($tmpfile);
unlink($tmpfile); unlink($tmpfile);
exit(); exit();
} }
} }
   
public function varnish_caching_varnish_version() public function varnish_caching_varnish_version()
{ {
?> ?>
<select name="varnish_caching_varnish_version" id="varnish_caching_varnish_version"> <select name="varnish_caching_varnish_version" id="varnish_caching_varnish_version">
<option value="3">3</option> <option value="3">3</option>
<option value="4">4</option> <option value="4">4</option>
</select> </select>
<p class="description"><?=__('Varnish Cache version', $this->plugin)?></p> <p class="description"><?=__('Varnish Cache version', $this->plugin)?></p>
<?php <?php
} }
   
public function varnish_caching_varnish_backends() public function varnish_caching_varnish_backends()
{ {
?> ?>
<input type="text" name="varnish_caching_varnish_backends" id="varnish_caching_varnish_backends" size="100" value="<?php echo get_option($this->prefix . 'varnish_backends'); ?>" /> <input type="text" name="varnish_caching_varnish_backends" id="varnish_caching_varnish_backends" size="100" value="<?php echo get_option($this->prefix . 'varnish_backends'); ?>" />
<p class="description"><?=__('Comma separated ip/ip:port. Example : 192.168.0.2,192.168.0.3:8080', $this->plugin)?></p> <p class="description"><?=__('Comma separated ip/ip:port. Example : 192.168.0.2,192.168.0.3:8080', $this->plugin)?></p>
<?php <?php
} }
   
public function varnish_caching_varnish_acls() public function varnish_caching_varnish_acls()
{ {
?> ?>
<input type="text" name="varnish_caching_varnish_acls" id="varnish_caching_varnish_acls" size="100" value="<?php echo get_option($this->prefix . 'varnish_acls'); ?>" /> <input type="text" name="varnish_caching_varnish_acls" id="varnish_caching_varnish_acls" size="100" value="<?php echo get_option($this->prefix . 'varnish_acls'); ?>" />
<p class="description"><?=__('Comma separated ip/ip range. Example : 192.168.0.2,192.168.1.1/24', $this->plugin)?></p> <p class="description"><?=__('Comma separated ip/ip range. Example : 192.168.0.2,192.168.1.1/24', $this->plugin)?></p>
<?php <?php
} }
   
private function _parse_conf_file($version, $file, $content) private function _parse_conf_file($version, $file, $content)
{ {
if ($file == 'default.vcl') { if ($file == 'default.vcl') {
$logged_in_cookie = get_option($this->prefix . 'cookie'); $logged_in_cookie = get_option($this->prefix . 'cookie');
$content = str_replace('flxn34napje9kwbwr4bjwz5miiv9dhgj87dct4ep0x3arr7ldif73ovpxcgm88vs', $logged_in_cookie, $content); $content = str_replace('flxn34napje9kwbwr4bjwz5miiv9dhgj87dct4ep0x3arr7ldif73ovpxcgm88vs', $logged_in_cookie, $content);
} else if ($file == 'conf/backend.vcl') { } else if ($file == 'conf/backend.vcl') {
if ($version == 3) { if ($version == 3) {
$content = ""; $content = "";
} else if ($version == 4) { } else if ($version == 4) {
$content = "import directors;\n\n"; $content = "import directors;\n\n";
} }
$backend = array(); $backend = array();
$ips = get_option($this->prefix . 'varnish_backends'); $ips = get_option($this->prefix . 'varnish_backends');
$ips = explode(',', $ips); $ips = explode(',', $ips);
$id = 1; $id = 1;
foreach ($ips as $ip) { foreach ($ips as $ip) {
if (strstr($ip, ":")) { if (strstr($ip, ":")) {
$_ip = explode(':', $ip); $_ip = explode(':', $ip);
$ip = $_ip[0]; $ip = $_ip[0];
$port = $_ip[1]; $port = $_ip[1];
} else { } else {
$port = 80; $port = 80;
} }
$content .= "backend backend" . $id . " {\n"; $content .= "backend backend" . $id . " {\n";
$content .= "\t.host = \"" . $ip . "\";\n"; $content .= "\t.host = \"" . $ip . "\";\n";
$content .= "\t.port = \"" . $port . "\";\n"; $content .= "\t.port = \"" . $port . "\";\n";
$content .= "}\n"; $content .= "}\n";
$backend[3] .= "\t{\n"; $backend[3] .= "\t{\n";
$backend[3] .= "\t\t.backend = backend" . $id . ";\n"; $backend[3] .= "\t\t.backend = backend" . $id . ";\n";
$backend[3] .= "\t}\n"; $backend[3] .= "\t}\n";
$backend[4] .= "\tbackends.add_backend(backend" . $id . ");\n"; $backend[4] .= "\tbackends.add_backend(backend" . $id . ");\n";
$id++; $id++;
} }
if ($version == 3) { if ($version == 3) {
$content .= "\ndirector backends round-robin {\n"; $content .= "\ndirector backends round-robin {\n";
$content .= $backend[3]; $content .= $backend[3];
$content .= "}\n"; $content .= "}\n";
$content .= "\nsub vcl_recv {\n"; $content .= "\nsub vcl_recv {\n";
$content .= "\tset req.backend = backends;\n"; $content .= "\tset req.backend = backends;\n";
$content .= "}\n"; $content .= "}\n";
} elseif ($version == 4) { } elseif ($version == 4) {
$content .= "\nsub vcl_init {\n"; $content .= "\nsub vcl_init {\n";
$content .= "\tnew backends = directors.round_robin();\n"; $content .= "\tnew backends = directors.round_robin();\n";
$content .= $backend[4]; $content .= $backend[4];
$content .= "}\n"; $content .= "}\n";
$content .= "\nsub vcl_recv {\n"; $content .= "\nsub vcl_recv {\n";
$content .= "\tset req.backend_hint = backends.backend();\n"; $content .= "\tset req.backend_hint = backends.backend();\n";
$content .= "}\n"; $content .= "}\n";
} }
} else if ($file == 'conf/acl.vcl') { } else if ($file == 'conf/acl.vcl') {
$acls = get_option($this->prefix . 'varnish_acls'); $acls = get_option($this->prefix . 'varnish_acls');
$acls = explode(',', $acls); $acls = explode(',', $acls);
$content = "acl cloudflare {\n"; $content = "acl cloudflare {\n";
$content .= "\t# set this ip to your Railgun IP (if applicable)\n"; $content .= "\t# set this ip to your Railgun IP (if applicable)\n";
$content .= "\t# \"1.2.3.4\";\n"; $content .= "\t# \"1.2.3.4\";\n";
$content .= "}\n"; $content .= "}\n";
$content .= "\nacl purge {\n"; $content .= "\nacl purge {\n";
$content .= "\t\"localhost\";\n"; $content .= "\t\"localhost\";\n";
$content .= "\t\"127.0.0.1\";\n"; $content .= "\t\"127.0.0.1\";\n";
foreach ($acls as $acl) { foreach ($acls as $acl) {
$content .= "\t\"" . $acl . "\";\n"; $content .= "\t\"" . $acl . "\";\n";
} }
$content .= "}\n"; $content .= "}\n";
} else if ($file == 'lib/purge.vcl') { } else if ($file == 'lib/purge.vcl') {
$purge_key = get_option($this->prefix . 'purge_key'); $purge_key = get_option($this->prefix . 'purge_key');
$content = str_replace('ff93c3cb929cee86901c7eefc8088e9511c005492c6502a930360c02221cf8f4', $purge_key, $content); $content = str_replace('ff93c3cb929cee86901c7eefc8088e9511c005492c6502a930360c02221cf8f4', $purge_key, $content);
} }
return $content; return $content;
} }
   
public function post_row_actions($actions, $post) public function post_row_actions($actions, $post)
{ {
if ($this->check_if_purgeable()) { if ($this->check_if_purgeable()) {
$actions = array_merge($actions, array( $actions = array_merge($actions, array(
'vcaching_purge_post' => sprintf('<a href="%s">' . __('Purge from Varnish', $this->plugin) . '</a>', wp_nonce_url(sprintf('admin.php?page=vcaching-plugin&tab=console&action=purge_post&post_id=%d', $post->ID), $this->plugin)) 'vcaching_purge_post' => sprintf('<a href="%s">' . __('Purge from Varnish', $this->plugin) . '</a>', wp_nonce_url(sprintf('admin.php?page=vcaching-plugin&tab=console&action=purge_post&post_id=%d', $post->ID), $this->plugin))
)); ));
} }
return $actions; return $actions;
} }
   
public function page_row_actions($actions, $post) public function page_row_actions($actions, $post)
{ {
if ($this->check_if_purgeable()) { if ($this->check_if_purgeable()) {
$actions = array_merge($actions, array( $actions = array_merge($actions, array(
'vcaching_purge_page' => sprintf('<a href="%s">' . __('Purge from Varnish', $this->plugin) . '</a>', wp_nonce_url(sprintf('admin.php?page=vcaching-plugin&tab=console&action=purge_page&post_id=%d', $post->ID), $this->plugin)) 'vcaching_purge_page' => sprintf('<a href="%s">' . __('Purge from Varnish', $this->plugin) . '</a>', wp_nonce_url(sprintf('admin.php?page=vcaching-plugin&tab=console&action=purge_page&post_id=%d', $post->ID), $this->plugin))
)); ));
} }
return $actions; return $actions;
} }
} }
   
$vcaching = new VCaching(); $vcaching = new VCaching();
   
// WP-CLI // WP-CLI
if (defined('WP_CLI') && WP_CLI) { if (defined('WP_CLI') && WP_CLI) {
include('wp-cli.php'); include('wp-cli.php');
} }
   
comments