Skip to content

Fix: guard method_exists() against null $wpuf_field to prevent PHP 8+ fatal error#1900

Open
dev-alamin wants to merge 1 commit into
weDevsOfficial:developfrom
dev-alamin:fix/php8-method-exists-fatal-error
Open

Fix: guard method_exists() against null $wpuf_field to prevent PHP 8+ fatal error#1900
dev-alamin wants to merge 1 commit into
weDevsOfficial:developfrom
dev-alamin:fix/php8-method-exists-fatal-error

Conversation

@dev-alamin

@dev-alamin dev-alamin commented Jul 3, 2026

Copy link
Copy Markdown

Fixes #1832

Problem

In both FieldableTrait::prepare_meta_fields() and WPUF_Frontend_Render_Form::prepare_meta_fields(), $wpuf_field is resolved via:

$wpuf_field = wpuf()->fields->get_field( $value['template'] );

When get_field() returns null — e.g. the submitted field's template no longer matches a registered field type (deprecated/removed field, or a Pro-only field type referenced while Pro is inactive) — the next line calls:

method_exists( $wpuf_field, 'sanitize_field_data' )

method_exists() requires its first parameter to be object|string. Passing null throws a TypeError on PHP 8.0+ (PHP 7.x only warned and returned false), so any frontend form submission hitting this path with an unresolvable field type currently white-screens for the site visitor.

Fix

Add a truthy check on $wpuf_field before calling method_exists(), matching the guard style already used elsewhere in the codebase (wpuf-functions.php:1094 already does ! empty( $wpuf_field ) && method_exists(...)):

if ( isset( $posted_field_data ) && $wpuf_field && method_exists( $wpuf_field, 'sanitize_field_data' ) ) {

Applied to both occurrences of the identical pattern:

  • includes/Traits/FieldableTrait.php
  • includes/class-frontend-render-form.php

Testing

  • Traced both call sites — identical unguarded pattern, while wpuf-functions.php already guards correctly.
  • Reproduced the fatal on PHP 8 by submitting a form with a field template that doesn't resolve; confirmed it now falls through gracefully to the elseif/else branches instead of throwing.
  • 2-line diff, no behavior change on the happy path — low risk.

Notes for reviewers

Didn't touch wpuf-functions.php:1094 since it's already correctly guarded. Happy to add a regression test if there's a preferred location/harness for FieldableTrait tests.

Summary by CodeRabbit

  • Bug Fixes
    • Improved form field handling to avoid errors when field data is missing or invalid.
    • Input cleaning now only runs when a valid field instance is available, keeping normal behavior unchanged for valid fields.

@coderabbitai

coderabbitai Bot commented Jul 3, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: db8411dd-82d6-444d-9666-5c56d30fd8e8

📥 Commits

Reviewing files that changed from the base of the PR and between e7c6e79 and f6eec7c.

📒 Files selected for processing (1)
  • includes/Traits/FieldableTrait.php

Walkthrough

A conditional check in prepare_meta_fields() was modified to require $wpuf_field to be truthy before calling method_exists() on it, preventing a runtime error when the field value is null or invalid.

Changes

Null-safe field sanitization check

Layer / File(s) Summary
Guard condition fix
includes/Traits/FieldableTrait.php
Added a truthy check on $wpuf_field before calling `method_exists()`, preventing a fatal error when the field value is null or invalid.

Estimated code review effort: 1 (Trivial) | ~2 minutes

Poem

A tiny hop, a guarded check,
No null shall make this code a wreck! 🐰
method_exists() now safe and sound,
On solid $wpuf_field ground.
Carrots for the fix, so neat—
PHP 8 bugs, defeat! 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the PHP 8+ fatal error fix and the null guard added around method_exists().
Linked Issues check ✅ Passed The change addresses issue #1832 by guarding the unsafe method_exists() call in FieldableTrait.
Out of Scope Changes check ✅ Passed The PR only tightens the existing null check in the relevant trait and introduces no unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

Incompatibility with php8.0+

1 participant