wp-seopress / wp-seopress-public

SEOPress, the best WordPress SEO plugin available on the market to rank higher in search engines.
https://www.seopress.org
31 stars 15 forks source link

Fatal error when adding `og:locale:alternate` link to non-existent post #36

Open vdwijngaert opened 3 years ago

vdwijngaert commented 3 years ago

This issue is in regards to this line of code:

https://github.com/wp-seopress/wp-seopress-public/blob/66ce66818c3a691d2b4560e3f82fe126f4710a1f/inc/functions/options-social.php#L387

In some cases, the wpml_post_language_details filter can return a WP_Error. Those cases are currently improperly handled. I just ran into an issue with an old translation record that points to a post that has since been deleted. This results in a fatal error:

Uncaught Error: Cannot use object of type WP_Error as array in wp-content/plugins/wp-seopress/inc/functions/options-social.php:389 Stack trace: 0 wp-includes/class-wp-hook.php(303): seopress_social_facebook_og_locale_hook('') 1 wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters(NULL, Array) 2 wp-includes/plugin.php(470): WP_Hook->do_action(Array) 3 wp-includes/general-template.php(3015): do_action('wp_head')

The code that handles this at WPMLs part:

    // (...)
    $post = get_post( $post_id );
    if ( empty( $post ) ) {
        // translators: Post id.
        return new WP_Error( 'missing_post', sprintf( __( 'No such post for ID = %d', 'sitepress' ), $post_id ) );
    }
    // (...)

I'm amazed they don't mention this in their documentation though.

wp-seopress commented 3 years ago

Thank you @vdwijngaert for your feedback.

We have added a fix in version 5.2.

You can test it by replacing line 389 in wp-content/plugins/wp-seopress/inc/functions/options-social.php by this one:

if (!is_wp_error( $my_post_language_details ) && !empty($my_post_language_details['locale']) && $my_post_language_details['different_language'] === true) {

ETA: this week.