| @@ -67,62 +67,36 @@ | ||
| 67 | 67 | return $collection; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | - * @throws BatchOperationFailed Thrown when one of the operations fails and $lenient is false. | |
| 71 | + * @throws BatchOperationFailed Thrown when one of the operations fails. | |
| 72 | 72 | * @throws FatalError Failed to save after batch. |
| 73 | 73 | */ |
| 74 | - public function process_batch( array $operations, bool $lenient = false ) { | |
| 74 | + public function process_batch( array $operations ) { | |
| 75 | 75 | $collection = $this->repo->load(); |
| 76 | 76 | $results = []; |
| 77 | 77 | $errors = []; |
| 78 | - $has_success = false; | |
| 79 | 78 | |
| 80 | 79 | $error_formatter = new Batch_Error_Formatter(); |
| 81 | 80 | |
| 82 | 81 | foreach ( $operations as $index => $operation ) { |
| 83 | 82 | try { |
| 84 | - $batch_result = $this->batch_processor->apply_operation( $collection, $operation ); | |
| 85 | - $has_success = true; | |
| 83 | + $results[] = $this->batch_processor->apply_operation( $collection, $operation ); | |
| 86 | 84 | |
| 87 | - if ( $lenient ) { | |
| 88 | - $results[] = $this->format_lenient_success_result( $index, $batch_result ); | |
| 89 | - } else { | |
| 90 | - $results[] = $batch_result; | |
| 91 | - } | |
| 92 | 85 | } catch ( \Exception $e ) { |
| 93 | - if ( $lenient ) { | |
| 94 | - $results[] = [ | |
| 95 | - 'index' => $index, | |
| 96 | - 'status' => 'error', | |
| 97 | - 'action' => $operation['type'] ?? '', | |
| 98 | - 'id' => $operation['id'] ?? ( $operation['variable']['id'] ?? null ), | |
| 99 | - 'code' => $error_formatter->error_code_for( $e ), | |
| 100 | - 'message' => $e->getMessage(), | |
| 101 | - ]; | |
| 102 | - } else { | |
| 103 | - $errors[ $this->batch_processor->operation_id( $operation, $index ) ] = [ | |
| 104 | - 'status' => $error_formatter->status_for( $e ), | |
| 105 | - 'code' => $error_formatter->error_code_for( $e ), | |
| 106 | - 'message' => $e->getMessage(), | |
| 107 | - ]; | |
| 108 | - } | |
| 86 | + $errors[ $this->batch_processor->operation_id( $operation, $index ) ] = [ | |
| 87 | + 'status' => $error_formatter->status_for( $e ), | |
| 88 | + 'code' => $error_formatter->error_code_for( $e ), | |
| 89 | + 'message' => $e->getMessage(), | |
| 90 | + ]; | |
| 109 | 91 | } |
| 110 | 92 | } |
| 111 | 93 | |
| 112 | - if ( ! $lenient && ! empty( $errors ) ) { | |
| 94 | + if ( ! empty( $errors ) ) { | |
| 113 | 95 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 114 | 96 | throw new BatchOperationFailed( 'Batch failed', $errors ); |
| 115 | 97 | } |
| 116 | 98 | |
| 117 | - if ( ! $has_success ) { | |
| 118 | - return [ | |
| 119 | - 'success' => false, | |
| 120 | - 'results' => $results, | |
| 121 | - 'watermark' => $collection->watermark(), | |
| 122 | - ]; | |
| 123 | - } | |
| 124 | - | |
| 125 | 99 | $watermark = $this->repo->save( $collection ); |
| 126 | 100 | |
| 127 | 101 | if ( false === $watermark ) { |
| 128 | 102 | throw new FatalError( 'Failed to save batch operations' ); |
| @@ -132,27 +106,8 @@ | ||
| 132 | 106 | 'success' => true, |
| 133 | 107 | 'results' => $results, |
| 134 | 108 | 'watermark' => $watermark, |
| 135 | 109 | ]; |
| 136 | - } | |
| 137 | - | |
| 138 | - private function format_lenient_success_result( int $index, array $batch_result ): array { | |
| 139 | - $result = [ | |
| 140 | - 'index' => $index, | |
| 141 | - 'status' => 'ok', | |
| 142 | - 'action' => $batch_result['type'] ?? '', | |
| 143 | - 'id' => $batch_result['id'] ?? null, | |
| 144 | - ]; | |
| 145 | - | |
| 146 | - if ( isset( $batch_result['variable']['label'] ) ) { | |
| 147 | - $result['label'] = $batch_result['variable']['label']; | |
| 148 | - } | |
| 149 | - | |
| 150 | - if ( isset( $batch_result['variable']['type'] ) ) { | |
| 151 | - $result['type'] = $batch_result['variable']['type']; | |
| 152 | - } | |
| 153 | - | |
| 154 | - return $result; | |
| 155 | 110 | } |
| 156 | 111 | |
| 157 | 112 | /** |
| 158 | 113 | * @throws FatalError If variable create fails or validation errors occur. |