PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.4
Elementor Website Builder – more than just a page builder v4.1.4
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / app / modules / import-export-customization / utils.php

utils.php in Elementor Website Builder – more than just a page builder 4.1.4, at app/modules/import-export-customization/utils.php

176 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\App\Modules\ImportExportCustomization;
4
5 use Elementor\Core\Utils\Str;
6 use Elementor\Modules\LandingPages\Module as Landing_Pages_Module;
7 use Elementor\Modules\FloatingButtons\Module as Floating_Buttons_Module;
8 use Elementor\TemplateLibrary\Source_Local;
9 use Elementor\Utils as ElementorUtils;
10
11 class Utils {
12
13 public static function read_json_file( $path ) {
14 if ( ! Str::ends_with( $path, '.json' ) ) {
15 $path .= '.json';
16 }
17
18 $file_content = ElementorUtils::file_get_contents( $path, true );
19
20 return $file_content ? json_decode( $file_content, true ) : [];
21 }
22
23 public static function map_old_new_post_ids( array $imported_data ) {
24 $result = [];
25
26 $result += $imported_data['templates']['succeed'] ?? [];
27
28 if ( isset( $imported_data['content'] ) ) {
29 foreach ( $imported_data['content'] as $post_type ) {
30 $result += $post_type['succeed'] ?? [];
31 }
32 }
33
34 if ( isset( $imported_data['wp-content'] ) ) {
35 foreach ( $imported_data['wp-content'] as $post_type ) {
36 $result += $post_type['succeed'] ?? [];
37 }
38 }
39
40 return $result;
41 }
42
43 public static function map_old_new_term_ids( array $imported_data ) {
44 $result = [];
45
46 if ( ! isset( $imported_data['taxonomies'] ) ) {
47 return $result;
48 }
49
50 foreach ( $imported_data['taxonomies'] as $post_type_taxonomies ) {
51 foreach ( $post_type_taxonomies as $taxonomy ) {
52 foreach ( $taxonomy as $term ) {
53 $result[ $term['old_id'] ] = $term['new_id'];
54 }
55 }
56 }
57
58 return $result;
59 }
60
61 public static function get_elementor_post_types( $exclude = [] ) {
62 $elementor_post_types = get_post_types_by_support( 'elementor' );
63
64 return array_filter( $elementor_post_types, function ( $value ) {
65 // Templates are handled in a separate process.
66 if ( 'elementor_library' === $value ) {
67 return false;
68 }
69
70 if ( ! empty( $exclude ) && in_array( $value, $exclude, true ) ) {
71 return false;
72 }
73
74 return 'elementor_library' !== $value;
75 } );
76 }
77
78 public static function get_builtin_wp_post_types( $exclude = [] ) {
79 $builtin_wp_post_types = [ 'post', 'page', 'nav_menu_item' ];
80
81 if ( ! empty( $exclude ) ) {
82 return array_diff( $builtin_wp_post_types, $exclude );
83 }
84
85 return $builtin_wp_post_types;
86 }
87
88 public static function get_registered_cpt_names() {
89 $post_types = get_post_types( [
90 'public' => true,
91 'can_export' => true,
92 '_builtin' => false,
93 ] );
94
95 unset(
96 $post_types[ Landing_Pages_Module::CPT ],
97 $post_types[ Source_Local::CPT ],
98 $post_types[ Floating_Buttons_Module::CPT_FLOATING_BUTTONS ]
99 );
100
101 return array_keys( $post_types );
102 }
103
104 /**
105 * Transform a string name to title format.
106 *
107 * @param $name
108 *
109 * @return string
110 */
111 public static function transform_name_to_title( $name ): string {
112 if ( empty( $name ) ) {
113 return '';
114 }
115
116 $title = str_replace( [ '-', '_' ], ' ', $name );
117
118 return ucwords( $title );
119 }
120
121 public static function get_import_sessions( $should_run_cleanup = false ) {
122 $import_sessions = get_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS, [] );
123
124 if ( $should_run_cleanup ) {
125 foreach ( $import_sessions as $session_id => $import_session ) {
126 if ( ! isset( $import_session['runners'] ) && isset( $import_session['instance_data'] ) ) {
127 $import_sessions[ $session_id ]['runners'] = $import_session['instance_data']['runners_import_metadata'] ?? [];
128
129 unset( $import_sessions[ $session_id ]['instance_data'] );
130 }
131 }
132
133 update_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS, $import_sessions );
134 }
135
136 return $import_sessions;
137 }
138
139 public static function update_space_between_widgets_values( $space_between_widgets ) {
140 $setting_exist = isset( $space_between_widgets['size'] );
141 $already_processed = isset( $space_between_widgets['column'] );
142
143 if ( ! $setting_exist || $already_processed ) {
144 return $space_between_widgets;
145 }
146
147 $size = strval( $space_between_widgets['size'] );
148 $space_between_widgets['column'] = $size;
149 $space_between_widgets['row'] = $size;
150 $space_between_widgets['isLinked'] = true;
151
152 return $space_between_widgets;
153 }
154
155 public static function resolve_label_conflict( string $label, array $existing_labels, int $max_length = 50 ): string {
156 $lower_label = strtolower( $label );
157
158 if ( ! in_array( $lower_label, $existing_labels, true ) ) {
159 return $label;
160 }
161
162 $suffix = 1;
163 $max_suffix_attempts = 1000;
164
165 do {
166 $suffix_str = '_' . $suffix;
167 $max_base_length = $max_length - strlen( $suffix_str );
168 $base_label = mb_substr( $label, 0, $max_base_length );
169 $new_label = $base_label . $suffix_str;
170 ++$suffix;
171 } while ( in_array( strtolower( $new_label ), $existing_labels, true ) && $suffix < $max_suffix_attempts );
172
173 return $new_label;
174 }
175 }
176