add wp-rocket

This commit is contained in:
nguyen dung
2022-02-18 19:09:35 +07:00
parent 39b8cb3612
commit 3110d00ee7
927 changed files with 271703 additions and 2 deletions

View File

@@ -0,0 +1,36 @@
<?php
namespace WP_Rocket\Addon\Varnish;
use WP_Rocket\Engine\Container\ServiceProvider\AbstractServiceProvider;
/**
* Service provider for Varnish Addon.
*/
class ServiceProvider extends AbstractServiceProvider {
/**
* The provides array is a way to let the container
* know that a service is provided by this service
* provider. Every service that is registered via
* this service provider must have an alias added
* to this array or it will be ignored.
*
* @var array
*/
protected $provides = [
'varnish',
'varnish_subscriber',
];
/**
* Registers the subscribers in the container.
*
* @since 3.3
*/
public function register() {
$this->getContainer()->add( 'varnish', 'WP_Rocket\Addon\Varnish\Varnish' );
$this->getContainer()->share( 'varnish_subscriber', 'WP_Rocket\Addon\Varnish\Subscriber' )
->withArgument( $this->getContainer()->get( 'varnish' ) )
->withArgument( $this->getContainer()->get( 'options' ) );
}
}

View File

@@ -0,0 +1,117 @@
<?php
namespace WP_Rocket\Addon\Varnish;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Subscriber for the Varnish Purge.
*
* @since 3.5
*/
class Subscriber implements Subscriber_Interface {
/**
* Varnish instance
*
* @var Varnish
*/
private $varnish;
/**
* WP Rocket options instance
*
* @var Options_Data
*/
private $options;
/**
* Constructor
*
* @param Varnish $varnish Varnish instance.
* @param Options_Data $options WP Rocket options instance.
*/
public function __construct( Varnish $varnish, Options_Data $options ) {
$this->varnish = $varnish;
$this->options = $options;
}
/**
* {@inheritdoc}
*/
public static function get_subscribed_events() {
return [
'before_rocket_clean_domain' => [ 'clean_domain', 10, 3 ],
'before_rocket_clean_file' => [ 'clean_file' ],
'before_rocket_clean_home' => [ 'clean_home', 10, 2 ],
];
}
/**
* Checks if Varnish cache should be purged
*
* @since 3.5
*
* @return bool
*/
private function should_purge() {
return (
/**
* Filters the use of the Varnish compatibility add-on
*
* @param bool $varnish_purge True to use, false otherwise.
*/
apply_filters( 'do_rocket_varnish_http_purge', false ) // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
||
(bool) $this->options->get( 'varnish_auto_purge', 0 )
);
}
/**
* Clears Varnish cache for the whole domain
*
* @param string $root The path of home cache file.
* @param string $lang The current lang to purge.
* @param string $url The home url.
* @return void
*/
public function clean_domain( $root, $lang, $url ) {
if ( ! $this->should_purge() ) {
return;
}
$this->varnish->purge( trailingslashit( $url ) . '?regex' );
}
/**
* Clears a specific page in Varnish cache
*
* @param string $url The url to purge.
* @return void
*/
public function clean_file( $url ) {
if ( ! $this->should_purge() ) {
return;
}
$this->varnish->purge( trailingslashit( $url ) . '?regex' );
}
/**
* Clears the homepage in Varnish cache
*
* @param string $root The path of home cache file.
* @param string $lang The current lang to purge.
* @return void
*/
public function clean_home( $root, $lang ) {
if ( ! $this->should_purge() ) {
return;
}
$home_url = trailingslashit( get_rocket_i18n_home_url( $lang ) );
$home_pagination_url = $home_url . trailingslashit( $GLOBALS['wp_rewrite']->pagination_base ) . '?regex';
$this->varnish->purge( $home_url );
$this->varnish->purge( $home_pagination_url );
}
}

View File

@@ -0,0 +1,129 @@
<?php
namespace WP_Rocket\Addon\Varnish;
/**
* Varnish cache purge
*
* @since 3.5
*/
class Varnish {
/**
* Send purge request to Varnish
*
* @since 3.5
*
* @param string $url The URL to purge.
* @return void
*/
public function purge( $url ) {
$parse_url = get_rocket_parse_url( $url );
$x_purge_method = 'default';
$regex = '';
if ( 'regex' === $parse_url['query'] ) {
$x_purge_method = 'regex';
$regex = '.*';
}
/**
* Filter the HTTP protocol (scheme)
*
* @since 2.7.3
* @param string $scheme The HTTP protocol
*/
$scheme = apply_filters( 'rocket_varnish_http_purge_scheme', 'http' );
/**
* Filters the headers to send with the Varnish purge request
*
* @since 3.1
*
* @param array $headers Headers to send.
*/
$headers = apply_filters(
'rocket_varnish_purge_headers',
[
/**
* Filters the host value passed in the request headers
*
* @since 2.8.15
* @param string $host The host value.
*/
'host' => apply_filters( 'rocket_varnish_purge_request_host', $parse_url['host'] ),
'X-Purge-Method' => $x_purge_method,
]
);
/**
* Filters the arguments passed to the Varnish purge request
*
* @since 3.5
*
* @param array $args Array of arguments for the request.
*/
$args = apply_filters(
'rocket_varnish_purge_request_args',
[
'method' => 'PURGE',
'blocking' => false,
'redirection' => 0,
'headers' => $headers,
]
);
foreach ( $this->get_varnish_ips() as $ip ) {
$host = ! empty( $ip ) ? $ip : str_replace( '*', '', $parse_url['host'] );
$purge_url_main = $scheme . '://' . $host . $parse_url['path'];
/**
* Filters the purge url.
*
* @since 3.6.3
*
* @param string $purge_url_full Full url contains the main url plus regex pattern.
* @param string $purge_url_main Main purge url without any additions params.
* @param string $regex Regex string.
*/
$purge_url = apply_filters(
'rocket_varnish_purge_url',
$purge_url_main . $regex,
$purge_url_main,
$regex
);
wp_remote_request( $purge_url, $args );
}
}
/**
* Gets an array of Varnish IPs to send the purge request to
*
* @return array
*/
private function get_varnish_ips() {
/**
* Filter the Varnish IP to call
*
* @since 2.6.8
* @param string|array $varnish_ip The Varnish IP
*/
$varnish_ip = apply_filters( 'rocket_varnish_ip', [] );
$constant = rocket_get_constant( 'WP_ROCKET_VARNISH_IP' );
if (
! empty( $constant )
&&
empty( $varnish_ip )
) {
$varnish_ip = $constant;
}
if ( empty( $varnish_ip ) ) {
$varnish_ip = [ '' ];
} elseif ( is_string( $varnish_ip ) ) {
$varnish_ip = (array) $varnish_ip;
}
return $varnish_ip;
}
}

View File

@@ -0,0 +1,54 @@
{
"name": "wp-media/module-varnish",
"description": "Varnish Addon for WP Rocket",
"homepage": "https://github.com/wp-media/module-varnish",
"license": "GPL-2.0+",
"authors": [
{
"name": "WP Media",
"email": "contact@wp-media.me",
"homepage": "https://wp-media.me"
}
],
"type": "library",
"config": {
"sort-packages": true
},
"support": {
"issues": "https://github.com/wp-media/module-varnish/issues",
"source": "https://github.com/wp-media/module-varnish"
},
"require-dev": {
"php": "^5.6 || ^7",
"brain/monkey": "^2.0",
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
"phpcompatibility/phpcompatibility-wp": "^2.0",
"phpstan/phpstan": "^0.12.3",
"phpunit/phpunit": "^5.7 || ^7",
"roave/security-advisories": "dev-master",
"szepeviktor/phpstan-wordpress": "^0.6",
"wp-coding-standards/wpcs": "^2",
"wp-media/event-manager": "^3.1",
"wp-media/module-container": "^2.4",
"wp-media/options": "^3.0",
"wp-media/phpunit": "^1.0"
},
"autoload": {
"psr-4": { "WP_Rocket\\Addon\\Varnish\\": "." }
},
"autoload-dev": {
"psr-4": { "WP_Rocket\\Tests\\": "Tests/" }
},
"scripts": {
"test-unit": "\"vendor/bin/wpmedia-phpunit\" unit path=Tests/Unit",
"test-integration": "\"vendor/bin/wpmedia-phpunit\" integration path=Tests/Integration/",
"run-tests": [
"@test-unit",
"@test-integration"
],
"install-codestandards": "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin::run",
"phpcs": "phpcs --basepath=.",
"phpcs-changed": "./bin/phpcs-changed.sh",
"phpcs:fix": "phpcbf"
}
}