<?php
add_filter( 'the_content', 'wpfresh5_content_filter' );
function wpfresh5_content_filter( $content ) {
	global $post;
	if ( !is_single() || !is_main_query() )
		return $content;
	$post_id = get_the_id();
	$disable_mon = get_post_meta( $post_id, '_dis_mon', true );
	if( $disable_mon )
		return $content;
	$keyword = $post_keyword = $cat_keyword = false;
	$my_post_id = $my_post_cat = 0;
	$post_cat = get_the_category( $post_id );
	if( $post_cat ) {
		if( is_array( $post_cat ) )
			$post_cat = $post_cat[0];
		$cat_keyword = get_term_meta( $post_cat->term_id, 'wpfs5_mon_kw', true );
	}
	$post_keyword = get_post_meta( $post_id, '_mon_kw', true );
	if( !empty( $post_keyword ) ) {
		$my_post_id = $post_id;
		$keyword = $post_keyword;
	}
	else if( !empty( $cat_keyword ) ) {
		$my_post_cat = $post_cat->term_id;
		$keyword = $cat_keyword;
	}
	// var_dump($post_keyword);
	// var_dump($cat_keyword);
	// var_dump($keyword);
	// var_dump($my_post_cat);
	$content =  wpfresh5_monetize_links_replace( $content );
	$content =  wpfresh5_monetize_amazon( $content, $keyword, $my_post_id, $my_post_cat );
	$content =  wpfresh5_monetize_clickbank( $content, $keyword, $my_post_id, $my_post_cat  );
	$content =  wpfresh5_monetize_ebay( $content, $keyword, $my_post_id, $my_post_cat );
	return $content;
}

function wpfresh5_monetize_links_replace( $content ) {
    $settings = wpfresh5_get_plugin_settings();
    $links = isset( $settings['monetize']['inline']['links'] ) ? $settings['monetize']['inline']['links'] : false;
	if( empty( $links ) )
		return $content;
	// $urls = array();
	// $keywords = array();
	if( $links ) {
		foreach( $links as $link ) {
			$replacement = '<a href="'.$link['url'].'" target="_blank">${0}</a>';
			$regEx = '\'(?!((<.*?)|(<a.*?)))(\b'. $link['keyword'] . '\b)(?!(([^<>]*?)>)|([^>]*?</a>))\'si';
			$content = preg_replace( $regEx, $replacement, $content);
		}
	}
	return $content;
}

function wpfresh5_monetize_prod( $title, $url, $image_url, $source = 'amazon', $button_txt = 'Buy Now', $price = -1, $image = 0 ) {
	$code = '<div class="wpfs5_am">
		<a class="nounder" href="'.$url.'" target="_blank">';
	if( $image ) {
		$code .= '<div class="wpfs5_thumb">
					<img class="wpfs5_am_i" src="'.$image_url.'"/>
				</div>';
			}
		$code .= '<div class="wpfs5_am_t">'.$title.'</div>';
			// var_dump($price);
	if( ( $price != -1 ) && ( !empty( $price ) ) )
		$code .= '<div class="wpfs5_am_p">'.$price.' $</div>';
			$code .= '
				<div class="wpfs5_am_src"><img src="'.WPFRESH5_URL.'/img/logo-'.$source.'.png" class="wpfs5_am_il" /></div>
			</a>
			<div class="wpfs5_buybtn">
    			<a class="wpfs5_am_l" href="'.$url.'" target="_blank">'.$button_txt.'</a>
    		</div>
    	</div>
	';
	return $code;
}

function wpfresh5_add_element_in_content( $element, $content, $settings ) {
	$float =  1;
	$margin =  0;
	$float_str = '';
	if( $float == 2 )
		$float_str = 'float:left';
	else if( $float == 3 )
		$float_str = 'float:right';
	$wrapper_open = '<div style="'.$float_str.';margin: '.$margin.'px!important">';
	if( $float == 1 )
		$wrapper_close = '</div><div style="clear:both"></div>';
	else
		$wrapper_close = '</div>';

	if( !isset( $settings['position'] ) || ( empty( $settings['position'] ) ) || ( $settings['position'] == 1 ) ) {
		// beginning of post
		return $wrapper_open.$element.$wrapper_close.$content;
	}
	else if( $settings['position'] == 2 ) {
		// end of post
		return $content.$wrapper_open.$element.$wrapper_close.'<div style="clear:both"></div>';
	}
	else if( $settings['position'] == 3 ) {
		// middle
	    $content = explode( '</p>', $content );
	    $new_content = '';
	    $num_paragraphs = count( $content );
	    $paragraphAfter = round( $num_paragraphs / 2 );
	    for ( $i = 0; $i < $num_paragraphs; $i++ ) {
	        if ( $i == $paragraphAfter ) {
	        	$new_content .= $wrapper_open.$element.$wrapper_close;
	        }

	        $new_content .= $content[$i] . '</p>';
	    }
	    return $new_content;
	}
	else if( $settings->position == 4 ) {
		// after x paragraphs
		$paragraphAfter = isset( $settings->paragraph ) ? intval( $settings->paragraph ) : 0; //Enter number of paragraphs to display ad after.
		if( !$paragraphAfter )
			return $content;
	    $content = explode( '</p>', $content );
	    $new_content = '';
	    $num_paragraphs = count( $content );
	    for ( $i = 0; $i < $num_paragraphs; $i++ ) {
	        if ( $i == $paragraphAfter ) {
	        	$new_content .= $wrapper_open.$element.$wrapper_close;
	        }

	        $new_content .= $content[$i] . '</p>';
	    }
	    return $new_content;
	}
}
/*function wpfresh5_escape_input_txt( $text ) {
	return htmlentities( $text, ENT_QUOTES, 'UTF-8' );
}*/
?>