From d53744187fb009a83baa565010b0c62121813542 Mon Sep 17 00:00:00 2001 From: "Jorge A. Torres" Date: Mon, 22 Jul 2024 18:34:41 +0100 Subject: [PATCH] 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 --- ...fix-prevent-type-erorr-in-batch-processing | 4 +++ .../BatchProcessingController.php | 32 ++++++++++++------- 2 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-prevent-type-erorr-in-batch-processing diff --git a/plugins/woocommerce/changelog/fix-prevent-type-erorr-in-batch-processing b/plugins/woocommerce/changelog/fix-prevent-type-erorr-in-batch-processing new file mode 100644 index 00000000000..4bca714e8c9 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-prevent-type-erorr-in-batch-processing @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Prevent possible type error in BatchProcessingController. diff --git a/plugins/woocommerce/src/Internal/BatchProcessing/BatchProcessingController.php b/plugins/woocommerce/src/Internal/BatchProcessing/BatchProcessingController.php index bffe26bc7aa..d0a502d4844 100644 --- a/plugins/woocommerce/src/Internal/BatchProcessing/BatchProcessingController.php +++ b/plugins/woocommerce/src/Internal/BatchProcessing/BatchProcessingController.php @@ -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; } /**