is_cron_disabled doesn’t make sense | WordPress.org

Hi there! I just noticed one of my sites getting really slow when performing some simple tasks (wp_delete_post for example).

Turns out it was very slow db queries to the sg-security logs table. Here is an example:

INSERT INTO wp_sgs_log_events (visitor_id, ts, activity, description, ip, hostname, code, object_id, type, action, visitor_type) VALUES (2, 1679387722, 'Deleted Post', 'Deleted Post - Benchmark for wp_delete_post #1', '127.0.0.1', 'localhost', 200, 'wpcli', 'post', 'delete', 'user')

That table was huge in my case. After digging through the code, I noticed this function:

/**
 * Check if wp cron is disabled and send error message.
 *
 * @since  1.0.0
 */
public static function is_cron_disabled() {
	if ( defined( 'DISABLE_WP_CRON' ) && true == DISABLE_WP_CRON ) {
		return 1;
	}

	return 0;
}

…and this one:

/**
 * Set the cron job for deleting old logs.
 *
 * @since  1.0.0
 */
public function set_sgs_logs_cron() {
	// Bail if cron is disabled.
	if ( 1 === Helper_Service::is_cron_disabled() ) {
		return;
	}

	if ( ! wp_next_scheduled( 'siteground_security_clear_logs_cron' ) ) {
		wp_schedule_event( time(), 'daily', 'siteground_security_clear_logs_cron' );
	}
}

The problem with this is, that the constant DISABLE_WP_CRON is only there to disable the WordPress fake cron. On my site I have a real cron job running. So set_sgs_logs_cron should actually always schedule the cleanup job, even if DISABLE_WP_CRON was set to false.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer