Mini Shell
<?php
header('Access-Control-Allow-Origin: *');
error_reporting(E_ALL);
ini_set('display_errors', 1);
if(file_exists("wp-load.php"))require( 'wp-load.php' );
else require( '../wp-load.php' );
if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
throw new Exception('Request method must be POST!');
}
//Make sure that the content type of the POST request has been set to application/json
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if(strcasecmp($contentType, 'application/json') != 0){
throw new Exception('Content type must be: application/json');
}
//Receive the RAW post data.
$content = trim(file_get_contents("php://input"));
//Attempt to decode the incoming RAW post data from JSON.
$decoded = json_decode($content, true);
//If json_decode failed, the JSON is invalid.
if(!is_array($decoded)){
throw new Exception('Received content contained invalid JSON!');
}
//$decoded = $decoded['product'];
if (isset($decoded['product']['title']) && !empty($decoded['product']['variants'][0]['price'])) {
global $wpdb;
$data = array(
'post_author' => 1,
'post_content' => '',
'post_content_filtered' => '',
'post_title' => $decoded['product']['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 , explode(',', $input['tags']), 'product_tag', false);
// add_post_meta($product_id, '_price', '17.99', true);
wp_set_object_terms($product_id, 'mmolazi', 'product_type'); // mmolazi for shopify json
add_post_meta($product_id, '_price', $decoded['product']['variants'][0]['price'], true);
add_post_meta($product_id, 'source', $decoded['product']['handle'], true);
add_post_meta($product_id, 'json_shopify', json_encode($decoded, JSON_HEX_QUOT), true);
/** auto make wp_term_relationships */
//$wpdb->insert( $wpdb->term_relationships, array('object_id' => $product_id, 'term_taxonomy_id' => 4, 'term_order' => 0) );
echo json_encode(['post_id' => WP_HOME.'?p='.$product_id]);
}