Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check return value of get_post_meta #1167

Merged
merged 8 commits into from
Jul 29, 2024
Merged

Check return value of get_post_meta #1167

merged 8 commits into from
Jul 29, 2024

Conversation

dhanendran
Copy link
Member

Description of the Change

Validate the return value of get_post_meta and make sure it is array.

Closes #1107

How to test the Change

  1. Enable debug logging in wp-config
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
  1. Run wp shell and call the function with an invalid post ID: \Distributor\Utils\prepare_meta('bug');
  2. Open the debug.log file in your wp-content directory and observe the no warning is thrown: PHP Warning: Invalid argument supplied for foreach() in .../includes/utils.php on line 427

Changelog Entry

Fixed - PHP warning about invalid argument supplied for foreach in \Distributor\Utils\prepare_meta()

Credits

Props @dhanendran

Checklist:

  • I agree to follow this project's Code of Conduct.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests pass.

@dhanendran dhanendran requested a review from a team as a code owner December 8, 2023 18:45
@dhanendran dhanendran requested review from iamdharmesh and removed request for a team December 8, 2023 18:45
@dhanendran dhanendran self-assigned this Dec 8, 2023
@jeffpaul jeffpaul added this to the 2.1.0 milestone Dec 8, 2023
@github-actions github-actions bot added the needs:code-review This requires code review. label Jul 3, 2024
Copy link
Member

@faisal-alvi faisal-alvi left a comment

Choose a reason for hiding this comment

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

@dhanendran Thanks for the PR. As the return value of get_post_meta() is mixed, it may return false and we will end up overwriting it with an array() which may throw the PHP warning in v8+.

includes/utils.php Show resolved Hide resolved
Copy link
Member

@faisal-alvi faisal-alvi left a comment

Choose a reason for hiding this comment

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

@kirtangajjar thanks for the fix. A couple of minor PHPCS issues are noted below.

An early return would work but will never reach the dt_prepared_meta filter. We can consider it as an exception though (i.e. empty meta would not reach the filter).

includes/utils.php Outdated Show resolved Hide resolved
includes/utils.php Show resolved Hide resolved
@jeffpaul jeffpaul requested a review from faisal-alvi July 8, 2024 15:48
@kirtangajjar
Copy link
Member

@faisal-alvi Made the requested changes. LMk if there are any more

includes/utils.php Outdated Show resolved Hide resolved
Copy link
Member

@faisal-alvi faisal-alvi left a comment

Choose a reason for hiding this comment

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

Ignoring E2E failures as they are being addressed in #1242

@jeffpaul jeffpaul merged commit c8069ca into develop Jul 29, 2024
13 of 18 checks passed
@jeffpaul jeffpaul deleted the fix/1107 branch July 29, 2024 21:18
@dkotter dkotter modified the milestones: 2.1.0, 2.0.5 Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs:code-review This requires code review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Utils\prepare_meta() does not validate return value of get_post_meta()
5 participants