Skip to content

Instantly share code, notes, and snippets.

@serguk89
Last active October 29, 2018 17:23
Show Gist options
  • Save serguk89/4cb9dca29d2f553a402768a285f0d526 to your computer and use it in GitHub Desktop.
Save serguk89/4cb9dca29d2f553a402768a285f0d526 to your computer and use it in GitHub Desktop.
function Generate_Image( $file, $post_id = 0 ){
$filepath = '/frontend/web';
if (file_exists(realpath($_SERVER['DOCUMENT_ROOT']) . $filepath . $file)) {
$file = str_replace(['/wp', ''], '', home_url($file));
} elseif (file_exists(realpath($_SERVER['DOCUMENT_ROOT'].'../www/') . $filepath . $file)) {
$file = str_replace(['/wp', 'test.'], '', home_url($file));
} else {
return false;
}
// Set variables for storage, fix file filename for query strings.
preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png|svg)\b/i', $file, $matches );
if ( ! $matches ) {
return new WP_Error( 'image_sideload_failed', __( 'Invalid image URL' ) );
}
$file_array = array();
$file_array['name'] = basename( $matches[0] );
// Download file to temp location.
$file_array['tmp_name'] = download_url( $file );
$file = wp_handle_sideload( $file_array, ['test_form' => 0] );
// Construct the attachment array.
$attachment = array(
'file' => $file['file'],
'post_mime_type' => $file['type'],
'guid' => $file['url'],
'post_parent' => $post_id,
'post_title' => $file_array['name'],
'post_content' => '',
);
// This should never be set as it would then overwrite an existing attachment.
unset( $attachment['ID'] );
// Save the attachment metadata
$id = wp_insert_attachment($attachment, false, $post_id);
return $id;
}
function Generate_Featured_Image( $file, $post_id){
if (!$id = Generate_Image($file, $post_id))
return false;
set_post_thumbnail( $post_id, $id );
}
add_action('init', function (){
if (!empty($_GET['parse'])) {
global $wpdb;
set_time_limit(0);
function delete_associated_media( $id ) {
$media = get_children( array(
'post_parent' => $id,
'post_type' => 'attachment'
) );
if( empty( $media ) ) {
return;
}
foreach( $media as $file ) {
wp_delete_attachment( $file->ID );
}
}
add_action( 'before_delete_post', 'delete_associated_media' );
$delete_post = array(
'post_type' => 'winner',
'post_status' => 'publish'
);
$posts = new WP_Query( $delete_post );
if ( $posts->have_posts() ) {
while ( $posts->have_posts() ) {
$posts->the_post();
wp_delete_post( get_the_ID());
}
}
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
$old_news = $wpdb->get_results("
SELECT
`winners`.`text`,
`winners`.`name`,
`winners`.`year`,
`winners`.`logo`,
`winners`.`location`,
`winners`.`description`,
`winners`.`website`,
`winners`.`image`,
`winners`.`video`,
`winners`.`show_scores`,
`winners`.`enable_link`,
`winners`.`show_booking`,
`country`.`name` as `country`,
`category`.`name` as `category`,
`category`.`photo` as `category_image`,
`category`.`icon_for_home`
FROM `winners`
LEFT JOIN `category` ON `category`.`id` = `winners`.`category`
LEFT JOIN `country` ON `country`.`id` = `winners`.`country`", ARRAY_A);
foreach ($old_news as $n) {
$data = [
'post_author' => 1,
'post_content' => $n['text'],
'post_title' => $n['name'],
'post_status' => 'publish',
'post_date' => $n['data'],
'post_date_gmt' => $n['data'],
'post_type' => 'winner',
'post_excerpt' => 'description',
];
$p = wp_insert_post($data);
if ($image = @json_decode($n['image'])[0]) {
Generate_Featured_Image($image, $p);
}
update_field('year', $n['year'], $p);
update_field('location', $n['location'], $p);
update_field('website', $n['website'], $p);
update_field('video', $n['video'], $p);
update_field('show_scores', $n['show_scores'], $p);
update_field('enable_link', $n['enable_link'], $p);
update_field('show_booking', $n['show_booking'], $p);
if (($image = @json_decode($n['logo'])[0]) && $attach = Generate_Image( $image ) ) {
update_field('logo', $attach, $p);
}
if (($image = @json_decode($n['bg'])[0]) && $attach = Generate_Image( $image ) ) {
update_field('banner', $attach, $p);
}
if ($cat_term = get_terms(['hide_empty' => false, 'taxonomy' => 'winner-category', 'name' => $n['category']])) {
$cat_term = $cat_term[0]->term_id;
} else {
$cat_term = wp_insert_term($n['category'], 'winner-category');
$cat_term = $cat_term['term_id'];
if ($cat_image = @json_decode($n['category_image'])[0]) {
if ($attach = Generate_Image( $cat_image ))
{
update_field('image', $attach, 'term_' . $cat_term);
}
}
if ($cat_image = @json_decode($n['icon_for_home'])[0]){
if ($attach = Generate_Image( $cat_image ))
{
update_field('icon_for_home', $attach, 'term_' . $cat_term);
}
}
}
wp_set_post_terms($p, $cat_term, 'winner-category');
if($cat_term = get_terms(['hide_empty' => false, 'taxonomy' => 'winner-country', 'name' => $n['country']])) {
$cat_term = $cat_term[0]->term_id;
} else {
$cat_term = wp_insert_term($n['country'], 'winner-country')->term_id;
}
wp_set_post_terms($p, $cat_term, 'winner-country');
}
die('success');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment