diff --git a/features/check-cron-duplicates.feature b/features/check-cron-duplicates.feature index 0449c2b..d63aa1e 100644 --- a/features/check-cron-duplicates.feature +++ b/features/check-cron-duplicates.feature @@ -26,6 +26,21 @@ Feature: Check for excess duplicate cron entries | cron-duplicates | success | All cron job counts are within normal operating expectations. | And STDERR should be empty + Scenario: Cron check is healthy when same hook has unique args (e.g. publish_future_post) + Given a wp-content/mu-plugins/plugin.php file: + """ + 'json' ) ); + WP_CLI::run_command( + array( 'cron', 'event', 'list' ), + array( + 'format' => 'json', + 'fields' => 'hook,args', + ) + ); $ret = ob_get_clean(); self::$crons = ! empty( $ret ) ? json_decode( $ret, true ) : array(); return self::$crons; diff --git a/src/Check/Cron_Duplicates.php b/src/Check/Cron_Duplicates.php index abb6b7d..b21eef5 100644 --- a/src/Check/Cron_Duplicates.php +++ b/src/Check/Cron_Duplicates.php @@ -19,11 +19,21 @@ public function run() { $job_counts = array(); $excess_duplicates = false; foreach ( $crons as $job ) { - if ( ! isset( $job_counts[ $job['hook'] ] ) ) { - $job_counts[ $job['hook'] ] = 0; + $key_data = array( $job['hook'], isset( $job['args'] ) ? $job['args'] : array() ); + if ( function_exists( 'wp_json_encode' ) ) { + $key = wp_json_encode( $key_data ); + } else { + $key = json_encode( $key_data ); } - ++$job_counts[ $job['hook'] ]; - if ( $job_counts[ $job['hook'] ] >= $this->threshold_count ) { + if ( false === $key ) { + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize + $key = serialize( $key_data ); + } + if ( ! isset( $job_counts[ $key ] ) ) { + $job_counts[ $key ] = 0; + } + ++$job_counts[ $key ]; + if ( $job_counts[ $key ] >= $this->threshold_count ) { $excess_duplicates = true; } }