← All changes
|
modules/variables/services/variables-service.php
+11
-96
4.3.0-beta3
→
3.35.0-dev4
View file →
| @@ -23,41 +23,8 @@ | ||
| 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 | - | |
| 60 | 27 | public function load() { |
| 61 | 28 | $collection = $this->repo->load()->serialize( true ); |
| 62 | 29 | foreach ( $collection['data'] as $id => $variable ) { |
| 63 | 30 | if ( ! ElementorUtils::has_pro() && Size_Variable_Prop_Type::get_key() === $variable['type'] ) { |
| @@ -67,62 +34,36 @@ | ||
| 67 | 34 | return $collection; |
| 68 | 35 | } |
| 69 | 36 | |
| 70 | 37 | /** |
| 71 | - * @throws BatchOperationFailed Thrown when one of the operations fails and $lenient is false. | |
| 38 | + * @throws BatchOperationFailed Thrown when one of the operations fails. | |
| 72 | 39 | * @throws FatalError Failed to save after batch. |
| 73 | 40 | */ |
| 74 | - public function process_batch( array $operations, bool $lenient = false ) { | |
| 41 | + public function process_batch( array $operations ) { | |
| 75 | 42 | $collection = $this->repo->load(); |
| 76 | 43 | $results = []; |
| 77 | 44 | $errors = []; |
| 78 | - $has_success = false; | |
| 79 | 45 | |
| 80 | 46 | $error_formatter = new Batch_Error_Formatter(); |
| 81 | 47 | |
| 82 | 48 | foreach ( $operations as $index => $operation ) { |
| 83 | 49 | try { |
| 84 | - $batch_result = $this->batch_processor->apply_operation( $collection, $operation ); | |
| 85 | - $has_success = true; | |
| 50 | + $results[] = $this->batch_processor->apply_operation( $collection, $operation ); | |
| 86 | 51 | |
| 87 | - if ( $lenient ) { | |
| 88 | - $results[] = $this->format_lenient_success_result( $index, $batch_result ); | |
| 89 | - } else { | |
| 90 | - $results[] = $batch_result; | |
| 91 | - } | |
| 92 | 52 | } 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 | - } | |
| 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 | + ]; | |
| 109 | 58 | } |
| 110 | 59 | } |
| 111 | 60 | |
| 112 | - if ( ! $lenient && ! empty( $errors ) ) { | |
| 61 | + if ( ! empty( $errors ) ) { | |
| 113 | 62 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 114 | 63 | throw new BatchOperationFailed( 'Batch failed', $errors ); |
| 115 | 64 | } |
| 116 | 65 | |
| 117 | - if ( ! $has_success ) { | |
| 118 | - return [ | |
| 119 | - 'success' => false, | |
| 120 | - 'results' => $results, | |
| 121 | - 'watermark' => $collection->watermark(), | |
| 122 | - ]; | |
| 123 | - } | |
| 124 | - | |
| 125 | 66 | $watermark = $this->repo->save( $collection ); |
| 126 | 67 | |
| 127 | 68 | if ( false === $watermark ) { |
| 128 | 69 | throw new FatalError( 'Failed to save batch operations' ); |
| @@ -134,27 +75,8 @@ | ||
| 134 | 75 | 'watermark' => $watermark, |
| 135 | 76 | ]; |
| 136 | 77 | } |
| 137 | 78 | |
| 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 | - } | |
| 156 | - | |
| 157 | 79 | /** |
| 158 | 80 | * @throws FatalError If variable create fails or validation errors occur. |
| 159 | 81 | */ |
| 160 | 82 | public function create( array $data ): array { |
| @@ -171,9 +93,8 @@ | ||
| 171 | 93 | $data['order'] = $collection->get_next_order(); |
| 172 | 94 | } |
| 173 | 95 | |
| 174 | 96 | $variable = Variable::from_array( $data ); |
| 175 | - $variable->validate(); | |
| 176 | 97 | |
| 177 | 98 | $collection->add_variable( $variable ); |
| 178 | 99 | |
| 179 | 100 | $watermark = $this->repo->save( $collection ); |
| @@ -199,9 +120,8 @@ | ||
| 199 | 120 | $collection->assert_label_is_unique( $data['label'], $id ); |
| 200 | 121 | } |
| 201 | 122 | |
| 202 | 123 | $variable->apply_changes( $data ); |
| 203 | - $variable->validate(); | |
| 204 | 124 | |
| 205 | 125 | $watermark = $this->repo->save( $collection ); |
| 206 | 126 | |
| 207 | 127 | if ( false === $watermark ) { |
| @@ -244,20 +164,15 @@ | ||
| 244 | 164 | public function restore( string $id, $overrides = [] ) { |
| 245 | 165 | $collection = $this->repo->load(); |
| 246 | 166 | $variable = $collection->find_or_fail( $id ); |
| 247 | 167 | |
| 248 | - $label = $variable->label(); | |
| 168 | + $collection->assert_limit_not_reached(); | |
| 249 | 169 | |
| 250 | 170 | if ( isset( $overrides['label'] ) ) { |
| 251 | - $label = $overrides['label']; | |
| 171 | + $collection->assert_label_is_unique( $overrides['label'], $variable->id() ); | |
| 252 | 172 | } |
| 253 | 173 | |
| 254 | - $collection->assert_limit_not_reached(); | |
| 255 | - | |
| 256 | - $collection->assert_label_is_unique( $label, $variable->id() ); | |
| 257 | - | |
| 258 | 174 | $variable->apply_changes( $overrides ); |
| 259 | - $variable->validate(); | |
| 260 | 175 | |
| 261 | 176 | $variable->restore(); |
| 262 | 177 | |
| 263 | 178 | $watermark = $this->repo->save( $collection ); |