Advertisement
Guest User

Untitled

a guest
Dec 7th, 2012
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.58 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Varnish HTTP Purge
  4. Plugin URI: http://wordpress.org/extend/plugins/varnish-http-purge/
  5. Description: Sends HTTP PURGE requests to URLs of changed posts/pages when they are modified. Works with Varnish 3.
  6. Version: 1.2.0-mod
  7. Author: Leon Weidauer
  8. Author URI: http:/sevenmil.es/
  9. License: Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0
  10. */
  11.  
  12. class VarnishPurger
  13. {
  14.     protected $purgeUrls = array();
  15.    
  16.     public function __construct()
  17.     {
  18.         foreach ($this->getRegisterEvents() as $event)
  19.         {
  20.             add_action($event, array($this, 'purgePost'));
  21.         }
  22.  
  23.         add_action('shutdown', array($this, 'executePurge'));
  24.     }
  25.  
  26.     protected function getRegisterEvents()
  27.     {
  28.         return array(
  29.             'publish_post',
  30.             'edit_post',
  31.             'deleted_post',
  32.         );
  33.     }
  34.  
  35.     public function executePurge()
  36.     {
  37.         $purgeUrls = array_unique($this->purgeUrls);
  38.  
  39.         foreach($purgeUrls as $url)
  40.         {
  41.             $this->purgeUrl($url);
  42.         }
  43.        
  44.         if (!empty($purgeUrls))
  45.         {
  46.             $this->purgeUrl(home_url());
  47.         }        
  48.     }
  49.  
  50.     protected function purgeUrl($url)
  51.     {
  52.         $c = curl_init($url);
  53.         curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'PURGE');
  54.     curl_setopt($c, CURLOPT_RETURNTRANSFER, true); // <-- new ;)
  55.         curl_exec($c);
  56.         curl_close($c);    
  57.     }
  58.  
  59.     public function purgePost($postId)
  60.     {
  61.         array_push($this->purgeUrls, get_permalink($postId));
  62.     }
  63. }
  64.  
  65. $purger = new VarnishPurger();
  66. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement