/** * Plugin Name: OPAIS Page Cache - Purge automático * Description: Limpa a page cache (advanced-cache.php / Redis) sempre que * um artigo é publicado, editado, apagado, ou recebe um * comentário aprovado. Sem dependência de nenhum plugin de * cache — funciona em conjunto com o drop-in advanced-cache.php. * Destino: /home/opaisao/public_html/wp-content/mu-plugins/opais-page-cache-purge.php */ if ( ! defined( 'ABSPATH' ) ) { exit; } // phpcs:disable /** * Liga ao Redis, reutilizando as constantes já definidas no wp-config.php. * Falha de forma silenciosa: se o Redis não estiver acessível, a purga * simplesmente não acontece (a TTL de segurança do advanced-cache.php * garante que a página não fica desactualizada para sempre). */ function opais_pc_purge_redis() { static $redis = null; static $tried = false; if ( null !== $redis || $tried ) { return $redis; } $tried = true; if ( ! class_exists( 'Redis' ) ) { return null; } try { $host = defined( 'WP_REDIS_HOST' ) ? WP_REDIS_HOST : '127.0.0.1'; $port = defined( 'WP_REDIS_PORT' ) ? (int) WP_REDIS_PORT : 6379; $database = defined( 'WP_REDIS_DATABASE' ) ? (int) WP_REDIS_DATABASE : 0; $r = new Redis(); if ( ! $r->pconnect( $host, $port, 1.0, 'opais-pagecache-pool' ) ) { return null; } if ( $database > 0 ) { $r->select( $database ); } $redis = $r; } catch ( Exception $e ) { $redis = null; } return $redis; } function opais_pc_key_for_url( $url ) { $parts = wp_parse_url( $url ); $host = isset( $parts['host'] ) ? $parts['host'] : ''; $path = isset( $parts['path'] ) ? $parts['path'] : '/'; return 'opais:page:' . md5( $host . $path ); } function opais_pc_purge_url( $url ) { if ( empty( $url ) ) { return; } $redis = opais_pc_purge_redis(); if ( ! $redis ) { return; } try { $redis->del( opais_pc_key_for_url( $url ) ); } catch ( Exception $e ) { // Falha silenciosa. } } /** * Purga o próprio artigo, a homepage, e todos os arquivos de * categoria/tag a que pertence (apenas a primeira página de cada; * páginas seguintes de paginação ficam cobertas pela TTL de segurança). */ function opais_pc_purge_post( $post_id ) { // Ignorar revisões e autosaves. if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) { return; } $post = get_post( $post_id ); if ( ! $post ) { return; } // Só faz sentido purgar tipos de conteúdo públicos. $post_type_obj = get_post_type_object( $post->post_type ); if ( ! $post_type_obj || ! $post_type_obj->public ) { return; } $urls = array(); $urls[] = get_permalink( $post_id ); $urls[] = home_url( '/' ); $categories = get_the_category( $post_id ); if ( $categories ) { foreach ( $categories as $cat ) { $urls[] = get_category_link( $cat->term_id ); } } $tags = get_the_tags( $post_id ); if ( $tags ) { foreach ( $tags as $tag ) { $urls[] = get_tag_link( $tag->term_id ); } } $urls = array_unique( array_filter( $urls ) ); foreach ( $urls as $url ) { opais_pc_purge_url( $url ); } } // Publicação/edição de qualquer post público. add_action( 'save_post', 'opais_pc_purge_post', 20, 1 ); // Cobre mudanças de estado (ex: agendado -> publicado, publicado -> rascunho). add_action( 'transition_post_status', function ( $new_status, $old_status, $post ) { if ( 'publish' === $new_status || 'publish' === $old_status ) { opais_pc_purge_post( $post->ID ); } }, 20, 3 ); // Apagar / mover para o lixo. add_action( 'wp_trash_post', 'opais_pc_purge_post' ); add_action( 'delete_post', 'opais_pc_purge_post' ); // Comentário aprovado (novas respostas alteram o HTML da página do artigo). add_action( 'comment_post', function ( $comment_id, $comment_approved ) { if ( 1 === (int) $comment_approved ) { $comment = get_comment( $comment_id ); if ( $comment ) { opais_pc_purge_post( $comment->comment_post_ID ); } } }, 20, 2 ); // Comentário aprovado manualmente a partir do painel (moderação). add_action( 'transition_comment_status', function ( $new_status, $old_status, $comment ) { if ( 'approved' === $new_status ) { opais_pc_purge_post( $comment->comment_post_ID ); } }, 20, 3 ); Jornal Opais – Página 828 – OPaís
Jornal Opais

Jornal Opais

Página 828 de 2036 1 827 828 829 2.036