Prevent type error in `BatchProcessingController` (#49728)

This PR introduces some type checking around the value of options involved in batch processing, particularly the one controlling which processors are enqueued.

* Add changelog

* Make BatchProcessingController's handling of internal options more robust

* Remove queue if invalid

* Implement suggestion
This commit is contained in:
Jorge A. Torres 2024-07-22 18:34:41 +01:00 committed by GitHub
parent 8c82d8dff3
commit d53744187f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 11 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Prevent possible type error in BatchProcessingController.

View File

@ -220,17 +220,19 @@ class BatchProcessingController {
* @return array Current state for the processor, or a "blank" state if none exists yet.
*/
private function get_process_details( BatchProcessorInterface $batch_processor ): array {
return get_option(
$this->get_processor_state_option_name( $batch_processor ),
array(
'total_time_spent' => 0,
'current_batch_size' => $batch_processor->get_default_batch_size(),
'last_error' => null,
'recent_failures' => 0,
'batch_first_failure' => null,
'batch_last_failure' => null,
)
$defaults = array(
'total_time_spent' => 0,
'current_batch_size' => $batch_processor->get_default_batch_size(),
'last_error' => null,
'recent_failures' => 0,
'batch_first_failure' => null,
'batch_last_failure' => null,
);
$process_details = get_option( $this->get_processor_state_option_name( $batch_processor ) );
$process_details = wp_parse_args( is_array( $process_details ) ? $process_details : array(), $defaults );
return $process_details;
}
/**
@ -349,7 +351,15 @@ class BatchProcessingController {
* @return array List (of string) of the class names of the enqueued processors.
*/
public function get_enqueued_processors(): array {
return get_option( self::ENQUEUED_PROCESSORS_OPTION_NAME, array() );
$enqueued_processors = get_option( self::ENQUEUED_PROCESSORS_OPTION_NAME, array() );
if ( ! is_array( $enqueued_processors ) ) {
$this->logger->error( 'Could not fetch list of processors. Clearing up queue.', array( 'source' => 'batch-processing' ) );
delete_option( self::ENQUEUED_PROCESSORS_OPTION_NAME );
$enqueued_processors = array();
}
return $enqueued_processors;
}
/**