Version Description
Download this release
Release Info
Developer | Ipstenu |
Plugin | Varnish HTTP Purge |
Version | 5.0.1 |
Comparing to | |
See all releases |
Code changes from version 5.0 to 5.0.1
- varnish-http-purge.php +80 -31
varnish-http-purge.php
CHANGED
@@ -3,7 +3,7 @@
|
|
3 |
* Plugin Name: Proxy Cache Purge
|
4 |
* Plugin URI: https://github.com/ipstenu/varnish-http-purge/
|
5 |
* Description: Automatically empty cached pages when content on your site is modified.
|
6 |
-
* Version: 5.0
|
7 |
* Author: Mika Epstein
|
8 |
* Author URI: https://halfelf.org/
|
9 |
* License: http://www.apache.org/licenses/LICENSE-2.0
|
@@ -35,7 +35,7 @@ class VarnishPurger {
|
|
35 |
* Version Number
|
36 |
* @var string
|
37 |
*/
|
38 |
-
public static $version = '5.0';
|
39 |
|
40 |
/**
|
41 |
* List of URLs to be purged
|
@@ -67,6 +67,7 @@ class VarnishPurger {
|
|
67 |
public function __construct() {
|
68 |
defined( 'VHP_VARNISH_IP' ) || define( 'VHP_VARNISH_IP', false );
|
69 |
defined( 'VHP_DEVMODE' ) || define( 'VHP_DEVMODE', false );
|
|
|
70 |
|
71 |
// Development mode defaults to off.
|
72 |
self::$devmode = array(
|
@@ -148,7 +149,7 @@ class VarnishPurger {
|
|
148 |
global $blog_id, $wp_db_version;
|
149 |
|
150 |
// If the DB version we detect isn't the same as the version core thinks
|
151 |
-
// we will
|
152 |
// should not be harmful.
|
153 |
if ( file_exists( WP_CONTENT_DIR . '/object-cache.php' ) && (int) get_option( 'db_version' ) !== $wp_db_version ) {
|
154 |
wp_cache_flush();
|
@@ -164,7 +165,7 @@ class VarnishPurger {
|
|
164 |
}
|
165 |
// @codingStandardsIgnoreEnd
|
166 |
|
167 |
-
// Add
|
168 |
add_filter( 'style_loader_src', array( 'VarnishDebug', 'nocache_cssjs' ), 10, 2 );
|
169 |
add_filter( 'script_loader_src', array( 'VarnishDebug', 'nocache_cssjs' ), 10, 2 );
|
170 |
}
|
@@ -244,7 +245,7 @@ class VarnishPurger {
|
|
244 |
|
245 |
/**
|
246 |
* Purge Message
|
247 |
-
* Informs of a
|
248 |
*
|
249 |
* @since 4.6
|
250 |
*/
|
@@ -330,7 +331,7 @@ class VarnishPurger {
|
|
330 |
}
|
331 |
|
332 |
/**
|
333 |
-
* Custom CSS to allow for
|
334 |
*
|
335 |
* @since 4.5.0
|
336 |
*/
|
@@ -586,9 +587,15 @@ class VarnishPurger {
|
|
586 |
public function execute_purge() {
|
587 |
$purge_urls = array_unique( $this->purge_urls );
|
588 |
|
589 |
-
if ( empty( $purge_urls ) &&
|
|
|
|
|
|
|
|
|
|
|
|
|
590 |
if ( isset( $_GET['vhp_flush_all'] ) && check_admin_referer( 'vhp-flush-all' ) ) {
|
591 |
-
// Flush Cache
|
592 |
$this->purge_url( $this->the_home_url() . '/?vhp-regex' );
|
593 |
} elseif ( isset( $_GET['vhp_flush_do'] ) && check_admin_referer( 'vhp-flush-do' ) ) {
|
594 |
if ( 'object' === $_GET['vhp_flush_do'] ) {
|
@@ -597,7 +604,7 @@ class VarnishPurger {
|
|
597 |
wp_cache_flush();
|
598 |
}
|
599 |
} elseif ( 'all' === $_GET['vhp_flush_do'] ) {
|
600 |
-
// Flush Cache
|
601 |
$this->purge_url( $this->the_home_url() . '/?vhp-regex' );
|
602 |
} else {
|
603 |
// Flush the URL we're on.
|
@@ -608,10 +615,6 @@ class VarnishPurger {
|
|
608 |
$this->purge_url( esc_url_raw( wp_unslash( $_GET['vhp_flush_do'] ) ) );
|
609 |
}
|
610 |
}
|
611 |
-
} else {
|
612 |
-
foreach ( $purge_urls as $url ) {
|
613 |
-
$this->purge_url( $url );
|
614 |
-
}
|
615 |
}
|
616 |
}
|
617 |
|
@@ -624,6 +627,12 @@ class VarnishPurger {
|
|
624 |
* @access protected
|
625 |
*/
|
626 |
public static function purge_url( $url ) {
|
|
|
|
|
|
|
|
|
|
|
|
|
627 |
$p = wp_parse_url( $url );
|
628 |
|
629 |
// Bail early if there's no host since some plugins are weird.
|
@@ -640,14 +649,16 @@ class VarnishPurger {
|
|
640 |
$x_purge_method = 'regex';
|
641 |
}
|
642 |
|
643 |
-
// Build a varniship to sail.
|
644 |
-
|
645 |
-
|
646 |
-
|
647 |
-
|
|
|
|
|
648 |
}
|
649 |
|
650 |
-
//
|
651 |
if ( is_array( $varniship ) ) {
|
652 |
// To each ship:
|
653 |
for ( $i = 0; $i++; $i < count( $varniship ) ) {
|
@@ -671,7 +682,7 @@ class VarnishPurger {
|
|
671 |
*/
|
672 |
|
673 |
// This is a very annoying check for DreamHost who needs to default to HTTPS without breaking
|
674 |
-
// people who've been around before
|
675 |
$server_hostname = gethostname();
|
676 |
switch ( substr( $server_hostname, 0, 3 ) ) {
|
677 |
case 'dp-':
|
@@ -687,7 +698,7 @@ class VarnishPurger {
|
|
687 |
if ( isset( $varniship ) && ! empty( $varniship ) ) {
|
688 |
$all_hosts = ( ! is_array( $varniship ) ) ? array( $varniship ) : $varniship;
|
689 |
} else {
|
690 |
-
// default is the main host,
|
691 |
$all_hosts = array( $p['host'] );
|
692 |
}
|
693 |
|
@@ -740,6 +751,7 @@ class VarnishPurger {
|
|
740 |
);
|
741 |
|
742 |
// Send response.
|
|
|
743 |
$response = wp_remote_request(
|
744 |
$purgeme,
|
745 |
array(
|
@@ -804,9 +816,6 @@ class VarnishPurger {
|
|
804 |
public function purge_post( $post_id ) {
|
805 |
|
806 |
/**
|
807 |
-
* Future Me: You may need this if you figure out how to use an array
|
808 |
-
* further down with versions of WP and their json versions.
|
809 |
-
* Maybe use global $wp_version;
|
810 |
* If this is a valid post we want to purge the post,
|
811 |
* the home page and any associated tags and categories
|
812 |
*/
|
@@ -824,13 +833,27 @@ class VarnishPurger {
|
|
824 |
* Future me: Consider an array? 4.7-?? use v2, and then adapt from there?
|
825 |
*/
|
826 |
if ( version_compare( get_bloginfo( 'version' ), '4.7', '>=' ) ) {
|
827 |
-
$
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
828 |
}
|
829 |
|
830 |
// array to collect all our URLs.
|
831 |
$listofurls = array();
|
832 |
|
833 |
-
// Verify we have a permalink and that we're a valid post status and
|
834 |
if ( false !== get_permalink( $post_id ) && in_array( $this_post_status, $valid_post_status, true ) && ! in_array( $this_post_type, $invalid_post_type, true ) ) {
|
835 |
|
836 |
// Post URL.
|
@@ -851,13 +874,13 @@ class VarnishPurger {
|
|
851 |
} elseif ( 'page' === $this_post_type ) {
|
852 |
$rest_permalink = get_rest_url() . $rest_api_route . '/pages/' . $post_id . '/';
|
853 |
}
|
854 |
-
}
|
855 |
|
856 |
-
|
857 |
-
|
|
|
858 |
}
|
859 |
|
860 |
-
// Add in AMP permalink for
|
861 |
// https://wordpress.org/plugins/amp/
|
862 |
if ( function_exists( 'amp_get_permalink' ) ) {
|
863 |
array_push( $listofurls, amp_get_permalink( $post_id ) );
|
@@ -973,12 +996,38 @@ class VarnishPurger {
|
|
973 |
}
|
974 |
|
975 |
// If the array isn't empty, proceed.
|
976 |
-
if (
|
|
|
|
|
977 |
// Strip off query variables
|
978 |
foreach ( $listofurls as $url ) {
|
979 |
$url = strtok( $url, '?' );
|
980 |
}
|
981 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
982 |
// Make sure each URL only gets purged once, eh?
|
983 |
$purgeurls = array_unique( $listofurls, SORT_REGULAR );
|
984 |
|
3 |
* Plugin Name: Proxy Cache Purge
|
4 |
* Plugin URI: https://github.com/ipstenu/varnish-http-purge/
|
5 |
* Description: Automatically empty cached pages when content on your site is modified.
|
6 |
+
* Version: 5.0.1
|
7 |
* Author: Mika Epstein
|
8 |
* Author URI: https://halfelf.org/
|
9 |
* License: http://www.apache.org/licenses/LICENSE-2.0
|
35 |
* Version Number
|
36 |
* @var string
|
37 |
*/
|
38 |
+
public static $version = '5.0.1';
|
39 |
|
40 |
/**
|
41 |
* List of URLs to be purged
|
67 |
public function __construct() {
|
68 |
defined( 'VHP_VARNISH_IP' ) || define( 'VHP_VARNISH_IP', false );
|
69 |
defined( 'VHP_DEVMODE' ) || define( 'VHP_DEVMODE', false );
|
70 |
+
defined( 'VHP_DOMAINS' ) || define( 'VHP_DOMAINS', false );
|
71 |
|
72 |
// Development mode defaults to off.
|
73 |
self::$devmode = array(
|
149 |
global $blog_id, $wp_db_version;
|
150 |
|
151 |
// If the DB version we detect isn't the same as the version core thinks
|
152 |
+
// we will flush DB cache. This may cause double dumping in some cases but
|
153 |
// should not be harmful.
|
154 |
if ( file_exists( WP_CONTENT_DIR . '/object-cache.php' ) && (int) get_option( 'db_version' ) !== $wp_db_version ) {
|
155 |
wp_cache_flush();
|
165 |
}
|
166 |
// @codingStandardsIgnoreEnd
|
167 |
|
168 |
+
// Add nocache to CSS and JS.
|
169 |
add_filter( 'style_loader_src', array( 'VarnishDebug', 'nocache_cssjs' ), 10, 2 );
|
170 |
add_filter( 'script_loader_src', array( 'VarnishDebug', 'nocache_cssjs' ), 10, 2 );
|
171 |
}
|
245 |
|
246 |
/**
|
247 |
* Purge Message
|
248 |
+
* Informs of a successful purge
|
249 |
*
|
250 |
* @since 4.6
|
251 |
*/
|
331 |
}
|
332 |
|
333 |
/**
|
334 |
+
* Custom CSS to allow for colouring.
|
335 |
*
|
336 |
* @since 4.5.0
|
337 |
*/
|
587 |
public function execute_purge() {
|
588 |
$purge_urls = array_unique( $this->purge_urls );
|
589 |
|
590 |
+
if ( ! empty( $purge_urls ) && is_array( $purge_urls ) ) {
|
591 |
+
// If there are URLs to purge and it's an array...
|
592 |
+
foreach ( $purge_urls as $url ) {
|
593 |
+
$this->purge_url( $url );
|
594 |
+
}
|
595 |
+
} elseif ( isset( $_GET ) ) {
|
596 |
+
// Otherwise, if we've passed a GET call...
|
597 |
if ( isset( $_GET['vhp_flush_all'] ) && check_admin_referer( 'vhp-flush-all' ) ) {
|
598 |
+
// Flush Cache recursive.
|
599 |
$this->purge_url( $this->the_home_url() . '/?vhp-regex' );
|
600 |
} elseif ( isset( $_GET['vhp_flush_do'] ) && check_admin_referer( 'vhp-flush-do' ) ) {
|
601 |
if ( 'object' === $_GET['vhp_flush_do'] ) {
|
604 |
wp_cache_flush();
|
605 |
}
|
606 |
} elseif ( 'all' === $_GET['vhp_flush_do'] ) {
|
607 |
+
// Flush Cache recursive.
|
608 |
$this->purge_url( $this->the_home_url() . '/?vhp-regex' );
|
609 |
} else {
|
610 |
// Flush the URL we're on.
|
615 |
$this->purge_url( esc_url_raw( wp_unslash( $_GET['vhp_flush_do'] ) ) );
|
616 |
}
|
617 |
}
|
|
|
|
|
|
|
|
|
618 |
}
|
619 |
}
|
620 |
|
627 |
* @access protected
|
628 |
*/
|
629 |
public static function purge_url( $url ) {
|
630 |
+
|
631 |
+
// Bail early if someone sent a non-URL
|
632 |
+
if ( false === filter_var( $url, FILTER_VALIDATE_URL ) ) {
|
633 |
+
return;
|
634 |
+
}
|
635 |
+
|
636 |
$p = wp_parse_url( $url );
|
637 |
|
638 |
// Bail early if there's no host since some plugins are weird.
|
649 |
$x_purge_method = 'regex';
|
650 |
}
|
651 |
|
652 |
+
// Build a varniship to sail. ⛵️
|
653 |
+
$varniship = ( VHP_VARNISH_IP !== false ) ? VHP_VARNISH_IP : get_site_option( 'vhp_varnish_ip' );
|
654 |
+
|
655 |
+
// If there are commas, and for whatever reason this didn't become an array
|
656 |
+
// properly, force it.
|
657 |
+
if ( ! is_array( $varniship ) && strpos( $varniship, ',' ) !== false ) {
|
658 |
+
$varniship = array_map( 'trim', explode( ',', $varniship ) );
|
659 |
}
|
660 |
|
661 |
+
// Now apply filters
|
662 |
if ( is_array( $varniship ) ) {
|
663 |
// To each ship:
|
664 |
for ( $i = 0; $i++; $i < count( $varniship ) ) {
|
682 |
*/
|
683 |
|
684 |
// This is a very annoying check for DreamHost who needs to default to HTTPS without breaking
|
685 |
+
// people who've been around before.
|
686 |
$server_hostname = gethostname();
|
687 |
switch ( substr( $server_hostname, 0, 3 ) ) {
|
688 |
case 'dp-':
|
698 |
if ( isset( $varniship ) && ! empty( $varniship ) ) {
|
699 |
$all_hosts = ( ! is_array( $varniship ) ) ? array( $varniship ) : $varniship;
|
700 |
} else {
|
701 |
+
// The default is the main host, converted into an array.
|
702 |
$all_hosts = array( $p['host'] );
|
703 |
}
|
704 |
|
751 |
);
|
752 |
|
753 |
// Send response.
|
754 |
+
// SSL Verify is required here since Varnish is HTTP only, but proxies are a thing.
|
755 |
$response = wp_remote_request(
|
756 |
$purgeme,
|
757 |
array(
|
816 |
public function purge_post( $post_id ) {
|
817 |
|
818 |
/**
|
|
|
|
|
|
|
819 |
* If this is a valid post we want to purge the post,
|
820 |
* the home page and any associated tags and categories
|
821 |
*/
|
833 |
* Future me: Consider an array? 4.7-?? use v2, and then adapt from there?
|
834 |
*/
|
835 |
if ( version_compare( get_bloginfo( 'version' ), '4.7', '>=' ) ) {
|
836 |
+
$json_disabled = false;
|
837 |
+
$json_disablers = array(
|
838 |
+
'disable-json-api/disable-json-api.php',
|
839 |
+
);
|
840 |
+
|
841 |
+
foreach ( $json_disablers as $json_plugin ) {
|
842 |
+
if ( is_plugin_active( $json_plugin ) ) {
|
843 |
+
$json_disabled = true;
|
844 |
+
}
|
845 |
+
}
|
846 |
+
|
847 |
+
// If json is NOT disabled...
|
848 |
+
if ( ! $json_disabled ) {
|
849 |
+
$rest_api_route = 'wp/v2';
|
850 |
+
}
|
851 |
}
|
852 |
|
853 |
// array to collect all our URLs.
|
854 |
$listofurls = array();
|
855 |
|
856 |
+
// Verify we have a permalink and that we're a valid post status and type.
|
857 |
if ( false !== get_permalink( $post_id ) && in_array( $this_post_status, $valid_post_status, true ) && ! in_array( $this_post_type, $invalid_post_type, true ) ) {
|
858 |
|
859 |
// Post URL.
|
874 |
} elseif ( 'page' === $this_post_type ) {
|
875 |
$rest_permalink = get_rest_url() . $rest_api_route . '/pages/' . $post_id . '/';
|
876 |
}
|
|
|
877 |
|
878 |
+
if ( isset( $rest_permalink ) ) {
|
879 |
+
array_push( $listofurls, $rest_permalink );
|
880 |
+
}
|
881 |
}
|
882 |
|
883 |
+
// Add in AMP permalink for official WP AMP plugin:
|
884 |
// https://wordpress.org/plugins/amp/
|
885 |
if ( function_exists( 'amp_get_permalink' ) ) {
|
886 |
array_push( $listofurls, amp_get_permalink( $post_id ) );
|
996 |
}
|
997 |
|
998 |
// If the array isn't empty, proceed.
|
999 |
+
if ( empty( $listofurls ) ) {
|
1000 |
+
return;
|
1001 |
+
} else {
|
1002 |
// Strip off query variables
|
1003 |
foreach ( $listofurls as $url ) {
|
1004 |
$url = strtok( $url, '?' );
|
1005 |
}
|
1006 |
|
1007 |
+
// If the DOMAINS setup is defined, we duplicate the URLs
|
1008 |
+
if ( false !== VHP_DOMAINS ) {
|
1009 |
+
// Split domains into an array
|
1010 |
+
$domains = explode( ',', VHP_DOMAINS );
|
1011 |
+
$newurls = array();
|
1012 |
+
|
1013 |
+
// Loop through all the domains
|
1014 |
+
foreach ( $domains as $a_domain ) {
|
1015 |
+
foreach ( $listofurls as $url ) {
|
1016 |
+
// If the URL contains the filtered home_url, and is NOT equal to the domain we're trying to replace, we will add it to the new urls
|
1017 |
+
if ( false !== strpos( $this->the_home_url(), $url ) && $this->the_home_url() !== $a_domain ) {
|
1018 |
+
$newurls[] = str_replace( $this->the_home_url(), $a_domain, $url );
|
1019 |
+
}
|
1020 |
+
// If the URL contains the raw home_url, and is NOT equal to the domain we're trying to replace, we will add it to the new urls
|
1021 |
+
if ( false !== strpos( home_url(), $url ) && home_url() !== $a_domain ) {
|
1022 |
+
$newurls[] = str_replace( home_url(), $a_domain, $url );
|
1023 |
+
}
|
1024 |
+
}
|
1025 |
+
}
|
1026 |
+
|
1027 |
+
// Merge all the URLs
|
1028 |
+
array_push( $listofurls, $newurls );
|
1029 |
+
}
|
1030 |
+
|
1031 |
// Make sure each URL only gets purged once, eh?
|
1032 |
$purgeurls = array_unique( $listofurls, SORT_REGULAR );
|
1033 |
|