Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Nginx
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 purge_all
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 purge_url
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 purgeRequest
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace NewfoldLabs\WP\Module\Performance\Cache\Types;
4
5use NewfoldLabs\WP\Module\Performance\Cache\Purgeable;
6use wpscholar\Url;
7
8/**
9 * Nginx cache type.
10 */
11class Nginx extends CacheBase implements Purgeable {
12
13    /**
14     * Purge all assets from the Nginx cache.
15     */
16    public function purge_all() {
17        $this->purgeRequest();
18    }
19
20    /**
21     * Purge the Nginx cache for a specific URL.
22     *
23     * @param string $url The URL to purge.
24     */
25    public function purge_url( $url ) {
26        $this->purgeRequest( $url );
27    }
28
29    /**
30     * Purge the cache.
31     *
32     * @param string $url The URL to purge.
33     */
34    protected function purgeRequest( $url = '' ) {
35        global $wp_version;
36
37        $url = $url ? new Url( $url ) : new Url( \home_url() );
38
39        $plugin_brand   = $this->getContainer()->plugin()->get( 'id' );
40        $plugin_version = $this->getContainer()->plugin()->version;
41
42        $args = array(
43            'method'     => 'PURGE',
44            'headers'    => array(
45                'host' => $url->host,
46            ),
47            'user-agent' => "WordPress/{$wp_version}{$url->host}{$plugin_brand}/v{$plugin_version}",
48            'sslverify'  => false,
49        );
50
51        // If WP_DEBUG is enabled, we want to wait for a response.
52        if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) {
53            $args['blocking'] = false;
54            $args['timeout']  = 0.01;
55        }
56
57        $path = '/' . ltrim( $url->path, '/' ) . '.*';
58
59        $http_url = $url::buildUrl(
60            array_merge(
61                $url->toArray(),
62                array(
63                    'scheme' => 'http',
64                    'host'   => '127.0.0.1:8080',
65                    'path'   => $path,
66                )
67            )
68        );
69
70        $https_url = $url::buildUrl(
71            array_merge(
72                $url->toArray(),
73                array(
74                    'scheme' => 'https',
75                    'host'   => '127.0.0.1:8443',
76                    'path'   => $path,
77                )
78            )
79        );
80
81        wp_remote_request( $http_url, $args );
82        wp_remote_request( $https_url, $args );
83    }
84}