Files
profile/inc/cache-cleaner-home.php
2026-01-12 13:37:40 +03:00

138 lines
5.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// Добавляем пункт меню в админ-панель
add_action('admin_menu', 'register_cache_cleaner_menu');
function register_cache_cleaner_menu() {
add_menu_page(
'Очистка кэша главной', // Заголовок страницы
'Очистка кэша', // Название в меню
'edit_posts', // Права доступа
'cache-cleaner', // Ярлык меню
'cache_cleaner_page', // Функция отображения
'dashicons-trash', // Иконка
100 // Позиция в меню
);
}
// Функция отображения страницы
function cache_cleaner_page() {
?>
<div class="wrap">
<h1>Очистка кэша главной страницы</h1>
<?php
// Обрабатываем запрос на очистку кэша
if (isset($_POST['clear_cache'])) {
$result = clear_and_regenerate_cache();
if ($result['success']) {
echo '<div class="notice notice-success is-dismissible">';
echo '<p><strong>Кэш очищен и файлы пересозданы!</strong></p>';
if (!empty($result['cleared_files'])) {
echo '<p>Удалены следующие файлы:</p>';
echo '<ul>';
foreach ($result['cleared_files'] as $file) {
echo '<li>' . esc_html($file) . '</li>';
}
echo '</ul>';
}
if (!empty($result['created_files'])) {
echo '<p>Созданы следующие файлы:</p>';
echo '<ul>';
foreach ($result['created_files'] as $file) {
echo '<li>' . esc_html($file) . '</li>';
}
echo '</ul>';
}
echo '</div>';
} else {
echo '<div class="notice notice-error is-dismissible">';
echo '<p><strong>Ошибка:</strong> ' . esc_html($result['message']) . '</p>';
echo '</div>';
}
}
?>
<form method="post">
<p>Нажмите кнопку ниже для очистки кэша главной страницы и пересоздания файлов:</p>
<?php submit_button('Очистить кэш и пересоздать файлы', 'primary', 'clear_cache'); ?>
</form>
</div>
<?php
}
// Функция очистки кэш-файлов и создания новых
function clear_and_regenerate_cache() {
$result = [
'success' => true,
'message' => '',
'cleared_files' => [],
'created_files' => []
];
try {
// Определяем пути к директориям
$fpcache_dir = trailingslashit(wp_upload_dir()['basedir']) . 'fpcache/';
$block_cache = trailingslashit(wp_upload_dir()['basedir']) . 'cached_template/';
// Массив файлов для очистки
$cache_files = [
$block_cache . 'template-parts/home/colon-item.html',
$block_cache . 'template-parts/home/list-items.html',
$block_cache . 'template-parts/home/main-item.html',
$block_cache . 'template-parts/home/news.html',
$fpcache_dir . 'regenerate/index.html'
];
// Очищаем файлы кэша
foreach ($cache_files as $file) {
if (is_file($file) && unlink($file)) {
$result['cleared_files'][] = $file;
} elseif (is_file($file)) {
$result['cleared_files'][] = $file . ' (не удалось удалить)';
}
}
// Создаем файлы last_modified с префиксами
$prefixes = ['profile_article', 'anew', 'yellow'];
$last_modified_dir = $fpcache_dir;
// Создаем директорию если она не существует
if (!file_exists($last_modified_dir)) {
wp_mkdir_p($last_modified_dir);
}
// Создаем файлы для каждого префикса
$create_files = [
$last_modified_dir.'.last_modified',
$last_modified_dir.'.last_modified_profile_article',
$last_modified_dir.'.last_modified_anew',
$last_modified_dir.'.last_modified_yellow'
];
foreach ($create_files as $create_file) {
$res = touch($create_file);
if (!$res){
$result['created_files'][] = $create_file . ' (не удалось создать)';
} else {
$result['created_files'][] = $create_file;
}
}
$result['message'] = 'Операция завершена успешно';
} catch (Exception $e) {
$result['success'] = false;
$result['message'] = $e->getMessage();
}
return $result;
}
?>