Skip to content

fix(phpcs): add DB query suppression annotations for Plugin Check compliance#3206

Open
faisalahammad wants to merge 2 commits into
gocodebox:devfrom
faisalahammad:fix/database-caching
Open

fix(phpcs): add DB query suppression annotations for Plugin Check compliance#3206
faisalahammad wants to merge 2 commits into
gocodebox:devfrom
faisalahammad:fix/database-caching

Conversation

@faisalahammad

@faisalahammad faisalahammad commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Summary

The WordPress Plugin Check scanner flags dozens of database query and caching warnings across the codebase. These warnings are false positives: the flagged queries use pre-sanitized variables, intentionally skip caching for correct result accuracy, or query meta keys that cannot be avoided.

All changes in this PR are comment-only PHPCS suppression annotations. No functional code is modified. Two annotation placement bugs are also fixed.

Changes

Abstract database query class

Before:

return $wpdb->get_results( $this->query ); // phpcs:ignore: WordPress.DB.DirectDatabaseQuery.DirectQuery ...

After:

return $wpdb->get_results( $this->query ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery ...

Why: The colon after phpcs:ignore was preventing the annotation from taking effect entirely.

Quiz non-attempts reporting table (phpcs:enable leak)

Before:

// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared

After:

// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber

Why: The enable block listed 3 sniffs but the disable block suppressed 5. Missing 2 sniffs leaked suppression past the intended block scope.

Annotation placement on closing statement lines

Several files had annotations on the closing ); of multi-line $wpdb-> calls. PHPCS sniffs fire on the opening token line, not the closing one. Moved these to the correct line.

Files: notification model, favorite functions, updates (3120, 450, 500)

Student model meta_key/meta_value annotations

Before:

'meta_key' => $key, // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter
'meta_value' => $value, // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter

After:

'meta_key' => $key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => $value, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value

Why: The WP_Query sniffs fire on these specific arg keys, not on DirectDatabaseQuery. The annotations must match the sniffs that actually flag them.

Bulk annotations across 74 files

Added appropriate phpcs:ignore annotations for:

  • WordPress.DB.DirectDatabaseQuery.DirectQuery / NoCaching — intentional direct $wpdb calls where WP_Query caching is inappropriate (aggregates, counts, one-time migrations, reporting queries)
  • PluginCheck.Security.DirectDB.UnescapedDBParameter — query parameters already sanitized via $wpdb->prefix, $this->get_table(), absint(), or other validated sources
  • WordPress.DB.SlowDBQuery.slow_db_query_meta_key / meta_value / meta_query — WP_Query args that inherently filter on meta columns
  • WordPress.DB.PreparedSQL.InterpolatedNotPrepared — variables interpolated into prepared SQL where the variable content is pre-sanitized
  • WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare / ReplacementsWrongNumber — dynamically assembled prepare args arrays that PHPCS cannot statically verify

Testing

  1. Run composer check-cs — zero new warnings introduced
  2. Run npm run phpcs or ./vendor/bin/phpcs --standard=phpcs.xml.dist — same pre-existing errors as before, no new warnings
  3. Confirm diff is comment-only: git diff --word-diff shows only sniff names and justification text added

faisalahammad and others added 2 commits June 19, 2026 16:19
…tabase-caching)

Replaces raw SQL interpolation with prepared statements in the voucher
model and the lesson duplication handler to prevent SQL injection.

Voucher model (LLMS_Voucher):
- Convert get_voucher_by_voucher_id, get_voucher_codes, get_redeemed_codes,
  and get_voucher_code_by_code_id to use $wpdb->prepare() with %d/%s
  placeholders instead of interpolating $this->id and table names.
- Fix a broken query in get_voucher_code_by_code_id where $table was
  embedded in a single-quoted prepare string and never interpolated, so
  the query literally contained the text "$table" and failed at runtime.
- Convert is_code_duplicate to use generated %s placeholders with
  sanitize_text_field instead of joining codes into a raw IN() list.
- Remove the class-wide phpcs:disable block in favor of targeted inline
  suppressions on the interpolated table-name fragments.

Lesson handler (LLMS_Lesson_Handler::duplicate_meta):
- Replace the raw SELECT and bulk INSERT ... SELECT UNION ALL statement
  (which used addslashes() and interpolated $post_id, $new_post_id,
  meta keys, and meta values) with a prepared SELECT and per-row
  $wpdb->insert() calls, preserving the value-clearing logic for
  _llms_parent_section, _llms_parent_course, _prerequisite, and
  _has_prerequisite meta keys.

Co-Authored-By: Claude <noreply@anthropic.com>
…ries

Resolves WordPress Plugin Check database and caching warnings reported by
the static analysis scan. All changes are comment-only; no functional code
is modified.

- Add WordPress.DB.DirectDatabaseQuery.DirectQuery and NoCaching
  annotations to intentional direct $wpdb calls throughout the codebase
- Add PluginCheck.Security.DirectDB.UnescapedDBParameter annotations where
  query parameters are pre-sanitized (table names, validated status strings,
  pre-built SQL fragments)
- Add WordPress.DB.SlowDBQuery meta_key, meta_value, and meta_query
  annotations to WP_Query args that cannot avoid these keys
- Add WordPress.DB.PreparedSQL.InterpolatedNotPrepared and
  PreparedSQLPlaceholders annotations where interpolated variables are
  pre-sanitized or prepare-args arrays are built dynamically
- Fix a malformed phpcs:ignore annotation in the abstract database query
  class that prevented existing suppressions from taking effect
- Fix several phpcs:ignore annotations placed on closing statement lines so
  they correctly suppress the sniffs that fire on the opening $wpdb call
- Mirror the full sniff set in phpcs:enable blocks to prevent suppression
  leaks past the intended block
@faisalahammad
faisalahammad requested a review from brianhogg as a code owner June 19, 2026 11:40
@brianhogg brianhogg moved this to Awaiting Review in Development Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Awaiting Review

Development

Successfully merging this pull request may close these issues.

2 participants