Skip to content

feat(ci): audit rtgodam_before/after_attachment_lookup hook integrity - #2072

Merged
KMchaudhary merged 17 commits into
developfrom
feature/adds-attachment-hooks-ci
Aug 25, 2026
Merged

KMchaudhary merged 17 commits into
developfrom
feature/adds-attachment-hooks-ci

Conversation

@Pathan-Amaankhan

@Pathan-Amaankhan Pathan-Amaankhan commented Aug 10, 2026 •

Copy link
Copy Markdown
Member

Summary

Adds automated CI + pre-commit checks that guard the rtgodam_before_attachment_lookup / rtgodam_after_attachment_lookup hook pair against silent regressions. These hooks let a plugin that centralizes attachments across a multisite network (e.g. wp-dam) switch site context around every place GoDAM reads/writes attachment data directly, then switch back. GoDAM only fires them — nothing previously stopped a hook from quietly disappearing, or new attachment-access code from shipping without the wrapping it needs.

What's included

  • bin/hook-check/balance.php — one check: every rtgodam_before_attachment_lookup in a scope must reach a matching rtgodam_after_attachment_lookup before the scope ends, and no after may fire with nothing open. Branch-aware around early return/throw/exit/die/wp_die()-style exits, so a guard clause that closes the wrap and returns early isn't a false alarm. Deliberately doesn't track whether a hook was removed or how much attachment-access code exists — that's a GitHub reviewer's job reading the diff, and a removed hook whose access call stays is caught directly (not historically) by the sibling coverage check below.
  • bin/hook-check/coverage.php — a finer-grained, per-call-site pass: flags every attachment-access call (get_post_meta, wp_get_attachment_url, etc.) or attachment-shaped query pattern (WP_Query, get_posts, get_children, raw $wpdb queries) that has no bracket open at that exact point, unless its argument is provably a pass-through of an already-safe value. Includes interprocedural call-graph tracing (arbitrary hops, with codebase-wide-uniqueness safeguards) so a parameter-sourced call covered only by a caller's caller doesn't false-positive.
  • bin/hook-check/shared.php — tokenizer/scope-walking primitives shared by both scripts, plus the godam-coverage-* directive parser (below). Extracted so the two scripts can't drift to different definitions of "what counts as firing a hook."
  • Inline godam-coverage-ignore / -disable / -enable / -ignore-file comments — the only acceptance mechanism, replacing an earlier baseline-JSON + update-baseline design entirely. Mirrors phpcs:ignore/disable/enable/ignoreFile minus a rule-code argument, since each checker only ever checks one thing:
    get_post_meta( $id, 'x', true ); // godam-coverage-ignore -- not attachment data, see docblock above
    
    // godam-coverage-disable -- whole migration is host-post-scoped, see class docblock
    ...several lines...
    // godam-coverage-enable
    A -disable with no matching -enable before end of file is a hard error, not a silent "rest of the file is fine." No baseline files, no update-baseline mode — every run judges only the code and its own inline comments as they currently stand.
  • .github/workflows/wp_dam_hook_integrity_on_pull_request.yml — runs both checks in CI, scoped to PRs touching inc/, admin/, assets/src/blocks/, or the checker scripts themselves.
  • bin/hook-check/pre-commit.sh (wired into .husky/pre-commit) — runs the same two checks locally pre-commit, over the same path scope.
  • package.json — godam-hooks:balance-check / godam-hooks:coverage-check, combined into lint:godam-hooks via npm-run-all --continue-on-error so a failure in one doesn't hide the other's findings.

Notable detection fixes along the way

  • A raw $wpdb query referencing {$wpdb->posts}/{$wpdb->postmeta} via string interpolation previously went undetected — PHP's tokenizer splits that into separate $wpdb/->/posts tokens with no wp_posts substring for the old check to find.
  • A WP_Query/get_posts/get_children call whose arguments come from a separate helper function (e.g. new WP_Query( build_args() )) previously read as "not attachment-shaped" and wasn't flagged at all, since the post_type literal lives inside a callee the scan never looks into. Now defaults to flagging it, matching this tool's existing philosophy elsewhere: a false positive costs a one-line dismissal comment, a false negative is a silent gap.

How it works

Single mode — the code plus its own inline godam-coverage-* comments are the complete, self-contained source of truth:

php bin/hook-check/balance.php
php bin/hook-check/coverage.php

A real finding means either add the hook pair, or — if the call genuinely isn't centralization-relevant attachment data — add the matching inline comment with a reason, right at the call site.

Adds three static-analysis scripts under bin/ that guard the
rtgodam_before_attachment_lookup / rtgodam_after_attachment_lookup hook
pair against silent regressions, each checked against an accepted JSON
baseline:

- godam-wp-dam-hook-check.php: per-file hook-fire count regressions and
  before/after balance within a scope.
- godam-attachment-access-coverage-check.php: new or growing
  attachment-access calls that aren't yet reviewed for hook coverage.
- godam-interprocedural-leak-check.php: a self-wrapped function whose
  hook stays open across a call site elsewhere.

Wires them into a new GitHub Actions workflow
(wp_dam_hook_integrity_on_pull_request.yml) and into the local
pre-commit hook via godam-hook-check-pre-commit.sh. Each script
supports a `check` mode (default, used in CI) and an `update-baseline`
mode (run locally after manually auditing new/changed code).
Copilot AI balanced review requested due to automatic review settings August 10, 2026 07:42
@Pathan-Amaankhan Pathan-Amaankhan self-assigned this Aug 10, 2026
@github-actions

github-actions Bot commented Aug 10, 2026 •

Copy link
Copy Markdown

🔍 WordPress Plugin Check Report

❌ Status: Failed

📊 Report

🎯 Total Issues ❌ Errors ⚠️ Warnings
17 1 16

❌ Errors (1)

📁 readme.txt (1 error)
📍 Line 🔖 Check 💬 Message
0 outdated_tested_upto_header Tested up to: 7.0 < 7.1. The "Tested up to" value in your plugin is not set to the current version of WordPress. This means your plugin will not show up in searches, as we require plugins to be compatible and documented as tested up to the most recent version of WordPress.

⚠️ Warnings (16)

📁 readme.txt (2 warnings)
📍 Line 🔖 Check 💬 Message
0 mismatched_plugin_name Plugin name "GoDAM - Organize WordPress Media Library & File Manager with Unlimited Folders for Images, Videos & more" is different from the name declared in plugin header "GoDAM".
0 trademarked_term The plugin name includes a restricted term. Your chosen plugin name - "GoDAM - Organize WordPress Media Library & File Manager with Unlimited Folders for Images, Videos & more" - contains the restricted term "wordpress" which cannot be used at all in your plugin name.
📁 composer.json (1 warning)
📍 Line 🔖 Check 💬 Message
0 missing_composer_json_file The "/vendor" directory using composer exists, but "composer.json" file is missing.
📁 assets/build/blocks/godam-gallery-v2/render.php (2 warnings)
📍 Line 🔖 Check 💬 Message
15 WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound Global variables defined by a theme/plugin should start with the theme/plugin prefix. Found: "$inner_block_video_ids".
23 WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound Global variables defined by a theme/plugin should start with the theme/plugin prefix. Found: "$inner_block_video_ids".
📁 assets/build/css/main.css (1 warning)
📍 Line 🔖 Check 💬 Message
0 EnqueuedStylesScope This style is being loaded in all contexts.
📁 assets/src/libs/analytics.min.js (5 warnings)
📍 Line 🔖 Check 💬 Message
0 EnqueuedScriptsScope This script is being loaded in all frontend contexts.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880 (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/2026/08/25/hello-world/ (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/sample-page/ (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/demo-attachment-post/ (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead.
📁 assets/build/js/main.min.js (5 warnings)
📍 Line 🔖 Check 💬 Message
0 EnqueuedScriptsScope This script is being loaded in all frontend contexts.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880 (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/2026/08/25/hello-world/ (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/sample-page/ (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/demo-attachment-post/ (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead.

🤖 Generated by WordPress Plugin Check Action • Learn more about Plugin Check

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Adds automated CI and local pre-commit guards to ensure GoDAM’s rtgodam_before_attachment_lookup / rtgodam_after_attachment_lookup hook pair remains present, balanced, and continues to cover attachment-access call sites as the codebase evolves.

Changes:

  • Added three PHP CLI audit scripts (hook-count regression + balance, attachment-access coverage, and interprocedural leak detection) with baseline JSONs.
  • Wired the checks into CI (new GitHub Actions workflow) and local development (Husky pre-commit + npm script).
  • Updated repo hygiene (.gitignore for .idea/).

Reviewed changes

Copilot reviewed 3 out of 11 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
package.json Adds an npm script to run the three hook-integrity checks.
bin/godam-wp-dam-hook-check.php New: baseline-backed wrap-count regression + within-scope before/after balance validation.
bin/godam-wp-dam-hook-baseline.json New: accepted baseline for wrap/access counts.
bin/godam-interprocedural-leak-check.php New: detects “self-wrapped callee + unwrapped follow-up” leak candidates, baseline-backed.
bin/godam-interprocedural-leak-baseline.json New: accepted baseline container for leak candidates.
bin/godam-hook-check-pre-commit.sh New: runs checks locally (only when relevant paths are staged).
bin/godam-attachment-access-coverage-check.php New: flags attachment-access calls that occur with no “before” hook open, baseline-backed.
bin/godam-attachment-access-coverage-baseline.json New: accepted baseline container for uncovered access call sites.
.husky/pre-commit Runs hook-integrity checks after existing lint:staged.
.gitignore Ignores JetBrains .idea/.
.github/workflows/wp_dam_hook_integrity_on_pull_request.yml New CI workflow to run the three checks on PRs touching relevant paths.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread package.json Resolved
@KMchaudhary

Copy link
Copy Markdown
Collaborator

@Pathan-Amaankhan

  • This CI will cause pre-commit failures for non-attachment post code.

And will you be able to segregate whether the hooks are called in the context of attachment or post on VideoJet's feature plugins?

Also, what if the developer tries to perform some post operation on a non-attachment post type where rtgodam_before_attachment_lookup & rtgodam_after_attachment_lookup hooks are not required, in that case, Husky will show the pre-commit error.

rtBot
rtBot previously requested changes Aug 23, 2026

@rtBot rtBot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code analysis identified issues

action-phpcs-code-review has identified potential problems in this pull request during automated scanning. We recommend reviewing the issues noted and that they are resolved.

phpcs scanning turned up:

🚫 1 error

⚠️ 1 warning


Powered by rtCamp's GitHub Actions Library

Comment thread bin/hook-check/shared.php Resolved
Comment thread bin/hook-check/shared.php Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 7 changed files in this pull request and generated 2 comments.

…nt_lookup hooks around media (#2080)

* feat: add rtgodam_before_attachment_lookup and rtgodam_after_attachment_lookup hooks around media

* feat: add pending rtgodam_before_attachment_lookup and rtgodam_after_attachment_lookup hooks around media

* fix: fixes core godam bugs

* refactor: update before/after attachmet CI baseline

* refactor: adds godam-coverage-ignore comments

* refactor: update since version

* feat: adds before/after media hook

* refactor: adds godam-coverage-ignore comments

* refactor: adds godam-coverage-ignore comments

* feat: adds before/after media hook

* feat: adds before/after media hook
@Pathan-Amaankhan
Pathan-Amaankhan dismissed rtBot’s stale review August 25, 2026 07:05

Reviews are already resolved.

@KMchaudhary
KMchaudhary merged commit 429db46 into develop Aug 25, 2026
4 of 6 checks passed
@KMchaudhary
KMchaudhary deleted the feature/adds-attachment-hooks-ci branch August 25, 2026 07:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants