├── README.markdown ├── lang ├── wp-varnish-fi.mo ├── wp-varnish-fi.po ├── wp-varnish-fr_FR.mo ├── wp-varnish-fr_FR.po ├── wp-varnish-pt_BR.mo ├── wp-varnish-pt_BR.po └── wp-varnish.pot ├── readme.txt ├── screenshot-1.png ├── todo.txt ├── uninstall.php ├── wordpress.vcl ├── wp-varnish.js └── wp-varnish.php /README.markdown: -------------------------------------------------------------------------------- 1 | WordPress Varnish 2 | ================= 3 | 4 | * Contributors: pkhamre, wfelipe, eitch, linickx 5 | * Donate link: http://github.com/pkhamre/wp-varnish 6 | * Tags: cache, caching, performance, varnish, purge, speed 7 | * Requires at least: 2.9.2 8 | * Tested up to: 3.6.1 9 | * Stable tag: 0.8 10 | 11 | WordPress Varnish is a simple plugin that purges new and edited content. 12 | 13 | Description 14 | ----------- 15 | 16 | This plugin purges your varnish cache when content is added or edited. This includes when a new post is 17 | added, a post is updated or when a comment is posted to your blog. 18 | 19 | To keep widgets like "Recent posts", "Recent comments" and such up to date, you should consider using ESI 20 | and include them through a text widget for arbitrary text or HTML. 21 | 22 | Installation 23 | ------------ 24 | 25 | This section describes how to install the plugin and get it working. 26 | 27 | 1. Install the plugin 'WordPress Varnish' through the 'Plugins' menu in 28 | WordPress 29 | 2. Activate the plugin through the 'Plugins' menu in WordPress 30 | 31 | Frequently Asked Questions 32 | -------------------------- 33 | 34 | ### Does this just work? 35 | 36 | Yes. 37 | 38 | ### But how should my varnish configuration file look like? 39 | 40 | I have provided a simple VCL that can be used as a reference. 41 | 42 | ### Does it work for Multi-Site (or WPMU)? 43 | 44 | Yes. Activating the plugin site-wide will provide the functionality to all 45 | blogs. Configuration can be done on the blogs individually, or can be global. 46 | If you want to configure Varnish servers globally, edit wp-config.php and 47 | include these lines just before "That's all, stop editing!" message: 48 | 49 | > global $varnish_servers; 50 | > $varnish_servers = array('192.168.0.1:80:secret','192.168.0.2:80:secret'); 51 | > define('VARNISH_SHOWCFG',1); 52 | 53 | The varnish servers array will configure multiple servers for sending the 54 | purges. If VARNISH_SHOWCFG is defined, configuration will be shown to all 55 | users who access the plugin configuration page (but they can't edit it). 56 | 57 | ### My Plugins are seeing the Varnish server's IP rather than the websurfer IP 58 | 59 | You could install Apache's mod_rpaf module: http://stderr.net/apache/rpaf/ 60 | 61 | or, in wp-config.php, near the top, put the following code: 62 | 63 | $temp_ip = explode(',', isset($_SERVER['HTTP_X_FORWARDED_FOR']) 64 | ? $_SERVER['HTTP_X_FORWARDED_FOR'] : 65 | (isset($_SERVER['HTTP_CLIENT_IP']) ? 66 | $_SERVER['HTTP_CLIENT_IP'] : $_SERVER['REMOTE_ADDR'])); 67 | $remote_addr = trim($temp_ip[0]); 68 | $_SERVER['REMOTE_ADDR'] = preg_replace('/[^0-9.:]/', '', $remote_addr ); 69 | 70 | The code takes some of the common headers and replaces the REMOTE_ADDR 71 | variable, allowing plugins that use the surfer's IP address to see the 72 | surfer's IP rather than the server's IP. 73 | 74 | Screenshots 75 | ----------- 76 | 77 | 1. Screenshot of the administration interface. 78 | 79 | Changelog 80 | --------- 81 | 82 | ### 0.8 83 | * Added secret handling to WPVarnishPurgeObject, Thanks Kit Westneat 84 | * Change WPVarnishPurgePurgeCommonObjects to WPVarnishPurgeCommonObjects, 85 | Thanks kitwestneat 86 | * added @ to supress Undefined offset notice 87 | * minor doc changes 88 | 89 | ### 0.7 90 | * Added purge when post changes from future to publish, Thanks Marcin Pietrzak 91 | * Added purge when theme switched, Thanks dupuis 92 | 93 | ### 0.6 94 | * Removed plugins_loaded action as it doesnt do what was expected re: Issue 95 | #12. Thank you Ben Favre, Pothi Kalimuthu and allinwonder 96 | 97 | ### 0.5 98 | * New .vcl to fix purge as per Issue #39, Thanks Ed Cooper 99 | 100 | ### 0.4 101 | 102 | * added rule to skip caching 404s in vcl 103 | * WordPress-Varnish UserAgent as per ticket #23 104 | * document use of mod_rpaf or code fix for remote IP, Ticket #36 105 | * clean data rather than reject to fix ticket #31 106 | * added plugins_loaded hook to fix ticket #12 107 | 108 | ### 0.3 109 | 110 | * Added internationalization code. Included pt_BR translation. 111 | * Support to Multi-Site and WPMU with global configuration. 112 | * Fix on URL purges for multiple domains and blogs on sub-directories. 113 | * Code clean up on some functions. 114 | * Added configuration for purging all blog, page and comments navigation. 115 | 116 | ### 0.2 117 | 118 | * Added multiple servers support and timeout configuration. 119 | 120 | ### 0.1 121 | 122 | * Initial release. 123 | 124 | Upgrade Notice 125 | -------------- 126 | 127 | ### 0.8 128 | * Added secret handling to WPVarnishPurgeObject, Thanks Kit Westneat 129 | 130 | ### 0.7 131 | * Added purge when post changes from future to publish, Thanks Marcin Pietrzak 132 | * Added purge when theme switched, Thanks dupuis 133 | 134 | ### 0.6 135 | * Removed plugins_loaded action as it doesnt do what was expected re: Issue 136 | #12. Thank you Ben Favre, Pothi Kalimuthu and allinwonder 137 | 138 | ### 0.3 139 | 140 | * Varnish PURGE configuration must support regex. wp-varnish will 141 | sometimes request with regex for special purges like refreshing 142 | all blog cache and refreshing comments. 143 | -------------------------------------------------------------------------------- /lang/wp-varnish-fi.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkhamre/wp-varnish/f526bbcfd639ff954a2323f2b1c92e2b5e51a00b/lang/wp-varnish-fi.mo -------------------------------------------------------------------------------- /lang/wp-varnish-fi.po: -------------------------------------------------------------------------------- 1 | msgid "" 2 | msgstr "" 3 | "Project-Id-Version: WP-Varnish\n" 4 | "Report-Msgid-Bugs-To: \n" 5 | "POT-Creation-Date: 2013-05-25 21:57+0200\n" 6 | "PO-Revision-Date: 2013-05-25 22:04+0200\n" 7 | "Last-Translator: Loïc Gerbaud \n" 8 | "Language-Team: WP-Opas \n" 9 | "Language: fi\n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | "X-Poedit-KeywordsList: __;_;gettext;gettext_noop\n" 14 | "X-Poedit-Basepath: .\n" 15 | "X-Generator: Poedit 1.5.5\n" 16 | "X-Poedit-SearchPath-0: ..\n" 17 | 18 | #: ../wp-varnish.php:194 19 | msgid "WP-Varnish Configuration" 20 | msgstr "WP-Varnish asetukset" 21 | 22 | #: ../wp-varnish.php:257 23 | msgid "Settings Saved!" 24 | msgstr "Asetukset tallennettu!" 25 | 26 | #: ../wp-varnish.php:259 27 | msgid "You do not have the privileges." 28 | msgstr "Sinulla ei ole tarvittavia oikeuksia." 29 | 30 | #: ../wp-varnish.php:271 31 | msgid "WordPress Varnish Administration" 32 | msgstr "WordPress Varnish ylläpito" 33 | 34 | #: ../wp-varnish.php:272 35 | msgid "IP address and port configuration" 36 | msgstr "IP-osoite ja portti asetukset" 37 | 38 | #: ../wp-varnish.php:279 39 | msgid "" 40 | "These values can't be edited since there's a global configuration located in " 41 | "wp-config.php. If you want to change these settings, please update " 42 | "the file or contact the administrator." 43 | msgstr "" 44 | "Näitä arvoja ei voi muokata, koska wp-config.php tiedostossa on " 45 | "globaali asetus. Jos haluat muuttaa asetuksia, muokkaa tiedostoa tai ota " 46 | "yhteyttä ylläpitäjään." 47 | 48 | #: ../wp-varnish.php:282 49 | msgid "Current configuration:" 50 | msgstr "Nykyiset asetukset:" 51 | 52 | #: ../wp-varnish.php:285 53 | msgid "Version: " 54 | msgstr "Versio:" 55 | 56 | #: ../wp-varnish.php:288 57 | msgid "Server: " 58 | msgstr "Palvelin :" 59 | 60 | #: ../wp-varnish.php:288 61 | msgid "Port: " 62 | msgstr "Portti:" 63 | 64 | #: ../wp-varnish.php:298 65 | msgid "Varnish Administration IP Address" 66 | msgstr "Varnish ylläpidon IP-osoite" 67 | 68 | #: ../wp-varnish.php:299 69 | msgid "Varnish Administration Port" 70 | msgstr "Varnish ylläpidon portti" 71 | 72 | #: ../wp-varnish.php:300 73 | msgid "Varnish Secret" 74 | msgstr "Varnish salaisuus" 75 | 76 | #: ../wp-varnish.php:319 77 | msgid "Add one more server" 78 | msgstr "Lisää uusi palvelin" 79 | 80 | #: ../wp-varnish.php:325 81 | msgid "Timeout" 82 | msgstr "Aikakatkaisu" 83 | 84 | #: ../wp-varnish.php:325 85 | msgid "seconds" 86 | msgstr "sekuntia" 87 | 88 | #: ../wp-varnish.php:327 89 | msgid "Use admin port instead of PURGE method." 90 | msgstr "Käytä ylläpidon porttia PURGE-metodin sijaan." 91 | 92 | #: ../wp-varnish.php:329 93 | msgid "" 94 | "Also purge all page navigation (experimental, use carefully, it will include " 95 | "a bit more load on varnish servers.)" 96 | msgstr "" 97 | "Tyhjennä myös kaikki sivuvalikot (kokeellinen, käytä harkiten, aiheuttaa " 98 | "hieman enemmän kuormaa Varnish-palvelimille)" 99 | 100 | #: ../wp-varnish.php:331 101 | msgid "" 102 | "Also purge all comment navigation (experimental, use carefully, it will " 103 | "include a bit more load on varnish servers.)" 104 | msgstr "" 105 | "Tyhjennä myös kaikki kommenttivalikot (kokeellinen, käytä harkiten, " 106 | "aiheuttaa hieman enemmän kuormaa Varnish-palvelimille)" 107 | 108 | #: ../wp-varnish.php:333 109 | msgid "Varnish Version" 110 | msgstr "Varnish versio" 111 | 112 | #: ../wp-varnish.php:335 113 | msgid "Save Changes" 114 | msgstr "Tallenna muutokset" 115 | 116 | #: ../wp-varnish.php:338 117 | msgid "Purge a URL" 118 | msgstr "Tyhjennä URL" 119 | 120 | #: ../wp-varnish.php:339 121 | msgid "Purge" 122 | msgstr "Tyhjennä" 123 | 124 | #: ../wp-varnish.php:342 125 | msgid "Purge All Blog Cache" 126 | msgstr "Tyhjennä koko sivuston välimuisti" 127 | 128 | #: ../wp-varnish.php:342 129 | msgid "" 130 | "Use only if necessary, and carefully as this will include a bit more load on " 131 | "varnish servers." 132 | msgstr "" 133 | "Käytä vain jos pakko, sillä tämä aiheuttaa enemmän kuormaa Varnish-" 134 | "palvelimille." 135 | -------------------------------------------------------------------------------- /lang/wp-varnish-fr_FR.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkhamre/wp-varnish/f526bbcfd639ff954a2323f2b1c92e2b5e51a00b/lang/wp-varnish-fr_FR.mo -------------------------------------------------------------------------------- /lang/wp-varnish-fr_FR.po: -------------------------------------------------------------------------------- 1 | msgid "" 2 | msgstr "" 3 | "Project-Id-Version: WP-Varnish 0.3\n" 4 | "Report-Msgid-Bugs-To: \n" 5 | "POT-Creation-Date: 2010-08-04 17:15-0300\n" 6 | "PO-Revision-Date: 2012-02-06 08:52+0100\n" 7 | "Last-Translator: Loïc Gerbaud \n" 8 | "Language-Team: Loïc Gerbaud \n" 9 | "Language: \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | "X-Poedit-KeywordsList: __;_;gettext;gettext_noop\n" 14 | "X-Poedit-Basepath: .\n" 15 | "X-Poedit-Language: French\n" 16 | "X-Poedit-Country: FRANCE\n" 17 | "X-Poedit-SearchPath-0: ..\n" 18 | 19 | #: ../wp-varnish.php:152 20 | msgid "WP-Varnish Configuration" 21 | msgstr "Configuration de WP-Varnish" 22 | 23 | #: ../wp-varnish.php:202 24 | msgid "Settings Saved!" 25 | msgstr "Réglages enregistrés !" 26 | 27 | #: ../wp-varnish.php:204 28 | msgid "You do not have the privileges." 29 | msgstr "Vous n'avez pas les privilèges suffisants." 30 | 31 | #: ../wp-varnish.php:215 32 | msgid "WordPress Varnish Administration" 33 | msgstr "Administration WordPress de Varnish" 34 | 35 | #: ../wp-varnish.php:216 36 | msgid "IP address and port configuration" 37 | msgstr "Configuration de l'adresse IP et du port" 38 | 39 | #: ../wp-varnish.php:222 40 | msgid "These values can't be edited since there's a global configuration located in wp-config.php. If you want to change these settings, please update the file or contact the administrator." 41 | msgstr "Ces valeurs peuvent être modifiées directement grâce à une configuration globale placée dans wp-config.php. Si vous souhaitez changer ces réglages, mettez ce fichier à jour ou contactez l'administrateur." 42 | 43 | #: ../wp-varnish.php:225 44 | msgid "Current configuration:" 45 | msgstr "Configuration actuelle :" 46 | 47 | #: ../wp-varnish.php:229 48 | msgid "Server: " 49 | msgstr "Serveur :" 50 | 51 | #: ../wp-varnish.php:229 52 | msgid "Port: " 53 | msgstr "Port :" 54 | 55 | #: ../wp-varnish.php:239 56 | msgid "Varnish Administration IP Address" 57 | msgstr "IP d'administration de Varnish" 58 | 59 | #: ../wp-varnish.php:240 60 | msgid "Varnish Administration Port" 61 | msgstr "Port d'administration de Varnish" 62 | 63 | #: ../wp-varnish.php:241 64 | msgid "Varnish Secret" 65 | msgstr "Clé \"secret\" de Varnish" 66 | 67 | #: ../wp-varnish.php:260 68 | msgid "Add one more server" 69 | msgstr "Ajouter un autre serveur" 70 | 71 | #: ../wp-varnish.php:266 72 | msgid "Timeout" 73 | msgstr "Timeout" 74 | 75 | #: ../wp-varnish.php:266 76 | msgid "seconds" 77 | msgstr "secondes" 78 | 79 | #: ../wp-varnish.php:268 80 | msgid "Use admin port instead of PURGE method." 81 | msgstr "Utiliser le port admin au lieu de la méthode PURGE." 82 | 83 | #: ../wp-varnish.php:270 84 | msgid "Also purge all page navigation (experimental, use carefully, it will include a bit more load on varnish servers.)" 85 | msgstr "Purger également toute la navigation des pages (expérimental, utilisez avec prudence, ceci implique plus de charge sur les serveurs varnish)." 86 | 87 | #: ../wp-varnish.php:272 88 | msgid "Also purge all comment navigation (experimental, use carefully, it will include a bit more load on varnish servers.)" 89 | msgstr "Purger également toute la navigation des commentaires (expérimental, utilisez avec prudence, ceci implique plus de charge sur les serveurs varnish)." 90 | 91 | #: ../wp-varnish.php:274 92 | msgid "Save Changes" 93 | msgstr "Enregistrer les changements" 94 | 95 | #: ../wp-varnish.php:276 96 | msgid "Purge All Blog Cache" 97 | msgstr "Purger tout le cache du blog" 98 | 99 | #: ../wp-varnish.php:276 100 | msgid "Use only if necessary, and carefully as this will include a bit more load on varnish servers." 101 | msgstr "Utilisez seulement si c'est nécessaire, et avec prudence car ceci implique plus de charge sur les serveurs varnish." 102 | 103 | -------------------------------------------------------------------------------- /lang/wp-varnish-pt_BR.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkhamre/wp-varnish/f526bbcfd639ff954a2323f2b1c92e2b5e51a00b/lang/wp-varnish-pt_BR.mo -------------------------------------------------------------------------------- /lang/wp-varnish-pt_BR.po: -------------------------------------------------------------------------------- 1 | msgid "" 2 | msgstr "" 3 | "Project-Id-Version: WP-Varnish 0.3\n" 4 | "Report-Msgid-Bugs-To: \n" 5 | "POT-Creation-Date: 2010-08-04 17:15-0300\n" 6 | "PO-Revision-Date: 2010-10-20 23:30-0300\n" 7 | "Last-Translator: Wilson Felipe \n" 8 | "Language-Team: Hugo Cisneiros , Wilson Felipe " 9 | "\n" 10 | "Language: \n" 11 | "MIME-Version: 1.0\n" 12 | "Content-Type: text/plain; charset=UTF-8\n" 13 | "Content-Transfer-Encoding: 8bit\n" 14 | "X-Poedit-KeywordsList: __;_;gettext;gettext_noop\n" 15 | "X-Poedit-Basepath: .\n" 16 | "X-Poedit-Language: Portuguese\n" 17 | "X-Poedit-Country: BRAZIL\n" 18 | "X-Poedit-SourceCharset: utf-8\n" 19 | "X-Poedit-SearchPath-0: wp-varnish\n" 20 | 21 | #: ../wp-varnish.php:152 22 | msgid "WP-Varnish Configuration" 23 | msgstr "Configuração do WP-Varnish" 24 | 25 | #: ../wp-varnish.php:202 26 | msgid "Settings Saved!" 27 | msgstr "Configurações Salvas!" 28 | 29 | #: ../wp-varnish.php:204 30 | msgid "You do not have the privileges." 31 | msgstr "Você não tem os privilégios necessários." 32 | 33 | #: ../wp-varnish.php:215 34 | msgid "WordPress Varnish Administration" 35 | msgstr "Administração do Varnish no WordPress" 36 | 37 | #: ../wp-varnish.php:216 38 | msgid "IP address and port configuration" 39 | msgstr "Configuração de Endereços IP e Portas" 40 | 41 | #: ../wp-varnish.php:222 42 | msgid "" 43 | "These values can't be edited since there's a global configuration located in " 44 | "wp-config.php. If you want to change these settings, please update " 45 | "the file or contact the administrator." 46 | msgstr "" 47 | "Estes valores não podem ser editados, pois existe uma configuração global " 48 | "localizada no wp-config.php. Se você deseja mudar estas " 49 | "configurações, por favor atualize o arquivo ou entre em contato com o " 50 | "administrador." 51 | 52 | #: ../wp-varnish.php:225 53 | msgid "Current configuration:" 54 | msgstr "Configuração atual:" 55 | 56 | #: ../wp-varnish.php:229 57 | msgid "Server: " 58 | msgstr "Servidor: " 59 | 60 | #: ../wp-varnish.php:229 61 | msgid "Port: " 62 | msgstr "Porta: " 63 | 64 | #: ../wp-varnish.php:239 65 | msgid "Varnish Administration IP Address" 66 | msgstr "Endereço IP de Administração do Varnish" 67 | 68 | #: ../wp-varnish.php:240 69 | msgid "Varnish Administration Port" 70 | msgstr "Porta de Administração do Varnish" 71 | 72 | #: ../wp-varnish.php:241 73 | msgid "Varnish Secret" 74 | msgstr "Varnish Secret" 75 | 76 | #: ../wp-varnish.php:260 77 | msgid "Add one more server" 78 | msgstr "Adicionar mais um servidor" 79 | 80 | #: ../wp-varnish.php:266 81 | msgid "Timeout" 82 | msgstr "Timeout" 83 | 84 | #: ../wp-varnish.php:266 85 | msgid "seconds" 86 | msgstr "segundos" 87 | 88 | #: ../wp-varnish.php:268 89 | msgid "Use admin port instead of PURGE method." 90 | msgstr "Usar porta de administração ao invés do método PURGE." 91 | 92 | #: ../wp-varnish.php:270 93 | msgid "" 94 | "Also purge all page navigation (experimental, use carefully, it will include " 95 | "a bit more load on varnish servers.)" 96 | msgstr "" 97 | "Também limpar o cache de toda navegação de páginas (experimental, use com " 98 | "cuidado, irá colocar um pouco de carga a mais nos servidores varnish.)" 99 | 100 | #: ../wp-varnish.php:272 101 | msgid "" 102 | "Also purge all comment navigation (experimental, use carefully, it will " 103 | "include a bit more load on varnish servers.)" 104 | msgstr "" 105 | "Também limpar o cache de toda navegação de comentários (experimental, use " 106 | "com cuidado, irá colocar um pouco de carga a mais nos servidores varnish.)" 107 | 108 | #: ../wp-varnish.php:274 109 | msgid "Save Changes" 110 | msgstr "Salvar Mudanças" 111 | 112 | #: ../wp-varnish.php:276 113 | msgid "Purge All Blog Cache" 114 | msgstr "Limpar Todo Cache do Blog" 115 | 116 | #: ../wp-varnish.php:276 117 | msgid "" 118 | "Use only if necessary, and carefully as this will include a bit more load on " 119 | "varnish servers." 120 | msgstr "" 121 | "Use apenas se necessário, e com cuidado pois irá colocar um pouco de carga a " 122 | "mais nos servidores varnish." 123 | -------------------------------------------------------------------------------- /lang/wp-varnish.pot: -------------------------------------------------------------------------------- 1 | msgid "" 2 | msgstr "" 3 | "Project-Id-Version: WP-Varnish 0.3\n" 4 | "Report-Msgid-Bugs-To: \n" 5 | "POT-Creation-Date: 2010-08-04 17:15-0300\n" 6 | "PO-Revision-Date: 2010-10-20 23:33-0200\n" 7 | "Last-Translator: Wilson Felipe \n" 8 | "Language-Team: Hugo Cisneiros , Wilson Felipe " 9 | "\n" 10 | "Language: \n" 11 | "MIME-Version: 1.0\n" 12 | "Content-Type: text/plain; charset=UTF-8\n" 13 | "Content-Transfer-Encoding: 8bit\n" 14 | "X-Poedit-KeywordsList: __;_;gettext;gettext_noop\n" 15 | "X-Poedit-Basepath: .\n" 16 | "X-Poedit-SearchPath-0: ..\n" 17 | 18 | #: ../wp-varnish.php:152 19 | msgid "WP-Varnish Configuration" 20 | msgstr "" 21 | 22 | #: ../wp-varnish.php:202 23 | msgid "Settings Saved!" 24 | msgstr "" 25 | 26 | #: ../wp-varnish.php:204 27 | msgid "You do not have the privileges." 28 | msgstr "" 29 | 30 | #: ../wp-varnish.php:215 31 | msgid "WordPress Varnish Administration" 32 | msgstr "" 33 | 34 | #: ../wp-varnish.php:216 35 | msgid "IP address and port configuration" 36 | msgstr "" 37 | 38 | #: ../wp-varnish.php:222 39 | msgid "" 40 | "These values can't be edited since there's a global configuration located in " 41 | "wp-config.php. If you want to change these settings, please update " 42 | "the file or contact the administrator." 43 | msgstr "" 44 | 45 | #: ../wp-varnish.php:225 46 | msgid "Current configuration:" 47 | msgstr "" 48 | 49 | #: ../wp-varnish.php:229 50 | msgid "Server: " 51 | msgstr "" 52 | 53 | #: ../wp-varnish.php:229 54 | msgid "Port: " 55 | msgstr "" 56 | 57 | #: ../wp-varnish.php:239 58 | msgid "Varnish Administration IP Address" 59 | msgstr "" 60 | 61 | #: ../wp-varnish.php:240 62 | msgid "Varnish Administration Port" 63 | msgstr "" 64 | 65 | #: ../wp-varnish.php:241 66 | msgid "Varnish Secret" 67 | msgstr "" 68 | 69 | #: ../wp-varnish.php:260 70 | msgid "Add one more server" 71 | msgstr "" 72 | 73 | #: ../wp-varnish.php:266 74 | msgid "Timeout" 75 | msgstr "" 76 | 77 | #: ../wp-varnish.php:266 78 | msgid "seconds" 79 | msgstr "" 80 | 81 | #: ../wp-varnish.php:268 82 | msgid "Use admin port instead of PURGE method." 83 | msgstr "" 84 | 85 | #: ../wp-varnish.php:270 86 | msgid "" 87 | "Also purge all page navigation (experimental, use carefully, it will include " 88 | "a bit more load on varnish servers.)" 89 | msgstr "" 90 | 91 | #: ../wp-varnish.php:272 92 | msgid "" 93 | "Also purge all comment navigation (experimental, use carefully, it will " 94 | "include a bit more load on varnish servers.)" 95 | msgstr "" 96 | 97 | #: ../wp-varnish.php:274 98 | msgid "Save Changes" 99 | msgstr "" 100 | 101 | #: ../wp-varnish.php:276 102 | msgid "Purge All Blog Cache" 103 | msgstr "" 104 | 105 | #: ../wp-varnish.php:276 106 | msgid "" 107 | "Use only if necessary, and carefully as this will include a bit more load on " 108 | "varnish servers." 109 | msgstr "" 110 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === WordPress Varnish === 2 | Contributors: pkhamre, wfelipe, eitch 3 | Donate link: http://github.com/pkhamre/wp-varnish 4 | Tags: cache, caching, performance, varnish, purge, speed 5 | Requires at least: 2.9.2 6 | * Tested up to: 3.6.1 7 | * Stable tag: 0.8 8 | 9 | WordPress Varnish is a simple plugin that purges new and edited content. 10 | 11 | == Description == 12 | 13 | This plugin purges your varnish cache when content is added or edited. This 14 | includes when a new post is added, a post is updated or when a comment is 15 | posted to your blog. 16 | 17 | To keep widgets like "Recent posts", "Recent comments" and such up to date, 18 | you should consider using ESI and include them through a text widget for 19 | arbitrary text or HTML. 20 | 21 | == Installation == 22 | 23 | This section describes how to install the plugin and get it working. 24 | 25 | 1. Install the plugin 'WordPress Varnish' through the 'Plugins' menu in 26 | WordPress 27 | 2. Activate the plugin through the 'Plugins' menu in WordPress 28 | 29 | == Frequently Asked Questions == 30 | 31 | = Does this just work? = 32 | 33 | Yes. 34 | 35 | = But how should my varnish configuration file look like? = 36 | 37 | I have provided a simple VCL that can be used as a reference. 38 | 39 | = Does it work for Multi-Site (or WPMU)? = 40 | 41 | Yes. Activating the plugin site-wide will provide the functionality to all 42 | blogs. Configuration can be done on the blogs individually, or can be global. 43 | If you want to configure Varnish servers globally, edit wp-config.php and 44 | include these lines just before "That's all, stop editing!" message: 45 | 46 | global $varnish_servers; 47 | $varnish_servers = array('192.168.0.1:80:secret','192.168.0.2:80:secret'); 48 | define('VARNISH_SHOWCFG',1); 49 | 50 | The varnish servers array will configure multiple servers for sending the 51 | purges. If VARNISH_SHOWCFG is defined, configuration will be shown to all 52 | users who access the plugin configuration page (but they can't edit it). 53 | 54 | = My Plugins are seeing the Varnish server's IP rather than the websurfer IP = 55 | 56 | You could install Apache's mod_rpaf module: http://stderr.net/apache/rpaf/ 57 | 58 | or, in wp-config.php, near the top, put the following code: 59 | 60 | $temp_ip = explode(',', isset($_SERVER['HTTP_X_FORWARDED_FOR']) 61 | ? $_SERVER['HTTP_X_FORWARDED_FOR'] : 62 | (isset($_SERVER['HTTP_CLIENT_IP']) ? 63 | $_SERVER['HTTP_CLIENT_IP'] : $_SERVER['REMOTE_ADDR'])); 64 | $remote_addr = trim($temp_ip[0]); 65 | $_SERVER['REMOTE_ADDR'] = preg_replace('/[^0-9.:]/', '', $remote_addr ); 66 | 67 | The code takes some of the common headers and replaces the REMOTE_ADDR 68 | variable, allowing plugins that use the surfer's IP address to see the 69 | surfer's IP rather than the server's IP. 70 | 71 | == Screenshots == 72 | 73 | 1. Screenshot of the administration interface. 74 | 75 | == Changelog == 76 | 77 | = 0.8 = 78 | * Added secret handling to WPVarnishPurgeObject, Thanks Kit Westneat 79 | * Change WPVarnishPurgePurgeCommonObjects to WPVarnishPurgeCommonObjects, 80 | Thanks kitwestneat 81 | * added @ to supress Undefined offset notice 82 | * minor doc changes 83 | 84 | = 0.7 = 85 | * Added purge when post changes from future to publish, Thanks Marcin Pietrzak 86 | * Added purge when theme switched, Thanks dupuis 87 | * Fixed multisite domain mapping purge, Thanks jasonheffner 88 | * Updated Finnish Translation, Thanks timoleinio 89 | 90 | = 0.6 = 91 | * Removed plugins_loaded action as it doesnt do what was expected re: Issue 92 | #12. Thank you Ben Favre, Pothi Kalimuthu and allinwonder 93 | 94 | = 0.5 = 95 | * New .vcl to fix purge as per Issue #39, Thanks Ed Cooper 96 | 97 | = 0.4 = 98 | 99 | * added rule to skip caching 404s in vcl 100 | * WordPress-Varnish UserAgent as per ticket #23 101 | * document use of mod_rpaf or code fix for remote IP, Ticket #36 102 | * clean data rather than reject to fix ticket #31 103 | * added plugins_loaded hook to fix ticket #12 104 | 105 | = 0.3 = 106 | * Added internationalization code. Included pt_BR translation. 107 | * Support to Multi-Site and WPMU with global configuration. 108 | * Fix on URL purges for multiple domains and blogs on sub-directories. 109 | * Code clean up on some functions. 110 | * Added configuration for purging all blog, page and comments navigation. 111 | 112 | = 0.2 = 113 | * Added multiple servers support and timeout configuration. 114 | 115 | = 0.1 = 116 | * Initial release. 117 | 118 | == Upgrade Notice == 119 | 120 | = 0.8 = 121 | * Added secret handling to WPVarnishPurgeObject, Thanks Kit Westneat 122 | 123 | = 0.7 = 124 | * Added purge when post changes from future to publish, Thanks Marcin Pietrzak 125 | * Added purge when theme switched, Thanks dupuis 126 | 127 | = 0.6 = 128 | * Removed plugins_loaded action as it doesnt do what was expected re: Issue 129 | #12. Thank you Ben Favre, Pothi Kalimuthu and allinwonder 130 | 131 | = 0.5 = 132 | * New .vcl to fix purge as per Issue #39, Thanks Ed Cooper 133 | 134 | = 0.3 = 135 | * Varnish PURGE configuration must support regex. wp-varnish will 136 | sometimes request with regex for special purges like refreshing 137 | all blog cache and refreshing comments. 138 | -------------------------------------------------------------------------------- /screenshot-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkhamre/wp-varnish/f526bbcfd639ff954a2323f2b1c92e2b5e51a00b/screenshot-1.png -------------------------------------------------------------------------------- /todo.txt: -------------------------------------------------------------------------------- 1 | * WPVarnishPurgeObject does not quite work yet. Fix. 2 | * Make the administration interface actually work. 3 | * Purge on other hooks. 4 | -------------------------------------------------------------------------------- /uninstall.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /wordpress.vcl: -------------------------------------------------------------------------------- 1 | backend default { 2 | .host = "127.0.0.1"; 3 | .port = "8080"; 4 | } 5 | 6 | acl purge { 7 | "localhost"; 8 | } 9 | 10 | sub vcl_recv { 11 | if (req.request == "BAN") { 12 | if(!client.ip ~ purge) { 13 | error 405 "Not allowed."; 14 | } 15 | ban("req.url ~ "+req.url+" && req.http.host == "+req.http.host); 16 | error 200 "Banned."; 17 | } 18 | 19 | if (req.request != "GET" && 20 | req.request != "HEAD" && 21 | req.request != "PUT" && 22 | req.request != "POST" && 23 | req.request != "TRACE" && 24 | req.request != "OPTIONS" && 25 | req.request != "DELETE") { 26 | return (pipe); 27 | } 28 | 29 | if (req.request != "GET" && req.request != "HEAD") { 30 | return (pass); 31 | } 32 | 33 | if (req.url ~ "wp-(login|admin)" || req.url ~ "preview=true") { 34 | return (pass); 35 | } 36 | 37 | remove req.http.cookie; 38 | return (lookup); 39 | } 40 | 41 | sub vcl_fetch { 42 | if (beresp.status == 404) { 43 | set beresp.ttl = 0m; 44 | return(hit_for_pass); 45 | } 46 | if (req.url ~ "wp-(login|admin)" || req.url ~ "preview=true") { 47 | return (hit_for_pass); 48 | } 49 | 50 | set beresp.ttl = 24h; 51 | return (deliver); 52 | } 53 | -------------------------------------------------------------------------------- /wp-varnish.js: -------------------------------------------------------------------------------- 1 | // our id for our rows, we can have rows added and removed 2 | // so we need a base 3 | var rowCount = 0; 4 | 5 | function createRow(tableID, id, addr, port, secret) { 6 | var row = document.createElement ('tr'); 7 | var td1 = document.createElement ('td'); 8 | var td2 = document.createElement ('td'); 9 | var td3 = document.createElement ('td'); 10 | var td4 = document.createElement ('td'); 11 | var wpv_addr = document.createElement ('input'); 12 | var wpv_port = document.createElement ('input'); 13 | var wpv_secret = document.createElement ('input'); 14 | var dRow = document.createElement ('input'); 15 | 16 | wpv_addr.className = "regular-text"; 17 | wpv_addr.type = "text"; 18 | wpv_addr.id = id; 19 | wpv_addr.name = "wpvarnish_addr[]"; 20 | wpv_addr.value = addr || ""; 21 | 22 | wpv_port.className = "small-text"; 23 | wpv_port.type = "text"; 24 | wpv_port.id = id; 25 | wpv_port.name = "wpvarnish_port[]"; 26 | wpv_port.value = port || ""; 27 | 28 | wpv_secret.className = "regular-text"; 29 | wpv_secret.type = "text"; 30 | wpv_secret.id = id; 31 | wpv_secret.name = "wpvarnish_secret[]"; 32 | wpv_secret.value = secret || ""; 33 | 34 | dRow.className = ""; 35 | dRow.type = "button"; 36 | dRow.name = "deleteRow"; 37 | dRow.value = "-"; 38 | dRow.id = id; 39 | dRow.onclick = function () { deleteRow(tableID, id); } 40 | 41 | td1.appendChild (wpv_addr); 42 | td2.appendChild (wpv_port); 43 | td3.appendChild (wpv_secret); 44 | td4.appendChild (dRow); 45 | row.appendChild (td1); 46 | row.appendChild (td2); 47 | row.appendChild (td3); 48 | row.appendChild (td4); 49 | 50 | return row; 51 | } 52 | 53 | function addRow(tableID, id, addr, port, secret) { 54 | var tbody = document.getElementById(tableID).getElementsByTagName ('tbody')[0]; 55 | 56 | rowCount++; 57 | var row = createRow(tableID, id, addr, port, secret); 58 | 59 | tbody.appendChild (row); 60 | } 61 | 62 | function deleteRow(tableID, rowID) { 63 | try { 64 | var tbody = document.getElementById(tableID).getElementsByTagName ('tbody')[0]; 65 | var trs = tbody.getElementsByTagName ('tr'); 66 | 67 | // the id = 0 we don't want to remove, as it is the header 68 | for (var i = 1; i < trs.length; i++) { 69 | // we use our own id, let's not mix up with table ids 70 | var id = (trs[i].getElementsByTagName ('input')[0]).id; 71 | if (id == rowID) { 72 | tbody.deleteRow (i); 73 | return; 74 | } 75 | } 76 | } catch(e) { 77 | alert(e); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /wp-varnish.php: -------------------------------------------------------------------------------- 1 | Pål-Kristian Hamre 7 | Description: A plugin for purging Varnish cache when content is published or edited. 8 | 9 | Copyright 2010 Pål-Kristian Hamre (email : post_at_pkhamre_dot_com) 10 | 11 | This program is free software; you can redistribute it and/or modify 12 | it under the terms of the GNU General Public License as published by 13 | the Free Software Foundation; either version 2 of the License, or 14 | (at your option) any later version. 15 | 16 | This program is distributed in the hope that it will be useful, 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | GNU General Public License for more details. 20 | 21 | You should have received a copy of the GNU General Public License 22 | along with this program; if not, write to the Free Software 23 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 24 | */ 25 | 26 | class WPVarnish { 27 | public $wpv_addr_optname; 28 | public $wpv_port_optname; 29 | public $wpv_secret_optname; 30 | public $wpv_timeout_optname; 31 | public $wpv_update_pagenavi_optname; 32 | public $wpv_update_commentnavi_optname; 33 | public $wpv_purgeactions; 34 | 35 | function WPVarnish() { 36 | global $post; 37 | 38 | $this->wpv_addr_optname = "wpvarnish_addr"; 39 | $this->wpv_port_optname = "wpvarnish_port"; 40 | $this->wpv_secret_optname = "wpvarnish_secret"; 41 | $this->wpv_timeout_optname = "wpvarnish_timeout"; 42 | $this->wpvarnish_purge_url_optname = "wpvarnish_purge_url"; 43 | $this->wpv_update_pagenavi_optname = "wpvarnish_update_pagenavi"; 44 | $this->wpv_update_commentnavi_optname = "wpvarnish_update_commentnavi"; 45 | $this->wpv_use_adminport_optname = "wpvarnish_use_adminport"; 46 | $this->wpv_vversion_optname = "wpvarnish_vversion"; 47 | $this->wpv_purgeactions = array(); 48 | $wpv_addr_optval = array ("127.0.0.1"); 49 | $wpv_port_optval = array (80); 50 | $wpv_secret_optval = array (""); 51 | $wpv_timeout_optval = 5; 52 | $wpv_update_pagenavi_optval = 0; 53 | $wpv_update_commentnavi_optval = 0; 54 | $wpv_use_adminport_optval = 0; 55 | $wpv_vversion_optval = 2; 56 | 57 | if ( (get_option($this->wpv_addr_optname) == FALSE) ) { 58 | add_option($this->wpv_addr_optname, $wpv_addr_optval, '', 'yes'); 59 | } 60 | 61 | if ( (get_option($this->wpv_port_optname) == FALSE) ) { 62 | add_option($this->wpv_port_optname, $wpv_port_optval, '', 'yes'); 63 | } 64 | 65 | if ( (get_option($this->wpv_secret_optname) == FALSE) ) { 66 | add_option($this->wpv_secret_optname, $wpv_secret_optval, '', 'yes'); 67 | } 68 | 69 | if ( (get_option($this->wpv_timeout_optname) == FALSE) ) { 70 | add_option($this->wpv_timeout_optname, $wpv_timeout_optval, '', 'yes'); 71 | } 72 | 73 | if ( (get_option($this->wpv_update_pagenavi_optname) == FALSE) ) { 74 | add_option($this->wpv_update_pagenavi_optname, $wpv_update_pagenavi_optval, '', 'yes'); 75 | } 76 | 77 | if ( (get_option($this->wpv_update_commentnavi_optname) == FALSE) ) { 78 | add_option($this->wpv_update_commentnavi_optname, $wpv_update_commentnavi_optval, '', 'yes'); 79 | } 80 | 81 | if ( (get_option($this->wpv_use_adminport_optname) == FALSE) ) { 82 | add_option($this->wpv_use_adminport_optname, $wpv_use_adminport_optval, '', 'yes'); 83 | } 84 | 85 | if ( (get_option($this->wpv_vversion_optname) == FALSE) ) { 86 | add_option($this->wpv_vversion_optname, $wpv_vversion_optval, '', 'yes'); 87 | } 88 | 89 | // Localization init 90 | add_action('init', array($this, 'WPVarnishLocalization')); 91 | 92 | // Add Administration Interface 93 | add_action('admin_menu', array($this, 'WPVarnishAdminMenu')); 94 | 95 | // Add Purge Links to Admin Bar 96 | add_action('admin_bar_menu', array($this, 'WPVarnishAdminBarLinks'), 100); 97 | 98 | // When posts/pages are published, edited or deleted 99 | // 'edit_post' is not used as it is also executed when a comment is changed, 100 | // causing the plugin to purge several URLs (WPVarnishPurgeCommonObjects) 101 | // that do not need purging. 102 | 103 | // When a post or custom post type is published, or if it is edited and its status is "published". 104 | add_action('publish_post', array($this, 'WPVarnishPurgePost'), 99); 105 | add_action('publish_post', array($this, 'WPVarnishPurgeCommonObjects'), 99); 106 | // When a page is published, or if it is edited and its status is "published". 107 | add_action('publish_page', array($this, 'WPVarnishPurgePost'), 99); 108 | add_action('publish_page', array($this, 'WPVarnishPurgeCommonObjects'), 99); 109 | // When an attachment is updated. 110 | add_action('edit_attachment', array($this, 'WPVarnishPurgePost'), 99); 111 | add_action('edit_attachment', array($this, 'WPVarnishPurgeCommonObjects'), 99); 112 | // Runs just after a post is added via email. 113 | add_action('publish_phone', array($this, 'WPVarnishPurgePost'), 99); 114 | add_action('publish_phone', array($this, 'WPVarnishPurgeCommonObjects'), 99); 115 | // Runs when a post is published via XMLRPC request, or if it is edited via XMLRPC and its status is "published". 116 | add_action('xmlrpc_publish_post', array($this, 'WPVarnishPurgePost'), 99); 117 | add_action('xmlrpc_publish_post', array($this, 'WPVarnishPurgeCommonObjects'), 99); 118 | // Runs when a future post or page is published. 119 | add_action('publish_future_post', array($this, 'WPVarnishPurgePost'), 99); 120 | add_action('publish_future_post', array($this, 'WPVarnishPurgeCommonObjects'), 99); 121 | // When post status is changed 122 | add_action('transition_post_status', array($this, 'WPVarnishPurgePostStatus'), 99, 3); 123 | add_action('transition_post_status', array($this, 'WPVarnishPurgeCommonObjectsStatus'), 99, 3); 124 | // When posts, pages, attachments are deleted 125 | add_action('deleted_post', array($this, 'WPVarnishPurgePost'), 99); 126 | add_action('deleted_post', array($this, 'WPVarnishPurgeCommonObjects'), 99); 127 | 128 | // When comments are made, edited or deleted 129 | // See: http://codex.wordpress.org/Plugin_API/Action_Reference#Comment.2C_Ping.2C_and_Trackback_Actions 130 | add_action('comment_post', array($this, 'WPVarnishPurgePostComments'),99); 131 | add_action('edit_comment', array($this, 'WPVarnishPurgePostComments'),99); 132 | add_action('deleted_comment', array($this, 'WPVarnishPurgePostComments'),99); 133 | add_action('trashed_comment', array($this, 'WPVarnishPurgePostComments'),99); 134 | add_action('pingback_post', array($this, 'WPVarnishPurgePostComments'),99); 135 | add_action('trackback_post', array($this, 'WPVarnishPurgePostComments'),99); 136 | add_action('wp_set_comment_status', array($this, 'WPVarnishPurgePostCommentsStatus'),99); 137 | 138 | // When Theme is changed, Thanks dupuis 139 | add_action('switch_theme',array($this, 'WPVarnishPurgeAll'), 99); 140 | 141 | // When a new plugin is loaded 142 | // this was added due to Issue #12, but, doesn't do what was intended 143 | // commenting this out gets rid of the incessant purging. 144 | //add_action('plugins_loaded',array($this, 'WPVarnishPurgeAll'), 99); 145 | 146 | // Do the actual purges only on shutdown to ensure a single URL is only purged once. IOK 2016-01-20 147 | add_action('shutdown',array($this,'WPVarnishPurgeOnExit'),99); 148 | } 149 | 150 | function WPVarnishLocalization() { 151 | load_plugin_textdomain('wp-varnish', false, dirname(plugin_basename( __FILE__ ) ) . '/lang/'); 152 | } 153 | 154 | // WPVarnishPurgeAll - Using a regex, clear all blog cache. Use carefully. 155 | function WPVarnishPurgeAll() { 156 | $this->WPVarnishPurgeObject('/.*'); 157 | } 158 | 159 | // WPVarnishPurgeURL - Using a URL, clear the cache 160 | function WPVarnishPurgeURL($wpv_purl) { 161 | $wpv_purl = preg_replace( '#^https?://[^/]+#i', '', $wpv_purl ); 162 | $this->WPVarnishPurgeObject($wpv_purl); 163 | } 164 | 165 | //wrapper on WPVarnishPurgeCommonObjects for transition_post_status 166 | function WPVarnishPurgeCommonObjectsStatus($old, $new, $post) { 167 | if ( $old != $new ) { 168 | if ( $old == 'publish' || $new == 'publish' ) { 169 | $this->WPVarnishPurgeCommonObjects($post->ID); 170 | } 171 | } 172 | } 173 | 174 | // Purge related objects 175 | function WPVarnishPurgeCommonObjects($post_id) { 176 | 177 | $post = get_post($post_id); 178 | // We need a post object in order to generate the archive URLs which are 179 | // related to the post. We perform a few checks to make sure we have a 180 | // post object. 181 | if ( ! is_object($post) || ! isset($post->post_type) || ! in_array( get_post_type($post), array('post') ) ) { 182 | // Do nothing for pages, attachments. 183 | return; 184 | } 185 | 186 | // NOTE: Policy for archive purging 187 | // By default, only the first page of the archives is purged. If 188 | // 'wpv_update_pagenavi_optname' is checked, then all the pages of each 189 | // archive are purged. 190 | if ( get_option($this->wpv_update_pagenavi_optname) == 1 ) { 191 | // Purge all pages of the archive. 192 | $archive_pattern = '(?:page/[\d]+/)?$'; 193 | } else { 194 | // Only first page of the archive is purged. 195 | $archive_pattern = '$'; 196 | } 197 | 198 | // Front page (latest posts OR static front page) 199 | $this->WPVarnishPurgeObject( '/' . $archive_pattern ); 200 | 201 | // Static Posts page (Added only if a static page used as the 'posts page') 202 | if ( get_option('show_on_front', 'posts') == 'page' && intval(get_option('page_for_posts', 0)) > 0 ) { 203 | $posts_page_url = preg_replace( '#^https?://[^/]+#i', '', get_permalink(intval(get_option('page_for_posts'))) ); 204 | $this->WPVarnishPurgeObject( $posts_page_url . $archive_pattern ); 205 | } 206 | 207 | // Feeds 208 | $this->WPVarnishPurgeObject( '/feed/(?:(atom|rdf)/)?$' ); 209 | 210 | // Category, Tag, Author and Date Archives 211 | 212 | // We get the URLs of the category and tag archives, only for 213 | // those categories and tags which have been attached to the post. 214 | 215 | // Category Archive 216 | $category_slugs = array(); 217 | foreach( get_the_category($post->ID) as $cat ) { 218 | $category_slugs[] = $cat->slug; 219 | } 220 | if ( ! empty($category_slugs) ) { 221 | if ( count($category_slugs) > 1 ) { 222 | $cat_slug_pattern = '(' . implode('|', $category_slugs) . ')'; 223 | } else { 224 | $cat_slug_pattern = implode('', $category_slugs); 225 | } 226 | $this->WPVarnishPurgeObject( '/' . get_option('category_base', 'category') . '/' . $cat_slug_pattern . '/' . $archive_pattern ); 227 | } 228 | 229 | // Tag Archive 230 | $tag_slugs = array(); 231 | foreach( get_the_tags($post->ID) as $tag ) { 232 | $tag_slugs[] = $tag->slug; 233 | } 234 | if ( ! empty($tag_slugs) ) { 235 | if ( count($tag_slugs) > 1 ) { 236 | $tag_slug_pattern = '(' . implode('|', $tag_slugs) . ')'; 237 | } else { 238 | $tag_slug_pattern = implode('', $tag_slugs); 239 | } 240 | $this->WPVarnishPurgeObject( '/' . get_option('tag_base', 'tag') . '/' . $tag_slug_pattern . '/' . $archive_pattern ); 241 | } 242 | 243 | // Author Archive 244 | $author_archive_url = preg_replace('#^https?://[^/]+#i', '', get_author_posts_url($post->post_author) ); 245 | $this->WPVarnishPurgeObject( $author_archive_url . $archive_pattern ); 246 | 247 | // Date based archives 248 | $archive_year = mysql2date('Y', $post->post_date); 249 | $archive_month = mysql2date('m', $post->post_date); 250 | $archive_day = mysql2date('d', $post->post_date); 251 | // Yearly Archive 252 | $archive_year_url = preg_replace('#^https?://[^/]+#i', '', get_year_link( $archive_year ) ); 253 | $this->WPVarnishPurgeObject( $archive_year_url . $archive_pattern ); 254 | // Monthly Archive 255 | $archive_month_url = preg_replace('#^https?://[^/]+#i', '', get_month_link( $archive_year, $archive_month ) ); 256 | $this->WPVarnishPurgeObject( $archive_month_url . $archive_pattern ); 257 | // Daily Archive 258 | $archive_day_url = preg_replace('#^https?://[^/]+#i', '', get_day_link( $archive_year, $archive_month, $archive_day ) ); 259 | $this->WPVarnishPurgeObject( $archive_day_url . $archive_pattern ); 260 | } 261 | 262 | //wrapper on WPVarnishPurgePost for transition_post_status 263 | function WPVarnishPurgePostStatus($old, $new, $post) { 264 | if ( $old != $new ) { 265 | if ( $old == 'publish' || $new == 'publish' ) { 266 | $this->WPVarnishPurgePost($post->ID); 267 | } 268 | } 269 | } 270 | 271 | // WPVarnishPurgePost - Purges a post object 272 | function WPVarnishPurgePost($post_id, $purge_comments=false) { 273 | 274 | $post = get_post($post_id); 275 | // We need a post object, so we perform a few checks. 276 | if ( ! is_object($post) || ! isset($post->post_type) || ! in_array( get_post_type($post), array('post', 'page', 'attachment') ) ) { 277 | return; 278 | } 279 | 280 | //$wpv_url = get_permalink($post->ID); 281 | // Here we do not use ``get_permalink()`` to get the post object's permalink, 282 | // because this function generates a permalink only for published posts. 283 | // So, for example, there is a problem when a post transitions from 284 | // status 'publish' to status 'draft', because ``get_permalink`` would 285 | // return a URL of the form, ``?p=123``, which does not exist in the cache. 286 | // For this reason, the following workaround is used: 287 | // http://wordpress.stackexchange.com/a/42988/14743 288 | // It creates a clone of the post object and pretends it's published and 289 | // then it generates the permalink for it. 290 | if (in_array($post->post_status, array('draft', 'pending', 'auto-draft'))) { 291 | $my_post = clone $post; 292 | $my_post->post_status = 'published'; 293 | $my_post->post_name = sanitize_title($my_post->post_name ? $my_post->post_name : $my_post->post_title, $my_post->ID); 294 | $wpv_url = get_permalink($my_post); 295 | } else { 296 | $wpv_url = get_permalink($post->ID); 297 | } 298 | 299 | $wpv_url = preg_replace( '#^https?://[^/]+#i', '', $wpv_url ); 300 | 301 | // Purge post comments feed and comment pages, if requested, before 302 | // adding multipage support. 303 | if ( $purge_comments === true ) { 304 | // Post comments feed 305 | $this->WPVarnishPurgeObject( $wpv_url . 'feed/(?:(atom|rdf)/)?$' ); 306 | // For paged comments 307 | if ( intval(get_option('page_comments', 0)) == 1 ) { 308 | if ( get_option($this->wpv_update_commentnavi_optname) == 1 ) { 309 | $this->WPVarnishPurgeObject( $wpv_url . 'comment-page-[\d]+/(?:#comments)?$' ); 310 | } 311 | } 312 | } 313 | 314 | // Add support for multipage content for posts and pages 315 | if ( in_array( get_post_type($post), array('post', 'page') ) ) { 316 | $wpv_url .= '([\d]+/)?$'; 317 | } 318 | // Purge object permalink 319 | $this->WPVarnishPurgeObject($wpv_url); 320 | 321 | // For attachments, also purge the parent post, if it is published. 322 | if ( get_post_type($post) == 'attachment' ) { 323 | if ( $post->post_parent > 0 ) { 324 | $parent_post = get_post( $post->post_parent ); 325 | if ( $parent_post->post_status == 'publish' ) { 326 | // If the parent post is published, then purge its permalink 327 | $wpv_url = preg_replace( '#^https?://[^/]+#i', '', get_permalink($parent_post->ID) ); 328 | $this->WPVarnishPurgeObject( $wpv_url ); 329 | } 330 | } 331 | } 332 | } 333 | 334 | // wrapper on WPVarnishPurgePostComments for comment status changes 335 | function WPVarnishPurgePostCommentsStatus($comment_id, $new_comment_status) { 336 | $this->WPVarnishPurgePostComments($comment_id); 337 | } 338 | 339 | // WPVarnishPurgePostComments - Purge all comments pages from a post 340 | function WPVarnishPurgePostComments($comment_id) { 341 | $comment = get_comment($comment_id); 342 | $post = get_post( $comment->comment_post_ID ); 343 | 344 | // Comments feed 345 | $this->WPVarnishPurgeObject( '/comments/feed/(?:(atom|rdf)/)?$' ); 346 | 347 | // Purge post page, post comments feed and post comments pages 348 | $this->WPVarnishPurgePost($post, $purge_comments=true); 349 | 350 | // Popup comments 351 | // See: 352 | // - http://codex.wordpress.org/Function_Reference/comments_popup_link 353 | // - http://codex.wordpress.org/Template_Tags/comments_popup_script 354 | $this->WPVarnishPurgeObject( '/.*comments_popup=' . $post->ID . '.*' ); 355 | 356 | } 357 | 358 | function WPVarnishPostID() { 359 | global $posts, $comment_post_ID, $post_ID; 360 | 361 | if ($post_ID) { 362 | return $post_ID; 363 | } elseif ($comment_post_ID) { 364 | return $comment_post_ID; 365 | } elseif (is_single() || is_page() && count($posts)) { 366 | return $posts[0]->ID; 367 | } elseif (isset($_REQUEST['p'])) { 368 | return (integer) $_REQUEST['p']; 369 | } 370 | 371 | return 0; 372 | } 373 | 374 | function WPVarnishAdminMenu() { 375 | if (!defined('VARNISH_HIDE_ADMINMENU')) { 376 | add_options_page(__('WP-Varnish Configuration','wp-varnish'), 'WP-Varnish', 'publish_posts', 'WPVarnish', array($this, 'WPVarnishAdmin')); 377 | } 378 | } 379 | 380 | function WPVarnishAdminBarLinks($admin_bar){ 381 | $admin_bar->add_menu( array( 382 | 'id' => 'wp-varnish', 383 | 'title' => __('Varnish','wp-varnish'), 384 | 'href' => admin_url('admin.php?page=WPVarnish') 385 | )); 386 | $admin_bar->add_menu( array( 387 | 'id' => 'clear-all-cache', 388 | 'parent' => 'wp-varnish', 389 | 'title' => 'Purge All Cache', 390 | 'href' => wp_nonce_url(admin_url('admin.php?page=WPVarnish&wpvarnish_clear_blog_cache&noheader=true'), 'wp-varnish') 391 | )); 392 | $admin_bar->add_menu( array( 393 | 'id' => 'clear-single-cache', 394 | 'parent' => 'wp-varnish', 395 | 'title' => 'Purge This Page', 396 | 'href' => wp_nonce_url(admin_url('admin.php?page=WPVarnish&wpvarnish_clear_post&noheader=true&post_id=' . $this->WPVarnishPostID() ), 'wp-varnish') 397 | )); 398 | } 399 | 400 | 401 | 402 | // WpVarnishAdmin - Draw the administration interface. 403 | function WPVarnishAdmin() { 404 | if ($_SERVER["REQUEST_METHOD"] == "GET") { 405 | if (current_user_can('manage_options')) { 406 | 407 | $nonce = $_REQUEST['_wpnonce']; 408 | 409 | if (isset($_GET['wpvarnish_clear_blog_cache']) && wp_verify_nonce( $nonce, 'wp-varnish' )) { 410 | $this->WPVarnishPurgeAll(); 411 | header('Location: '.admin_url('admin.php?page=WPVarnish')); 412 | } 413 | 414 | if (isset($_GET['wpvarnish_clear_post']) && wp_verify_nonce( $nonce, 'wp-varnish' )) { 415 | $this->WPVarnishPurgePost($_GET['post_id']); 416 | header('Location: '.admin_url('admin.php?page=WPVarnish')); 417 | } 418 | } 419 | }elseif ($_SERVER["REQUEST_METHOD"] == "POST") { 420 | if (current_user_can('manage_options')) { 421 | if (isset($_POST['wpvarnish_admin'])) { 422 | cleanSubmittedData('wpvarnish_port', '/[^0-9]/'); 423 | cleanSubmittedData('wpvarnish_addr', '/[^0-9.]/'); 424 | if (!empty($_POST["$this->wpv_addr_optname"])) { 425 | $wpv_addr_optval = $_POST["$this->wpv_addr_optname"]; 426 | update_option($this->wpv_addr_optname, $wpv_addr_optval); 427 | } 428 | 429 | if (!empty($_POST["$this->wpv_port_optname"])) { 430 | $wpv_port_optval = $_POST["$this->wpv_port_optname"]; 431 | update_option($this->wpv_port_optname, $wpv_port_optval); 432 | } 433 | 434 | if (!empty($_POST["$this->wpv_secret_optname"])) { 435 | $wpv_secret_optval = $_POST["$this->wpv_secret_optname"]; 436 | update_option($this->wpv_secret_optname, $wpv_secret_optval); 437 | } 438 | 439 | if (!empty($_POST["$this->wpv_timeout_optname"])) { 440 | $wpv_timeout_optval = $_POST["$this->wpv_timeout_optname"]; 441 | update_option($this->wpv_timeout_optname, $wpv_timeout_optval); 442 | } 443 | 444 | if (!empty($_POST["$this->wpv_update_pagenavi_optname"])) { 445 | update_option($this->wpv_update_pagenavi_optname, 1); 446 | } else { 447 | update_option($this->wpv_update_pagenavi_optname, 0); 448 | } 449 | 450 | if (!empty($_POST["$this->wpv_update_commentnavi_optname"])) { 451 | update_option($this->wpv_update_commentnavi_optname, 1); 452 | } else { 453 | update_option($this->wpv_update_commentnavi_optname, 0); 454 | } 455 | 456 | if (!empty($_POST["$this->wpv_use_adminport_optname"])) { 457 | update_option($this->wpv_use_adminport_optname, 1); 458 | } else { 459 | update_option($this->wpv_use_adminport_optname, 0); 460 | } 461 | 462 | if (!empty($_POST["$this->wpv_vversion_optname"])) { 463 | $wpv_vversion_optval = $_POST["$this->wpv_vversion_optname"]; 464 | update_option($this->wpv_vversion_optname, $wpv_vversion_optval); 465 | } 466 | } 467 | 468 | if (isset($_POST['wpvarnish_purge_url_submit'])) { 469 | $this->WPVarnishPurgeURL($_POST["$this->wpvarnish_purge_url_optname"]); 470 | } 471 | 472 | if (isset($_POST['wpvarnish_clear_blog_cache'])) 473 | $this->WPVarnishPurgeAll(); 474 | 475 | ?>

wpv_timeout_optname); 482 | $wpv_update_pagenavi_optval = get_option($this->wpv_update_pagenavi_optname); 483 | $wpv_update_commentnavi_optval = get_option($this->wpv_update_commentnavi_optname); 484 | $wpv_use_adminport_optval = get_option($this->wpv_use_adminport_optname); 485 | $wpv_vversion_optval = get_option($this->wpv_vversion_optname); 486 | ?> 487 |
488 | 489 |

490 |

491 |
492 | " . __("These values can't be edited since there's a global configuration located in wp-config.php. If you want to change these settings, please update the file or contact the administrator.",'wp-varnish') . "

\n"; 498 | // Also, if defined, show the varnish servers configured (VARNISH_SHOWCFG) 499 | if (defined('VARNISH_SHOWCFG')) { 500 | echo "

" . __("Current configuration:",'wp-varnish') . "

\n"; 501 | echo "
    "; 502 | if ( isset($varnish_version) && $varnish_version ) 503 | echo "
  • " . __("Version: ",'wp-varnish') . $varnish_version . "
  • "; 504 | foreach ($varnish_servers as $server) { 505 | @list ($host, $port, $secret) = explode(':', $server); 506 | echo "
  • " . __("Server: ",'wp-varnish') . $host . "
    " . __("Port: ",'wp-varnish') . $port . "
  • "; 507 | } 508 | echo "
"; 509 | } 510 | } else { 511 | // If not defined in wp-config.php, use individual configuration. 512 | ?> 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 531 |
532 | 533 |
534 | 535 | 536 | 537 | 538 | 539 |
540 | 543 |

:

544 | 545 |

/>

546 | 547 |

/>

548 | 549 |

/>

550 | 551 |

:

552 | 553 |

" />

554 | 555 |

556 | : 557 | " /> 558 |

559 | 560 |

" />

561 |
562 |
563 | wpv_purgeactions[$wpv_url]=$wpv_url; 571 | } 572 | function WPVarnishPurgeOnExit() { 573 | $purgeurls = array_keys($this->wpv_purgeactions); 574 | foreach($purgeurls as $wpv_url) { 575 | $this->WPVarnishActuallyPurgeObject($wpv_url); 576 | } 577 | } 578 | function WPVarnishActuallyPurgeObject($wpv_url) { 579 | global $varnish_servers; 580 | 581 | // added this hook to enable other plugins do something when cache is purged 582 | do_action( 'WPVarnishPurgeObject', $wpv_url ); 583 | 584 | if (is_array($varnish_servers)) { 585 | foreach ($varnish_servers as $server) { 586 | list ($host, $port, $secret) = explode(':', $server); 587 | $wpv_purgeaddr[] = $host; 588 | $wpv_purgeport[] = $port; 589 | $wpv_secret[] = $secret; 590 | } 591 | } else { 592 | $wpv_purgeaddr = get_option($this->wpv_addr_optname); 593 | $wpv_purgeport = get_option($this->wpv_port_optname); 594 | $wpv_secret = get_option($this->wpv_secret_optname); 595 | } 596 | 597 | $wpv_timeout = get_option($this->wpv_timeout_optname); 598 | $wpv_use_adminport = get_option($this->wpv_use_adminport_optname); 599 | global $varnish_version; 600 | if ( isset($varnish_version) && in_array($varnish_version, array(2,3)) ) 601 | $wpv_vversion_optval = $varnish_version; 602 | else 603 | $wpv_vversion_optval = get_option($this->wpv_vversion_optname); 604 | 605 | // check for domain mapping plugin by donncha 606 | if (function_exists('domain_mapping_siteurl')) { 607 | $wpv_wpurl = domain_mapping_siteurl('NA'); 608 | } else { 609 | $wpv_wpurl = get_bloginfo('url'); 610 | } 611 | $wpv_replace_wpurl = '/^https?:\/\/([^\/]+)(.*)/i'; 612 | $wpv_host = preg_replace($wpv_replace_wpurl, "$1", $wpv_wpurl); 613 | $wpv_blogaddr = preg_replace($wpv_replace_wpurl, "$2", $wpv_wpurl); 614 | $wpv_url = $wpv_blogaddr . $wpv_url; 615 | 616 | // allow custom purge functions and stop if they return false 617 | if (function_exists($this->wpv_custom_purge_obj_f)) { 618 | $f = $this->wpv_custom_purge_obj_f; 619 | if (!$f($wpv_url, $wpv_host)) 620 | return; 621 | } 622 | 623 | for ($i = 0; $i < count ($wpv_purgeaddr); $i++) { 624 | $varnish_sock = fsockopen($wpv_purgeaddr[$i], $wpv_purgeport[$i], $errno, $errstr, $wpv_timeout); 625 | if (!$varnish_sock) { 626 | error_log("wp-varnish error: $errstr ($errno) on server $wpv_purgeaddr[$i]:$wpv_purgeport[$i]"); 627 | continue; 628 | } 629 | 630 | if($wpv_use_adminport) { 631 | $buf = fread($varnish_sock, 1024); 632 | if(preg_match('/(\w+)\s+Authentication required./', $buf, $matches)) { 633 | # get the secret 634 | $secret = $wpv_secret[$i]; 635 | fwrite($varnish_sock, "auth " . $this->WPAuth($matches[1], $secret) . "\n"); 636 | $buf = fread($varnish_sock, 1024); 637 | if(!preg_match('/^200/', $buf)) { 638 | error_log("wp-varnish error: authentication failed using admin port on server $wpv_purgeaddr[$i]:$wpv_purgeport[$i]"); 639 | fclose($varnish_sock); 640 | continue; 641 | } 642 | } 643 | if ($wpv_vversion_optval == 3) { 644 | $out = "ban req.url ~ ^$wpv_url$ && req.http.host == $wpv_host\n"; 645 | } else { 646 | $out = "purge req.url ~ ^$wpv_url && req.http.host == $wpv_host\n"; 647 | } 648 | } else { 649 | $out = "BAN $wpv_url HTTP/1.0\r\n"; 650 | $out .= "Host: $wpv_host\r\n"; 651 | $out .= "User-Agent: WordPress-Varnish plugin\r\n"; 652 | $out .= "Connection: Close\r\n\r\n"; 653 | } 654 | fwrite($varnish_sock, $out); 655 | fclose($varnish_sock); 656 | } 657 | } 658 | 659 | function WPAuth($challenge, $secret) { 660 | $ctx = hash_init('sha256'); 661 | hash_update($ctx, $challenge); 662 | hash_update($ctx, "\n"); 663 | hash_update($ctx, $secret . "\n"); 664 | hash_update($ctx, $challenge); 665 | hash_update($ctx, "\n"); 666 | $sha256 = hash_final($ctx); 667 | 668 | return $sha256; 669 | } 670 | } 671 | 672 | $wpvarnish = new WPVarnish(); 673 | 674 | // Helper functions 675 | function cleanSubmittedData($varname, $regexp) { 676 | // FIXME: should do this in the admin console js, not here 677 | // normally I hate cleaning data and would rather validate before submit 678 | // but, this fixes the problem in the cleanest method for now 679 | foreach ($_POST[$varname] as $key=>$value) { 680 | $_POST[$varname][$key] = preg_replace($regexp,'',$value); 681 | } 682 | } 683 | ?> 684 | --------------------------------------------------------------------------------