Reply To: コメント承認時にbcacheをクリアする方法

TOP Forums 使い方全般(Fixing KUSANAGI) コメント承認時にbcacheをクリアする方法 Reply To: コメント承認時にbcacheをクリアする方法

#1562
katakura
Participant

    こんにちは、 even-eleven さん。

    bcache on の状態で、コメントが承認されたタイミングにキャッシュクリアしたい場合は、以下のコードをテーマの functions.php に組み込んでみてください。

    
    add_action( 'comment_approved_comment', 'my_clear_single_cache', 10, 2 );    
    function my_clear_single_cache( $comment_id, $comment ) {
            global $cache_db;
     
            $regexes = get_option( 'sitemanager_device_rules', array() );
            $groups  = array_keys( $regexes );
            $groups  = array_merge( array( '' ), $groups );
     
            $permalink = get_permalink( $comment->comment_post_ID );
            $permalink = parse_url( $permalink );
            $path      = $permalink['path'];
            if ( isset( $permalink['query'] ) && $permalink['query'] ) {
                $path .= '?' . $permalink['query'];
            }
     
            $hashes = array();
            foreach ( $groups as $group ) {
                $device_url = array(
                    $group,
                    $permalink['scheme'],
                    $permalink['host'],
                    $path,
                );
                $device_url = implode( '|', $device_url );
                $hashes[]   = md5( $device_url );
            }
            $hashes = implode( "', '", $hashes );
     
            $sql = "
    DELETE
    FROM    <code>{$cache_db->prefix}site_cache</code>
    WHERE   <code>type</code> = 'single'
    AND     <code>hash</code> IN ( '{$hashes}' )
    ";
            $cache_db->query( $sql );
    }