=== 404ops Upload Settings ===
Contributors: spwhite
Tags: upload, file upload, mime types, upload size, user roles
Requires at least: 7.0
Tested up to: 7.1
Requires PHP: 8.0
Stable tag: 1.8.4
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Control file types and upload size, site-wide or per user role, using WordPress's native filters — no core file edits required.

== Description ==

Control which file types can be uploaded, and the maximum file size, from one settings screen. Set both for the whole site, and optionally override either for individual user roles. It uses WordPress's own upload filters, so WordPress core files are never modified.

Nothing is restricted until you configure it. A freshly activated plugin stores no extension list and no size cap, and accepts exactly what WordPress accepted before.

**What's included:**

* Allowed file extensions: a comma-separated list such as `jpg,png,pdf`. An extension covers its format (`jpg` also allows `.jpeg` and `.jpe`), and a pipe group such as `jpg|jpeg` is accepted.
* Maximum upload size in megabytes. Without large file uploads it cannot exceed your server's PHP upload limits, which the settings page shows.
* Large file uploads (optional, off by default): the Media Library uploader sends big files in pieces, so files above the PHP limits can be uploaded. Every rule still applies to the finished file.
* Per-role rules: override the extensions, the size, or both for any registered role. A blank field uses the site-wide setting.
* Multiple roles: a user with several roles gets the largest size cap and the combined extension list of those roles.
* Real file-content checks: an allowed extension is not enough. The plugin also reads the file's bytes and refuses a renamed script or a mismatched type. SVG uploads are sanitised of scripts before they are saved.
* Per-user rules and storage quotas, set one line per person.
* An upload activity log of every allowed or refused upload, with the reason for each refusal.
* Anomaly screening: five local signals flag suspicious uploads into a screening log for review.
* Network baseline (multisite): a network-admin screen captures a baseline policy and pushes it to selected sites. Each site keeps control of its own settings.
* Optional AI-assisted rule authoring: describe the policy you want, and review a proposed set of rules before anything is saved. It needs an AI provider you connect yourself.

The External services section lists the only outside services the plugin can contact.

== Installation ==
1. Upload the plugin through `Plugins > Add New > Upload Plugin`, or copy it to `wp-content/plugins/`.
2. Activate 404ops Upload Settings through the `Plugins` screen. Activation on its own restricts nothing.
3. Go to 404ops > Upload Settings to configure the allowed extensions, the maximum upload size, and — optionally — per-role overrides for either.

== Frequently Asked Questions ==

= Where can I get help? =
Visit https://supportminer.404ops.com to open a support request.

= Does this plugin send any data outside my site? =
Upload enforcement never does: it runs entirely through WordPress's own upload filters on your server. Anomaly screening never does either: its signals are computed entirely on your server from upload metadata. The two exceptions are both optional, both administrator-initiated, and both go only to the AI provider the site itself is configured with (bring your own key). AI-assisted rule authoring: when an administrator clicks "Generate proposal for review", what leaves the site is the site's current rule shape (list mode, extension lists, size caps, the server's PHP upload ceiling), the role names, and the description text that administrator typed — nothing else, and never any user account data. "Explain with AI" on a screening flag: what leaves the site is that one flag's metadata (the uploader's role, the file's extension and size, the size cap that governed it, which signals fired) and aggregate counts (uploads in the burst window, the role's median upload size, copies of the same content checksum, the account's total upload history) — never file contents, never the filename, never a username or IP address. No other feature makes any outbound call.

= Does this modify WordPress core files? =
No. The plugin only uses WordPress's native `upload_mimes`, `upload_size_limit`, `wp_handle_upload_prefilter`, `wp_handle_sideload_prefilter` and `wp_check_filetype_and_ext` filters, plus, for large file uploads, the plupload settings filters and its own admin-ajax action.

= Can I allow different file types, or a different size limit, for different roles? =
Yes. The per-role table on the settings page has a row for every registered role, with its own allowed-extensions field and its own maximum-size field. Fill in either field to override the site-wide setting for that role, and leave a field blank for that role to keep using the site-wide value. No role has an override until you create one.

= What happens to a user who has more than one role? =
They get the most permissive of their roles' rules: the largest maximum size any of their roles sets, and the combined list of extensions their roles allow. Adding a role to a user can only widen what they may upload, never narrow it below what another of their roles already allowed.

= Can the AI change my upload settings by itself? =
No — it can only propose. The AI generates a suggestion from your plain-language description; the proposal is shown as a diff (current value versus proposed value, one line per rule element), each element is validated server-side against the same rules the manual form enforces, and nothing at all is saved until you press "Apply accepted changes". Values that fail validation are marked refused with the reason and cannot be accepted, even by a forged request. A pending proposal expires after 30 minutes. Applying writes through the exact same save handlers as the manual settings form, and settings the proposal cannot express (content verification, SVG sanitization, per-user rules) are never touched by it.

= What is Upload Screening, and can it block an upload? =
404ops > Upload Screening is a review log, not a second enforcement layer. It flags uploads that PASSED every rule but matched an anomaly signal: a burst of uploads from one account (or one IP address for guests) inside a short window, a file far larger than its role's recent median, a run of files sitting just under the size cap, the same content uploaded repeatedly (matched by a locally computed checksum), or a file type a well-established user has never uploaded before. Each flag records only metadata — who, when, file name/size/extension, and a checksum prefix — and screening never blocks, quarantines, or deletes anything: the upload rules remain the only enforcement. Every signal and threshold is configurable (or switchable off entirely) under 404ops > Upload Settings > Anomaly Screening, flags are kept on the same rolling retention as the activity log, and the whole feature can be turned off, whereupon it records nothing.

= On multisite, can I set one upload policy for the whole network? =
Yes — as a baseline, not a lock. The network admin screen (Network Settings > Upload Settings) captures a baseline policy from any site you have already configured, shows which sites deviate from it and on which settings, and pushes it to every site or to selected ones. Pushing writes only the settings whose value actually differs, deletes nothing, and never takes a site's settings screen away from its own administrator: a site can change its settings again afterwards, and the deviation table shows that it has. Per-user rules and quotas are never pushed, because one site's usernames say nothing about another's, and a pushed size cap is still capped by each site's own PHP upload limits unless that site has large file uploads on.

= Does activating the plugin change what my site accepts? =
No. A fresh install stores neither an extension list nor a size cap, and nothing is restricted until you save a setting. Versions up to 1.0.3 did write a 12-extension list and a 10MB cap into the database on activation; when you upgrade, a stored value that still exactly matches that old default is reset to "no restriction" and a one-time notice in wp-admin tells you it happened, so you can re-enter the restriction deliberately if you wanted it. A stored value that differs from that old default in any way is left untouched.

= Can I exceed my server's PHP upload limits? =
Only through the Media Library uploader, and only with "Large File Uploads" turned on (404ops > Upload Settings). It then sends a big file in pieces that each fit under `upload_max_filesize` and `post_max_size`, and joins them on your server, so the limit becomes the maximum size you set — or free disk space if you set none. The finished file goes through every rule as a normal upload: allowed types (refused at the first piece), size caps and quotas (checked as pieces arrive), content checks, SVG sanitising and the activity log. Pieces of an unfinished upload sit in a private folder under `wp-content/uploads` with random names, and are deleted after a day.

Not covered, and stated on the settings screen: the block editor's own Upload buttons and drag-and-drop send a file to the REST API in one request, which WordPress cannot split, so they, importers and front-end forms still stop at the PHP limits. In the block editor, use the Media Library button for a large file. With large file uploads off, a size above the PHP limit is capped to it and the settings page tells you.

= Where are the debug logs stored, and are they public? =
When `WP_DEBUG` is enabled, logs are written to `wp-content/plugins/404ops-upload-settings/logs/`. On activation the plugin writes an `.htaccess` file in that directory denying direct web access. That is effective on Apache; servers that do not read `.htaccess` files, such as nginx, ignore it, and there the directory is covered only by your own server configuration. No logs are written unless `WP_DEBUG` is on.

= What happens if I leave the allowed extensions field empty? =
The plugin falls back to WordPress's own default set of allowed MIME types.

= Does the plugin check what's actually inside an uploaded file, or only its name and extension? =
Both, and this is defence in depth, not a guarantee. With "Verify file contents match the extension" on (the default), the plugin reads the file's real bytes and refuses the upload when they positively identify a different, or an executable, type -- a `.php` script (or other script/shell/executable content) renamed to end in `.pdf`, say. It does not refuse merely because the server's own file-type detection cannot be more specific than a generic answer: a real `.docx` that a server reports only as a generic ZIP file, for instance, is still accepted, because a ZIP-shaped answer is expected for that format, not evidence of anything wrong. Ordinary image uploads (JPEG, PNG, GIF, and similar) are unaffected: WordPress already verifies those against their own bytes on every upload, with or without this plugin, and this setting does not duplicate that check.

If you allow the `svg` extension, "Sanitise SVG uploads" (also on by default) additionally parses the SVG against an allow-list of ordinary SVG elements (shapes, containers, text, gradients, filters, and similar) and removes anything else, along with `<script>` tags, event-handler attributes (`onload`, `onclick`, and similar), SMIL animations retargeted at an event handler or href, `javascript:`/`vbscript:`/`data:` URLs, and dangerous CSS constructs inside `<style>` blocks and `style` attributes -- refusing the file outright if it cannot be parsed safely. This exists because SVG is an XML document format that can carry a working script, and a script inside a file in your Media Library runs in the browser of any administrator who merely previews it.

Neither check is a substitute for server-side antivirus/malware scanning, which this plugin does not perform and does not send any file anywhere to have performed. They narrow what a file can *contain* and *claim to be*; they do not scan for known malware signatures. If your host or security plugin offers file scanning, keep using it alongside this plugin, not instead of it.

= Can a developer change the rule for one user, or log refused uploads? =
Yes. The `cse_upload_settings_effective_rule` filter receives the rule the settings produce for the user uploading (`extensions`, `max_size` in bytes, and where each came from) and that `WP_User`. Return it with either field changed to give one person, rather than a whole role, their own rule. A value of the wrong type is ignored.

The `cse_upload_settings_blocked` action fires whenever the plugin refuses an upload, with the reason (`extension`, `size`, `quota`, `no_extension`, `content_mismatch`, `svg_unreadable`, `svg_unsafe`, `svg_write_failed` or `disk_space`), the file (including the message the uploader saw, in `error`), the rule it broke, and the user's ID.

= What happens to my settings when I delete the plugin? =
By default, WordPress keeps your allowed extensions, size limit, per-role rules, and content-security toggles in the database. Reinstalling the plugin restores those settings. If you would rather deleting the plugin also erase this stored configuration, check "Remove all Upload Settings data when the plugin is deleted" under 404ops > Upload Settings > Plugin Data before you delete it; that setting itself defaults to off. Either way, simply deactivating the plugin never removes stored settings -- only deleting it can, and only once you have opted in. On multisite, the host administrator must deactivate the plugin across the installation and run the coordinated removal procedure from the [Upload Settings product page](https://404ops.com/plugins/404ops-upload-settings). The host tool keeps the shared files in place until it has verified every site's activation state and applied the site's own data choice. When WP_DEBUG is enabled, new debug logs and their .htaccess hint use this site's uploads/404ops-upload-settings-logs directory. Explicit uninstall removes known runtime logs regardless of the settings toggle. Unrelated files and the protections they rely on remain. Logs from older versions under the plugin's logs directory are not moved or deleted by activation or upgrade. WordPress may remove that old plugin directory when it deletes the plugin. Pieces of unfinished large uploads are removed on deactivation and on every deletion, regardless of this setting.

= Where are optional debug logs stored? =
From 1.8.4, WP_DEBUG logging uses the current site's WordPress uploads base plus /404ops-upload-settings-logs/, rather than writing inside the installed plugin. The directory is created only when a debug message is written. Unavailable, nonlocal, unsafe or unwritable storage skips the optional write. Old plugin-directory logs are not migrated or automatically read. The generated .htaccess is an Apache hint only: it needs the relevant AllowOverride configuration; nginx and other servers need their own deny rule. This does not make uploads private. Do not enable debug logging for sensitive production data without appropriate server access controls.

== External services ==

The plugin makes no outside request for upload enforcement, anomaly screening, the activity log or the network baseline. All of those run on your own server.

**AI providers (optional).** Only when an administrator uses AI-assisted rule authoring or "Explain with AI" on a screening flag, and only if an AI provider is connected on the shared AI Connection screen (404ops > AI Connection). Nothing is sent until an administrator clicks "Generate proposal for review" or "Explain with AI".

* Rule authoring sends the site's current rule shape, the role names, and the description the administrator typed. It sends no user account data.
* "Explain with AI" sends that one flagged upload's metadata (the uploader's role, the file's extension and size, and the signals that fired) and aggregate counts. It never sends file contents, filenames, usernames or IP addresses.

The providers are OpenAI (`api.openai.com`), Anthropic (`api.anthropic.com`), Google Gemini (`generativelanguage.googleapis.com`), OpenRouter (`openrouter.ai`, which forwards to the model you selected), an OpenAI-compatible base URL you enter, and Ollama (by default `localhost`). Each provider's own terms and privacy policy apply to the request.

AI provider policy references for the optional shared AI connection:

* OpenAI: [API services agreement](https://openai.com/policies/services-agreement/) and [privacy policy](https://openai.com/policies/privacy-policy/).
* Anthropic: [commercial terms](https://www.anthropic.com/legal/commercial-terms) and [privacy policy](https://www.anthropic.com/legal/privacy).
* Gemini: [API terms](https://ai.google.dev/gemini-api/terms) and [Google privacy policy](https://policies.google.com/privacy).
* OpenRouter: [terms](https://openrouter.ai/terms) and [privacy policy](https://openrouter.ai/privacy). OpenRouter can route requests to another model provider; review the provider selected in your account as well.

For Ollama or an OpenAI-compatible endpoint, requests go to the server URL you configure. Review that server operator's terms and privacy policy. The plugin sends requests to the configured address; its operator determines any onward processing. The information sent and the actions that trigger a request are described above.

== Screenshots ==

1. Upload Restrictions: list mode, allowed file extensions, and maximum file size.
2. Content Security options and per-role upload overrides.
3. Server Limits: PHP upload limits compared with the plugin setting.

== Changelog ==

= 1.8.4 =
* Fixed: upload limits account for WordPress multisite constraints, and oversized chunked uploads return HTTP 413.
* Fixed: coordinated multisite removal verifies all activation records and honors each site's cleanup choice before removing the shared package. The host procedure is available from the product page.
* Fixed: optional debug logs now use a per-site uploads directory instead of the installed plugin tree. Activation leaves the package untouched; explicit uninstall removes only known new runtime logs. Existing logs are not migrated.

= 1.8.3 =
* Maintenance: bundles ai-client 0.4.3, which normalizes the AI Connection nonce before verification and rejects malformed inputs.

= 1.8.2 =
* Added: guarded read-only administrative status interfaces and five bundled draft language catalogs.
* Improved: development test-session and catalog packaging checks.

= 1.8.1 =
* Code-quality clean-up for the WordPress.org Plugin Check: database table names are bound as identifiers, and suppressed notices name the specific rule they cover. No change in behaviour.

= 1.8.0 =
* New: large file uploads (off by default). The Media Library uploader sends big files in pieces, so files above the PHP upload limits can be uploaded, with every upload rule still applied to the finished file.
* New: the settings screen shows the largest file with and without large file uploads; a blank maximum size means no limit.
* New: a footer line on the settings screen names the other free 404ops tools not active on the site. Each user can hide it, and a filter turns it off.
* Fixed: a network policy push stored the server maximum instead of the pushed size cap.

= 1.7.6 =
* Refreshed wording and support link.

= 1.7.5 =
* Admin menu labels and page headings drop the 404ops brand prefix; the generated Composer loader is hardened for Plugin Check.

= 1.7.4 =
* Plugin URI points to the 404ops.com plugins page.

= 1.7.2 =
* Changed: Bundled libraries updated; translatable strings now use this plugin's text domain.

= 1.7.1 =
* Requires WordPress 7.0 or newer (it was 6.9); tested up to WordPress 7.1.

= 1.7.0 =
* Changed: Upload Settings, Upload Activity and Upload Screening now sit under the shared 404ops menu.
* Fixed: the first save of a size on a new site stored the server maximum instead of the size typed.
* Fixed: a saved upload size limit could not be raised from the settings form.
* Fixed: a clean save shows WordPress's "Settings saved." confirmation again.
* Accessibility: eight settings fields that had no accessible name now have one.

= 1.6.0 =
* Maintenance: bundles its own prefixed UI kit. No other change.

= 1.5.1 =
* Maintenance: AI features use a private, namespaced copy of the AI client. If you use AI features, re-enter your AI connection once.

= 1.5.0 =
* New: anomalous-upload screening, with five local signals flagging suspicious uploads into a screening log.

= 1.4.0 =
* New: AI-assisted rule authoring. Describe the policy in plain language, and review the proposal before anything is saved.

= 1.3.0 =
* New: network-wide policy management on multisite, with a baseline, a deviation table and a push to selected sites.

= 1.2.0 =
* New: per-user upload rules and cumulative storage quotas.
* New: an upload activity log of every allowed or refused upload.
* Security: the file's actual bytes are checked against its extension, and SVG uploads are sanitised.
* Changed: deleting the plugin keeps its settings unless you opt in to removing them.

= 1.1.0 =
* Added: per-role upload rules, with a multi-role result that is the most permissive of the roles.
* Changed: activation no longer writes a default extension list or size cap. An unconfigured install restricts nothing.
* Fixed: allowing an extension allows its format, so `jpg` admits `.jpeg` and `.jpe`.
* Fixed: the size cap also applies to files imported from a URL and to REST API raw uploads.

= 1.0.3 =
* Fixed: the "Settings" link on the Plugins list page now appears.

= 1.0.2 =
* Initial release: 404ops identity, readme and translation template.

== Upgrade Notice ==

= 1.8.2 =
Includes checked draft translations and administrative status reporting. Human linguistic review is separate.

= 1.8.0 =
Adds optional large file uploads for the Media Library uploader (off until you turn it on) and fixes pushed network size caps. Safe to update.

= 1.7.3 =
Readme and WordPress.org listing corrections only; no change to plugin behaviour. Safe to update.

= 1.7.2 =
Bundled libraries updated. Safe to update.

= 1.7.1 =
Now requires WordPress 7.0 or newer and is tested up to 7.1. Safe to update.

= 1.7.0 =
Moves to the shared 404ops menu, requires WordPress 6.9 or newer, and fixes the first-save size and the raise-the-limit bugs. Safe to update.

= 1.6.0 =
Bundles its own prefixed UI kit. Safe to update.

= 1.5.1 =
Maintenance release: AI features use a private copy of the AI client; re-enter your AI connection once. Safe to update.

= 1.5.0 =
Upload screening with an admin log — surfaces anomalies for review, blocks nothing. Safe to update.

= 1.4.0 =
Optional AI rule authoring (BYOK). The plugin sends one request only when you click Generate. Safe to update.

= 1.3.0 =
Adds the multisite network policy screen. Single-site installs see no change.

= 1.1.0 =
Adds per-role upload rules. Activation no longer imposes a default extension list or 10MB cap, and on upgrade a stored value that exactly matches that old default is reset to "no restriction" and reported in wp-admin.

= 1.0.2 =
Submission-readiness release. No functional upload-handling changes.
