jakewrfoster / query-monitor-save-post-queries

Capture and display queries during a save post event.
1 stars 0 forks source link

plugin causing WP-CLI fatal error #6

Open columbian-chris opened 4 months ago

columbian-chris commented 4 months ago

Description of the bug

The plugin is causing WP-CLI to fail. Here is the error using a simple WP-CLI command:

$ wp maintenance-mode status
Fatal error: Uncaught Error: Class "QM_Collector_DB_Queries" not found in /var/www/example.com/htdocs/wp-content/plugins/query-monitor-save-post-queries/php/Collector_Save_Post_Queries.php:14
Stack trace:
#0 /var/www/example.com/htdocs/wp-content/plugins/query-monitor-save-post-queries/query-monitor-save-post-queries.php(71): require_once()
#1 /var/www/example.com/htdocs/wp-includes/class-wp-hook.php(324): qmspq_plugin_setup()
#2 /var/www/example.com/htdocs/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
#3 /var/www/example.com/htdocs/wp-includes/plugin.php(517): WP_Hook->do_action()
#4 /var/www/example.com/htdocs/wp-settings.php(678): do_action()
#5 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1374): require('...')
#6 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1293): WP_CLI\Runner->load_wordpress()
#7 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/LaunchRunner.php(28): WP_CLI\Runner->start()
#8 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/bootstrap.php(83): WP_CLI\Bootstrap\LaunchRunner->process()
#9 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/wp-cli.php(32): WP_CLI\bootstrap()
#10 phar:///usr/local/bin/wp/php/boot-phar.php(20): include('...')
#11 /usr/local/bin/wp(4): include('...')
#12 {main}
  thrown in /var/www/example.com/htdocs/wp-content/plugins/query-monitor-save-post-queries/php/Collector_Save_Post_Queries.php on line 14
Error: There has been a critical error on this website.Learn more about troubleshooting WordPress. There has been a critical error on this website.

Steps To Reproduce

Install plugin, run any WP-CLI command from the command line.

Additional Information

If you look in Query Monitor's main bootstrap file, it includes a block of code that ensures it doesn't run via WP-CLI:

if ( 'cli' === php_sapi_name() && ! defined( 'QM_TESTS' ) ) {
    # For the time being, let's not load QM when using the CLI because we've no persistent storage and no means of
    # outputting collected data on the CLI. This will hopefully change in a future version of QM.
    return;
}

Adding this code to query-monitor-save-post-queries.php at line 67 did solve this issue for me.

While on the subject, it might also be wise to add some of Query Monitor's other conditionals for not running in that file as well. I do see you already have a check for QM_DISABLED on line 63. Or maybe what we really here is to use different hooks, such as using the qm/collectors to register the collector (here's an example usage in another Query Monitor extension plugin) instead of using the after_setup_theme hook (line 97).

jakewrfoster commented 2 weeks ago

Sorry I didn't see this sooner @columbian-chris! I will take a look at this as soon as I can!

columbian-chris commented 2 weeks ago

No problem!