<?php
function ow_news_merge_tags( $input ) {
	//array of all merge tags
	$merge_tags_list = array(
		'/\%\%newsletter_link\%\%/' => '<a href="'.get_site_url().'/newsletter/" target="_blank">'.get_site_url().'/newsletter/</a>'
	);
	//loop through each merge tag replacing tag with content
	foreach( $merge_tags_list as $tag => $value ) {
		$input = preg_replace( $tag, $value, $input );
	}
	return $input;
}


// CREATE THE IRIS OPENWEBSITE NEWS LIST
function iris_openwebsite_news() {
	global $iris_db;
	$news_array = $iris_db->get_results( "SELECT * FROM iris_open_website_news WHERE news_item_expiry_date > NOW() OR news_item_expiry_date = '0000-00-00 00:00:00' ORDER BY news_item_post_date DESC LIMIT 3" );
	
	$news_article = '';
	
	foreach ( $news_array as $news_item ) {
		
		$news_article .= '<div class="news-article">';
		$news_article .= '<p><span class="iris-news-title">'.$news_item->news_item_title.'</span>';
		$news_article .= '<span class="iris-news-date">'.date('d/m/Y', strtotime($news_item->news_item_post_date)).'</span></p>';
		$news_article .= '<div class="iris-news-content">';
		if( $news_item->news_image_url != null ) {
			$news_article .= '<p class="iris-news-image"><img src="'.$news_item->news_image_url.'" alt="'.$news_item->news_item_title.'"/></p>';
			$news_article .= '<div class="iris-news-text">'.wpautop( ow_news_merge_tags( $news_item->news_item_content ) ).'</div>';
		} else {
			$news_article .= '<div class="iris-news-content full-width">'.wpautop( ow_news_merge_tags( $news_item->news_item_content ) ).'</div>';
		}
		$news_article .= '</div><p class="clearfix"></p></div>';
	}
	if($news_article == "") {
		$news_article .= "There is currently no IRIS WebPortal News";
	}
	echo $news_article;
}
iris_openwebsite_news();
?>