=== Team Messenger ===
Contributors: syeedalireza, AlirezaAminadeh
Tags: chat, messaging, team, groups, encryption
Requires at least: 6.2
Tested up to: 6.9
Requires PHP: 7.4
Stable tag: 1.0.8
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Real-time team chat for logged-in users with optional E2E, groups, notifications, and admin UI.

== Description ==

Team Messenger is a WordPress plugin that adds secure, real-time messaging for logged-in users. It combines direct (1:1) chats, group rooms, optional end-to-end encryption for private threads, media uploads with administrator-controlled policies, presence, notification badges, pinned messages, and dedicated UIs on both the public site and in wp-admin.

Technical details and changelog notes may be published on the plugin support page or documentation as they are updated.

=== English — Numbered feature list ===

1. Real-time direct messaging between logged-in WordPress users over the REST API (`team_messenger/v1`), with pagination for message history.

2. Optional end-to-end encryption (E2E) for direct messages: clients can exchange messages flagged as encrypted; public keys are stored per user via dedicated REST endpoints (`/keys`). Group chats remain readable on the server (plaintext server-side).

3. Full group chat lifecycle: create, read, update, and delete groups; set name, description, and a group avatar; list members; add or remove members; promote or demote roles via PATCH on members.

4. Rich media in chats: images, video, and audio attachments; optional voice messages recorded in the browser (MediaRecorder, typically WebM/WebA) with server-side MIME handling for reliable uploads.

5. Two upload contexts on `/media/upload`: `chat` for conversation attachments and `avatar` for group profile images, each with independent size and format rules enforced on the server.

6. Administrator-configurable media policy: separate maximum file sizes for chat (0.1–100 MB) and group avatars (0.1–50 MB); toggles for allowed chat categories (image / video / audio) and allowed avatar formats (e.g. JPEG, PNG, GIF, WebP). Limits are exposed to the client as hints; the server performs final validation.

7. Flexible storage for uploaded files: attachments can live in the WordPress Media Library or in a plugin-protected directory (`.htaccess` where applicable), with authenticated download through `/media/download/{file}` for the private storage path.

8. “Saved messages” (self-chat): a personal notebook thread with the current user as both sides, for notes and files.

9. Recent conversations: when no search query is sent, the user list prioritizes people you have recently messaged, without exposing the entire user directory.

10. Privacy-conscious user search: requires at least three characters and returns a capped number of results.

11. Presence (online / last seen): clients send a throttled heartbeat to `POST /presence`; `last_seen` and `is_online` are returned in user lists only when the viewer has shared DM history with that user or shares a group (privacy-preserving).

12. Notifications: `GET /notifications/counts` returns separate counts and text previews for new personal and group activity since the last acknowledgment; `POST /notifications/ack` supports `scope`: `all`, `personal`, or `group`.

13. WordPress admin bar integration: when the toolbar is visible, separate nodes show personal and group chat badges; dropdowns load preview snippets (periodic polling) and allow clearing notifications by scope. Non-administrators link to the site home; users with `manage_options` can jump to the full messenger screen.

14. Dashboard widgets (administrators): two WordPress Dashboard widgets—personal (1:1) and group—provide inline chat with the same REST capabilities as the main messenger (including pins and upload limits), visible only to users with `manage_options` who have not disabled admin chat.

15. Pinned messages: one pin per direct thread (stored in `team_messenger_direct_pins`) and one per group (`pinned_message_id`); pin/unpin from the floating widget, `[team_messenger_web_chat]`, admin messenger, and dashboard widgets; a pinned strip shows a snippet and scroll-to-message; pins clear if the underlying message is deleted.

16. Read state: opening a direct thread marks incoming messages from the partner as read (`is_read`). For groups, per-user last-read timestamps sync when the thread is opened; your own outgoing group messages can show read receipts (“sent” vs “read”) when all other members have caught up.

17. Message lifecycle: edit or soft-delete your own messages in direct and group threads (API supports edit/delete on owned messages).

18. Data retention: a daily scheduled event permanently removes soft-deleted direct and group messages whose `deleted_at` is older than 30 days (GDPR-oriented housekeeping).

19. Multiple surfaces: floating chat widget on the front end; full-page `[team_messenger_web_chat]` shortcode with configurable HTML fallback for guests; dedicated admin “Messenger” screen; optional floating FAB on all wp-admin screens (toggle in settings, respects per-user “hide admin chat”); admin bar badges; two dashboard widgets for admins.

20. Customization in settings: primary accent color, widget corner placement, welcome text, shortcode guest content (visual/HTML editor), and site-wide “show messenger on all wp-admin screens.”

21. Per-user preferences on the user profile: optionally hide front-end chat (widget + shortcode UI) and/or all wp-admin chat (floating messenger, full messenger page, admin bar nodes, dashboard widgets). Shortcode pages show a short notice if frontend chat is disabled for that user.

22. Abuse protection: rate limiting on sending messages and on creating groups to reduce spam and overload.

23. Internationalization: text domain `team-messenger`; Persian (`fa_IR`) and other translations can ship via the `languages` folder; English is the source language.

=== فارسی — فهرست شماره‌دار امکانات ===

۱. پیام‌رسانی مستقیم (یک‌به‌یک) بین کاربران واردشدهٔ وردپرس از طریق REST API با نام فضای `team_messenger/v1` و صفحه‌بندی تاریخچهٔ پیام.

۲. رمزنگاری سرتاسری اختیاری برای گفتگوی خصوصی: امکان ذخیرهٔ پیام با پرچم رمزشده؛ کلیدهای عمومی با endpointهای `/keys` ذخیره و بازیابی می‌شوند. در گروه، متن روی سرور به‌صورت متن عادی قابل پردازش است.

۳. مدیریت کامل چت گروهی: ایجاد، ویرایش، حذف گروه؛ نام، توضیحات و آواتار گروه؛ فهرست و افزودن/حذف عضو؛ تغییر نقش با PATCH روی عضو.

۴. رسانهٔ غنی: تصویر، ویدیو و صوت؛ پیام صوتی با ضبط در مرورگر (معمولاً WebM/WebA) و نرمال‌سازی MIME در سرور.

۵. دو زمینهٔ آپلود در `/media/upload`: `chat` برای پیوست گفتگو و `avatar` برای تصویر پروفایل گروه، هر کدام با سقف حجم و فرمت مستقل و اعتبارسنجی سروری.

۶. سیاست رسانه از پنل مدیر: حداکثر حجم جدا برای چت (۰٫۱ تا ۱۰۰ مگابایت) و آواتار (۰٫۱ تا ۵۰ مگابایت)؛ روشن/خاموش بودن دسته‌های مجاز چت و فرمت‌های مجاز آواتار؛ اعداد به‌عنوان راهنما به کلاینت می‌رود، تصمیم نهایی با سرور است.

۷. ذخیره‌سازی انعطاف‌پذیر: کتابخانهٔ رسانهٔ وردپرس یا پوشهٔ محافظت‌شدهٔ افزونه با `.htaccess`؛ برای مسیر اختصاصی، دانلود امن با `/media/download/{file}` فقط برای کاربر احراز هویت‌شده.

۸. «پیام‌های ذخیره‌شده» (چت با خود): فضای شخصی برای یادداشت و فایل با همان کاربر به‌عنوان طرف گفتگو.

۹. گفتگوهای اخیر: بدون query جستجو، اولویت با کسانی است که اخیراً پیام داده‌اید؛ کل دایرکتوری کاربران افشا نمی‌شود.

۱۰. جستجوی کاربر با حریم خصوصی: حداقل سه کاراکتر و سقف تعداد نتیجه.

۱۱. حضور (آنلاین / آخرین فعالیت): ضربان throttled با `POST /presence`؛ نمایش `last_seen` و `is_online` فقط اگر سابقهٔ پیام خصوصی مشترک یا عضویت گروه مشترک وجود داشته باشد.

۱۲. اعلان‌ها: `GET /notifications/counts` شمارنده و پیش‌نمایش متن برای پیام شخصی و گروهی از زمان آخرین تأیید؛ `POST /notifications/ack` با `all` / `personal` / `group`.

۱۳. نوار ابزار وردپرس: دو گره جدا برای چت شخصی و گروهی با شمارنده و منوی کشویی (پolling برای پیش‌نمایش؛ پاک‌سازی محدوده‌ای). غیرمدیر به خانه؛ مدیر با `manage_options` به پیام‌رسان کامل.

۱۴. ویجت داشبورد (مدیران): دو ویجت جدا در پیشخوان وردپرس برای گفتگوی شخصی و گروهی با همان API (از جمله پین و محدودیت آپلود)؛ فقط برای `manage_options` و اگر چت ادمین خاموش نباشد.

۱۵. پیام پین‌شده: یک پین برای هر گفتگوی دو نفره و یک پین برای هر گروه؛ در ویجت، شورت‌کد، پیام‌رسان ادمین و داشبورد؛ نوار پین با اسکرول به پیام؛ با حذف پیام، پین هم حذف می‌شود.

۱۶. وضعیت خوانده‌شدن: باز کردن گفتگوی خصوصی، پیام‌های ورودی طرف را `is_read` می‌کند؛ در گروه، timestamp آخرین بازدید هر کاربر همگام می‌شود؛ برای پیام‌های خروجی خودتان می‌توان رسید «همه خواندند» را نشان داد.

۱۷. چرخهٔ پیام: ویرایش و حذف نرم پیام‌های خود در خصوصی و گروه از طریق API.

۱۸. نگهداری داده: رویداد زمان‌بندی‌شدهٔ روزانه؛ حذف دائمی رکوردهای `deleted_at` قدیمی‌تر از ۳۰ روز (جهت‌گیری GDPR).

۱۹. چند رابط: ویجت شناور سایت؛ شورت‌کد `[team_messenger_web_chat]` با محتوای قابل تنظیم برای مهمان؛ صفحهٔ پیام‌رسان ادمین؛ دکمهٔ شناور اختیاری در کل wp-admin (تنظیم سراسری + احترام به خاموش بودن چت ادمین برای کاربر)؛ آیتم نوار ابزار؛ دو ویجت داشبورد.

۲۰. شخصی‌سازی در تنظیمات: رنگ اصلی، گوشهٔ ویجت، متن خوش‌آمد، محتوای مهمان شورت‌کد، و روشن/خاموش بودن پیام‌رسان در تمام صفحات پنل.

۲۱. ترجیحات کاربری در پروفایل: پنهان کردن چت سایت و/یا چت wp-admin (شناور، صفحهٔ پیام‌رسان، نوار ابزار، ویجت‌ها). روی صفحهٔ شورت‌کد در صورت خاموش بودن، اعلان کوتاه.

۲۲. محافظت در برابر سوءاستفاده: محدودیت نرخ برای ارسال پیام و ایجاد گروه.

۲۳. چندزبانه: دامنهٔ `team-messenger`؛ پوشهٔ `languages` شامل fa_IR و POT؛ زبان پیش‌فرض متن انگلیسی است.

== Installation ==

1. Upload the plugin files to `/wp-content/plugins/team-messenger`, or install the ZIP from Plugins > Add New.
2. Activate the plugin through the **Plugins** screen.
3. Configure options under the plugin’s **Settings** submenu if needed.

== Frequently Asked Questions ==

= Does this work for guests? =

The chat UI is for logged-in users. Shortcodes can show configurable fallback content for visitors.

= Where are translations? =

Language packs are loaded automatically when hosted on WordPress.org. The `languages` folder ships `.pot` and community `.po` files.

= آیا برای مهمان‌ها چت فعال است؟ =

رابط چت برای کاربران واردشده است. در شورت‌کد می‌توان محتوای جایگزین برای بازدیدکنندگان تنظیم کرد.

= ترجمه‌ها کجاست؟ =

فایل‌های ترجمه در پوشهٔ `languages` قرار دارند؛ برای fa_IR نیز پشتیبانی در نظر گرفته شده است.

== Screenshots ==

1. Floating chat widget and conversations
2. Admin messenger and dashboard widgets

== Changelog ==

= 1.0.8 =
* Security: Replaced the generic `check_permission` (logged-in only) on the `/media/download/{file}` REST endpoint with a dedicated `check_media_download_permission` callback that performs full ownership verification before serving any file. The check confirms the requesting user is either: (a) the sender or receiver of a direct message containing the file, (b) a member of a group whose group message contains the file, or (c) a member of the group whose avatar is that file. Site administrators (`manage_options`) retain access for moderation purposes. Non-matching requests receive a 403 response.

= 1.0.7 =
* Fixed: Plugin URI updated to the correct public documentation page.
* Fixed: Added plugin owner (syeedalireza) to the Contributors list in readme.txt.
* Fixed: Removed remote CDN dependency for the Vazirmatn font (jsDelivr); all font files (woff2) are now bundled locally inside the plugin.
* Fixed: Removed inline `<style>` block from the admin global chat partial; critical FAB styles are now output via `wp_add_inline_style()` attached to the enqueued stylesheet handle.
* Fixed: Removed `require_once wp-admin/includes/media.php` that was included without any subsequent call to a function from that file, violating the core-file loading exception rule.
* Fixed: Shortcode callback `render_web_chat_shortcode` now wraps the guest fallback output in `wp_kses_post()` after `do_shortcode()` execution to ensure all returned markup is properly escaped.
* Verified: Plugin Check (PCP) reports no errors or warnings on a clean WordPress installation with WP_DEBUG enabled.

= 1.0.6 =
* Maintenance and Plugin Check compliance improvements.

== Upgrade Notice ==

= 1.0.8 =
Critical security release: the media download endpoint now enforces per-file ownership checks. Update immediately.

= 1.0.7 =
Security and compliance release addressing all issues raised during the WordPress.org plugin review. Update strongly recommended.

= 1.0.6 =
Maintenance release; update recommended before publishing to the plugin directory.
