Last active
December 15, 2015 21:59
-
-
Save InoPlugs/5329781 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'admin_init', 'init_avia_convert_featured_images'); | |
function init_avia_convert_featured_images() | |
{ | |
if(!get_option('avia_converted_featured_images')) | |
{ | |
avia_convert_featured_images(); | |
//set the avia_images_converted key so this function will never be executed again | |
update_option('avia_converted_featured_images', 1); | |
} | |
} | |
function avia_convert_featured_images() | |
{ | |
$post_types = array('post','page','portfolio'); | |
$count = 0; | |
//iterate over each post type | |
foreach($post_types as $type) | |
{ | |
//get all posts of a certain type | |
$the_query = new WP_Query( array('post_type' => $type, 'posts_per_page'=>-1) ); | |
//check if any post were retrieved | |
if(!empty($the_query->posts)) | |
{ | |
//iterate over each post and check if the post has a gallery key | |
foreach($the_query->posts as $entry) | |
{ | |
//since there might be hundreds of posts make sure php doesnt time out | |
@set_time_limit(45); | |
$post_id = $entry->ID; | |
$meta = avia_post_meta($post_id); | |
//check for featured image | |
$slides = avia_post_meta($post_id, 'slideshow'); | |
if(empty($slides[0]['slideshow_image'])) | |
{ | |
$args = array( | |
'post_type' => 'attachment', | |
'numberposts' => -1, | |
'post_status' => null, | |
'post_parent' => $post_id | |
); | |
$attachments = get_posts( $args ); | |
$featured_image_id = get_post_thumbnail_id($post_id); | |
//if images were found convert them to an array and save that array as slideshow meta array so it can be used by the new image manager | |
if(!empty($attachments) || !empty($featured_image_id)) | |
{ | |
$slides = array(); | |
if(!empty($featured_image_id)) | |
{ | |
$featured_image = get_posts(array('p' => $featured_image_id, 'post_type' => 'attachment')); | |
$caption = empty($featured_image->post_content) ? $featured_image->post_excerpt : $featured_image->post_content; | |
$slides[] = array( | |
'slideshow_image' => $featured_image_id, | |
'slideshow_caption_title' => $featured_image->post_title, | |
'slideshow_caption' => $caption | |
); | |
} | |
if(!empty($attachments)) | |
{ | |
foreach($attachments as $attachment) | |
{ | |
if($attachment->ID == $featured_image_id) continue; | |
//create the slides array and then save it as meta value | |
$caption = empty($attachment->post_content) ? $attachment->post_excerpt : $attachment->post_content; | |
$slides[] = array( | |
'slideshow_image' => $attachment->ID, | |
'slideshow_caption_title' => $attachment->post_title, | |
'slideshow_caption' => $caption | |
); | |
} | |
} | |
} | |
if(!empty($slides)) | |
{ | |
$meta['slideshow'] = $slides; | |
} | |
// wheter or not we found any images, destroy the linking to hidden post by renaming the key. | |
$meta['gallery_image'] = $meta['gallery_image']."-imported"; | |
update_post_meta($post_id , '_avia_elements_theme_compatibility_mode', $meta); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment