/
var
/
tmp
/
parabrincar
/
export
/
Upload File
HOME
<?php require_once __DIR__ . '/../wp-load.php'; require_once __DIR__ . '/../wp-admin/includes/taxonomy.php'; function Generate_Featured_Image($images_url, $images_name, $post_id, $featuredImage = true) { $upload_dir = wp_upload_dir(); $toappend = ""; for($i=0;$i<sizeof($images_url);$i++){ $image_data = file_get_contents($images_url[$i]); $filename = basename($images_url[$i]); echo $filename; if (wp_mkdir_p($upload_dir['path'])) { $file = $upload_dir['path'] . '/' . $filename; } else { $file = $upload_dir['basedir'] . '/' . $filename; } file_put_contents($file, $image_data); $wp_filetype = wp_check_filetype($filename, null); if(empty($images_name[$i]) || !strlen($images_name[$i])) $images_name[$i] = $filename; $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_file_name($images_name[$i]), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment($attachment, $file, $post_id); // require_once('../../wp-admin/includes/image.php'); require_once('../wp-admin/includes/image.php'); $attach_data = wp_generate_attachment_metadata($attach_id, $file); $res1= wp_update_attachment_metadata($attach_id, $attach_data); // print_r($attach_id); if($i === 0 && $featuredImage) $res2= set_post_thumbnail($post_id, $attach_id); else { $toappend .= $attach_id.","; } } if($toappend!="") update_post_meta( $post_id, '_product_image_gallery', $toappend); return $attach_id; } $file = fopen('export.json', 'r'); $json = fread($file, filesize('export.json')); fclose($file); $products = json_decode($json); $file = fopen('progress.txt', 'r'); $progress = fread($file, filesize('progress.txt')); fclose($file); $progress = explode(',', $progress); $file = fopen('progress.txt', 'a'); foreach($products as $id => $post){ if(in_array($post->ID, $progress)) continue; fwrite($file, ',' . $post->ID); $postMeta = $post->post_meta; $postTerms = $post->post_terms; $featureImage = $post->featured_image; $galleryImages = $post->gallery_images; unset($post->post_meta); unset($post->post_terms); unset($post->featured_image); unset($post->gallery_images); unset($post->ID); $product_id = wp_insert_post( (array)$post ); foreach($postTerms as $taxonomy => $terms){ wp_set_object_terms($product_id, $terms, $taxonomy ); } foreach( $postMeta as $key => $value){ update_post_meta( $product_id, $key, $value[0] ); } $hasFeatureImage = strlen($featureImage); if($hasFeatureImage){ Generate_Featured_Image([$featureImage], [], $product_id, $featureImage); } Generate_Featured_Image($galleryImages, [], $product_id, !$featureImage); } fclose($file); ?>