' ) || ! preg_match( '/]/i', $html ) ) { return $html; } foreach ( headers_list() as $h ) { if ( 0 === stripos( $h, 'content-type:' ) && false === stripos( $h, 'text/html' ) ) { return $html; } } $start = microtime( true ); $html = tibospeed_optimize( $html ); $cache = ! empty( $GLOBALS['tibospeed_cache_this'] ) && 200 === http_response_code() && ! ( defined( 'DONOTCACHEPAGE' ) && DONOTCACHEPAGE ) && ! is_404(); if ( $cache ) { $uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '/'; $host = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : ''; $dir = tibospeed_url_dir( ( is_ssl() ? 'https://' : 'http://' ) . $host . strtok( $uri, '?' ) ); if ( $dir ) { $mobile = tibospeed_get( 'cache_mobile' ) && wp_is_mobile(); $stamp = "\n\n"; $file = $dir . '/' . ( $mobile ? 'index-mobile.html' : 'index.html' ); if ( tibospeed_write( $file, $html . $stamp ) && function_exists( 'gzencode' ) ) { tibospeed_write( $file . '.gz', gzencode( $html . $stamp, 6 ) ); } if ( ! headers_sent() ) { header( 'X-TiboSpeed: ' . ( defined( 'TIBOSPEED_MISS' ) ? 'miss, saved' : 'saved' ) ); } } } return $html; } /* -------------------------------------------------------------------------- Temizleme */ /** * Bir adresin önbelleğini sil (alt sayfalarına dokunmaz). * * @param string $url Adres. * @param bool $paged Sayfalama (/page/2/…) klasörlerini de sil. * @return bool */ function tibospeed_purge_url( $url, $paged = false ) { $dir = tibospeed_url_dir( $url ); if ( ! $dir || ! is_dir( $dir ) ) { return false; } foreach ( array( 'index.html', 'index.html.gz', 'index-mobile.html', 'index-mobile.html.gz' ) as $f ) { if ( is_file( $dir . '/' . $f ) ) { tibospeed_fs()->delete( $dir . '/' . $f ); } } if ( $paged ) { tibospeed_rmdir( $dir . '/page' ); } do_action( 'tibospeed_purged_url', $url ); return true; } /** * Tüm sayfa önbelleğini sil. */ function tibospeed_purge_all() { tibospeed_rmdir( tibospeed_dir( 'html' ) ); wp_mkdir_p( tibospeed_dir( 'html' ) ); update_option( 'tibospeed_purged', time(), false ); do_action( 'tibospeed_purged_all' ); } /** * Bir yazı değişince etkilenen adresler. * * @param int $id Yazı. * @return array [ adres => sayfalama da silinsin mi ] */ function tibospeed_related_urls( $id ) { $post = get_post( $id ); if ( ! $post ) { return array(); } $u = array(); $u[ home_url( '/' ) ] = true; $link = get_permalink( $post ); if ( $link ) { $u[ $link ] = false; } $pfp = (int) get_option( 'page_for_posts' ); if ( $pfp && 'post' === $post->post_type ) { $u[ get_permalink( $pfp ) ] = true; } $arch = get_post_type_archive_link( $post->post_type ); if ( $arch ) { $u[ $arch ] = true; } foreach ( get_object_taxonomies( $post->post_type, 'objects' ) as $tax ) { if ( ! $tax->public ) { continue; } $terms = get_the_terms( $post, $tax->name ); if ( $terms && ! is_wp_error( $terms ) ) { foreach ( $terms as $t ) { $l = get_term_link( $t ); if ( ! is_wp_error( $l ) ) { $u[ $l ] = true; } foreach ( get_ancestors( $t->term_id, $tax->name ) as $a ) { $l = get_term_link( (int) $a, $tax->name ); if ( ! is_wp_error( $l ) ) { $u[ $l ] = true; } } } } } if ( post_type_supports( $post->post_type, 'author' ) ) { $u[ get_author_posts_url( (int) $post->post_author ) ] = true; } if ( 'product' === $post->post_type && function_exists( 'wc_get_page_permalink' ) ) { $u[ wc_get_page_permalink( 'shop' ) ] = true; } if ( 'page' === $post->post_type && $post->post_parent ) { $u[ get_permalink( $post->post_parent ) ] = false; } return apply_filters( 'tibospeed_related_urls', $u, $post ); } /** * Yazı ve ilgili sayfaları temizle, sonra yeniden ısıt. * * @param int $id Yazı. */ function tibospeed_purge_post( $id ) { static $done = array(); if ( isset( $done[ $id ] ) || wp_is_post_revision( $id ) || wp_is_post_autosave( $id ) ) { return; } $done[ $id ] = 1; $pt = get_post_type( $id ); if ( ! $pt || ! is_post_type_viewable( $pt ) ) { // Elementor şablonu, menü öğesi gibi görünmeyen türler: tüm sayfalarda kullanılıyor olabilir. if ( in_array( $pt, array( 'elementor_library', 'wp_template', 'wp_template_part', 'wp_navigation', 'wp_global_styles', 'wp_block' ), true ) ) { tibospeed_purge_all(); tibospeed_preload_all(); } return; } $urls = tibospeed_related_urls( $id ); foreach ( $urls as $u => $paged ) { tibospeed_purge_url( $u, $paged ); } tibospeed_preload_queue( array_keys( $urls ) ); } // Yazı yayınlandı, güncellendi, yayından kalktı, çöpe atıldı. add_action( 'transition_post_status', function ( $new, $old, $post ) { if ( 'publish' === $new || 'publish' === $old ) { tibospeed_purge_post( $post->ID ); } }, 10, 3 ); add_action( 'post_updated', function ( $id, $after, $before ) { // Kalıcı bağlantı değiştiyse eski adresin önbelleği de gitsin. if ( 'publish' === $before->post_status && $before->post_name !== $after->post_name ) { tibospeed_purge_url( get_permalink( $before ) ); } }, 10, 3 ); add_action( 'before_delete_post', 'tibospeed_purge_post' ); add_action( 'wp_set_comment_status', function ( $cid ) { $c = get_comment( $cid ); if ( $c ) { tibospeed_purge_post( (int) $c->comment_post_ID ); } } ); add_action( 'comment_post', function ( $cid, $approved ) { if ( 1 === $approved ) { $c = get_comment( $cid ); tibospeed_purge_post( (int) $c->comment_post_ID ); } }, 10, 2 ); // WooCommerce: stok ve fiyat. foreach ( array( 'woocommerce_product_set_stock', 'woocommerce_variation_set_stock' ) as $tibospeed_h ) { add_action( $tibospeed_h, function ( $product ) { $id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id(); tibospeed_purge_post( $id ); } ); } // Sitenin tamamını etkileyen değişiklikler. foreach ( array( 'switch_theme', 'customize_save_after', 'wp_update_nav_menu', 'update_option_sidebars_widgets', 'activated_plugin', 'deactivated_plugin', 'update_option_permalink_structure', 'update_option_blogname', 'update_option_blogdescription', 'elementor/core/files/clear_cache', 'update_option_wtk_settings', 'update_option_wts_settings', 'update_option_tibokvkk_settings', 'update_option_tiboseo_settings', 'woocommerce_settings_saved' ) as $tibospeed_h ) { add_action( $tibospeed_h, function () { tibospeed_purge_all(); tibospeed_preload_all(); } ); } add_action( 'upgrader_process_complete', function () { tibospeed_purge_all(); tibospeed_preload_all(); } ); // Tema ayarları (Tibo temaları dahil her tema) değişince. add_action( 'updated_option', function ( $opt ) { if ( 0 === strpos( $opt, 'theme_mods_' ) || in_array( $opt, array( 'elementor_active_kit' ), true ) ) { tibospeed_purge_all(); } } ); /* -------------------------------------------------------------------------- Yönetim çubuğu */ add_action( 'admin_bar_menu', function ( $bar ) { if ( ! current_user_can( 'manage_options' ) ) { return; } $bar->add_node( array( 'id' => 'tibospeed', 'title' => 'TiboSpeed', 'href' => admin_url( 'admin.php?page=tibospeed' ), ) ); $bar->add_node( array( 'parent' => 'tibospeed', 'id' => 'tibospeed-all', 'title' => esc_html__( 'Clear all cache', 'tibospeed' ), 'href' => wp_nonce_url( admin_url( 'admin-post.php?action=tibospeed_purge&what=all' ), 'tibospeed_purge' ), ) ); if ( ! is_admin() ) { global $wp; $bar->add_node( array( 'parent' => 'tibospeed', 'id' => 'tibospeed-this', 'title' => esc_html__( 'Clear the cache for this page', 'tibospeed' ), 'href' => wp_nonce_url( admin_url( 'admin-post.php?action=tibospeed_purge&what=url&u=' . rawurlencode( home_url( add_query_arg( array(), $wp->request ) . '/' ) ) ), 'tibospeed_purge' ), ) ); $bar->add_node( array( 'parent' => 'tibospeed', 'id' => 'tibospeed-test', 'title' => esc_html__( 'View this page without optimization', 'tibospeed' ), 'href' => add_query_arg( 'tibospeed', 'off' ), ) ); } }, 90 ); add_action( 'admin_post_tibospeed_purge', function () { if ( ! current_user_can( 'manage_options' ) ) { wp_die( '', '', array( 'response' => 403 ) ); } check_admin_referer( 'tibospeed_purge' ); $what = isset( $_GET['what'] ) ? sanitize_key( wp_unslash( $_GET['what'] ) ) : 'all'; if ( 'url' === $what && ! empty( $_GET['u'] ) ) { $u = esc_url_raw( wp_unslash( $_GET['u'] ) ); tibospeed_purge_url( $u ); tibospeed_preload_queue( array( $u ) ); } else { tibospeed_purge_all(); tibospeed_preload_all(); } $back = wp_get_referer() ? wp_get_referer() : admin_url( 'admin.php?page=tibospeed' ); wp_safe_redirect( add_query_arg( 'tibospeed_msg', 'purged', $back ) ); exit; } ); /** * Önbellek istatistikleri. * * @return array [ sayfa sayısı, bayt ] */ function tibospeed_cache_stats() { $n = 0; $size = 0; $dir = tibospeed_dir( 'html' ); if ( is_dir( $dir ) ) { $it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir, FilesystemIterator::SKIP_DOTS ) ); foreach ( $it as $f ) { if ( $f->isFile() ) { $size += $f->getSize(); if ( 'html' === $f->getExtension() ) { ++$n; } } } } return array( $n, $size ); }