johnbillion / query-monitor

The developer tools panel for WordPress
https://querymonitor.com
GNU General Public License v2.0
1.58k stars 207 forks source link

Avoid PHP Warning when there is no queried post object #853

Closed christianwach closed 6 months ago

christianwach commented 6 months ago

I'm encountering situations where I'm seeing the following PHP Warning in my logs:

PHP Warning:  Attempt to read property "post_content" on null in /path/to/httpdocs/wp-content/plugins/query-monitor/collectors/block_editor.php on line 150

This mostly happens when viewing CiviCRM admin pages... but also elsewhere, though I can't recall where 🙄

Anyway, this PR simply checks that there is a queried object before trying to access the corresponding Post's content.

johnbillion commented 6 months ago

Thanks for the PR! I'm pretty hesitant to add this because if is_singular() is true but get_queried_object() returns null then something is very broken on the site.

Have you contacted CiviCRM about this?

christianwach commented 6 months ago

@johnbillion Thanks for the quick response. It's not guaranteed that CiviCRM is the cause - there are a number of other plugins active. I'll do some digging to find out why this happens and provide more context.

christianwach commented 6 months ago

@johnbillion Turns out it's BuddyPress that's responsible, though only during heartbeat requests. As the docblock description says, BuddyPress "essentially fakes WordPress into thinking that there is content where, in fact, there is none (at least, no WordPress post content)." I'll raise the issue over there, though I can see why BuddyPress does what it does.

Here's the trimmed raw data for everyone's reference:

[15-Feb-2024 11:34:01 UTC] Array
(
  [REQUEST_URI] => /wp-admin/admin-ajax.php
  [POST] => Array
    (
      [interval] => 60
      [_nonce] => c1973191d6
      [action] => heartbeat
      [screen_id] => toplevel_page_bp-groups
      [has_focus] => false
    )

)

[15-Feb-2024 11:34:01 UTC] Array
(
  [method] => QM_Collector_Block_Editor::process
  [wp_query] => WP_Query Object
    (
      [query] => Array
        (
          [pagename] => wp-admin/admin.php
          [page] => 
        )
      [query_vars] => Array
        (
          [pagename] => wp-admin/admin.php
          [page] => 
          [p] => 0
          [post_parent] => 
          [subpost] => 
          [subpost_id] => 
          [name] => 
          [page_id] => 0
          [paged] => 0
        )
      [tax_query] => 
      [meta_query] => 
      [date_query] => 
      [queried_object] => 
      [queried_object_id] => 
      [post_count] => 0
      [current_post] => -1
      [before_loop] => 1
      [in_the_loop] => 
      [comment_count] => 0
      [current_comment] => -1
      [found_posts] => 0
      [max_num_pages] => 0
      [max_num_comment_pages] => 0
      [is_single] => 
      [is_preview] => 
      [is_page] => 1
      [is_archive] => 
      [is_date] => 
      [is_year] => 
      [is_month] => 
      [is_day] => 
      [is_time] => 
      [is_author] => 
      [is_category] => 
      [is_tag] => 
      [is_tax] => 
      [is_search] => 
      [is_feed] => 
      [is_comment_feed] => 
      [is_trackback] => 
      [is_home] => 
      [is_privacy_policy] => 
      [is_404] => 
      [is_embed] => 
      [is_paged] => 
      [is_admin] => 1
      [is_attachment] => 
      [is_singular] => 1
      [is_robots] => 
      [is_favicon] => 
      [is_posts_page] => 
      [is_post_type_archive] => 
    )
  [is_singular] => 1
  [get_queried_object_id] => 0
  [backtrace] => #0 /path/to/httpdocs/wp-content/plugins/query-monitor/classes/Collectors.php(84): QM_Collector_Block_Editor->process()
#1 /path/to/httpdocs/wp-content/plugins/query-monitor/classes/Dispatcher.php(108): QM_Collectors->process()
#2 /path/to/httpdocs/wp-content/plugins/query-monitor/dispatchers/AJAX.php(53): QM_Dispatcher->get_outputters('headers')
#3 /path/to/httpdocs/wp-includes/class-wp-hook.php(324): QM_Dispatcher_AJAX->dispatch('')
#4 /path/to/httpdocs/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array)
#5 /path/to/httpdocs/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#6 /path/to/httpdocs/wp-includes/load.php(1266): do_action('shutdown')
#7 [internal function]: shutdown_action_hook()
#8 {main}
)

[15-Feb-2024 11:34:01 UTC] PHP Warning:  Attempt to read property "post_content" on null in /path/to/httpdocs/wp-content/plugins/query-monitor/collectors/block_editor.php on line 140
johnbillion commented 6 months ago

Thanks for looking into it 👍 Let's see what the folks at BuddyPress say.

christianwach commented 6 months ago

@johnbillion I'm going to close this because IMO the essence of your initial response is correct (although nothing is "very broken" as such) and it's good that QM has revealed that there is an issue in BuddyPress.

FWIW, it's more complicated than just faking posts - it's BuddyPress resetting the query during all heartbeat requests and thus WP_Query->is_page = true in WP_Query->parse_query() because $qv['pagename'] is whatever $_SERVER['REQUEST_URI'] is set to.

Thanks for staying skeptical 👍