generated from alleyinteractive/create-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-page-cache-control.php
105 lines (90 loc) · 3.12 KB
/
wp-page-cache-control.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* Plugin Name: WP Page Cache Control
* Plugin URI: https://github.com/alleyinteractive/wp-page-cache-control
* Description: Control and modify the page cache for multiple hosting providers.
* Version: 0.1.3
* Author: Sean Fisher
* Author URI: https://github.com/alleyinteractive/wp-page-cache-control
* Requires at least: 5.9
* Tested up to: 6.2
*
* Text Domain: wp-page-cache-control
* Domain Path: /languages/
*
* @package wp-page-cache-control
*/
use Alley\WP\WP_Page_Cache_Control\Header;
use Alley\WP\WP_Page_Cache_Control\Providers\Provider;
use function Alley\WP\WP_Page_Cache_Control\detect_provider;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Root directory to this plugin.
*/
define( 'WP_PAGE_CACHE_CONTROL_DIR', __DIR__ );
// Check if Composer is installed by checking for the existence of the Str class.
if ( ! class_exists( \Mantle\Support\Str::class ) ) {
// Check if the autoloader exists. Otherwise, display an admin notice and bail.
if ( ! file_exists( __DIR__ . '/vendor/wordpress-autoload.php' ) ) {
\add_action(
'admin_notices',
function () {
?>
<div class="notice notice-error">
<p><?php esc_html_e( 'Composer is not installed and wp-page-cache-control cannot load. Try using a `*-built` branch if the plugin is being loaded as a submodule.', 'wp-page-cache-control' ); ?></p>
</div>
<?php
}
);
return;
} else {
// Load Composer dependencies.
require_once __DIR__ . '/vendor/wordpress-autoload.php';
}
}
// Load the plugin's main files.
require_once __DIR__ . '/src/assets.php';
/**
* The cache provider instance.
*
* @var Provider|null
*/
static $wp_page_cache_control_provider = null;
/**
* Retrieve the cache provider instance.
*
* @throws InvalidArgumentException If the provider class does not exist.
*
* @return Provider
*/
function wp_page_cache_control(): Provider {
global $wp_page_cache_control_provider;
if ( ! isset( $wp_page_cache_control_provider ) ) {
/**
* Filter the cache provider class.
*
* @param string $wp_page_cache_control_provider The cache provider class.
*/
$wp_page_cache_control_provider = apply_filters( 'wp_page_cache_control_provider', detect_provider() );
if ( empty( $wp_page_cache_control_provider ) || ! class_exists( $wp_page_cache_control_provider ) ) {
throw new InvalidArgumentException(
esc_html( "Invalid provider class provided. Expected class to exist: {$wp_page_cache_control_provider}" )
);
}
$wp_page_cache_control_provider = new $wp_page_cache_control_provider();
}
return $wp_page_cache_control_provider;
}
add_action( 'muplugins_loaded', __NAMESPACE__ . '\\wp_page_cache_control' ); // @phpstan-ignore-line should not return anything
/**
* Setup the header handler to send the headers on the 'send_headers' action.
* Also, setup the cache provider to fire their headers as well. Runs late to
* catch any changes that may happen earlier in 'send_headers'.
*/
function wp_page_cache_control_send_headers(): void {
wp_page_cache_control()->send_headers();
Header::send_headers();
}
add_action( 'send_headers', __NAMESPACE__ . '\\wp_page_cache_control_send_headers', PHP_INT_MAX );