Sử dụng Filters trong Init Chat Engine

Plugin Init Chat Engine không chỉ là một khung chat realtime đơn thuần – nó được thiết kế như một nền tảng mở, cho phép developer can thiệp sâu bằng filters và actions. Thông qua các hook này, bạn có thể bổ sung metadata vào tin nhắn, thay đổi cách plugin lọc từ cấm, mở rộng giao diện frontend hoặc tích hợp với hệ thống user / VIP / profile của riêng bạn mà không phải sửa core plugin. Bài viết này cung cấp các ví dụ thực tế, sẵn dùng để copy vào theme hoặc plugin của bạn.

Sử dụng Filters trong Init Chat Engine

Init Chat Engine cung cấp các filters/actions cho phép developer can thiệp vào quá trình xử lý tin nhắn, mở rộng dữ liệu JSON của API, hoặc thay đổi hành vi UI. Tất cả ví dụ dưới đây có thể đặt vào functions.php (theme) hoặc custom plugin của bạn.

1. Chuyển chiến lược lọc từ cấm

Mặc định plugin dùng chiến lược substring (hung hãn, bắt cả “https://” trong đoạn chữ). Nếu bạn muốn chỉ bắt những từ tách biệt (như “spam”, nhưng không bắt “spamming”), dùng strategy word.

add_filter(
    'init_plugin_suite_chat_engine_word_filter_strategy',
    function ( $strategy, $settings, $message ) {
        return 'word'; // hoặc 'regex'
    },
    10,
    3
);

2. Bổ sung từ cấm từ code (không cần UI)

Ví dụ thêm các domain spam như Discord, Telegram và URL dạng https://.

add_filter(
    'init_plugin_suite_chat_engine_blocked_words',
    function ( $words, $settings, $message ) {
        $words[] = 'https://';
        $words[] = 'discord.gg';
        $words[] = 't.me';
        return array_values( array_unique( $words ) );
    },
    10,
    3
);

3. Bypass lọc khi user là VIP hoặc Moderator

Nếu user có meta ice_vip = 1 hoặc có quyền moderate_comments thì bỏ qua lọc từ.

add_filter(
    'init_plugin_suite_chat_engine_bypass_filter',
    function ( $bypass, $message, $user, $settings ) {
        if ( $user instanceof WP_User ) {
            if ( user_can( $user, 'moderate_comments' ) ) {
                return true;
            }
            $vip = get_user_meta( $user->ID, 'ice_vip', true );
            if ( (int) $vip === 1 ) {
                return true;
            }
        }
        return $bypass;
    },
    10,
    4
);

4. Ghi log khi có tin nhắn bị chặn

Hữu ích trong quá trình debug hoặc audit nội dung người dùng gửi.

add_action(
    'init_plugin_suite_chat_engine_word_block_hit',
    function ( $blocked_word, $raw_message, $strategy ) {
        if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
            error_log(
                sprintf(
                    '[ICE] Blocked | word="%s" | strategy=%s | msg="%s"',
                    $blocked_word,
                    $strategy,
                    wp_strip_all_tags( $raw_message )
                )
            );
        }
    },
    10,
    3
);

5. Enrich message: thêm profile_url, VIP flag, role

Hook này cho phép bạn thêm dữ liệu vào mỗi message trước khi trả về JSON — cực hữu ích nếu frontend muốn hiển thị huy hiệu VIP, role, hoặc link profile.

add_filter(
    'init_plugin_suite_chat_engine_enrich_message_row',
    function ( $row, $user ) {
        if ( $user instanceof WP_User ) {
            $row['roles']       = array_values( (array) $user->roles );
            $row['is_admin']    = user_can( $user, 'manage_options' ) ? 1 : 0;
            $row['vip']         = (int) get_user_meta( $user->ID, 'ice_vip', true );
            $row['profile_url'] = get_author_posts_url( $user->ID );
        } else {
            $row['roles']       = [];
            $row['is_admin']    = 0;
            $row['vip']         = 0;
            $row['profile_url'] = '';
        }
        return $row;
    },
    10,
    2
);

6. Frontend: biến tên user thành link profile

Hook này chạy trên frontend sau mỗi tin nhắn được render. Không cần chỉnh HTML gốc của plugin.

// USER PROFILE INIT CHAT ENGINE
window.initChatEngineMessageElementHook = function (el, msg, isCurrentUser) {
    if (!msg) return;

    // 1) Link tên đến profile_url (nếu có)
    if (msg.profile_url) {
        const nameEl = el.querySelector('.init-chatbox-author');
        if (nameEl && nameEl.tagName !== 'A') {
            const a = document.createElement('a');
            a.href = msg.profile_url;
            a.rel = 'noopener';
            a.className = nameEl.className + ' uk-link-reset';
            a.textContent = nameEl.textContent;
            nameEl.replaceWith(a);
        }
    }
};

7. Ví dụ bật regex toàn hệ thống

// Bật regex cho word filter
add_filter(
    'init_plugin_suite_chat_engine_word_filter_strategy',
    fn( $strategy, $settings, $message ) => 'regex',
    10,
    3
);

add_filter(
    'init_plugin_suite_chat_engine_blocked_words',
    function ( $words ) {
        return [
            '/https?:\/\/[^\s]+/iu',
            '/\b(discord\.gg|t\.me)\b/iu'
        ];
    },
    10,
    3
);

Bình luận


  • Không có bình luận.

Init Toolbox

Nhấn Ctrl + \ trên máy tính, hoặc vuốt sang trái ở bất kỳ đâu trên mobile.

Đăng nhập





Đang tải...