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

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

#1564
even-eleven
Participant

    無事にbcacheをクリアすることができました。
    ありがとうございます。
    DBでキャッシュがクリアされることも確認しまして、問題なく動いております。
    1点、知らなかったのですが、コメントを自動承認しているとcomment_approved_commentが発生せず、承認状態でcomment_postだけ発生するようでして、comment_postを拾った上で承認済みかチェックする形になりました。

    add_action( 'comment_post', 'my_clear_single_cache', 10, 2 );
    function my_clear_single_cache( $comment_id, $comment_approved ) {
    if ( 1 !== $comment_approved ) {
    return;
    }
    $comment = get_comment( $comment_id );
    if ( ! $comment ) {
    return;
    }

    $post_id = $comment->comment_post_ID;

    global $cache_db;
    if ( ! is_object( $cache_db ) || ! method_exists( $cache_db, 'prepare' ) ) {
    error_log( 'KUSANAGI bcache clear: cache_db object not available. Cannot clear bcache for comment_id: ' . $comment_id );
    return;
    }

    $regexes = get_option( 'sitemanager_device_rules', array() );
    $groups = array_keys( $regexes );
    $groups = array_merge( array( '' ), $groups );

    $url = get_permalink( $post_id );
    $permalink = parse_url( $url );
    if ( ! $permalink || ! isset( $permalink['path'], $permalink['scheme'], $permalink['host'] ) ) {
    error_log('Failed to parse permalink or missing components for comment_id: ' . $comment_id);
    return;
    }

    $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 );
    }

    if ( empty( $hashes ) ) {
    return;
    }

    $placeholders = implode( ', ', array_fill( 0, count( $hashes ), '%s' ) );
    $sql = $cache_db->prepare(
    "DELETE FROM {$cache_db->prefix}site_cache WHERE type = 'single' AND hash IN ( $placeholders )",
    $hashes
    );

    if ( $sql ) {
    $cache_db->query( $sql );
    }

    }