golamrobbani
1/30/2019 - 9:00 AM

image migration

image migration

<?php
require 'wp-blog-header.php';
$post_type     = 'education';
$limit         = 4;
$image_gallery = array();
$pdf_gallery   = array();

function do_migration($limit, $offset)
{
	global $wpdb;
	$site_url = 'http://kcparent.com';
	$posts    = $wpdb->get_results("select * from wp_posts where post_type='education' AND post_status='publish' limit $limit offset $offset");

	foreach ($posts as $post) {
		$ispublished = get_post_meta($post->ID, 'ispublished', true);

		/*if (!empty($ispublished)) {
			if ($ispublished == 't') {
				$post_arg = array(
					'ID'           => $post->ID,
					'post_status'   => 'publish',
				);
				wp_update_post( $post_arg );
			}else{
				$post_arg = array(
					'ID'           => $post->ID,
					'post_status'   => 'draft',
				);
				wp_update_post($post_arg);
			}
		}*/

		$image1 = get_post_meta($post->ID, '_thumbnail_id', true);
         if (!empty($image1)) {
             $image_gallery[$image1] = wp_get_attachment_thumb_url($image1);
         }
         $image2 = get_post_meta($post->ID, 'image2', true);
         if (!empty($image2)) {
             $image_url                     = migration_upload_image($site_url . $image2);
             $attachment_id                 = migration_get_image_id($image_url);
             $image_gallery[$attachment_id] = $image_url;
         }
         $image3 = get_post_meta($post->ID, 'image3', true);
         if (!empty($image3)) {
             $image_url                     = migration_upload_image($site_url . $image3);
             $attachment_id                 = migration_get_image_id($image_url);
             $image_gallery[$attachment_id] = $image_url;
         }
         $image4 = get_post_meta($post->ID, 'image4', true);
         if (!empty($image4)) {
             $image_url                     = migration_upload_image($site_url . $image4);
             $attachment_id                 = migration_get_image_id($image_url);
             $image_gallery[$attachment_id] = $image_url;
         }

         $image5 = get_post_meta($post->ID, 'image5', true);
         if (!empty($image5)) {
             $image_url                     = migration_upload_image($site_url . $image5);
             $attachment_id                 = migration_get_image_id($image_url);
             $image_gallery[$attachment_id] = $image_url;
         }
         $image6 = get_post_meta($post->ID, 'image6', true);
         if (!empty($image6)) {
             $image_url                     = migration_upload_image($site_url . $image6);
             $attachment_id                 = migration_get_image_id($image_url);
             $image_gallery[$attachment_id] = $image_url;
         }



         $PDF1 = get_post_meta($post->ID, 'PDF1', true);
         if (!empty($PDF1)) {
             $pdf_array =explode(',', $PDF1);
             $url_string=preg_replace('/\s/', '', $pdf_array[0]);
             $pdf_url = $site_url.stripslashes(ltrim($url_string, "["));
             if (!empty($pdf_url)) {
                 $upload_pdf_url  = insert_attachment($pdf_url,$post->ID);
                 $attachment_id   = migration_get_image_id($upload_pdf_url);

                 migration_update_attachemnt_title($upload_pdf_url,$pdf_array[1]);
                 $pdf_gallery[$attachment_id] = $upload_pdf_url;

             }
         }

         $PDF2 = get_post_meta($post->ID, 'PDF2', true);
         if (!empty($PDF2)) {
             $pdf_array =explode(',', $PDF2);
             $url_string=preg_replace('/\s/', '', $pdf_array[0]);
             $pdf_url = $site_url.stripslashes(ltrim($url_string, "["));
             if (!empty($pdf_url)) {
                 $upload_pdf_url  = insert_attachment($pdf_url,$post->ID);
                 $attachment_id   = migration_get_image_id($upload_pdf_url);
                 migration_update_attachemnt_title($upload_pdf_url,$pdf_array[1]);
                 $pdf_gallery[$attachment_id] = $upload_pdf_url;

             }
         }


         $PDF3 = get_post_meta($post->ID, 'PDF3', true);
         if (!empty($PDF3)) {
             $pdf_array =explode(',', $PDF3);
             $url_string=preg_replace('/\s/', '', $pdf_array[0]);
             $pdf_url = $site_url.stripslashes(ltrim($url_string, "["));
             if (!empty($pdf_url)) {
                 $upload_pdf_url  = insert_attachment($pdf_url,$post->ID);
                 $attachment_id   = migration_get_image_id($upload_pdf_url);
                 migration_update_attachemnt_title($upload_pdf_url,$pdf_array[1]);
                 $pdf_gallery[$attachment_id] = $upload_pdf_url;

             }
         }


         $PDF4 = get_post_meta($post->ID, 'PDF4', true);
         if (!empty($PDF4)) {
             $pdf_array =explode(',', $PDF4);
             $url_string=preg_replace('/\s/', '', $pdf_array[0]);
             $pdf_url = $site_url.stripslashes(ltrim($url_string, "["));
             if (!empty($pdf_url)) {
                 $upload_pdf_url  = insert_attachment($pdf_url,$post->ID);
                 $attachment_id   = migration_get_image_id($upload_pdf_url);
                 migration_update_attachemnt_title($upload_pdf_url,$pdf_array[1]);
                 $pdf_gallery[$attachment_id] = $upload_pdf_url;

             }
         }



         $PDF5 = get_post_meta($post->ID, 'PDF5', true);
         if (!empty($PDF5)) {
             $pdf_array =explode(',', $PDF5);
             $url_string=preg_replace('/\s/', '', $pdf_array[0]);
             $pdf_url = $site_url.stripslashes(ltrim($url_string, "["));
             if (!empty($pdf_url)) {
                 $upload_pdf_url  = insert_attachment($pdf_url,$post->ID);
                 $attachment_id   = migration_get_image_id($upload_pdf_url);
                 migration_update_attachemnt_title($upload_pdf_url,$pdf_array[1]);
                 $pdf_gallery[$attachment_id] = $upload_pdf_url;

             }
         }

         $pdffile = get_post_meta($post->ID, 'pdffile', true);
         if (!empty($pdffile)) {
             $pdf_array =explode(',', $pdffile);
             $url_string=preg_replace('/\s/', '', $pdf_array[0]);
             $pdf_url = $site_url.stripslashes(ltrim($url_string, "["));
             if (!empty($pdf_url)) {
                 $upload_pdf_url  = insert_attachment($pdf_url,$post->ID);
                 $attachment_id   = migration_get_image_id($upload_pdf_url);
                 migration_update_attachemnt_title($upload_pdf_url,$pdf_array[1]);
                 $pdf_gallery[$attachment_id] = $upload_pdf_url;

             }
         }

         $PDFa = get_post_meta($post->ID, 'PDFa', true);
         if (!empty($PDFa)) {
             $pdf_array =explode(',', $PDFa);
             $url_string=preg_replace('/\s/', '', $pdf_array[0]);
             $pdf_url = $site_url.stripslashes(ltrim($url_string, "["));
             if (!empty($pdf_url)) {
                 $upload_pdf_url  = insert_attachment($pdf_url,$post->ID);
                 $attachment_id   = migration_get_image_id($upload_pdf_url);
                 migration_update_attachemnt_title($upload_pdf_url,$pdf_array[1]);
                 $pdf_gallery[$attachment_id] = $upload_pdf_url;

             }
         }


         $PDFb = get_post_meta($post->ID, 'PDFb', true);
         if (!empty($PDFb)) {
             $pdf_array =explode(',', $PDFb);
             $url_string=preg_replace('/\s/', '', $pdf_array[0]);
             $pdf_url = $site_url.stripslashes(ltrim($url_string, "["));
             if (!empty($pdf_url)) {
                 $upload_pdf_url  = insert_attachment($pdf_url,$post->ID);
                 $attachment_id   = migration_get_image_id($upload_pdf_url);
                 migration_update_attachemnt_title($upload_pdf_url,$pdf_array[1]);
                 $pdf_gallery[$attachment_id] = $upload_pdf_url;

             }
         }

         $PDFc = get_post_meta($post->ID, 'PDFc', true);
         if (!empty($PDFc)) {
             $pdf_array =explode(',', $PDFc);
             $url_string=preg_replace('/\s/', '', $pdf_array[0]);
             $pdf_url = $site_url.stripslashes(ltrim($url_string, "["));
             if (!empty($pdf_url)) {
                 $upload_pdf_url  = insert_attachment($pdf_url,$post->ID);
                 $attachment_id   = migration_get_image_id($upload_pdf_url);
                 migration_update_attachemnt_title($upload_pdf_url,$pdf_array[1]);
                 $pdf_gallery[$attachment_id] = $upload_pdf_url;

             }
         }




         update_post_meta($post->ID, '_directory_pdf_gallery', $pdf_gallery);
         update_post_meta($post->ID, '_directory_img_gallery', $image_gallery);
         $image_gallery = [];
         $pdf_gallery = [];


	}

}


function migration_upload_image($url, $return = 'src', $post_id = null)
{
	require_once(ABSPATH . 'wp-admin/includes/media.php');
	require_once(ABSPATH . 'wp-admin/includes/file.php');
	require_once(ABSPATH . 'wp-admin/includes/image.php');
	return media_sideload_image($url, $post_id, null, $return);
}


function insert_attachment($url, $post_id)
{


	// Need to require these files
	if (!function_exists('media_handle_upload')) {
		require_once(ABSPATH . "wp-admin" . '/includes/image.php');
		require_once(ABSPATH . "wp-admin" . '/includes/file.php');
		require_once(ABSPATH . "wp-admin" . '/includes/media.php');
	}

	//$url = "http://s.wordpress.org/style/images/wp3-logo.png";
	$tmp = download_url($url);
	if (is_wp_error($tmp)) {
		// download failed, handle error
	}
	$post_id    = 1;
	$desc       = "The WordPress Logo";
	$file_array = array();


	preg_match('/[^\?]+\.(jpg|jpe|jpeg|gif|png|pdf)/i', $url, $matches);
	$file_array['name']     = basename($matches[0]);
	$file_array['tmp_name'] = $tmp;

	// If error storing temporarily, unlink
	if (is_wp_error($tmp)) {
		@unlink($file_array['tmp_name']);
		$file_array['tmp_name'] = '';
	}

	// do the validation and storage stuff
	$id = media_handle_sideload($file_array, $post_id, $desc);

	// If error storing permanently, unlink
	if (is_wp_error($id)) {
		@unlink($file_array['tmp_name']);
		return $id;
	}

	$src = wp_get_attachment_url($id);
	return $src;
}


function migration_get_image_id($image_url)
{
	global $wpdb;
	$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url));
	return $attachment[0];
}

function migration_update_attachemnt_title($image_url, $title)
{
	global $wpdb;
	$attachment = $wpdb->get_col($wpdb->prepare("UPDATE  $wpdb->posts SET post_title='$title' WHERE guid='%s';", $image_url));
}


$offset    = !empty($_GET['offset']) ? intval($_GET['offset']) : 0;
$next_page = $offset + $limit;
$total     = $wpdb->get_var("select count(ID) from wp_posts where post_type='education' AND post_status='publish'");

echo "<h1 style='text-align: center;margin-top: 20px;'>MIGRATION - PROGRESS {$offset} of {$total}</h1>";
if ($next_page > $total) {
	var_dump($total);
	wp_die("ALL COMPLETE");
}
do_migration($limit, $offset);
?>
<script>
	window.location = "<?php echo trailingslashit(site_url('/')) . basename(__FILE__) . "?offset={$next_page}";?>";
</script>