dosya yapısı
Kod:Tümünü seç
wp-content/
└── plugins/
└── my-super-cache/
├── my-super-cache.php
├── admin/
│ └── admin-page.php
└── includes/
├── core-functions.php
└── cache-handler.php
-------------------------------------------------------------------------------------
Kod:Tümünü seç
<?php
/*
Plugin Name: My Super Cache
Description: Özelleştirilebilir ve optimize edilmiş cache eklentisi.
Version: 1.0
Author: [Sizin İsminiz]
Text Domain: my-super-cache
*/
// Güvenlik Kontrolü
if (!defined('ABSPATH')) {
exit; // Doğrudan erişimi engelle
}
// Eklenti Sabitleri
define('MSC_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('MSC_CACHE_DIR', WP_CONTENT_DIR . '/cache/my-super-cache/');
// Gerekli Dosyaları Dahil Et
require_once MSC_PLUGIN_DIR . 'includes/core-functions.php';
require_once MSC_PLUGIN_DIR . 'includes/cache-handler.php';
require_once MSC_PLUGIN_DIR . 'admin/admin-page.php';
-------------------------------------------------------------------------------------
Kod:Tümünü seç
<?php
function msc_start_cache() {
if (!msc_is_caching_enabled()) return;
if (msc_is_cache_available()) {
msc_serve_cache();
} else {
ob_start(); // Tamponlamayı başlat
}
}
add_action('init', 'msc_start_cache', 0); // Önceliği artırarak erken başlatma
function msc_end_cache() {
$buffer = ob_get_contents();
ob_end_clean();
// Tamponun boş olup olmadığını kontrol edelim
if (!empty($buffer)) {
$cache_file = msc_get_cache_file();
if (!is_dir(MSC_CACHE_DIR)) {
mkdir(MSC_CACHE_DIR, 0755, true);
}
file_put_contents($cache_file, $buffer); // İçeriği cache dosyasına yaz
} else {
error_log('Tampon boş, içerik kaydedilemedi.');
}
echo $buffer; // İçeriği tarayıcıya gönder
}
add_action('shutdown', 'msc_end_cache');
// Cache Kontrol Fonksiyonları
function msc_is_caching_enabled() {
$enabled = get_option('msc_cache_enabled', true);
$not_logged_in = !is_user_logged_in();
$not_admin = !is_admin();
return $enabled && $not_logged_in && $not_admin;
}
function msc_get_cache_file() {
$cache_key = md5($_SERVER['REQUEST_URI']);
return MSC_CACHE_DIR . $cache_key . '.html';
}
function msc_is_cache_available() {
$cache_file = msc_get_cache_file();
$cache_expiry = get_option('msc_cache_expiry', 3600);
return file_exists($cache_file) && (time() - filemtime($cache_file)) < $cache_expiry;
}
function msc_serve_cache() {
// İstatistikleri Güncelle
msc_update_cache_stats('hit');
// Cache'den içeriği sun
readfile(msc_get_cache_file());
exit;
}
function msc_cache_callback($buffer) {
// İstatistikleri Güncelle
msc_update_cache_stats('miss');
// Yüklenme süresini kaydet
global $msc_start_time;
$load_time = microtime(true) - $msc_start_time;
msc_update_load_time($load_time);
$cache_file = msc_get_cache_file();
if (!is_dir(MSC_CACHE_DIR)) {
mkdir(MSC_CACHE_DIR, 0755, true);
}
file_put_contents($cache_file, $buffer);
return $buffer;
}
// İçerik Güncellemelerini İzleme ve Cache Temizleme
function msc_clear_cache_on_update($post_id) {
if (!get_option('msc_auto_cache_clear', true)) return;
msc_clear_post_cache($post_id);
}
add_action('save_post', 'msc_clear_cache_on_update');
add_action('delete_post', 'msc_clear_cache_on_update');
add_action('trashed_post', 'msc_clear_cache_on_update');
function msc_clear_cache_on_comment($comment_id, $comment_approved, $commentdata) {
if (!get_option('msc_auto_cache_clear', true)) return;
if ($comment_approved) {
$post_id = $commentdata['comment_post_ID'];
msc_clear_post_cache($post_id);
}
}
add_action('comment_post', 'msc_clear_cache_on_comment', 10, 3);
add_action('edit_comment', 'msc_clear_cache_on_comment', 10, 3);
-------------------------------------------------------------------------------------
Kod:Tümünü seç
<?php
// Yönetici Menü Öğesi Ekleme
function msc_add_admin_menu() {
add_options_page(
'My Super Cache Ayarları',
'My Super Cache',
'manage_options',
'my-super-cache',
'msc_options_page'
);
}
add_action('admin_menu', 'msc_add_admin_menu');
// Ayarlar Sayfası İçeriği
function msc_options_page() {
// Yetki Kontrolü
if (!current_user_can('manage_options')) {
return;
}
// Ayarları Kaydetme
if (isset($_POST['msc_settings_submit'])) {
check_admin_referer('msc_settings_nonce');
update_option('msc_cache_enabled', isset($_POST['msc_cache_enabled']));
update_option('msc_cache_expiry', intval($_POST['msc_cache_expiry']));
update_option('msc_auto_cache_clear', isset($_POST['msc_auto_cache_clear']));
msc_clear_cache();
echo '<div class="updated"><p>Ayarlar kaydedildi ve cache temizlendi.</p></div>';
}
// İstatistikleri Sıfırlama
if (isset($_POST['msc_reset_stats'])) {
check_admin_referer('msc_reset_stats_nonce');
delete_option('msc_cache_stats');
delete_option('msc_load_times');
echo '<div class="updated"><p>İstatistikler sıfırlandı.</p></div>';
}
// Cache Bilgilerini ve İstatistikleri Alma
$cache_enabled = get_option('msc_cache_enabled', true);
$cache_expiry = get_option('msc_cache_expiry', 3600);
$auto_cache_clear = get_option('msc_auto_cache_clear', true);
$cache_info = msc_get_cache_info();
$cache_stats = get_option('msc_cache_stats', [
'hit' => 0,
'miss' => 0,
'last_reset' => time(),
]);
// Cache Oranı Hesaplama
$total_requests = $cache_stats['hit'] + $cache_stats['miss'];
$cache_hit_rate = $total_requests > 0 ? ($cache_stats['hit'] / $total_requests) * 100 : 0;
// Yüklenme Sürelerini Alma
$load_times = get_option('msc_load_times', []);
$average_load_time = !empty($load_times) ? array_sum($load_times) / count($load_times) : 0;
?>
<div class="wrap">
<h1>My Super Cache Ayarları</h1>
<form method="post" action="">
<?php wp_nonce_field('msc_settings_nonce'); ?>
<table class="form-table">
<tr>
<th scope="row">Cache Etkinleştir</th>
<td>
<input type="checkbox" name="msc_cache_enabled" <?php checked($cache_enabled, true); ?>>
<p class="description">Cache sistemini etkinleştirmek veya devre dışı bırakmak için işaretleyin.</p>
</td>
</tr>
<tr>
<th scope="row">Cache Süresi (saniye)</th>
<td>
<input type="number" name="msc_cache_expiry" value="<?php echo esc_attr($cache_expiry); ?>" min="60">
<p class="description">Cache'in geçerlilik süresini belirtin. Örneğin, 3600 saniye = 1 saat.</p>
</td>
</tr>
<tr>
<th scope="row">Otomatik Cache Temizleme</th>
<td>
<input type="checkbox" name="msc_auto_cache_clear" <?php checked($auto_cache_clear, true); ?>>
<p class="description">İçerik güncellendiğinde ilgili cache dosyalarını otomatik olarak temizle.</p>
</td>
</tr>
</table>
<?php submit_button('Ayarları Kaydet', 'primary', 'msc_settings_submit'); ?>
</form>
<h2>Cache Bilgisi</h2>
<p><strong>Cache Dosya Sayısı:</strong> <?php echo esc_html($cache_info['file_count']); ?></p>
<p><strong>Toplam Cache Boyutu:</strong> <?php echo size_format($cache_info['total_size']); ?></p>
<form method="post" action="">
<?php wp_nonce_field('msc_clear_cache_nonce'); ?>
<?php submit_button('Cache\'i Temizle', 'secondary', 'msc_clear_cache'); ?>
</form>
<h2>Cache İstatistikleri</h2>
<p><strong>Cache Hit:</strong> <?php echo esc_html($cache_stats['hit']); ?></p>
<p><strong>Cache Miss:</strong> <?php echo esc_html($cache_stats['miss']); ?></p>
<p><strong>Cache Hit Oranı:</strong> <?php echo number_format($cache_hit_rate, 2); ?>%</p>
<p><strong>Ortalama Sayfa Yüklenme Süresi:</strong> <?php echo number_format($average_load_time, 4); ?> saniye</p>
<p><strong>Son Sıfırlama Tarihi:</strong> <?php echo date('Y-m-d H:i:s', $cache_stats['last_reset']); ?></p>
<canvas id="cacheStatsChart" width="400" height="200"></canvas>
<form method="post" action="">
<?php wp_nonce_field('msc_reset_stats_nonce'); ?>
<?php submit_button('İstatistikleri Sıfırla', 'secondary', 'msc_reset_stats'); ?>
</form>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
const ctx = document.getElementById('cacheStatsChart').getContext('2d');
const cacheStatsChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ['Cache Hit', 'Cache Miss'],
datasets: [{
data: [<?php echo $cache_stats['hit']; ?>, <?php echo $cache_stats['miss']; ?>],
backgroundColor: ['#4CAF50', '#F44336'],
}]
},
options: {
responsive: true,
plugins: {
legend: {
position: 'bottom',
},
title: {
display: true,
text: 'Cache Performansı'
}
}
},
});
</script>
<?php
}
// Cache Temizleme İşlemi
function msc_handle_cache_clear() {
if (isset($_POST['msc_clear_cache'])) {
check_admin_referer('msc_clear_cache_nonce');
msc_clear_cache();
wp_redirect(admin_url('options-general.php?page=my-super-cache&cache_cleared=1'));
exit;
}
}
add_action('admin_init', 'msc_handle_cache_clear');
// İstatistikleri Sıfırlama İşlemi
function msc_handle_stats_reset() {
if (isset($_POST['msc_reset_stats'])) {
check_admin_referer('msc_reset_stats_nonce');
delete_option('msc_cache_stats');
delete_option('msc_load_times');
wp_redirect(admin_url('options-general.php?page=my-super-cache&stats_reset=1'));
exit;
}
}
add_action('admin_init', 'msc_handle_stats_reset');
// Yönetici Sayfası Stil ve Scriptleri
function msc_enqueue_admin_scripts($hook) {
if ($hook !== 'settings_page_my-super-cache') {
return;
}
wp_enqueue_script('chartjs', 'https://cdn.jsdelivr.net/npm/chart.js', [], '3.7.0', true);
}
add_action('admin_enqueue_scripts', 'msc_enqueue_admin_scripts');
-------------------------------------------------------------------------------------
Kod:Tümünü seç
<?php
// Cache Temizleme Fonksiyonu
function msc_clear_cache() {
if (!is_dir(MSC_CACHE_DIR)) {
return;
}
$files = glob(MSC_CACHE_DIR . '*.html');
if ($files) {
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
}
}
}
}
function msc_clear_post_cache($post_id) {
$permalink = get_permalink($post_id);
$cache_key = md5(parse_url($permalink, PHP_URL_PATH));
$cache_file = MSC_CACHE_DIR . $cache_key . '.html';
// Cache dosyası varsa sil
if (file_exists($cache_file)) {
unlink($cache_file);
}
// İlgili kategori ve etiket sayfalarının cache'ini temizle
msc_clear_taxonomy_cache($post_id);
}
function msc_clear_taxonomy_cache($post_id) {
$taxonomies = get_object_taxonomies(get_post_type($post_id));
foreach ($taxonomies as $taxonomy) {
$terms = wp_get_post_terms($post_id, $taxonomy);
foreach ($terms as $term) {
$term_link = get_term_link($term, $taxonomy);
$cache_key = md5(parse_url($term_link, PHP_URL_PATH));
$cache_file = MSC_CACHE_DIR . $cache_key . '.html';
if (file_exists($cache_file)) {
unlink($cache_file);
}
}
}
}
// Cache Bilgisi Alma Fonksiyonu
function msc_get_cache_info() {
$file_count = 0;
$total_size = 0;
if (is_dir(MSC_CACHE_DIR)) {
$files = glob(MSC_CACHE_DIR . '*.html');
$file_count = count($files);
foreach ($files as $file) {
$total_size += filesize($file);
}
}
return [
'file_count' => $file_count,
'total_size' => $total_size,
];
}
// Cache İstatistiklerini Güncelleme
function msc_update_cache_stats($type) {
$stats = get_option('msc_cache_stats', [
'hit' => 0,
'miss' => 0,
'last_reset' => time(),
]);
if ($type === 'hit') {
$stats['hit'] += 1;
} elseif ($type === 'miss') {
$stats['miss'] += 1;
}
update_option('msc_cache_stats', $stats);
}
// Yüklenme Süresini Güncelleme
function msc_update_load_time($load_time) {
$load_times = get_option('msc_load_times', []);
$load_times[] = $load_time;
// Son 100 isteği tut
if (count($load_times) > 100) {
array_shift($load_times);
}
update_option('msc_load_times', $load_times);
}
-------------------------------------------------------------------------------------