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