Skip to content

Instantly share code, notes, and snippets.

@Avakulenko
Created October 20, 2016 08:15
Show Gist options
  • Save Avakulenko/ea1dfb79dfe4f28ac0349e7d7f3a969f to your computer and use it in GitHub Desktop.
Save Avakulenko/ea1dfb79dfe4f28ac0349e7d7f3a969f to your computer and use it in GitHub Desktop.
add_action('template_redirect', function() {
if (!isset($_GET['import_ciso_workshops'])) {
return;
}
global $wpdb;
$data_to_import = json_decode(file_get_contents(dirname(__FILE__) . '/import_data/Programs/ciso-workshops.json'), TRUE);
if (empty($data_to_import)) {
echo 'Invalid file';
exit;
}
foreach ($data_to_import as $index => $data_item) {
$post_type = 'crb_program';
$posts_exists = crb_post_exists('crb_program', $data_item['name']);
if ($posts_exists) {
echo 'Post ' . $data_item['name'] . ' already exists <h3>' . $posts_exists . '</h3><br>';
continue;
}
if ($data_item['date']) {
//[date format] 2016-08-24 13:36:01
$post_timestamp = strtotime($data_item['date']);
$post_date = date('Y-m-d', $post_timestamp);
}
$args = array(
'post_content' => $data_item['single_data']['content'],
'post_title' => $data_item['name'],
'post_excerpt' => $data_item['description'],
'post_date' => $post_date,
'post_status' => 'publish',
'post_type' => 'crb_program',
);
$post_id = wp_insert_post($args);
if (!is_wp_error($post_id)) {
// Import Topics
$topic_terms = [];
$tags_terms = [];
$topics = $data_item['single_data']['topics'];
if ($topics) {
foreach ($topics as $topic_name) {
$topic_term = get_term_by('name', htmlspecialchars($topic_name), 'crb_topic');
if ($topic_term) {
$topic_terms[] = $topic_term->term_id;
}
}
}
if (!empty($topic_terms)) {
wp_set_post_terms($post_id, $topic_terms, 'crb_topic');
}
// Import thumbnail
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($data_item['image_original']);
$has_original = true;
if (is_wp_error($image_data)) {
echo 'Original image does not exists';
$image_data = file_get_contents($data_item['image']);
$has_original = false;
}
if ($has_original) {
$filename = basename($data_item['image_original']);
} else {
$filename = basename($data_item['image']);
}
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 );
$res1= wp_update_attachment_metadata( $attach_id, $attach_data );
$res2= set_post_thumbnail( $post_id, $attach_id );
// Update post meta
if ($data_item['date']) {
update_post_meta($post_id, '_crb_program_date', $post_date);
}
if ($data_item['link']) {
update_post_meta($post_id, '_crb_program_old_link', $data_item['link']);
}
// Attach PDFs
$pdfs = $data_item['pdfs'];
if ($pdfs) {
foreach ($pdfs as $num => $pdf) {
$pdf_data = file_get_contents($pdf['link']);
$pdf_filename = basename($pdf['link']);
if(wp_mkdir_p($upload_dir['path'])) {
$pdf_file = $upload_dir['path'] . '/' . $pdf_filename;
} else {
$pdf_file = $upload_dir['basedir'] . '/' . $pdf_filename;
}
file_put_contents($pdf_file, $pdf_data);
$wp_filetype = wp_check_filetype($pdf_filename, null );
$pdf_attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($pdf_filename),
'post_content' => '',
'post_status' => 'inherit'
);
$pdf_attach_id = wp_insert_attachment( $pdf_attachment, $pdf_file, $post_id );
update_post_meta($post_id, '_crb_program_pdfs_-_file_' . $num, $pdf_attach_id );
update_post_meta($post_id, '_crb_program_pdfs_-_label_' . $num, $pdf['text'] );
}
}
// Top Intro Image intro_image_original
$top_image_data = file_get_contents($data_item['single_data']['intro_image_original']);
$top_image_filename = basename($data_item['single_data']['intro_image_original']);
if(wp_mkdir_p($upload_dir['path'])) {
$top_file = $upload_dir['path'] . '/' . $top_image_filename;
} else {
$top_file = $upload_dir['basedir'] . '/' . $top_image_filename;
}
file_put_contents($top_file, $top_image_data);
$wp_filetype = wp_check_filetype($top_image_filename, null );
$top_attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($top_image_filename),
'post_content' => '',
'post_status' => 'inherit'
);
$top_attach_id = wp_insert_attachment( $top_attachment, $top_file, $post_id );
$top_attach_data = wp_generate_attachment_metadata( $top_attach_id, $top_file );
$top_res1= wp_update_attachment_metadata( $top_attach_id, $top_attach_data );
if ($top_attach_id) {
update_post_meta($post_id, '_crb_program_top_image', $top_attach_id);
}
// Attach Images To Post
$additional_images = $data_item['single_data']['images'];
if ($additional_images) {
$additional_images_array = array();
foreach ($additional_images as $num => $additional_image) {
$additional_image_data = file_get_contents($additional_image);
$additional_filename = basename($additional_image);
if(wp_mkdir_p($upload_dir['path'])) {
$additional_file = $upload_dir['path'] . '/' . $additional_filename;
} else {
$additional_file = $upload_dir['basedir'] . '/' . $additional_filename;
}
file_put_contents($additional_file, $additional_image_data);
$wp_filetype = wp_check_filetype($additional_filename, null );
$additional_attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($additional_filename),
'post_content' => '',
'post_status' => 'inherit'
);
$additional_attach_id = wp_insert_attachment( $additional_attachment, $additional_file, $post_id );
$additional_attach_data = wp_generate_attachment_metadata( $additional_attach_id, $additional_file );
$additional_res1= wp_update_attachment_metadata( $additional_attach_id, $additional_attach_data );
if ($additional_attach_id) {
$new_image_url = wp_get_attachment_url($additional_attach_id);
$additional_images_array[$additional_image] = $new_image_url;
}
}
if ($additional_images_array) {
$fixer = new CDS_Content_Fixer($data_item['single_data']['content'], $additional_images_array);
$post_body_with_local_imgs = $fixer->fix_img_srcs();
wp_update_post(array(
'ID' => $post_id,
'post_content' => $post_body_with_local_imgs
));
}
}
echo ($index + 1) . ' Successfully improrted post: ' . $data_item['name'] . '<br>';
} else {
echo 'Can not import post ' . $data['name'] . '<br>';
}
}
exit;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment