← All changes
|
modules/variables/services/variables-service.php
+87
-9
4.1.0-dev2
→
4.3.0-beta3
View file →
| @@ -23,8 +23,41 @@ | ||
| 23 | 23 | public function get_variables_list(): array { |
| 24 | 24 | return $this->load()['data']; |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | + public function find_by_label_or_id( string $needle ): ?array { | |
| 28 | + $needle = trim( $needle ); | |
| 29 | + $needle = ltrim( $needle, '-' ); | |
| 30 | + | |
| 31 | + if ( '' === $needle ) { | |
| 32 | + return null; | |
| 33 | + } | |
| 34 | + | |
| 35 | + $variables = $this->get_variables_list(); | |
| 36 | + | |
| 37 | + if ( isset( $variables[ $needle ] ) ) { | |
| 38 | + $variable = $variables[ $needle ]; | |
| 39 | + | |
| 40 | + if ( ! empty( $variable['deleted'] ) ) { | |
| 41 | + return null; | |
| 42 | + } | |
| 43 | + | |
| 44 | + return array_merge( [ 'id' => $needle ], $variable ); | |
| 45 | + } | |
| 46 | + | |
| 47 | + foreach ( $variables as $id => $variable ) { | |
| 48 | + if ( ! empty( $variable['deleted'] ) ) { | |
| 49 | + continue; | |
| 50 | + } | |
| 51 | + | |
| 52 | + if ( strcasecmp( $variable['label'] ?? '', $needle ) === 0 ) { | |
| 53 | + return array_merge( [ 'id' => $id ], $variable ); | |
| 54 | + } | |
| 55 | + } | |
| 56 | + | |
| 57 | + return null; | |
| 58 | + } | |
| 59 | + | |
| 27 | 60 | public function load() { |
| 28 | 61 | $collection = $this->repo->load()->serialize( true ); |
| 29 | 62 | foreach ( $collection['data'] as $id => $variable ) { |
| 30 | 63 | if ( ! ElementorUtils::has_pro() && Size_Variable_Prop_Type::get_key() === $variable['type'] ) { |
| @@ -34,36 +67,62 @@ | ||
| 34 | 67 | return $collection; |
| 35 | 68 | } |
| 36 | 69 | |
| 37 | 70 | /** |
| 38 | - * @throws BatchOperationFailed Thrown when one of the operations fails. | |
| 71 | + * @throws BatchOperationFailed Thrown when one of the operations fails and $lenient is false. | |
| 39 | 72 | * @throws FatalError Failed to save after batch. |
| 40 | 73 | */ |
| 41 | - public function process_batch( array $operations ) { | |
| 74 | + public function process_batch( array $operations, bool $lenient = false ) { | |
| 42 | 75 | $collection = $this->repo->load(); |
| 43 | 76 | $results = []; |
| 44 | 77 | $errors = []; |
| 78 | + $has_success = false; | |
| 45 | 79 | |
| 46 | 80 | $error_formatter = new Batch_Error_Formatter(); |
| 47 | 81 | |
| 48 | 82 | foreach ( $operations as $index => $operation ) { |
| 49 | 83 | try { |
| 50 | - $results[] = $this->batch_processor->apply_operation( $collection, $operation ); | |
| 84 | + $batch_result = $this->batch_processor->apply_operation( $collection, $operation ); | |
| 85 | + $has_success = true; | |
| 51 | 86 | |
| 87 | + if ( $lenient ) { | |
| 88 | + $results[] = $this->format_lenient_success_result( $index, $batch_result ); | |
| 89 | + } else { | |
| 90 | + $results[] = $batch_result; | |
| 91 | + } | |
| 52 | 92 | } catch ( \Exception $e ) { |
| 53 | - $errors[ $this->batch_processor->operation_id( $operation, $index ) ] = [ | |
| 54 | - 'status' => $error_formatter->status_for( $e ), | |
| 55 | - 'code' => $error_formatter->error_code_for( $e ), | |
| 56 | - 'message' => $e->getMessage(), | |
| 57 | - ]; | |
| 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 | + } | |
| 58 | 109 | } |
| 59 | 110 | } |
| 60 | 111 | |
| 61 | - if ( ! empty( $errors ) ) { | |
| 112 | + if ( ! $lenient && ! empty( $errors ) ) { | |
| 62 | 113 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 63 | 114 | throw new BatchOperationFailed( 'Batch failed', $errors ); |
| 64 | 115 | } |
| 65 | 116 | |
| 117 | + if ( ! $has_success ) { | |
| 118 | + return [ | |
| 119 | + 'success' => false, | |
| 120 | + 'results' => $results, | |
| 121 | + 'watermark' => $collection->watermark(), | |
| 122 | + ]; | |
| 123 | + } | |
| 124 | + | |
| 66 | 125 | $watermark = $this->repo->save( $collection ); |
| 67 | 126 | |
| 68 | 127 | if ( false === $watermark ) { |
| 69 | 128 | throw new FatalError( 'Failed to save batch operations' ); |
| @@ -73,8 +132,27 @@ | ||
| 73 | 132 | 'success' => true, |
| 74 | 133 | 'results' => $results, |
| 75 | 134 | 'watermark' => $watermark, |
| 76 | 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; | |
| 77 | 155 | } |
| 78 | 156 | |
| 79 | 157 | /** |
| 80 | 158 | * @throws FatalError If variable create fails or validation errors occur. |