Mini Shell
<?php
//version 1.0.8
header("Content-Type: application/json");
// requirements: install plugin : https://wordpress.org/plugins/featured-image-by-url/
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '-1');
error_reporting(E_ALL);
if(file_exists("wp-load.php"))require( 'wp-load.php' );
else require( '../wp-load.php' );
//$_DOMAIN_CLOUD = 'https://storage.'.$_SERVER['HTTP_HOST'].'/';
$_DOMAIN_CLOUD = 'https://i2.wp.com/';
$link = (isset($_GET['url'])) ? $_GET['url'] : 0;
$extra_tags = (isset($_GET['extra_tags'])) ? explode(',', $_GET['extra_tags']) : array();
$querystr = "
SELECT DISTINCT meta_value
FROM $wpdb->postmeta
WHERE meta_key = 'source' AND meta_value = '".$link."'
ORDER BY meta_value ASC LIMIT 1
";
$post_source = $wpdb->get_results( $querystr, OBJECT );
// echo "<pre>";
// echo $querystr;
// var_dump($post_source);
// echo "</pre>";
if (!empty($post_source[0]->meta_value)){
$response = array();
$response[0] = array(
'link'=> $link,
'error'=> True,
'message'=> "Da import roi"
);
echo json_encode($response);
exit('exit source');
}
$domain = parse_url($link, PHP_URL_HOST);
// echo $domain; echo "<br/>";
$context = stream_context_create(
array(
"http" => array(
"header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
)
)
);
$data = file_get_contents($link, false, $context);
$json = json_decode( htmlspecialchars_decode($data), true );
$json = $json['product'];
$post = array(
'post_content' => $json['body_html'],
'post_name' => empty($json['handle'])?$json['handle']:sanitize_title($json['title']),
'post_title' => $json['title'],
'post_status' => 'publish',
'post_type' => 'product',
);
$wp_error = True;
$product_id = wp_insert_post( $post, $wp_error );
if (empty($product_id)) {
// echo "<pre>";
// var_dump($wp_error);
// echo "</pre>";
exit('failed: cannot import wp_insert_post');
}
// $category_name = $json['product_type'];
$term_ids = array();
if (!empty($json['categories'])) {
foreach($json['categories'] as $category_name){
$x_cat_id = 0;
if (!empty($category_name)) {
$slug = sanitize_title($category_name);
// $cat_id = category_exists($category_name);
$term = get_term_by('name', $category_name, 'product_cat');
if (!$term) {
$new_term = wp_insert_term(
$category_name,
'product_cat',
array(
'slug' => $slug,
)
);
if(!is_wp_error($new_term)){
$x_cat_id = isset( $new_term['term_id'] ) ? $new_term['term_id'] : 0;
}
}else {
$x_cat_id = $term->term_id;
}
}
if (!empty($x_cat_id)) {
array_push($term_ids, $x_cat_id);
}
}
}
if (!empty($term_ids)) {
wp_set_object_terms( $product_id, $term_ids, 'product_cat' );
}
$tags = $extra_tags;
if (!empty($json['tags'])) {
// if (empty($extra_tags)){
// $tags = explode(',', $json['tags']);
// } else {
// $tags = array_merge($extra_tags, explode(',', $json['tags']));
// }
$tags = array_merge($extra_tags, explode(',', $json['tags']));
}
if (!empty($tags)){
wp_set_object_terms($product_id , $tags, 'product_tag');
}
//SET THE PRODUCT CATEGORIES
// wp_set_object_terms($product_id, array('Cat Name 1', 'Cat Name 2'), 'product_cat');
//SET THE PRODUCT TAGS
// wp_set_object_terms($product_id, array('tag1','tag2','tag3'), 'product_tag');
//SET THE WOO PRODUCT TYPE
wp_set_object_terms($product_id, 'variable', 'product_type');
update_post_meta($product_id, 'source', $link);
$link_image = str_ireplace('https://', $_DOMAIN_CLOUD, $json['image']['src']);
// echo $link_image. "<br/>";
$image_url = array(
'img_url' => $link_image,
);
update_post_meta($product_id, '_knawatfibu_url', $image_url);
update_post_meta($product_id, '_knawatfibu_alt', $json['title']);
// $fifu_list_url = array();
$_knawatfibu_wcgallary = array();
$images = array();
$pos = 0;
foreach($json['images'] as $key => $image) {
$link_image = str_ireplace('https://', $_DOMAIN_CLOUD, $image['src']);
// echo $link_image. "<br/>";
$_knawatfibu_wcgallary[$pos++]['url'] = $link_image;
$images[$image['id']] = $link_image;
}
update_post_meta($product_id, '_knawatfibu_wcgallary', $_knawatfibu_wcgallary);
/** auto make wp_term_relationships */
// $wpdb->insert( $wpdb->term_relationships, array('object_id' => $product_id, 'term_taxonomy_id' => 4, 'term_order' => 0) );
// echo "<pre>";
// var_dump($_knawatfibu_wcgallary);
// echo "</pre>";
$product_link = "https://".$_SERVER['HTTP_HOST'] . "?p={$product_id}";
// echo ('Product added. product ID: <a href="'.$product_link.'" target="_blank">' . $product_id.'</a><br>');
// $taxonomy_ids = wc_get_attribute_taxonomy_ids();
// var_dump($taxonomy_ids);
// echo "<br/>";
list($host_domain) = explode(".", $_SERVER['HTTP_HOST']);
$attr_options = array();
$attributes = array();
$pos = 1;
foreach ($json['options'] as $value) {
if(!empty($value['values'])) {
$key = strtolower($value['name']);
$attributes[$key]['name'] = $value['name'];
$attributes[$key]['position'] = $value['position'];
$attributes[$key]['is_visible'] = true;
$attributes[$key]['is_variation'] = true;
$attributes[$key]['value'] = implode("|", $value['values']);
$attr_options["option". $pos] = "attribute_".$key;
$pos++;
}
}
// echo "<pre>";
// var_dump($attributes);
// echo "</pre>";
$metas = array();
// $metas['_knawatfibu_wcgallary'] = !empty($_knawatfibu_wcgallary)?serialize($_knawatfibu_wcgallary):'';
// $metas['_product_attributes'] = !empty($attributes)?serialize($attributes):'';
$metas['_product_attributes'] = !empty($attributes)?$attributes:'';
$metas['_sku'] = !empty($json['sku'])?$json['sku']:'';
foreach ($metas as $key => $value) {
$data = array();
$data["post_id"] = $product_id;
$data["meta_key"] = $key;
$data["meta_value"] = $value;
// $wpdb->insert( $wpdb->postmeta, $data );
update_post_meta($product_id, $key, $value);
}
foreach ($json["variants"] as $variant){
$variant_title = $json['title']. " - " . $variant['title'];
$variant_name = $json['title']. " " . $variant['title'];
$data = array(
'post_parent' => $product_id,
'post_title' => $variant_title,
'post_name' => sanitize_title($variant_name),
'post_type' => "product_variation",
'post_status' => "publish"
);
$variant_id = wp_insert_post( $data, $wp_error );
if ( !empty($variant['image_id']) && !empty($images[$variant['image_id']])) {
update_post_meta($variant_id, '_knawatfibu_url', $images[$variant['image_id']]);
update_post_meta($variant_id, '_knawatfibu_alt', $variant_title);
}
$metas = array();
$price = $variant['price'] + 6.99;
$regular_price = $price;
$variant['compare_at_price'] = $price + (0.5 * $price );
$sale_price = 0;
if (!empty($variant['compare_at_price'])) {
if ($variant['compare_at_price'] > $variant['price']){
$sale_price = $price;
$regular_price = $variant['compare_at_price'];
$price = $regular_price;
}
}
$metas['_sku'] = !empty($variant['sku'])?strtoupper($variant['sku']):'';
$metas['_regular_price'] = !empty($regular_price)?$regular_price:'';
$metas['_sale_price'] = !empty($sale_price)?$sale_price:'';
$metas['_weight'] = !empty($variant['grams'])?round($variant['grams']/1000, 2):'1';
$metas['_manage_stock'] = 'no';
$metas['_price'] = $price;
foreach( $attr_options as $key => $value){
$metas[$value] = !empty($variant[$key])?$variant[$key]:'';
}
// echo "<pre>";
// var_dump($metas);
// echo "</pre>";
foreach ($metas as $key => $value){
$data = array();
$data["post_id"] = $variant_id;
$data["meta_key"] = $key;
$data["meta_value"] = $value;
// $wpdb->insert( $wpdb->postmeta, $data );
update_post_meta( $variant_id, $key, $value );
}
}
// echo "success";
$response = array();
$response[0] = array(
'product_id' => $product_id,
'product_link'=> $product_link,
'error'=> False,
'message'=> $message
);
echo json_encode($response);