| 1 |
<?php |
| 2 |
/** |
| 3 |
* Domain mapping compatability. |
| 4 |
* |
| 5 |
* @package PoweredCache\Compat |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace PoweredCache\Compat; |
| 9 |
|
| 10 |
use function PoweredCache\Utils\delete_page_cache; |
| 11 |
use function PoweredCache\Utils\get_page_cache_dir; |
| 12 |
use function PoweredCache\Utils\remove_dir; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly. |
| 16 |
} |
| 17 |
|
| 18 |
add_action( 'mercator.mapping.deleted', __NAMESPACE__ . '\\mapping_cleanup' ); |
| 19 |
add_action( 'powered_cache_clean_site_cache_dir', __NAMESPACE__ . '\\maybe_clean_mapped_domain_dir' ); |
| 20 |
add_action( 'powered_cache_advanced_cache_purge_post', __NAMESPACE__ . '\\maybe_purge_on_post_update', 10, 2 ); |
| 21 |
add_action( 'powered_cache_advanced_cache_purge_on_comment_status_change', __NAMESPACE__ . '\\maybe_purge_on_comment_status_change', 10, 2 ); |
| 22 |
add_action( 'powered_cache_create_config_file', __NAMESPACE__ . '\\maybe_create_config_files', 10, 3 ); |
| 23 |
add_action( 'wp_delete_site', __NAMESPACE__ . '\\purge_on_site_delete' ); // works on WP 5.1+ |
| 24 |
add_action( 'powered_cache_after_clean_up', __NAMESPACE__ . '\\maybe_delete_config' ); |
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* Delete caching directory and configuration of mapped domain |
| 29 |
* during the mapping delete |
| 30 |
* |
| 31 |
* @param Object $mapping Mapping object |
| 32 |
* |
| 33 |
* @since 2.0 |
| 34 |
*/ |
| 35 |
function mapping_cleanup( $mapping ) { |
| 36 |
$domain = $mapping->get_domain(); |
| 37 |
$site_path = wp_parse_url( esc_url( $domain ), PHP_URL_HOST ); |
| 38 |
$base_dir = get_page_cache_dir(); |
| 39 |
|
| 40 |
$site_cache_dir = $base_dir . $site_path; |
| 41 |
|
| 42 |
if ( file_exists( $site_cache_dir ) ) { |
| 43 |
remove_dir( $site_cache_dir ); |
| 44 |
} |
| 45 |
|
| 46 |
$config_dir = WP_CONTENT_DIR . '/pc-config'; |
| 47 |
$config_file_name = 'config-' . $site_path . '.php'; |
| 48 |
$config_file = trailingslashit( $config_dir ) . $config_file_name; |
| 49 |
|
| 50 |
if ( file_exists( $config_file ) ) { |
| 51 |
unlink( $config_file ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Remove mapped domains when purging the site cache |
| 57 |
* |
| 58 |
* @since 2.0 |
| 59 |
*/ |
| 60 |
function maybe_clean_mapped_domain_dir() { |
| 61 |
$mapped_domains = get_mapped_domains(); |
| 62 |
if ( ! $mapped_domains ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
$base_dir = get_page_cache_dir(); |
| 67 |
|
| 68 |
foreach ( $mapped_domains as $domain ) { |
| 69 |
$site_path = wp_parse_url( $domain, PHP_URL_HOST ); |
| 70 |
$site_cache_dir = $base_dir . $site_path; |
| 71 |
if ( file_exists( $site_cache_dir ) ) { |
| 72 |
remove_dir( $site_cache_dir ); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Delete corresponding URLs from the mapped domain too. |
| 79 |
* |
| 80 |
* @param int $post_id Post ID. |
| 81 |
* @param array $urls The list of URLs that deleted. |
| 82 |
* |
| 83 |
* @since 2.0 |
| 84 |
*/ |
| 85 |
function maybe_purge_on_post_update( $post_id, $urls ) { |
| 86 |
$mapped_domains = get_mapped_domains(); |
| 87 |
if ( ! $mapped_domains ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
foreach ( $mapped_domains as $domain ) { |
| 92 |
$new_purge_urls = str_replace( site_url(), $domain, $urls ); |
| 93 |
foreach ( $new_purge_urls as $new_purge_url ) { |
| 94 |
delete_page_cache( $new_purge_url ); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Delete corresponding URL from the mapped domain too. |
| 101 |
* |
| 102 |
* @param int $post_id Post ID. |
| 103 |
* @param string $post_url Post ID of the comment. |
| 104 |
*/ |
| 105 |
function maybe_purge_on_comment_status_change( $post_id, $post_url ) { |
| 106 |
$mapped_domains = get_mapped_domains(); |
| 107 |
if ( ! $mapped_domains ) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
foreach ( $mapped_domains as $domain ) { |
| 112 |
$post_url = str_replace( site_url(), $domain, $post_url ); |
| 113 |
delete_page_cache( $post_url ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
/** |
| 119 |
* Get the list of mapped domains |
| 120 |
* |
| 121 |
* @return array |
| 122 |
* @since 2.0 |
| 123 |
*/ |
| 124 |
function get_mapped_domains() { |
| 125 |
$mapped_domains = []; |
| 126 |
|
| 127 |
if ( class_exists( '\Mercator\Mapping' ) ) { |
| 128 |
$mappings = \Mercator\Mapping::get_by_site( get_current_blog_id() ); |
| 129 |
if ( ! is_wp_error( $mappings ) && $mappings ) { |
| 130 |
foreach ( $mappings as $mapping ) { |
| 131 |
if ( $mapping->is_active() ) { |
| 132 |
$mapped_domain = $mapping->get_domain(); |
| 133 |
$mapped_domains[] = esc_url( $mapped_domain ); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
return $mapped_domains; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
/** |
| 144 |
* Crete configuration pages for mapped domains. |
| 145 |
* |
| 146 |
* @param string $config_file The path of the configuration file. |
| 147 |
* @param string $config_file_string The contents of the configurations. |
| 148 |
* @param bool $network_wide Whether network-wide configuration or not. |
| 149 |
* |
| 150 |
* @since 2.0 |
| 151 |
*/ |
| 152 |
function maybe_create_config_files( $config_file, $config_file_string, $network_wide ) { |
| 153 |
$config_dir = WP_CONTENT_DIR . '/pc-config'; |
| 154 |
|
| 155 |
// skip on network-wide? |
| 156 |
if ( $network_wide ) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
$mapped_domains = get_mapped_domains(); |
| 161 |
|
| 162 |
// don't have any active mapped domains? |
| 163 |
if ( ! $mapped_domains ) { |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
foreach ( $mapped_domains as $domain ) { |
| 168 |
$url_parts = wp_parse_url( $domain ); |
| 169 |
$config_file_name = 'config-' . $url_parts['host'] . '.php'; |
| 170 |
|
| 171 |
$config_file = trailingslashit( $config_dir ) . $config_file_name; |
| 172 |
file_put_contents( $config_file, $config_file_string ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
| 173 |
} |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Cleanup mapping on site deletion. |
| 179 |
* |
| 180 |
* @param \WP_Site $site_object Site object. |
| 181 |
*/ |
| 182 |
function purge_on_site_delete( $site_object ) { |
| 183 |
if ( class_exists( '\Mercator\Mapping' ) ) { |
| 184 |
$mappings = \Mercator\Mapping::get_by_site( $site_object->id ); |
| 185 |
if ( ! is_wp_error( $mappings ) && $mappings ) { |
| 186 |
foreach ( $mappings as $mapping ) { |
| 187 |
mapping_cleanup( $mapping ); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
/** |
| 195 |
* Delete config files for the mapped domains |
| 196 |
*/ |
| 197 |
function maybe_delete_config() { |
| 198 |
$mapped_domains = get_mapped_domains(); |
| 199 |
if ( ! $mapped_domains ) { |
| 200 |
return; |
| 201 |
} |
| 202 |
|
| 203 |
$config_dir = WP_CONTENT_DIR . '/pc-config'; |
| 204 |
|
| 205 |
foreach ( $mapped_domains as $domain ) { |
| 206 |
$url_parts = wp_parse_url( $domain ); |
| 207 |
$config_file_name = 'config-' . $url_parts['host'] . '.php'; |
| 208 |
|
| 209 |
$config_file = trailingslashit( $config_dir ) . $config_file_name; |
| 210 |
if ( file_exists( $config_file ) ) { |
| 211 |
unlink( $config_file ); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|