Skip to content

Fix trying to access non-existent array element#22227

Merged
igorschoester merged 1 commit intotrunkfrom
fix/read-property-on-null
May 6, 2025
Merged

Fix trying to access non-existent array element#22227
igorschoester merged 1 commit intotrunkfrom
fix/read-property-on-null

Conversation

@pls78
Copy link
Copy Markdown
Member

@pls78 pls78 commented May 2, 2025

Context

Summary

This PR can be summarized in the following changelog entry:

  • Fixes a bug in the retrieval of post categories titles where a non-existent term might be referenced. Props to @bor0.

Relevant technical choices:

  • I opted to use the reset() PHP function instead of the null coalescence operator so that we can still retrieve the first existent term.

Test instructions

Test instructions for the acceptance test before the PR gets merged

This PR can be acceptance tested by following these steps:

  • Have Query Monitor installed an active
  • Make sure you're on PHP 8.X
  • Create a post and set some categories for it
  • Add the following code snippet to your WordPress installation (either by creating a mu-plugin or using a plugin such as Code Snippets)
    add_filter(
          'get_the_terms',
          function( $terms ) {
                  if ( ! is_array( $terms ) ) {
                          return $terms;
                  }
                  /* ...some code that reworks the array based on conditions... */
                  return array_slice( $terms, 1, null, true );
          },
          10,
          1
    );
    
  • Edit the post you just created and verify:
    • Without this PR you get a warning stating Undefined array key 0 in Query Monitor
    • With this PR the warning is gone

Relevant test scenarios

  • Changes should be tested with the browser console open
  • Changes should be tested on different posts/pages/taxonomies/custom post types/custom taxonomies
  • Changes should be tested on different editors (Default Block/Gutenberg/Classic/Elementor/other)
  • Changes should be tested on different browsers
  • Changes should be tested on multisite

Test instructions for QA when the code is in the RC

  • QA should use the same steps as above.

Impact check

This PR affects the following parts of the plugin, which may require extra testing:

UI changes

  • This PR changes the UI in the plugin. I have added the 'UI change' label to this PR.

Other environments

  • This PR also affects Shopify. I have added a changelog entry starting with [shopify-seo], added test instructions for Shopify and attached the Shopify label to this PR.

Documentation

  • I have written documentation for this change. For example, comments in the Relevant technical choices, comments in the code, documentation on Confluence / shared Google Drive / Yoast developer portal, or other.

Quality assurance

  • I have tested this code to the best of my abilities.
  • During testing, I had activated all plugins that Yoast SEO provides integrations for.
  • I have added unit tests to verify the code works as intended.
  • If any part of the code is behind a feature flag, my test instructions also cover cases where the feature flag is switched off.
  • I have written this PR in accordance with my team's definition of done.
  • I have checked that the base branch is correctly set.

Innovation

  • No innovation project is applicable for this PR.
  • This PR falls under an innovation project. I have attached the innovation label.
  • I have added my hours to the WBSO document.

Fixes #

@pls78 pls78 added community-patch changelog: bugfix Needs to be included in the 'Bugfixes' category in the changelog labels May 2, 2025
@coveralls
Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 934e1a2bcd432404d056234481a8922001c8d17c

Details

  • 0 of 1 (0.0%) changed or added relevant line in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 52.518%

Changes Missing Coverage Covered Lines Changed/Added Lines %
inc/class-wpseo-replace-vars.php 0 1 0.0%
Totals Coverage Status
Change from base Build d61962685a45f26231a7d9f86216a0cf2c242f60: 0.0%
Covered Lines: 29147
Relevant Lines: 56574

💛 - Coveralls

@pls78 pls78 marked this pull request as ready for review May 2, 2025 09:27
@pls78 pls78 linked an issue May 2, 2025 that may be closed by this pull request
Copy link
Copy Markdown
Contributor

@igorschoester igorschoester left a comment

Choose a reason for hiding this comment

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

AT ✅

@igorschoester igorschoester added this to the 25.2 milestone May 6, 2025
@igorschoester igorschoester merged commit bd1848d into trunk May 6, 2025
32 checks passed
@igorschoester igorschoester deleted the fix/read-property-on-null branch May 6, 2025 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: bugfix Needs to be included in the 'Bugfixes' category in the changelog community-patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix PHP warning when trying to access non-existent array value

3 participants