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

Feeds: Possible non-object while running the redirects #22

Closed MatzeKitt closed 4 years ago

MatzeKitt commented 4 years ago

Feeds can lead to a non-existing post object. This may result in a Trying to get property 'post_type' of non-object here in line 74, since is_singular is not reliable at this moment (see more in the trac ticket #29660).

The full error message:

ErrorException: Notice: Trying to get property 'post_type' of non-object
#8 /wp-includes/class-wp-query.php(4103): WP_Query::is_singular
#7 /wp-includes/query.php(731): is_singular
#6 /wp-content/plugins/disable-blog/public/class-disable-blog-public.php(74): Disable_Blog_Public::redirect_posts
#5 /wp-includes/class-wp-hook.php(288): WP_Hook::apply_filters
#4 /wp-includes/class-wp-hook.php(312): WP_Hook::do_action
#3 /wp-includes/plugin.php(478): do_action
#2 /wp-includes/template-loader.php(13): require_once
#1 /wp-blog-header.php(19): require
#0 /index.php(17): null

My proposed solution:
Add a check if the post object exists before running is_singular.

if( is_singular( 'post' ) ) {

    global $post;

would become:

global $post;

if( $post instanceof WP_Post && is_singular( 'post' ) ) {
joshuadavidnelson commented 4 years ago

Hey @KittMedia - thanks for following up on this! (Just noting for myself later and any others, this relates to your WP.org support forum ticket

I'll test this out and wrap it into the next release, which I had planned for a couple weeks ago but haven't had time to finalize. I will push for it this week.

MatzeKitt commented 4 years ago

Oh, you‘re right. I already forgot about this support thread. My bad.

joshuadavidnelson commented 4 years ago

No, it's great! I had gone down a rabbit hole trying to source the problem when you initially posted there, but I think your fix looks like a winner.