← All changes
|
modules/variables/services/variables-service.php
+33
-0
4.1.0-beta1
→
4.2.4
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'] ) { |