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

261 lines 7.0 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 backup_option_value($key, $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
135 public static function update_option($key, $value, $autoload = 'no') {
136 self::backup_option_value($key, $autoload);
137 return update_option($key, $value, $autoload);
138 }
139
140 public static function import_page_settings( $id, $settings ) {
141 $extra_settings = [
142 'page_on_front' => [
143 'show_on_front' => 'page'
144 ]
145 ];
146 if ( isset( $settings['page_for_posts'] ) && $settings['page_for_posts'] ) {
147 self::update_option( 'page_for_posts', $id );
148 }
149 if ( isset( $settings['show_on_front'] ) && $settings['show_on_front'] ) {
150 self::update_option( 'page_on_front', $id );
151 self::update_option( 'show_on_front', 'page' );
152 }
153 if ( ! empty( $settings['page_settings'] ) ) {
154 foreach ( $settings['page_settings'] as $option_name => $val ) {
155 self::update_option( $option_name, $id );
156 if ( array_key_exists( $option_name, $extra_settings ) ) {
157 foreach ( $extra_settings[ $option_name ] as $name => $value ) {
158 self::update_option( $name, $value );
159 }
160 }
161 }
162 }
163 }
164
165 public static function upload_logo($url) {
166 if(empty($url)) {
167 return ['error' => __('URL is empty', 'templately')];
168 }
169
170 // Validate URL and ensure scheme is present
171 if ( ! wp_http_validate_url( $url ) || !parse_url( $url, PHP_URL_SCHEME ) ) {
172 return ['error' => __('Invalid URL', 'templately')];
173 }
174
175 // Download image and get MIME type
176 $temp_file = download_url( $url );
177 if ( is_wp_error( $temp_file ) ) {
178 return ['error' => __('Failed to download image', 'templately')];
179 }
180
181 // Validate image type using wp_get_image_mime
182 $mime_type = wp_get_image_mime( $temp_file );
183 if ( ! $mime_type || !in_array( $mime_type, get_allowed_mime_types() ) ) {
184 unlink( $temp_file );
185 return ['error' => __('Invalid image type', 'templately')];
186 }
187
188 $file_info = wp_check_filetype_and_ext( $temp_file, basename( $url ), get_allowed_mime_types() );
189 if ( ! $file_info['ext'] || $file_info['type'] !== $mime_type ) {
190 unlink( $temp_file );
191 return ['error' => __('File type and extension check failed', 'templately')];
192 }
193
194 // Prepare the file for sideloading
195 $file = array(
196 'name' => basename($url),
197 'type' => $mime_type,
198 'tmp_name' => $temp_file,
199 'error' => 0,
200 'size' => filesize($temp_file),
201 );
202
203 // Load the WordPress media handler
204 require_once(ABSPATH . 'wp-admin/includes/media.php');
205 require_once(ABSPATH . 'wp-admin/includes/file.php');
206 require_once(ABSPATH . 'wp-admin/includes/image.php');
207
208 // Handle the sideload
209 $id = media_handle_sideload($file, 0);
210
211 if (is_wp_error($id)) {
212 @unlink($file['tmp_name']);
213 return ['error' => __('Failed to sideload image', 'templately')];
214 }
215
216 return [
217 'id' => $id,
218 'url' => esc_url_raw(wp_get_attachment_url($id)),
219 ];
220 }
221
222 /**
223 * Inserts a template into the Gutenberg editor.
224 *
225 * @param mixed $data
226 * @param int $postId
227 * @return array
228 */
229 public static function import_and_replace_attachments($content, $postId = 0) {
230 // Instantiate GutenbergHelper
231 $helper = new GutenbergHelper();
232
233 $data = [
234 'content' => $content,
235 ];
236
237 // Organize URLs from the content
238 $organizedUrls = $helper->parse_images($data['content']);
239 if(empty($organizedUrls)){
240 return $content;
241 }
242
243 // Define template settings
244 $template_settings = [
245 'post_id' => $postId,
246 '__attachments' => $organizedUrls,
247 ];
248
249 // Map post IDs and disable logging
250 $helper->map_post_ids[$postId] = $postId;
251 $helper->shouldLog = false;
252
253 // Prepare the helper with the data and settings
254 $helper->prepare($data, $template_settings);
255
256 // Update the content in the data array
257 $content = wp_unslash($helper->get_content());
258
259 return $content;
260 }
261 }