PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.1.4
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.1.4
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 3.1.10 All 111 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.4, at includes/Core/Importer/Utils/Utils.php

218 lines 5.8 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 get_backup_options() {
106 global $wpdb;
107
108 $prefix = '__templately_';
109 $table_name = $wpdb->options; // Assuming default options table name
110
111 $sql = "SELECT option_name, option_value FROM {$table_name} WHERE option_name LIKE %s";
112 $prepared_sql = $wpdb->prepare($sql, array("$prefix%")); // Escape wildcard for security
113
114 $results = $wpdb->get_results($prepared_sql);
115
116 $templately_options = array();
117 foreach ($results as $row) {
118 $name = str_replace($prefix, '', $row->option_name);
119 $templately_options[$name] = maybe_unserialize($row->option_value);
120 }
121
122 return $templately_options;
123 }
124
125 public static function update_option( $key, $value, $autoload = 'no' ){
126 $old_value = get_option($key);
127 if($old_value){
128 update_option( "__templately_$key", $old_value, $autoload );
129 }
130 else{
131 add_option( "__templately_$key", $old_value, '', $autoload );
132 }
133
134 return update_option( $key, $value, $autoload );
135 }
136
137 public static function import_page_settings( $id, $settings ) {
138 $extra_settings = [
139 'page_on_front' => [
140 'show_on_front' => 'page'
141 ]
142 ];
143 if ( isset( $settings['page_for_posts'] ) && $settings['page_for_posts'] ) {
144 self::update_option( 'page_for_posts', $id );
145 }
146 if ( isset( $settings['show_on_front'] ) && $settings['show_on_front'] ) {
147 self::update_option( 'page_on_front', $id );
148 self::update_option( 'show_on_front', 'page' );
149 }
150 if ( ! empty( $settings['page_settings'] ) ) {
151 foreach ( $settings['page_settings'] as $option_name => $val ) {
152 self::update_option( $option_name, $id );
153 if ( array_key_exists( $option_name, $extra_settings ) ) {
154 foreach ( $extra_settings[ $option_name ] as $name => $value ) {
155 self::update_option( $name, $value );
156 }
157 }
158 }
159 }
160 }
161
162 public static function upload_logo($url) {
163 if(empty($url)) {
164 return ['error' => __('URL is empty', 'templately')];
165 }
166
167 // Validate URL and ensure scheme is present
168 if ( ! wp_http_validate_url( $url ) || !parse_url( $url, PHP_URL_SCHEME ) ) {
169 return ['error' => __('Invalid URL', 'templately')];
170 }
171
172 // Download image and get MIME type
173 $temp_file = download_url( $url );
174 if ( is_wp_error( $temp_file ) ) {
175 return ['error' => __('Failed to download image', 'templately')];
176 }
177
178 // Validate image type using wp_get_image_mime
179 $mime_type = wp_get_image_mime( $temp_file );
180 if ( ! $mime_type || !in_array( $mime_type, get_allowed_mime_types() ) ) {
181 unlink( $temp_file );
182 return ['error' => __('Invalid image type', 'templately')];
183 }
184
185 $file_info = wp_check_filetype_and_ext( $temp_file, basename( $url ), get_allowed_mime_types() );
186 if ( ! $file_info['ext'] || $file_info['type'] !== $mime_type ) {
187 unlink( $temp_file );
188 return ['error' => __('File type and extension check failed', 'templately')];
189 }
190
191 // Prepare the file for sideloading
192 $file = array(
193 'name' => basename($url),
194 'type' => $mime_type,
195 'tmp_name' => $temp_file,
196 'error' => 0,
197 'size' => filesize($temp_file),
198 );
199
200 // Load the WordPress media handler
201 require_once(ABSPATH . 'wp-admin/includes/media.php');
202 require_once(ABSPATH . 'wp-admin/includes/file.php');
203 require_once(ABSPATH . 'wp-admin/includes/image.php');
204
205 // Handle the sideload
206 $id = media_handle_sideload($file, 0);
207
208 if (is_wp_error($id)) {
209 @unlink($file['tmp_name']);
210 return ['error' => __('Failed to sideload image', 'templately')];
211 }
212
213 return [
214 'id' => $id,
215 'url' => esc_url_raw(wp_get_attachment_url($id)),
216 ];
217 }
218 }