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

TypeError in class-wpseo-meta.php due to Invalid Argument for array_column() #21900

Open
8 of 11 tasks
velasea-kw opened this issue Dec 4, 2024 · 1 comment
Open
8 of 11 tasks

Comments

@velasea-kw
Copy link

velasea-kw commented Dec 4, 2024

  • I've read and understood the contribution guidelines.
  • I've searched for any related issues and avoided creating a duplicate issue.

Please give us a description of what happened

When editing a post, the following fatal error occurs, preventing the page from loading properly:
Uncaught TypeError: array_column(): Argument #1 ($array) must be of type array, bool given in /var/www/wp-content/plugins/wordpress-seo/inc/class-wpseo-meta.php:1028
The error happens when $repository->query()->select()->where_in()->find_array() in class-wpseo-meta.php fails and returns false. This causes array_column() to throw a TypeError, as it expects an array.

To Reproduce

Step-by-step reproduction instructions

  1. Install and activate the Yoast SEO plugin version 24.0.
  2. Use the Divi theme (version 4.27.4).
  3. Edit any post in the WordPress dashboard.
  4. Observe the error displayed in the logs and inability to proceed.

Expected results

  1. The page should load without errors, and the plugin should handle cases where $repository->query()->select()->where_in()->find_array() fails gracefully by defaulting to an empty array.

Actual results

  1. The error blocks functionality, specifically in the WordPress editor, and the page does not load as expected.

Screenshots, screen recording, code snippet

Error Details
File: /var/www/wp-content/plugins/wordpress-seo/inc/class-wpseo-meta.php
Line: 1028

Stack Trace:

Uncaught TypeError: array_column(): Argument #1 ($array) must be of type array, bool given in /var/www/wp-content/plugins/wordpress-seo/inc/class-wpseo-meta.php:1028
Stack trace:
#0 ...

Suggested fix

The issue can be resolved by validating the $post_types variable before passing it to array_column(). Below is the adjusted code for the affected block in class-wpseo-meta.php:

// Check if post ids is not empty.
if ( ! empty( $post_ids ) ) {
    // Get the post subtypes for the posts that share the keyword.
    $post_types = $repository->query()
        ->select( 'object_sub_type' )
        ->where_in( 'object_id', $post_ids )
        ->find_array();

    // Ensure $post_types is an array before using array_column().
    if ( is_array( $post_types ) ) {
        // Get object_sub_type from each subarray in $post_types.
        $post_types = array_column( $post_types, 'object_sub_type' );
    } else {
        // Default to an empty array if the query fails.
        $post_types = [];
    }
} else {
    $post_types = [];
}

return $post_types;

Technical info

  • If relevant, which editor is affected (or editors):
  • Block Editor
  • Gutenberg Editor
  • Elementor Editor
  • Classic Editor
  • Other:
  • Which browser is affected (or browsers):
  • Chrome
  • Firefox
  • Safari
  • Other:

Used versions

  • Device you are using:
  • Operating system:
  • PHP version: 8.0.30
  • WordPress version: 6.6.2
  • WordPress Theme: Divi (version 4.27.4)
  • Yoast SEO version: 24.0
  • Gutenberg plugin version:
  • Elementor plugin version:
  • Classic Editor plugin version:
  • Relevant plugins in case of a bug:
@josevarghese
Copy link
Contributor

Hi @velasea-kw

Thanks for reporting this issue. I tested this on my lab site and noticed that the edit post loads without fatal errors with Yoast SEO 24.0 and Divi theme 4.27.4. So, could you let me know whether you have tested this case on a fresh WP installation without any other plugins active besides Yoast SEO? If not, please try this again and let us know the results or whether you missed any steps.

We look forward to hearing from you and are happy to help you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants