<?php

/**
 * In version 2.4.0 featured images were changed to use ACF gallery rather than MultiPostThumbnails.
 *
 */
class Intense_Migration_2_4_0 extends Intense_Migration {
	public function up(){
		parent::up();

		//Migrate features images

		$intense_visions_options = get_option( 'intense_visions_options' );		
		
		//Testing loop to get featured images for posts.
		$featured_image_count = $intense_visions_options['intense_featured_images'];

		$post_types = array(
			'page',
			'post',
			'intense_portfolio',
			'intense_project',
			'intense_news',
			'intense_team',
			'intense_locations',
			'intense_events',
			'intense_clients',
			'intense_recipes'
		);

		$imageList = "";

		foreach ( $post_types as &$value ) {
			$query = new WP_Query('posts_per_page=-1&post_type=' . $value);
			while ( $query->have_posts() ) : $query->the_post();
					if ( has_post_thumbnail() ) {
						$post_type = $query->post->post_type;

						for ( $i = 0; $i <= ( $featured_image_count ) ; $i++ ) {
							$image_name = 'feature-image-' . ($i + 1);  // sets image name as feature-image-1, feature-image-2 etc.

							if ( get_post_meta( $query->post->ID, "{$post_type}_{$image_name}_thumbnail_id", true ) ) {
								$ids[] = get_post_meta( $query->post->ID, "{$post_type}_{$image_name}_thumbnail_id", true);
								$imageList .= get_post_meta( $query->post->ID, "{$post_type}_{$image_name}_thumbnail_id", true) . " ";
							}
						}

						if ( $imageList != "" ) {
							//$output .= $post_type . ': ' . $query->post->ID . ' - ' . $imageList . '<br />';
							//$output .= "<pre>" . print_r($ids,true) . "</pre>";
							//the following will create all images for the gallery of each post that has more than one featured image
							update_field( 'field_54acba1d5bea7', $ids , $query->post->ID );
						}

						$imageList = "";
						unset( $ids );
					}
			endwhile;
			wp_reset_postdata();
		}

		//enable shortcodes and CSS for new shortcodes
		
		$intense_visions_options['intense_active_shortcodes']['intense_post_media'] = 1;
		$intense_visions_options['intense_shortcode_css']['animated'] = 1;
		update_option( 'intense_visions_options', $intense_visions_options );
	}

	public function down() {
		parent::down();

		
	}
}
