PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.1.2
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.1.2
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 All 112 releases
templately / includes / Core / Importer / Utils / Utils.php

Utils.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.1.2, at includes/Core/Importer/Utils/Utils.php

195 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Core\Importer\Utils;
4
5 use Exception;
6 use Templately\Utils\Base;
7
8 class Utils extends Base {
9 /**
10 * @throws Exception
11 */
12 public static function read_json_file( $path ) {
13 if ( ! file_exists( $path ) ) {
14 throw new Exception( __( 'JSON file not exists. ' . basename( $path ), 'templately' ) );
15 }
16
17 $file_content = self::file_get_contents( $path );
18
19 return $file_content ? json_decode( $file_content, true ) : [];
20 }
21
22 /**
23 * @param $file
24 * @param mixed ...$args
25 *
26 * @return false|string
27 */
28 public static function file_get_contents( $file, ...$args ) {
29 if ( ! is_file( $file ) || ! is_readable( $file ) ) {
30 return false;
31 }
32
33 return file_get_contents( $file, ...$args );
34 }
35
36 public static function get_builtin_wp_post_types(): array {
37 $post_type_args = [
38 'show_in_nav_menus' => true,
39 'public' => true
40 ];
41 $_post_types = get_post_types( $post_type_args, 'objects' );
42
43 return array_merge( array_keys( $_post_types ), [ 'nav_menu_item', 'wp_navigation' ] );
44 }
45
46 public static function map_old_new_post_ids( array $imported_data ) {
47 $result = [];
48
49 $result += $imported_data['templates']['succeed'] ?? [];
50
51 if ( isset( $imported_data['content'] ) ) {
52 foreach ( $imported_data['content'] as $post_type ) {
53 $result += $post_type['succeed'] ?? [];
54 }
55 }
56
57 if ( isset( $imported_data['wp-content'] ) ) {
58 foreach ( $imported_data['wp-content'] as $post_type ) {
59 $result += $post_type['succeed'] ?? [];
60 }
61 }
62
63 return $result;
64 }
65
66 public static function map_old_new_term_ids( array $imported_data ) {
67 $result = [];
68
69 if ( isset( $imported_data['terms'] ) ) {
70 foreach ( $imported_data['terms'] as $post_type ) {
71 $result += $post_type['succeed'] ?? [];
72 }
73 }
74
75 return $result;
76 }
77
78 public static function map_old_new_term_ids_el( array $imported_data ): array {
79 $result = [];
80
81 if ( ! isset( $imported_data['taxonomies'] ) ) {
82 return $result;
83 }
84
85 foreach ( $imported_data['taxonomies'] as $post_type_taxonomies ) {
86 foreach ( $post_type_taxonomies as $taxonomy ) {
87 foreach ( $taxonomy as $term ) {
88 $result[ $term['old_id'] ] = $term['new_id'];
89 }
90 }
91 }
92
93 return $result;
94 }
95
96 /**
97 * @param string $platform
98 *
99 * @return ImportHelper
100 */
101 public static function get_json_helper( string $platform ) {
102 return $platform === 'elementor' ? new ElementorHelper() : new GutenbergHelper();
103 }
104
105 public static function update_option( $key, $value, $autoload = 'no' ){
106 $bk_value = get_option("__templately_$key");
107 if($bk_value === false){
108 $old_value = get_option($key);
109 add_option( "__templately_$key", $old_value, '', $autoload );
110 }
111 return update_option( $key, $value, $autoload );
112 }
113
114 public static function import_page_settings( $id, $settings ) {
115 $extra_settings = [
116 'page_on_front' => [
117 'show_on_front' => 'page'
118 ]
119 ];
120 if ( isset( $settings['page_for_posts'] ) && $settings['page_for_posts'] ) {
121 self::update_option( 'page_for_posts', $id );
122 }
123 if ( isset( $settings['show_on_front'] ) && $settings['show_on_front'] ) {
124 self::update_option( 'page_on_front', $id );
125 self::update_option( 'show_on_front', 'page' );
126 }
127 if ( ! empty( $settings['page_settings'] ) ) {
128 foreach ( $settings['page_settings'] as $option_name => $val ) {
129 self::update_option( $option_name, $id );
130 if ( array_key_exists( $option_name, $extra_settings ) ) {
131 foreach ( $extra_settings[ $option_name ] as $name => $value ) {
132 self::update_option( $name, $value );
133 }
134 }
135 }
136 }
137 }
138
139 public static function upload_logo($url) {
140 if(empty($url)) {
141 return ['error' => __('URL is empty', 'templately')];
142 }
143
144 // Validate URL and ensure scheme is present
145 if ( ! wp_http_validate_url( $url ) || !parse_url( $url, PHP_URL_SCHEME ) ) {
146 return ['error' => __('Invalid URL', 'templately')];
147 }
148
149 // Download image and get MIME type
150 $temp_file = download_url( $url );
151 if ( is_wp_error( $temp_file ) ) {
152 return ['error' => __('Failed to download image', 'templately')];
153 }
154
155 // Validate image type using wp_get_image_mime
156 $mime_type = wp_get_image_mime( $temp_file );
157 if ( ! $mime_type || !in_array( $mime_type, get_allowed_mime_types() ) ) {
158 unlink( $temp_file );
159 return ['error' => __('Invalid image type', 'templately')];
160 }
161
162 $file_info = wp_check_filetype_and_ext( $temp_file, basename( $url ), get_allowed_mime_types() );
163 if ( ! $file_info['ext'] || $file_info['type'] !== $mime_type ) {
164 unlink( $temp_file );
165 return ['error' => __('File type and extension check failed', 'templately')];
166 }
167
168 // Prepare the file for sideloading
169 $file = array(
170 'name' => basename($url),
171 'type' => $mime_type,
172 'tmp_name' => $temp_file,
173 'error' => 0,
174 'size' => filesize($temp_file),
175 );
176
177 // Load the WordPress media handler
178 require_once(ABSPATH . 'wp-admin/includes/media.php');
179 require_once(ABSPATH . 'wp-admin/includes/file.php');
180 require_once(ABSPATH . 'wp-admin/includes/image.php');
181
182 // Handle the sideload
183 $id = media_handle_sideload($file, 0);
184
185 if (is_wp_error($id)) {
186 @unlink($file['tmp_name']);
187 return ['error' => __('Failed to sideload image', 'templately')];
188 }
189
190 return [
191 'id' => $id,
192 'url' => esc_url_raw(wp_get_attachment_url($id)),
193 ];
194 }
195 }