Skip to content

Instantly share code, notes, and snippets.

@Zenger
Last active December 28, 2015 00:29
Show Gist options
  • Save Zenger/7413503 to your computer and use it in GitHub Desktop.
Save Zenger/7413503 to your computer and use it in GitHub Desktop.
Blank WordPress Importer. Can be used as a starting point to import files from custom CMS to PHP
<?php
require_once "wp-load.php";
ob_implicit_flush(true); ob_end_flush(); //Instant output woohooo thx ##php
include_once( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php' );
$serv = new wpdb("root", "password", "user", "localhost");
global $wpdb;
set_time_limit(0);
define("THUMB_PATH", "http://yoursite.com/img/products/%s/thumbs/%s");
define("URL", "http://localhost/site/");
function download_and_set_attachment($image_url, $post_id)
{
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($image_url);
$filename = basename($image_url);
$filename = str_replace( array("%20", "%", "%25") , "-" , $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 );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
}
function download_file($image_url)
{
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($image_url);
$filename = basename($image_url);
$filename = str_replace( array("%20", "%", "%25") , "-" , $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);
return $upload_dir['url'] . "/" . $filename;
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>to WordPress</title>
</head>
<body>
<pre><?php
$cat_alias = array(
1 => 29,
2 => 30,
4 => 31,
5 => 32,
6 => 33,
7 => 34,
8 => 35,
9 => 37,
10 => 38,
11 => 36,
14 => 39,
15 => 40,
16 => 41,
17 => 42
);
#/*
$existing_cats = $serv->get_results("SELECT id FROM product_categories");
foreach($existing_cats as $category)
{
echo "-------------------- Working in category: ".$category->id . " ----------------------------\n";
$products = $serv->get_results("SELECT * FROM products WHERE product_category_id = ". $category->id );
$category_id = $category->id;
foreach($products as $npost)
{
echo $npost->id . ". ". trim($npost->title_rom) . " \n";
$product_id = $npost->id;
$inserted = wp_insert_post( array(
'post_title' => $npost->title_rom,
'post_content' => $npost->content_rom,
'post_date' => $npost->modified,
'post_name' => $npost->slug,
'post_status' => 'publish',
'post_type' => 'product',
) , true );
#$_POST['icl_post_language'] = $language_code = 'ru'; // change the language code
$inserted_ru = wp_insert_post( array(
'post_title' => $npost->title_rus,
'post_content' => $npost->content_rus,
'post_date' => $npost->modified,
'post_name' => $npost->slug,
'post_status' => 'publish',
'post_type' => 'product',
) , true );
if (!is_wp_error( $inserted ))
{
$thumb_filename = $serv->get_var("SELECT filename FROM product_photos WHERE product_id = " . $product_id);
$colors = $serv->get_results("SELECT color_id FROM colors_products WHERE product_id = 10");
$colorstr_ro = "[colors ";
$colorstr_ru = "[colors ";
foreach($colors as $cid)
{
$cid = $cid->color_id;
list($color) = $serv->get_results ("SELECT title_rom, title_rus, hex FROM colors WHERE id = " . $cid);
$colorstr_ro .= $color->title_rom .'="#'.$color->hex.'" ';
$colorstr_ru .= $color->title_rus .'="#'.$color->hex.'" ';
}
$colorstr_ro .= "]";
$colorstr_ru .= "]";
wp_set_object_terms( $inserted, $cat_alias[$category_id], "product_cat" );
download_and_set_attachment( sprintf(THUMB_PATH, $product_id, rawurlencode( rawurldecode( $thumb_filename ) ) ), $inserted );
update_post_meta( $inserted, 'colors', $colorstr_ro );
update_post_meta( $inserted, '_regular_price', $npost->price);
@$ssimage = download_file( "http://yoursite.com/img/products/" . $npost->img_sizes);
update_post_meta( $inserted, 'stored_image', $ssimage);
wp_set_object_terms( $inserted_ru, $cat_alias[$category_id], "product_cat" );
download_and_set_attachment( sprintf(THUMB_PATH, $product_id, rawurlencode( rawurldecode( $thumb_filename ) ) ), $inserted_ru );
update_post_meta( $inserted_ru, 'colors', $colorstr_ru );
update_post_meta( $inserted_ru, '_regular_price', $npost->price);
@$ssimage = download_file( "http://yoursite.com/img/products/" . $npost->img_sizes);
update_post_meta( $inserted_ru, 'stored_image', $ssimage);
$trid = wpml_get_content_trid( 'post_product', $inserted);
$wpdb->update( $wpdb->prefix.'icl_translations', array( 'trid' => $trid, 'language_code' => 'ru', 'source_language_code' => wpml_get_default_language() ), array( 'element_id' => $inserted_ru ) );
}
else
{
echo "Failed: " . $product_id;
}
}
}
ob_flush(); flush();
# */
/*
list($npost) = $serv->get_results("SELECT * FROM products WHERE id = 237");
$inserted = wp_insert_post( array(
'post_title' => $npost->title_rom,
'post_content' => $npost->content_rom,
'post_date' => $npost->modified,
'post_name' => $npost->slug,
'post_status' => 'publish',
'post_type' => 'product',
) , true );
#$_POST['icl_post_language'] = $language_code = 'ru'; // change the language code
$inserted_ru = wp_insert_post( array(
'post_title' => $npost->title_rus,
'post_content' => $npost->content_rus,
'post_date' => $npost->modified,
'post_name' => $npost->slug,
'post_status' => 'publish',
'post_type' => 'product',
) , true );
if (!is_wp_error( $inserted ))
{
$thumb_filename = $serv->get_var("SELECT filename FROM product_photos WHERE product_id = 237");
var_dump($thumb_filename);
$colors = $serv->get_results("SELECT color_id FROM colors_products WHERE product_id = 237");
$colorstr_ro = "[colors ";
$colorstr_ru = "[colors ";
foreach($colors as $cid)
{
$cid = $cid->color_id;
$color = $serv->get_results ("SELECT title_rom, title_rus, hex FROM colors WHERE id = " . $cid);
$colorstr_ro .= $color->title_rom .'="'.$color->hex.'" ';
$colorstr_ru .= $color->title_rus .'="'.$color->hex.'" ';
}
$colorstr_ro .= "]";
$colorstr_ru .= "]";
echo printf(THUMB_PATH, 237, $thumb_filename);
wp_set_object_terms( $inserted, 29, "product_cat" );
download_and_set_attachment( sprintf(THUMB_PATH, 237, $thumb_filename), $inserted );
update_post_meta( $inserted, 'colors', $colors );
update_post_meta( $inserted, '_regular_price', $npost->price);
update_post_meta( $inserted, 'stored_image', download_file( "http://yoursite.com/img/products/" . $npost->img_sizes) );
wp_set_object_terms( $inserted_ru, 29, "product_cat" );
download_and_set_attachment( sprintf(THUMB_PATH, 237, $thumb_filename), $inserted_ru );
update_post_meta( $inserted_ru, 'colors', $colors );
update_post_meta( $inserted_ru, '_regular_price', $npost->price);
update_post_meta( $inserted_ru, 'stored_image', download_file( "http://yoursite.com/img/products/" . $npost->img_sizes) );
$trid = wpml_get_content_trid( 'post_product', $inserted);
$wpdb->update( $wpdb->prefix.'icl_translations', array( 'trid' => $trid, 'language_code' => 'ru', 'source_language_code' => wpml_get_default_language() ), array( 'element_id' => $inserted_ru ) );
}
*/
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment