← All changes
|
modules/variables/services/variables-service.php
+54
-9
4.2.4
→
4.3.0-beta3
View file →
| @@ -67,36 +67,62 @@ | ||
| 67 | 67 | return $collection; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | - * @throws BatchOperationFailed Thrown when one of the operations fails. | |
| 71 | + * @throws BatchOperationFailed Thrown when one of the operations fails and $lenient is false. | |
| 72 | 72 | * @throws FatalError Failed to save after batch. |
| 73 | 73 | */ |
| 74 | - public function process_batch( array $operations ) { | |
| 74 | + public function process_batch( array $operations, bool $lenient = false ) { | |
| 75 | 75 | $collection = $this->repo->load(); |
| 76 | 76 | $results = []; |
| 77 | 77 | $errors = []; |
| 78 | + $has_success = false; | |
| 78 | 79 | |
| 79 | 80 | $error_formatter = new Batch_Error_Formatter(); |
| 80 | 81 | |
| 81 | 82 | foreach ( $operations as $index => $operation ) { |
| 82 | 83 | try { |
| 83 | - $results[] = $this->batch_processor->apply_operation( $collection, $operation ); | |
| 84 | + $batch_result = $this->batch_processor->apply_operation( $collection, $operation ); | |
| 85 | + $has_success = true; | |
| 84 | 86 | |
| 87 | + if ( $lenient ) { | |
| 88 | + $results[] = $this->format_lenient_success_result( $index, $batch_result ); | |
| 89 | + } else { | |
| 90 | + $results[] = $batch_result; | |
| 91 | + } | |
| 85 | 92 | } catch ( \Exception $e ) { |
| 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 | - ]; | |
| 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 | + } | |
| 91 | 109 | } |
| 92 | 110 | } |
| 93 | 111 | |
| 94 | - if ( ! empty( $errors ) ) { | |
| 112 | + if ( ! $lenient && ! empty( $errors ) ) { | |
| 95 | 113 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 96 | 114 | throw new BatchOperationFailed( 'Batch failed', $errors ); |
| 97 | 115 | } |
| 98 | 116 | |
| 117 | + if ( ! $has_success ) { | |
| 118 | + return [ | |
| 119 | + 'success' => false, | |
| 120 | + 'results' => $results, | |
| 121 | + 'watermark' => $collection->watermark(), | |
| 122 | + ]; | |
| 123 | + } | |
| 124 | + | |
| 99 | 125 | $watermark = $this->repo->save( $collection ); |
| 100 | 126 | |
| 101 | 127 | if ( false === $watermark ) { |
| 102 | 128 | throw new FatalError( 'Failed to save batch operations' ); |
| @@ -106,8 +132,27 @@ | ||
| 106 | 132 | 'success' => true, |
| 107 | 133 | 'results' => $results, |
| 108 | 134 | 'watermark' => $watermark, |
| 109 | 135 | ]; |
| 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; | |
| 110 | 155 | } |
| 111 | 156 | |
| 112 | 157 | /** |
| 113 | 158 | * @throws FatalError If variable create fails or validation errors occur. |