joshuadavidnelson / disable-blog

All the power of WordPress, without a blog. This plugin removes blog related functionality.
https://wordpress.org/plugins/disable-blog/
GNU General Public License v2.0
52 stars 6 forks source link

Fatal error when WooCommerce is activated (Comments) #11

Closed Mahjouba91 closed 8 years ago

Mahjouba91 commented 8 years ago

Hello,

First, my website is on last version of WordPress, your plugin and WooCommerce.

I tried the plugin on my private website and show me a PHP fatal error on activation about woocommerce comments.

I spoted where the bug come from admin/class-disable-blog-admin.php :

public function filter_wp_count_comments( $comments, $post_id ) {

        // if this is grabbing all the comments, filter out the 'post' comments.
        if( 0 == $post_id )
            $comments = $this->get_comment_counts();

        // If we filtered it above, it needs to be an object, not an array.
        if( ! empty( $comments ) ) {
            $comments = (object) $comments;
        }

        return $comments;
    }

It bugs in the casting operation (array to object).

Here is the fatal error : Fatal error: Uncaught Error: Cannot use object of type stdClass as array in \wp-content\plugins\woocommerce\includes\class-wc-comments.php:272 Stack trace: #0 \wp-includes\plugin.php(235): WC_Comments::wp_count_comments(Object(stdClass), 0) #1 wp-includes\comment.php(1113): apply_filters('wp_count_commen...', Object(stdClass), 0) #2 wp-admin\menu.php(72): wp_count_comments() #3 wp-admin\admin.php(138): require('C:\\xampp\\htdocs...') #4 wp-admin\plugins.php(10): require_once('C:\\xampp\\htdocs...') #5 {main} thrown in \wp-content\plugins\woocommerce\includes\class-wc-comments.php on line 272

joshuadavidnelson commented 8 years ago

@Mahjouba91 Thanks for reporting this error. I haven't run tests with WooCommerce, I'll track down the issue and fix it.

joshuadavidnelson commented 8 years ago

I'm not able to replicate the issue yet, but I think I see the problem.

Ultimately the $comments variable needs to be delivered as an object (see the return in wp_count_comments), but it looks like WooCommerce is filtering it when it is an array, but after disable-blog filters it (and the filter is setup as an array that gets modified into an object).

joshuadavidnelson commented 8 years ago

Alright, looking back at it, I think the reason I can't replicate the error is I assumed the most recent version of WooCommerce. WooCommerce should be setting this variable to an array (or checking it is an array) prior to using it as one, since other plugins/themes can filter it and have to return it as an object.

It looks like they committed a change that does that in July. Are you running the most recent version of WooCommerce (2.6.4)? Because there is a blank link in class-wc-comments.php line 272, referenced in your error, and I can only replicate the issue on version 2.6.2 of WooCommerce. Updating WooCommerce should solve the issue.

I've updated the issue branch to include some backwards compatibility in my plugin for the older versions of WooCommerce, which I'll put in a new release sometime this next week. Let me know if there are other issues or if this didn't solve your problem.

Mahjouba91 commented 8 years ago

Oh I'm sorry, I gave you wrong informations, my WooCommerce version was not updated, I'm still on 2.5 branch. I hope there will be a way to ensure backward compa with all recent versions to avoid fatal error like that.

Thanks for the review ;)

joshuadavidnelson commented 8 years ago

@Mahjouba91 Yes, the issue branch appears to work on your version, giving backward compatibility for WooCommerce. I'll merge into production and release an update sometime soon, within the week.

Mahjouba91 commented 8 years ago

Your patch seems good, I really apreciate your work thank you ;)

joshuadavidnelson commented 8 years ago

Finally got around to updating the repo (here and on WP.org). Let me know if there are any other issues!