PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.4.5
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.4.5
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
← All changes | includes/Core/Importer/Utils/Utils.php +190 -43 3.1.10 → 3.4.5 View file →
@@ -2,11 +2,16 @@
2 2
3 3 namespace Templately\Core\Importer\Utils;
4 4
5 5 use Exception;
6 +use Templately\Core\Importer\FullSiteImport;
7 +use Templately\Core\Importer\WPImport;
6 8 use Templately\Utils\Base;
9 +use Templately\Utils\Helper;
7 10
8 11 class Utils extends Base {
12 +
13 +
9 14 /**
10 15 * @throws Exception
11 16 */
12 17 public static function read_json_file( $path ) {
@@ -59,8 +64,14 @@
59 64 $result += $post_type['succeed'] ?? [];
60 65 }
61 66 }
62 67
68 + // add attachments data
69 + if ( !empty( $imported_data['attachments']['succeed'] ) ) {
70 + $result += $imported_data['attachments']['succeed'] ?? [];
71 + }
72 +
73 +
63 74 return $result;
64 75 }
65 76
66 77 public static function map_old_new_term_ids( array $imported_data ) {
@@ -151,9 +162,13 @@
151 162 self::update_option( 'show_on_front', 'page' );
152 163 }
153 164 if ( ! empty( $settings['page_settings'] ) ) {
154 165 foreach ( $settings['page_settings'] as $option_name => $val ) {
155 - self::update_option( $option_name, $id );
166 + $__val = $id;
167 + if($option_name === 'fluent_cart_store_settings'){
168 + $__val = $val;
169 + }
170 + self::update_option( $option_name, $__val );
156 171 if ( array_key_exists( $option_name, $extra_settings ) ) {
157 172 foreach ( $extra_settings[ $option_name ] as $name => $value ) {
158 173 self::update_option( $name, $value );
159 174 }
@@ -161,9 +176,9 @@
161 176 }
162 177 }
163 178 }
164 179
165 - public static function upload_logo($url) {
180 + public static function upload_logo($url, $session_id) {
166 181 if(empty($url)) {
167 182 return ['error' => __('URL is empty', 'templately')];
168 183 }
169 184
@@ -171,52 +186,19 @@
171 186 if ( ! wp_http_validate_url( $url ) || !parse_url( $url, PHP_URL_SCHEME ) ) {
172 187 return ['error' => __('Invalid URL', 'templately')];
173 188 }
174 189
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 - }
190 + $post_data = self::prepare_post_data($url);
191 + $wp_importer = new WPImport( null, ['fetch_attachments' => true, 'session_id' => $session_id] );
192 + $attachment_id = $wp_importer->process_attachment($post_data, $url);
180 193
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')];
194 + if(is_wp_error($attachment_id)){
195 + return ['error' => $attachment_id->get_error_message()];
186 196 }
187 197
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 198 return [
217 - 'id' => $id,
218 - 'url' => esc_url_raw(wp_get_attachment_url($id)),
199 + 'id' => (int) $attachment_id,
200 + 'url' => esc_url_raw(wp_get_attachment_url($attachment_id)),
219 201 ];
220 202 }
221 203
222 204 /**
@@ -257,5 +239,170 @@
257 239 $content = wp_unslash($helper->get_content());
258 240
259 241 return $content;
260 242 }
261 -}
243 +
244 + public static function prepare_post_data($image_url, $post_parent = null, $logger = null) {
245 + $filetype = wp_check_filetype(basename($image_url));
246 + if (!$filetype['type']) {
247 + if(is_callable($logger)){
248 + // call the logger function
249 + call_user_func($logger, 'prepare', 'Error: Unable to determine the file type.', -1, 'eventLog');
250 + }
251 + return null;
252 + }
253 +
254 + $post_data = array(
255 + 'post_title' => basename($image_url),
256 + 'post_content' => '',
257 + 'post_status' => 'inherit',
258 + 'post_mime_type' => $filetype['type'],
259 + 'guid' => $image_url,
260 + );
261 +
262 + if($post_parent){
263 + $post_data['post_parent'] = $post_parent; // Set the parent post
264 + }
265 +
266 + if (preg_match('%wp-content/uploads/([0-9]{4}/[0-9]{2})%', $image_url, $matches)) {
267 + $post_data['upload_date'] = $matches[1];
268 + }
269 + else{
270 + $post_data['upload_date'] = date('Y/m');
271 + }
272 +
273 + return $post_data;
274 + }
275 +
276 + // Static version of get_session_data
277 + public static function get_all_session_data(): array {
278 + $data = get_option(FullSiteImport::SESSION_OPTION_KEY, []);
279 + return $data ?? [];
280 + }
281 +
282 + // Static version of get_session_data
283 + public static function get_session_data($session_id): array {
284 + $data = get_option(FullSiteImport::SESSION_OPTION_KEY, []);
285 + return $data[$session_id] ?? [];
286 + }
287 +
288 + // Static version of update_session_data
289 + public static function update_session_data($session_id, $data): bool {
290 + $old_data = get_option(FullSiteImport::SESSION_OPTION_KEY, []);
291 + return update_option(FullSiteImport::SESSION_OPTION_KEY, Helper::recursive_wp_parse_args([$session_id => $data], $old_data));
292 + }
293 +
294 + // Delete specific session data by ID
295 + public static function delete_session_data($session_id): bool {
296 + $all_data = get_option(FullSiteImport::SESSION_OPTION_KEY, []);
297 +
298 + if (!isset($all_data[$session_id])) {
299 + return false; // Session ID doesn't exist
300 + }
301 +
302 + unset($all_data[$session_id]);
303 + return update_option(FullSiteImport::SESSION_OPTION_KEY, $all_data);
304 + }
305 +
306 + public static function get_session_id(){
307 + $session_id = null;
308 + if(!empty($_REQUEST['session_id'])){
309 + $session_id = sanitize_text_field($_REQUEST['session_id']);
310 + }
311 + return $session_id;
312 + }
313 +
314 + public static function get_session_data_by_id(): array {
315 + if($session_id = self::get_session_id()){
316 + return self::get_session_data($session_id);
317 + }
318 + throw new Exception(__('Invalid Session ID.', 'templately'));
319 + }
320 +
321 + public static function update_session_data_by_id($data): bool {
322 + if($session_id = self::get_session_id()){
323 + return self::update_session_data($session_id, $data);
324 + }
325 + throw new Exception(__('Invalid Session ID.', 'templately'));
326 + }
327 +
328 +
329 +
330 + /**
331 + * Clean up directory using RecursiveIteratorIterator approach
332 + * This method handles directory cleanup with proper validation
333 + *
334 + * @param string $dir_path The directory path to clean up
335 + * @return bool True on success, false on failure
336 + */
337 + public static function cleanup_directory($dir_path) {
338 + if (empty($dir_path) || !file_exists($dir_path) || !is_dir($dir_path)) {
339 + return false;
340 + }
341 +
342 + try {
343 + $files = new \RecursiveIteratorIterator(
344 + new \RecursiveDirectoryIterator($dir_path, \RecursiveDirectoryIterator::SKIP_DOTS),
345 + \RecursiveIteratorIterator::CHILD_FIRST
346 + );
347 +
348 + foreach ($files as $fileinfo) {
349 + $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
350 + $todo($fileinfo->getRealPath());
351 + }
352 +
353 + rmdir($dir_path);
354 + return true;
355 + } catch (Exception $e) {
356 + return false;
357 + }
358 + }
359 +
360 +
361 +
362 +
363 +
364 +
365 +
366 +
367 +
368 + /**
369 + * Clean session data by pack ID, keeping only the current session
370 + * Removes all session entries with the same pack_id except the current session
371 + *
372 + * @param string $pack_id The pack ID to match for cleanup
373 + * @param string $current_session_id The current session ID to preserve
374 + * @return array Array of removed session IDs
375 + */
376 + public static function clean_session_data_by_pack_id($pack_id, $current_session_id) {
377 + if (empty($pack_id) || empty($current_session_id)) {
378 + return [];
379 + }
380 +
381 + $all_session_data = self::get_all_session_data();
382 + $removed_session_ids = [];
383 +
384 +
385 + foreach ($all_session_data as $session_id => $session_data) {
386 + // Skip the current session
387 + if ($session_id === $current_session_id) {
388 + continue;
389 + }
390 +
391 + // Remove sessions that have the same pack_id
392 + if (isset($session_data['id']) && $session_data['id'] === $pack_id) {
393 + unset($all_session_data[$session_id]);
394 + $removed_session_ids[] = $session_id;
395 + }
396 + }
397 +
398 + // Update the option with cleaned data if any sessions were removed
399 + if (!empty($removed_session_ids)) {
400 + update_option(FullSiteImport::SESSION_OPTION_KEY, $all_session_data);
401 + }
402 +
403 + return $removed_session_ids;
404 + }
405 +
406 +
407 +
408 +}