Mini Shell

Direktori : /home/runcloud/webapps/wpdefault/import/
Upload File :
Current File : /home/runcloud/webapps/wpdefault/import/blanket-json.php

<?php
header("access-control-allow-origin: *");
	header('Content-type: application/json');

error_reporting(E_ALL);
ini_set('display_errors', 1);

if(file_exists("wp-load.php"))require( 'wp-load.php' );
else require( '../wp-load.php' );

$title = $_GET['title'];
$image = $_GET['image'];
$type = $_GET['type'];

if (isset($title) && ($type == 'blanket')) {

    $input = [
        'title' => $title,
        'image' => $image
    ];

    $querystr = "
            SELECT DISTINCT meta_value
            FROM wp_postmeta
            WHERE meta_key = 'source' AND meta_value = '".$image."'
            ORDER BY meta_value ASC LIMIT 1
        ";
    $post_source = $wpdb->get_results( $querystr, OBJECT );

    if (empty($post_source[0]->meta_value)){
        echo json_encode(['post_id' => WP_HOME.'?p='.auto_product($input)]);
    } else {
	    echo json_encode(['error' => 'exits']);
    }

}


function auto_product($input){
    global $wpdb;
    $data1 = '{"product":{"id":5062484164747,"title":"Dreamship Fleece Blanket","body_html":"The ultra-soft, silky smooth, lightweight fleece blanket features your art or photography vibrantly printed on one side. Plush warm enough for an in-home accessory and lightweight enough to take on-the-go","vendor":"Dreamship","product_type":"","created_at":"2020-11-25T06:54:03-05:00","handle":"dreamship-fleece-blanket","updated_at":"2020-11-25T06:57:01-05:00","published_at":"2020-11-25T06:56:02-05:00","template_suffix":"","published_scope":"web","tags":"","variants":[{"id":34384088596619,"product_id":5062484164747,"title":"Small (30x40in)","price":"42.99","sku":"746","position":1,"compare_at_price":"","fulfillment_service":"manual","inventory_management":"shopify","option1":"Small (30x40in)","option2":null,"option3":null,"created_at":"2020-11-25T06:55:40-05:00","updated_at":"2020-11-25T06:57:01-05:00","taxable":false,"barcode":"","grams":0,"image_id":null,"weight":0.0,"weight_unit":"lb","requires_shipping":true},{"id":34384088629387,"product_id":5062484164747,"title":"Medium (50x60in)","price":"59.99","sku":"747","position":2,"compare_at_price":"","fulfillment_service":"manual","inventory_management":"shopify","option1":"Medium (50x60in)","option2":null,"option3":null,"created_at":"2020-11-25T06:55:40-05:00","updated_at":"2020-11-25T06:57:01-05:00","taxable":false,"barcode":"","grams":0,"image_id":null,"weight":0.0,"weight_unit":"lb","requires_shipping":true},{"id":34384088662155,"product_id":5062484164747,"title":"Large (60x80in)","price":"79.99","sku":"748","position":3,"compare_at_price":"","fulfillment_service":"manual","inventory_management":"shopify","option1":"Large (60x80in)","option2":null,"option3":null,"created_at":"2020-11-25T06:55:40-05:00","updated_at":"2020-11-25T06:57:01-05:00","taxable":false,"barcode":"","grams":0,"image_id":null,"weight":0.0,"weight_unit":"lb","requires_shipping":true}],"options":[{"id":6565269438603,"product_id":5062484164747,"name":"Size","position":1,"values":["Small (30x40in)","Medium (50x60in)","Large (60x80in)"]}],"images":[{"id":16697975701643,"product_id":5062484164747,"position":1,"created_at":"2020-11-25T06:56:35-05:00","updated_at":"2020-11-25T06:56:35-05:00","alt":null,"width":1024,"height":1024,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/0386\/8321\/3963\/products\/preview_BaLSpSL.jpg?v=1606305395","variant_ids":[]}],"image":{"id":16697975701643,"product_id":5062484164747,"position":1,"created_at":"2020-11-25T06:56:35-05:00","updated_at":"2020-11-25T06:56:35-05:00","alt":null,"width":1024,"height":1024,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/0386\/8321\/3963\/products\/preview_BaLSpSL.jpg?v=1606305395","variant_ids":[]}}}';
	$data_json = json_decode($data1, true);
	$data_json['product']['image']['src'] = $input['image'];
	$data_json['product']['images'][0]['src'] = $input['image'];
	$json = json_encode($data_json);
    $data = array(
        'post_author' => 1,
        'post_content' => $data_json['product']['body_html'],
        'post_content_filtered' => '',
        'post_title' => $input['title'],
        'post_excerpt' => '',
        'post_status' => 'publish',
        'post_type' => 'product',
        'post_category' => array(),
        'comment_status' => 'open',
        'ping_status' => 'closed',
        'post_password' => '',
        'to_ping' =>  '',
        'pinged' => '',
        'post_parent' => 0,
        'menu_order' => 0,
        'guid' => '',
        'import_id' => 0,
        'context' => '',
    );
    $product_id = wp_insert_post( $data, $wp_error );
    
	wp_set_object_terms($product_id, 'mmolazi', 'product_type'); // mmolazi, mmolazi_gearment
    add_post_meta($product_id, '_price', '42.99', true);
    add_post_meta($product_id, 'json_shopify', $json, true);
    add_post_meta($product_id, 'source', $input['image'], true);


    return $product_id;
}