<?php
define('DB_DRIVER', 'mysql');

require_once( 'wp-config.php' );
require_once( 'wp-load.php' );

require_once('wp-admin/includes/file.php' );
require_once( 'wp-admin/includes/media.php');
require_once( 'wp-admin/includes/file.php');
require_once('wp-admin/includes/image.php');
require_once ('wp-admin/includes/post.php');
global $wpdb; 
// basic options for PDO 
$dboptions = array(
    PDO::ATTR_PERSISTENT => FALSE,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
//die("dddhfdgj");
//connect with the server
try {
    $DB = new PDO(DB_DRIVER . ':host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD, $dboptions);
} catch (Exception $ex) {
    echo($ex->getMessage());
    die;
}
//print_r($DB);

$url = 'https://api.uniqueschoolapp.ie/feeds/gallary?idschool=218';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result, true));
//die("hfffdgj");
	$galleries = json_decode($result);
/*	$args = array(
        'post_type'      => 'gallery',
        'posts_per_page' => -1
    );
     $posts = get_posts( $args );
    foreach ( $posts as $post ) {
		  $my_post = array(
		  'ID'           => $post->ID,
		  'post_status'    => 'draft',
		);
      wp_update_post( $my_post );
    }*/
    
    foreach ($galleries->data->albums AS $gallery) {
		$gallery_data == array();
		$found_post = post_exists( $gallery->title,'','','gallery');
        if(!$found_post) {
			$postdate = date("Y-m-d H:i:s", $gallery->datee);
			$gallery_post_id = wp_insert_post(
				array(
					'post_author' => 1,
					'post_content' => $gallery->description,  // content of the article,
					'post_title' => $gallery->title,
					 'post_date'=> $postdate,  // title of the article
					'post_status' => 'publish',   // i just published the post directly, but you can change the status as of your need
					'post_type' => 'gallery'  // if any custom post type, you can use it here
				)
			);
			//  $gallery_data['_thumbnail_id'] = $image_id;
			$gallery_data['gallery_id'] = $gallery->id;
			//wp_reset_query();
			update_post_meta($gallery_post_id, 'gallery_data', $gallery_data);
			
			$image_url = $gallery->thumbnil;

			$file = array();
			$file['name'] = wp_basename($image_url);
			$file['tmp_name'] = download_url($image_url);

			$attachmentId = media_handle_sideload($file, $gallery_post_id);
			wp_update_attachment_metadata( $attachmentId, $file );
			 
			set_post_thumbnail( $gallery_post_id, $attachmentId );
			if ( is_wp_error($attachmentId) ) {
				@unlink($file['tmp_name']);
				echo "Could not do media_handle_sideload";
			}

			$galleries_img = $gallery->content;
			$counter = 0;
			$items = array();
			$value = array();
			foreach ($galleries_img AS $gallery_media) {
				/*	echo "<pre>";
					print_r($gallery_media);
					echo "</pre>";*/
					//get Image aaaaaaaaaand save in wordpress. Get Image id.
					$gallery_media == array();
					
					
					$file1 = array();
					$file1['name'] = wp_basename($gallery_media->url);
					$file1['tmp_name'] = download_url($gallery_media->url);
				//echo "<pre>";
						//print_r($file1);
					//	echo "</pre>";

					echo $attachmentIds = media_handle_sideload($file1, 0);
					 //wp_update_attachment_metadata( $attachmentIds, $file1 );
					//foreach($attachmentId as $att_id){
				//	array(
						//$gallery_media['images_'.$counter.'_gallery_image'] = $attachmentIds,
						
						//)
						$image_name	= "images_".$counter."_gallery_image";
						$items['\''.$image_name.'\''] = $attachmentIds;
						$value[] = array( 'field_5f69d9a898341' => $attachmentIds );
					//}
						$counter++;
					
				}
			   
			echo "==".$gallery_post_id."==";
			echo "<pre>";
			print_r($value);
			echo "</pre>";
			update_field( 'field_5f69d99298340', $value, $gallery_post_id );
		}
		else
		{
			 wp_update_post(array(
				'ID'    =>  $found_post,
				'post_status'   =>  'publish'
				));
		}
}
/*
$args = array(
        'post_type'      => 'gallery',
        'post_status'    => 'draft',
        'posts_per_page' => -1
    );
     $posts = get_posts( $args );
    foreach ( $posts as $post ) {
		  $my_post = array(
		  'ID'           => $post->ID,
		  'post_status'    => 'trash',
		);
      wp_update_post( $my_post );
    }
*/
