awesomemotive / easy-digital-downloads

Sell digital downloads through WordPress
https://easydigitaldownloads.com
GNU General Public License v2.0
863 stars 474 forks source link

(PHP8.x) Attempt to read property "post_type" on int warning message #9728

Open alexmangini opened 2 months ago

alexmangini commented 2 months ago

Bug Report

When calling the_excerpt() within a single post that refers to a post (that no longer exists) on a Custom Post Type Archive page I get the following warning from includes/templates-functions.php line 462

Attempt to read property "post_type" on int wp-content/plugins/easy-digital-downloads/includes/template-functions.php:462 edd_after_download_content() wp-includes/class-wp-hook.php:324 apply_filters('the_content') wp-includes/formatting.php:3992

Information (if a specific version is affected):

PHP Version: 8.3.6

EDD Version (or branch): 3.2.12

WordPress Version: 6.5.2

Any other relevant information:

The edd_after_download_content() function calls global $post which usually returns an object, but it seems when the post is not found it returns an int of the post ID in reference which is causing the error on this check in the function:

if ( $post && $post->post_type == 'download' ...

I have patched this function on my installation to remove the global call and use get_ functions to get the post ID and post type check.

As far as I have tested, the purchase buttons still appear at the end of single downloads but I haven't tested further than that:

function edd_after_download_content( $content ) {
    if ( get_post_type() == 'download' && is_singular( 'download' ) && is_main_query() && !post_password_required() ) {
        ob_start();
        do_action( 'edd_after_download_content', get_the_ID() );
        $content .= ob_get_clean();
    }

    return $content;
}