user_login; } } // Prevent bot from responding to its own posts $post_author_id = get_post_field( 'post_author', $post_id ); if ( $bot_user_id && $post_author_id == $bot_user_id ) { return false; } // Check forum access restrictions if ( ! $this->is_forum_allowed( $forum_id ) ) { return false; } // 1. Check for mention (only if bot username is configured) if ( $this->has_bot_mention( $post_content, $bot_username ) ) { return true; } // 2. Check for keywords if ( $this->has_trigger_keywords( $post_content ) ) { return true; } // Add other trigger conditions here (e.g., scheduled tasks) return false; // No trigger condition met } /** * Check if the forum is allowed based on access control settings * * @param int $forum_id The forum ID to check * @return bool True if forum is allowed, false otherwise */ private function is_forum_allowed( $forum_id ) { $restriction_mode = get_option( 'ai_bot_forum_restriction', 'all' ); if ( $restriction_mode === 'selected' ) { $allowed_forums = get_option( 'ai_bot_allowed_forums', array() ); if ( ! empty( $allowed_forums ) && ! in_array( $forum_id, (array) $allowed_forums ) ) { return false; } } return true; } /** * Check if post content contains a bot mention * * @param string $post_content The post content to check * @param string $bot_username The bot's username (or null if not configured) * @return bool True if bot is mentioned, false otherwise */ private function has_bot_mention( $post_content, $bot_username ) { if ( $bot_username && preg_match( '/@' . preg_quote( $bot_username, '/' ) . '/i', $post_content ) ) { return true; } return false; } /** * Check if post content contains any trigger keywords * * @param string $post_content The post content to check * @return bool True if trigger keywords are found, false otherwise */ private function has_trigger_keywords( $post_content ) { $keywords_string = get_option( 'ai_bot_trigger_keywords', '' ); if ( ! empty( $keywords_string ) ) { // Split keywords by comma or newline, trim whitespace, remove empty entries $keywords = preg_split( '/[\s,]+/', $keywords_string, -1, PREG_SPLIT_NO_EMPTY ); $keywords = array_map( 'trim', $keywords ); $keywords = array_filter( $keywords ); if ( ! empty( $keywords ) ) { // Create a regex pattern to match any keyword (case-insensitive) $escaped_keywords = array_map( function( $keyword ) { return preg_quote( $keyword, '/' ); }, $keywords ); $pattern = '/\b(' . implode( '|', $escaped_keywords ) . ')\b/i'; if ( preg_match( $pattern, $post_content ) ) { return true; } } } return false; } }