Repository navigation
Remove short ot_ prefix from postmeta keys & identifiers; debug-gate error_log (1.1.1) - #25
Conversation
Proactive hardening before the wp.org resubmission. The round-1 review scoped the prefix finding to CPT slugs (fixed in 1.1.0 → opentr_*); this extends the cleanup to every other shared/global identifier so a stricter re-reviewer can't reopen it. - Postmeta keys: _ot_* → _opentrust_*, with a v4→v5 DB migration (rename_postmeta_keys_v5) that mirrors the proven v4 CPT-rename pattern: idempotent bulk UPDATEs, runs once on upgrade, retries safely if interrupted, busts caches via the existing maybe_upgrade() tail. - Query vars ot_policy_slug/ot_version → opentrust_*; rewrite rules recompiled by the existing flush transient. - Meta-box IDs, admin column IDs, import/export form field names and the $_FILES key → opentrust_* (request-scoped, no migration). - LEGACY_META_MAP added as the single source of truth for the migration, the IO import-remap (keeps pre-1.1.1 export archives importable), and uninstall cleanup. - error_log() calls routed through a new WP_DEBUG-gated OpenTrust::debug_log(). - wpml-config.xml keys renamed (+ the previously-missing cert artifact id); uninstall.php clears orphaned attachment + user meta. - Version bumped to 1.1.1, DB schema to 5, POT regenerated. Out of scope by design: CSS classes, data-ot-* attributes, local $ot_ variables, and the ot_enc_v1: ciphertext marker — none are reviewable shared registries. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
📝 WalkthroughWalkthroughThis PR migrates the OpenTrust plugin from legacy ChangesOpenTrust Metadata Key and Identifier Migration
Estimated code review effort 🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
includes/class-opentrust.php (1)
330-346:⚠️ Potential issue | 🟠 Major | ⚡ Quick winUUID backfill may duplicate UUIDs for existing installations upgrading to v5.
For installs upgrading from v1–v4, the execution order is v4 → v2 → v5. At v2 backfill time:
- Existing posts have
_ot_uuid(old key)meta_querychecks_opentrust_uuidNOT EXISTS → TRUE- Backfill writes new
_opentrust_uuidvalues- Later, v5 rename converts
_ot_uuid→_opentrust_uuid- Result: posts end up with two
_opentrust_uuidrows (different values)Consider either reordering so v5 runs before v2, or updating the backfill to skip posts that already have the legacy
_ot_uuidkey:Proposed fix
private static function backfill_uuids(): void { $posts = get_posts([ 'post_type' => OpenTrust_CPT::ALL, 'post_status' => 'any', 'posts_per_page' => -1, 'fields' => 'ids', 'meta_query' => [ + 'relation' => 'AND', [ 'key' => '_opentrust_uuid', 'compare' => 'NOT EXISTS', ], + [ + 'key' => '_ot_uuid', + 'compare' => 'NOT EXISTS', + ], ], ]); foreach ($posts as $post_id) { update_post_meta((int) $post_id, '_opentrust_uuid', wp_generate_uuid4()); } }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@includes/class-opentrust.php` around lines 330 - 346, The backfill_uuids() routine can create duplicate _opentrust_uuid entries when a legacy _ot_uuid exists; modify backfill_uuids() to skip posts that already have the legacy key by extending the meta_query (or add a runtime check inside the foreach) to ensure neither _opentrust_uuid nor _ot_uuid is present before calling update_post_meta, so only posts lacking both keys receive a newly generated wp_generate_uuid4(); reference the backfill_uuids() method and the meta keys '_opentrust_uuid' and '_ot_uuid' when making the change.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@includes/class-opentrust-cpt.php`:
- Around line 867-869: Validate and gate the attachment IDs before calling
update_post_meta: cast the posted IDs with absint but then verify the attachment
exists and is an attachment (use get_post($id) and check $post->post_type ===
'attachment') and only save if valid; for the badge/artifact metas
(_opentrust_cert_badge_id, _opentrust_cert_artifact_id) additionally restrict
mime types as appropriate (e.g., badge -> image/* via wp_get_attachment_metadata
or get_post_mime_type) and for the policy PDF field (the metadata saved around
lines 929-933) enforce get_post_mime_type($id) === 'application/pdf' before
updating; keep sanitize_textarea_field for _opentrust_cert_description and do
not call update_post_meta if validation fails.
---
Outside diff comments:
In `@includes/class-opentrust.php`:
- Around line 330-346: The backfill_uuids() routine can create duplicate
_opentrust_uuid entries when a legacy _ot_uuid exists; modify backfill_uuids()
to skip posts that already have the legacy key by extending the meta_query (or
add a runtime check inside the foreach) to ensure neither _opentrust_uuid nor
_ot_uuid is present before calling update_post_meta, so only posts lacking both
keys receive a newly generated wp_generate_uuid4(); reference the
backfill_uuids() method and the meta keys '_opentrust_uuid' and '_ot_uuid' when
making the change.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 73aafc28-cf06-4c2e-97b4-a23aa829d3cd
📒 Files selected for processing (22)
assets/js/admin.jsincludes/class-opentrust-admin-ai.phpincludes/class-opentrust-admin-tools.phpincludes/class-opentrust-admin.phpincludes/class-opentrust-catalog.phpincludes/class-opentrust-chat-corpus.phpincludes/class-opentrust-chat-summarizer.phpincludes/class-opentrust-cpt.phpincludes/class-opentrust-io.phpincludes/class-opentrust-render.phpincludes/class-opentrust-repository.phpincludes/class-opentrust-version.phpincludes/class-opentrust.phpincludes/data/certification-catalog.phpincludes/data/data-practice-catalog.phpincludes/data/subprocessor-catalog.phpincludes/providers/class-opentrust-chat-provider.phplanguages/opentrust.potopentrust.phpreadme.txtuninstall.phpwpml-config.xml
| update_post_meta($post_id, '_opentrust_cert_badge_id', absint( wp_unslash( $_POST['opentrust_cert_badge_id'] ?? 0 ) )); | ||
| update_post_meta($post_id, '_opentrust_cert_artifact_id', absint( wp_unslash( $_POST['opentrust_cert_artifact_id'] ?? 0 ) )); | ||
| update_post_meta($post_id, '_opentrust_cert_description', sanitize_textarea_field( wp_unslash( $_POST['opentrust_cert_description'] ?? '' ) )); |
There was a problem hiding this comment.
Validate attachment IDs and enforce the promised PDF constraint.
These fields still trust the posted attachment IDs too much: the certification badge/artifact paths accept any integer, and the policy path accepts any attachment MIME even though the UI presents it as a PDF upload. A bad media selection or crafted POST can leave broken or mislabeled asset links in admin/public views.
Suggested fix
- update_post_meta($post_id, '_opentrust_cert_badge_id', absint( wp_unslash( $_POST['opentrust_cert_badge_id'] ?? 0 ) ));
- update_post_meta($post_id, '_opentrust_cert_artifact_id', absint( wp_unslash( $_POST['opentrust_cert_artifact_id'] ?? 0 ) ));
+ $badge_id = absint( wp_unslash( $_POST['opentrust_cert_badge_id'] ?? 0 ) );
+ if ($badge_id > 0 && get_post_type($badge_id) === 'attachment') {
+ update_post_meta($post_id, '_opentrust_cert_badge_id', $badge_id);
+ } else {
+ delete_post_meta($post_id, '_opentrust_cert_badge_id');
+ }
+
+ $artifact_id = absint( wp_unslash( $_POST['opentrust_cert_artifact_id'] ?? 0 ) );
+ if (
+ $artifact_id > 0 &&
+ get_post_type($artifact_id) === 'attachment' &&
+ get_post_mime_type($artifact_id) === 'application/pdf'
+ ) {
+ update_post_meta($post_id, '_opentrust_cert_artifact_id', $artifact_id);
+ } else {
+ delete_post_meta($post_id, '_opentrust_cert_artifact_id');
+ }
update_post_meta($post_id, '_opentrust_cert_description', sanitize_textarea_field( wp_unslash( $_POST['opentrust_cert_description'] ?? '' ) ));- if ($attachment_id > 0 && get_post_type($attachment_id) === 'attachment') {
+ if (
+ $attachment_id > 0 &&
+ get_post_type($attachment_id) === 'attachment' &&
+ get_post_mime_type($attachment_id) === 'application/pdf'
+ ) {
update_post_meta($post_id, '_opentrust_policy_attachment_id', $attachment_id);
} else {
delete_post_meta($post_id, '_opentrust_policy_attachment_id');
}Also applies to: 929-933
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@includes/class-opentrust-cpt.php` around lines 867 - 869, Validate and gate
the attachment IDs before calling update_post_meta: cast the posted IDs with
absint but then verify the attachment exists and is an attachment (use
get_post($id) and check $post->post_type === 'attachment') and only save if
valid; for the badge/artifact metas (_opentrust_cert_badge_id,
_opentrust_cert_artifact_id) additionally restrict mime types as appropriate
(e.g., badge -> image/* via wp_get_attachment_metadata or get_post_mime_type)
and for the policy PDF field (the metadata saved around lines 929-933) enforce
get_post_mime_type($id) === 'application/pdf' before updating; keep
sanitize_textarea_field for _opentrust_cert_description and do not call
update_post_meta if validation fails.
rename_postmeta_keys_v5() ran AFTER the v1→v2 UUID back-fill. On a v1.0.x install upgrading straight to 1.1.1, backfill_uuids() saw no _opentrust_uuid (the post still had the legacy _ot_uuid), stamped a fresh one, and then the v5 rename renamed _ot_uuid → _opentrust_uuid — leaving two _opentrust_uuid rows with different values on every pre-existing post. Move the `if ($current < 5)` branch ahead of `if ($current < 2)` so legacy _ot_uuid rows are renamed first; the back-fill then correctly sees the existing _opentrust_uuid and skips. Corrects the misleading "ordering-independent" comment. Caught by CodeRabbit and an independent migration audit. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Summary
Proactive hardening pass before the WordPress.org resubmission. Round 1 of the review scoped the prefix finding to CPT slugs (already fixed in 1.1.0 →
opentr_*). This PR extends the cleanup to every other shared/global identifier so a stricter re-reviewer can't reopen the argument, and gates the plugin'serror_log()calls behindWP_DEBUGto pre-empt a Plugin Check warning.Neither item was flagged by the reviewer — both are pre-emptive.
What changed
Postmeta keys —
_ot_*→_opentrust_*across all ~44 plugin-owned keys, with a v4→v5 DB migration (OpenTrust::rename_postmeta_keys_v5()) that mirrors the proven v4 CPT-rename pattern: idempotent bulkUPDATEs keyed onmeta_key(catches posts, revisions, attachments), runs once on upgrade, retries safely if interrupted (opentrust_db_versiononly advances at the tail ofmaybe_upgrade()), and busts caches via the existing tail logic.Query vars —
ot_policy_slug/ot_version→opentrust_policy_slug/opentrust_version. Rewrite rules are recompiled by the existing end-of-maybe_upgrade()flush transient; pretty-permalink URLs are unaffected.Request-scoped identifiers — meta-box IDs, admin list-table column IDs, import/export form field
names, and the$_FILESkey →opentrust_*. No migration needed.LEGACY_META_MAP— new constant inOpenTrust_CPT, the single source of truth powering the v5 migration, the IO import remap (remap_legacy_meta_keys()keeps pre-1.1.1 export archives importable), and uninstall cleanup.error_log()— 3 unconditional calls routed through a newWP_DEBUG-gatedOpenTrust::debug_log()helper (one justifiedphpcs:ignoreinstead of three).Peripheral —
wpml-config.xmlkeys renamed (+ the previously-missing_opentrust_cert_artifact_id);uninstall.phpclears orphaned attachment + user meta; version bumped to 1.1.1, DB schema to 5, POT regenerated.Out of scope by design
CSS classes (
ot-*),data-ot-*attributes, local$ot_*variables, and theot_enc_v1:ciphertext marker — none are reviewable shared registries; renaming them is churn/risk with zero compliance value.Back-compat
maybe_upgrade()runs the v5 postmeta rename; content, revisions, version history, translations and URLs all preservedremap_legacy_meta_keys()rewrites keys at import timeTest plan
WP_DEBUG=true): create one of each CPT, fill every meta box, save, reload — fields persist; trust center + policy/version URLs render; CPT list columns showmaybe_upgrade()runs v5, all content still renders,wp_postmetahas zero_ot_*rowsremap_legacy_meta_keys())WP_DEBUGoff → noerror_logoutput; on → diagnostics still fire🤖 Generated with Claude Code
Summary by CodeRabbit
Release v1.1.1
Improvements
Bug Fixes
Chores