OG Media Cleaner developer's guide¶
This page lists the hooks, REST routes, MCP tools, WP-CLI commands and stored data of OG Media Cleaner. For the screens and settings, see the user guide.
Media Cleaner loads only in wp-admin, REST requests and WP-CLI. It does not run on the front end.
Items marked Pro come from the Pro code, which loads only when the licence is active.
Actions¶
Parser actions¶
A parser hooks into these actions, finds media IDs or URLs, and registers them as references. The core object is the global $ogmc.
| Action | Arguments | When it fires |
|---|---|---|
ogmc_initialize_parsers |
none | Before a scan, when the parsers load. |
ogmc_scan_once |
none | Once at the start of the content analysis. Use it for options pages, theme settings and other site-wide data. |
ogmc_scan_postmeta |
$post_id |
For each post being analysed, before its content. Use it for post meta. |
ogmc_scan_post |
$html, $post_id |
For each post being analysed, with its post_content. |
ogmc_scan_widget |
$widget (the registered widget array) |
For each active widget in a sidebar. |
ogmc_scan_widgets |
none | Once, after all active widgets were scanned. |
ogmc_check_file_init |
none | Before files are checked in a scan or cleanup step. The Pro code builds its file caches here. |
Helpers on $ogmc that parsers use:
| Method | What it does |
|---|---|
add_reference_id( $idOrIds, $type, $origin = null, $extra = null ) |
Marks one or more attachment IDs as used. $type is a label shown in the References tab. $origin is usually the post ID. |
add_reference_url( $urlOrUrls, $type, $origin = null, $extra = null ) |
Marks one or more file URLs as used. |
get_urls_from_html( $html ) |
Returns the media URLs found in a piece of HTML. |
get_from_meta( $meta, $lookFor, &$ids, &$urls ) |
Walks an array or object and collects IDs and URLs found under the keys in $lookFor. |
Example: a parser for a plugin that stores a gallery in post meta.
add_action( 'ogmc_scan_postmeta', function ( $post_id ) {
global $ogmc;
$ids = array();
$urls = array();
$data = get_post_meta( $post_id, '_my_plugin_gallery', true );
if ( ! empty( $data ) ) {
$ogmc->get_from_meta( $data, array( 'image_id', 'image_url' ), $ids, $urls );
}
if ( $ids ) {
$ogmc->add_reference_id( $ids, 'MY PLUGIN (ID)', $post_id );
}
if ( $urls ) {
$ogmc->add_reference_url( $urls, 'MY PLUGIN (URL)', $post_id );
}
} );
Filters¶
| Filter | Arguments | When it fires |
|---|---|---|
ogmc_check_media |
$is_used (bool), $attachment_id, $is_broken |
For each attachment in a Clean Library scan. Return true to treat it as used. |
ogmc_is_file_broken |
$is_broken (bool), $attachment_id |
When checking whether an attachment's file is missing. Default: the file does not exist on disk. |
ogmc_check_file |
$issue (false or issue), $path |
For each file in an uploads folder scan. Pro fills this in. |
ogmc_list_uploaded_files |
$files (null), $path, $offset, $limit |
Lists the files of a folder for an uploads folder scan. Pro fills this in. |
ogmc_file_page_info |
$info |
Paging information for the uploads folder listing. Pro fills this in. |
ogmc_excluded_dirs |
$dirs (array of folder names) |
Pro. Folders skipped in uploads folder scans. Defaults include the trash, cache, log and backup folders of common plugins. |
ogmc_valid_image_extensions |
$extensions |
The extensions that count as images. Default: jpg, jpeg, png, gif, bmp, tiff, ico, webp, avif. |
ogmc_affect_html_from_builder |
$html, $post_id |
Lets a builder parser change the HTML before OG Gallery shortcodes in it are resolved. |
ogmc_acf_urls |
$urls, $ids, $post_id |
Pro. URLs found in ACF blocks of a post. |
ogmc_acf_ids |
$ids, $urls, $post_id |
Pro. IDs found in ACF blocks of a post. |
ogmc_wprm_taxonomy_image_keys |
$keys (taxonomy => term meta key) |
Pro. Extra WP Recipe Maker taxonomies whose term images should be read. |
ogmc_allow_setup |
$allowed (default current_user_can( 'manage_options' )) |
Who can open the settings and use the settings routes. |
ogmc_allow_usage |
$allowed (default current_user_can( 'manage_options' )) |
Who can open the dashboard and use the scan and cleanup routes. |
ogmc_allow_cleanup |
$allowed (default true) |
Return false to block every deletion, even for administrators. |
ogmc_request_time_budget |
$seconds (default the lower of 30 and max_execution_time), $execution_limit |
How long one scan request may run. Clamped to at least 5 seconds and to the PHP limit. |
ogmc_max_analysis_document_bytes |
$bytes |
Overrides the Document Limit setting. -1 removes the limit. |
ogmc_directory_picker_limit |
$max (default 5000) |
The most folders shown in Pick Folder. |
ogmc_previewable_trash_mimes |
$mimes |
Image types that can be previewed from the trash. SVG is left out on purpose. |
ogmc_max_trash_preview_bytes |
$bytes (default 8 MB) |
The largest trash file that can be previewed. |
ogmc_trace_references |
$enabled (default false) |
With Logs on, logs every reference as it is added. |
ogmc_strict_parser_query_guard |
$strict (default false), $query, $label |
During parser development, return true to throw an error when a parser runs an unbounded SELECT. Do not use it on live scans. |
ogmc_plugin_title |
$title |
The plugin title. Pro adds "(Pro)". |
Example: keep everything in a folder used by your theme.
add_filter( 'ogmc_check_media', function ( $is_used, $attachment_id, $is_broken ) {
$file = get_attached_file( $attachment_id );
if ( $file && strpos( wp_normalize_path( $file ), '/uploads/brand/' ) !== false ) {
return true;
}
return $is_used;
}, 10, 3 );
Example: block deletion for everyone while a migration runs.
REST API¶
Namespace: og-media-cleaner/v1. Every route needs a logged-in user and a wp_rest nonce. "Settings" routes check ogmc_allow_setup, the others check ogmc_allow_usage (both default to manage_options). These routes serve the plugin's own screens and can change between versions.
| Method | Route | Permission | Purpose |
|---|---|---|---|
| POST | /update_options |
Settings | Save settings. |
| POST | /reset_options |
Settings | Reset settings to defaults. |
| GET | /all_settings |
Usage | Read all settings. |
| POST | /auto_buffers/plan, /auto_buffers/measure, /auto_buffers/preview, /auto_buffers/apply |
Settings | The Auto Buffer server benchmark. |
| POST | /reset_db |
Settings | Delete Cleaner DB. |
| GET | /preflight |
Usage | Check the server is ready for a scan. |
| POST | /run/start |
Usage | Start a scan run. |
| GET | /run/status |
Usage | Read the state of a run. |
| POST | /run/complete, /run/fail, /run/pause, /run/cancel |
Usage | Finish, fail, pause or cancel a run. |
| POST | /run/unlock_cleanup |
Usage | Unlock Cleanup. |
| POST | /reset_issues, /reset_references, /reset_issues_and_references |
Usage | Clear results or references before a scan. |
| POST | /extract_references |
Usage | Analyse content and store references. |
| POST | /retrieve_medias |
Usage | List Media Library entries to check. |
| POST | /retrieve_files |
Usage | List uploads folder files to check. |
| POST | /retrieve_hash_duplicates |
Usage | Find identical files. |
| POST | /check_targets |
Usage | Match media or files against the references. |
| GET | /get_progress |
Usage | Read scan progress. |
| POST | /clear_progress |
Usage | Clear scan progress. |
| GET | /uploads_directory_hierarchy |
Usage, Pro | Folder tree for Pick Folder. |
| POST | /count |
Usage | Count results. |
| POST | /all_ids |
Usage | IDs of all results matching a search. |
| GET | /stats |
Usage | Result totals and sizes. |
| GET | /entries |
Usage | Paged results for a tab. |
| GET | /duplicates_group |
Usage | All copies of one duplicate file. |
| POST | /set_ignore |
Usage | Ignore or stop ignoring results. |
| POST | /delete |
Usage | Move results to the trash, or delete trashed results permanently. |
| POST | /recover |
Usage | Restore results from the trash. |
| POST | /repair |
Usage | Add a file back to the Media Library. |
| POST | /force_trash_all |
Usage | Force Empty Trash. |
| POST | /force_clean_trash |
Usage | Force Clean Trash. |
| GET | /trash_inventory |
Usage | What the trash holds. |
| GET | /trash_preview |
Usage | Preview an image in the trash. |
| GET | /export |
Usage | Export results as CSV. |
| GET | /tail_logs |
Usage | Read the scan log. |
| POST | /clear_logs |
Usage | Clear the scan log. |
| GET | /support_bundles |
Usage | List saved Support IDs. |
| POST | /delete_support_bundle |
Usage | Delete a Support ID. |
MCP tools¶
With MCP enabled in the settings and OG AI Core active, Media Cleaner adds these tools to OG AI Core's MCP server (through the ogaie_mcp_tools and ogaie_mcp_callback filters). Like all OG AI Core agent tools, they are for administrators only.
| Tool | Arguments | What it does |
|---|---|---|
ogmc_get_capabilities |
none | Active parsers, plugins found that need the Pro parsers, and what the scan cannot see. Call it first. |
ogmc_get_status |
none | Scan method, counts from the last scan, whether a scan is staged or paused, and whether cleanup is allowed. |
ogmc_scan_start |
method (media, files or duplicates, optional), content (bool, optional) |
Stages a new scan and returns its run_id. Published results are not touched. |
ogmc_scan_step |
run_id (required) |
Runs the next time-limited batch. Call it until finished is true. |
ogmc_scan_publish |
run_id (required) |
Publishes a finished scan so it replaces the previous results. Fails if any phase is incomplete. |
ogmc_scan_cancel |
run_id (required) |
Cancels a staged scan. Previous results are kept. |
ogmc_get_issues |
filter (issues, ignored or trash), search, limit (1–100, default 25), skip |
Lists results of the last published scan. |
ogmc_explain_issue |
entry_id (required) |
Why one item was reported, which parsers ran, and what to verify by hand. |
ogmc_ignore |
entry_ids (required, up to 100), ignore (bool, default true) |
Ignores or stops ignoring items. Nothing is deleted. |
ogmc_trash |
entry_ids (required, up to 100) |
Moves items to the Media Cleaner trash. Reversible. |
ogmc_recover |
entry_ids (required, up to 100) |
Restores items from the trash. |
ogmc_delete_permanently |
entry_ids (required, up to 100), confirm (required, must be exactly PERMANENT) |
Deletes items for good. Only after the user asks for permanent deletion. |
WP-CLI¶
Pro. The command is wp og-media-cleaner.
| Command | What it does |
|---|---|
wp og-media-cleaner scan [media\|filesystem] [check-content\|uncheck-content] [check-media\|uncheck-media] |
Runs a full scan and publishes it. Without a method it uses the one in the settings (only media and filesystem work here). check-media applies to filesystem scans. |
wp og-media-cleaner issues |
Lists the current results. |
wp og-media-cleaner trash |
Lists what is in the trash. |
wp og-media-cleaner delete [<id>...] |
Deletes the given result IDs. Without IDs it deletes every current result. Files go to the trash unless Skip Trash is on. |
wp og-media-cleaner recover [<id>...] |
Restores the given IDs from the trash. Without IDs it restores everything in the trash. |
wp og-media-cleaner empty |
Permanently deletes everything in the trash. |
wp og-media-cleaner scan media check-content
wp og-media-cleaner issues
wp og-media-cleaner delete 12 15 18
Options, meta keys and tables¶
Options
| Name | What it holds |
|---|---|
ogmc_options |
All settings, as one array. Keys: method, content, filesystem_content, media_library, images_only, attach_is_use, thumbnails_only, dirs_filter, files_filter, hide_thumbnails, hide_warning, skip_trash, expert_mode, mcp_support, clean_uninstall, debuglogs, php_error_logs, shortcodes_disabled, output_buffer_cleaning_disabled, medias_buffer, posts_buffer, analysis_buffer, file_op_buffer, uploads_file_buffer, refs_buffer, analysis_document_limit, delay, posts_per_page, repair_mode, thumbnail_force_issues. |
ogmc_active_run_id |
The ID of the published scan run. |
ogmc_schema_version |
Version of the database tables. |
ogmc_trash_secret |
Random value used to name the private storage folder. |
ogmc_license |
Licence record. |
Transients: ogmc_progress, ogmc_cached_urls, ogmc_cached_ids.
Post type
While a Media Library item is in the Media Cleaner trash, its attachment is parked with the post type wmpc-trash. Recovering it turns it back into an attachment. Emptying the trash deletes it.
Tables
| Table | What it holds |
|---|---|
{prefix}mclean_scan |
Scan results: one row per reported media item or file, with its reason, size and ignored or trashed state. |
{prefix}mclean_refs |
References found in your content. |
{prefix}mclean_runs |
Scan runs and their state. |
{prefix}mclean_work |
Work in progress for staged scans. |
{prefix}mclean_operations |
Journal of trash, delete and recover operations. |
{prefix}mclean_duplicates |
Content hashes for duplicate scans. |
Files
The trash and the log live in a private folder in wp-content, named .og-media-cleaner-private- plus a hash. The log is logs/og-media-cleaner.log inside it.
Constants¶
| Constant | What it does |
|---|---|
OGMC_TRASH_DIR |
Set it in wp-config.php to move the trash folder. It must be outside the uploads folder, or cleanup is blocked. |
OGMC_VERSION |
Plugin version (read only). |
OGMC_PATH, OGMC_URL |
Plugin folder path and URL (read only). |
JavaScript¶
On its own screens the plugin adds the global ogmc_media_cleaner, with api_url (the REST base), rest_nonce, is_pro, is_registered, cleanup_allowed, results_method and options (the current settings). It is meant for the plugin's own app and has no public events.