<?php

include WPFRESH5_DIR.'/lib/monetize/amazon.php';
include WPFRESH5_DIR.'/lib/monetize/ebay.php';
include WPFRESH5_DIR.'/lib/monetize/clickbank.php';

function wpfresh5_save_settings_monetize() {
    $settings = wpfresh5_get_plugin_settings();

    $amazon_key = isset( $_POST['wpfresh5_amazon_key'] ) ? trim( $_POST['wpfresh5_amazon_key'] ) : '';
    $amazon_secret = isset( $_POST['wpfresh5_amazon_secret'] ) ? trim( $_POST['wpfresh5_amazon_secret'] ) : '';
    $amazon_tag = isset( $_POST['wpfresh5_amazon_tag'] ) ? trim( $_POST['wpfresh5_amazon_tag'] ) : '';
    $amazon_country = isset( $_POST['wpfresh5_amazon_country'] ) ? trim( $_POST['wpfresh5_amazon_country'] ) : 'com';
    $amazon_enabled = isset( $_POST['wpfresh5_amazon_enabled'] ) ? intval( $_POST['wpfresh5_amazon_enabled'] ) : 0;
    $amazon_keywords = isset( $_POST['wpfresh5_amazon_keywords'] ) ? trim( $_POST['wpfresh5_amazon_keywords'] ) : '';
    $amazon_category = isset( $_POST['wpfresh5_amazon_category'] ) ? trim( $_POST['wpfresh5_amazon_category'] ) : '';
    $amazon_display_price = isset( $_POST['wpfresh5_amazon_display_price'] ) ? intval( $_POST['wpfresh5_amazon_display_price'] ) : 0;
    $amazon_display_image = isset( $_POST['wpfresh5_amazon_display_image'] ) ? intval( $_POST['wpfresh5_amazon_display_image'] ) : 0;
    $amazon_num_ads = isset( $_POST['wpfresh5_amazon_num_ads'] ) ? intval( $_POST['wpfresh5_amazon_num_ads'] ) : 0;
    $amazon_per_row = isset( $_POST['wpfresh5_amazon_per_row'] ) ? intval( $_POST['wpfresh5_amazon_per_row'] ) : 0;
    $amazon_enabled = isset( $_POST['wpfresh5_amazon_enabled'] ) ? intval( $_POST['wpfresh5_amazon_enabled'] ) : 0;
    $amazon_txt = isset( $_POST['wpfresh5_amazon_txt'] ) ? trim( $_POST['wpfresh5_amazon_txt'] ) : '';
    $amazon_position = isset( $_POST['wpfresh5_amazon_position'] ) ? intval( $_POST['wpfresh5_amazon_position'] ) : 0;
    $amazon_buy_button = isset( $_POST['wpfresh5_amazon_buy_button'] ) ? trim( $_POST['wpfresh5_amazon_buy_button'] ) : '';

    $clickbank_id = isset( $_POST['wpfresh5_clickbank_id'] ) ? trim( $_POST['wpfresh5_clickbank_id'] ) : '';
    $clickbank_enabled = isset( $_POST['wpfresh5_clickbank_enabled'] ) ? intval( $_POST['wpfresh5_clickbank_enabled'] ) : 0;
    $clickbank_popular = isset( $_POST['wpfresh5_clickbank_popular'] ) ? intval( $_POST['wpfresh5_clickbank_popular'] ) : 0;
    $clickbank_keywords = isset( $_POST['wpfresh5_clickbank_keywords'] ) ? trim( $_POST['wpfresh5_clickbank_keywords'] ) : '';
    // $clickbank_category = isset( $_POST['wpfresh5_clickbank_category'] ) ? trim( $_POST['wpfresh5_clickbank_category'] ) : '';
    $clickbank_display_price = isset( $_POST['wpfresh5_clickbank_display_price'] ) ? intval( $_POST['wpfresh5_clickbank_display_price'] ) : 0;
    $clickbank_display_image = isset( $_POST['wpfresh5_clickbank_display_image'] ) ? intval( $_POST['wpfresh5_clickbank_display_image'] ) : 0;
    $clickbank_num_ads = isset( $_POST['wpfresh5_clickbank_num_ads'] ) ? intval( $_POST['wpfresh5_clickbank_num_ads'] ) : 0;
    $clickbank_per_row = isset( $_POST['wpfresh5_clickbank_per_row'] ) ? intval( $_POST['wpfresh5_clickbank_per_row'] ) : 0;
    $clickbank_enabled = isset( $_POST['wpfresh5_clickbank_enabled'] ) ? intval( $_POST['wpfresh5_clickbank_enabled'] ) : 0;
    $clickbank_txt = isset( $_POST['wpfresh5_clickbank_txt'] ) ? trim( $_POST['wpfresh5_clickbank_txt'] ) : '';
    $clickbank_position = isset( $_POST['wpfresh5_clickbank_position'] ) ? intval( $_POST['wpfresh5_clickbank_position'] ) : 0;
    $clickbank_buy_button = isset( $_POST['wpfresh5_clickbank_buy_button'] ) ? trim( $_POST['wpfresh5_clickbank_buy_button'] ) : '';

    $ebay_appid = isset( $_POST['wpfresh5_ebay_appid'] ) ? trim( $_POST['wpfresh5_ebay_appid'] ) : '';
    $ebay_campaignid = isset( $_POST['wpfresh5_ebay_cid'] ) ? trim( $_POST['wpfresh5_ebay_cid'] ) : '';
    $ebay_country = isset( $_POST['wpfresh5_ebay_country'] ) ? trim( $_POST['wpfresh5_ebay_country'] ) : 'com';
    $ebay_enabled = isset( $_POST['wpfresh5_ebay_enabled'] ) ? intval( $_POST['wpfresh5_ebay_enabled'] ) : 0;
    $ebay_keywords = isset( $_POST['wpfresh5_ebay_keywords'] ) ? trim( $_POST['wpfresh5_ebay_keywords'] ) : '';
    $ebay_category = isset( $_POST['wpfresh5_ebay_category'] ) ? trim( $_POST['wpfresh5_ebay_category'] ) : '';
    $ebay_display_price = isset( $_POST['wpfresh5_ebay_display_price'] ) ? intval( $_POST['wpfresh5_ebay_display_price'] ) : 0;
    $ebay_display_image = isset( $_POST['wpfresh5_ebay_display_image'] ) ? intval( $_POST['wpfresh5_ebay_display_image'] ) : 0;
    $ebay_num_ads = isset( $_POST['wpfresh5_ebay_num_ads'] ) ? intval( $_POST['wpfresh5_ebay_num_ads'] ) : 0;
    $ebay_per_row = isset( $_POST['wpfresh5_ebay_per_row'] ) ? intval( $_POST['wpfresh5_ebay_per_row'] ) : 0;
    $ebay_enabled = isset( $_POST['wpfresh5_ebay_enabled'] ) ? intval( $_POST['wpfresh5_ebay_enabled'] ) : 0;
    $ebay_txt = isset( $_POST['wpfresh5_ebay_txt'] ) ? trim( $_POST['wpfresh5_ebay_txt'] ) : '';
    $ebay_position = isset( $_POST['wpfresh5_ebay_position'] ) ? intval( $_POST['wpfresh5_ebay_position'] ) : 0;
    $ebay_buy_button = isset( $_POST['wpfresh5_ebay_buy_button'] ) ? trim( $_POST['wpfresh5_ebay_buy_button'] ) : '';

    $inline_links = isset( $_POST['wpfresh5_monetize_links'] ) ? $_POST['wpfresh5_monetize_links'] : array();
    $settings['monetize'] = array(
    	'inline' => array(
    		'links' => $inline_links
    	),
        'amazon' => array(
            'key' => $amazon_key,
            'secret' => $amazon_secret,
            'tag' => $amazon_tag,
            'country' => $amazon_country,
            'enabled' => $amazon_enabled,
            'keywords' => $amazon_keywords,
            'category' => $amazon_category,
            'display_price' => $amazon_display_price,
            'display_image' => $amazon_display_image,
            'num_ads' => $amazon_num_ads,
            'per_row' => $amazon_per_row,
            'enabled' => $amazon_enabled,
            'txt' => $amazon_txt,
            'position' => $amazon_position,
            'buy_button' => $amazon_buy_button
        ),
        'clickbank' => array(
            'id' => $clickbank_id,
            'enabled' => $clickbank_enabled,
            'popular' => $clickbank_popular,
            'keywords' => $clickbank_keywords,
            // 'category' => $clickbank_category,
            'display_price' => $clickbank_display_price,
            'display_image' => $clickbank_display_image,
            'num_ads' => $clickbank_num_ads,
            'per_row' => $clickbank_per_row,
            'enabled' => $clickbank_enabled,
            'txt' => $clickbank_txt,
            'position' => $clickbank_position,
            'buy_button' => $clickbank_buy_button
        ),
        'ebay' => array(
            'appid' => $ebay_appid,
            'campaignid' => $ebay_campaignid,
            'country' => $ebay_country,
            'enabled' => $ebay_enabled,
            'keywords' => $ebay_keywords,
            'category' => $ebay_category,
            'display_price' => $ebay_display_price,
            'display_image' => $ebay_display_image,
            'num_ads' => $ebay_num_ads,
            'per_row' => $ebay_per_row,
            'enabled' => $ebay_enabled,
            'txt' => $ebay_txt,
            'position' => $ebay_position,
            'buy_button' => $ebay_buy_button
        ),
    );
    delete_transient( 'wpfs5_amazon' );
    delete_transient( 'wpfs5_ebay' );
    delete_transient( 'wpfs5_clickbank' );

    wpfresh5_save_plugin_settings( $settings );
    return true;
}

function wpfresh5_settings_monetize_tab_inline( $settings = false ) {
    $settings = isset( $settings['monetize']['inline']['links'] ) ? $settings['monetize']['inline']['links'] : false;
	wpfresh5_inline_links_html( $settings );
}

function wpfresh5_inline_links_html( $links ) {
	$str = '<div class="wpautoc-inline-links">
		<table id="wpfresh5-inline-links-table" class="wpfresh5-inline-links-table widefat form-table"><thead><tr><th scope="col" style="width:34%">Keyword '.wpfresh5_pop_admin('The keyword to be linked inside the article').'</th><th scope="col" style="width:60%">URL '.wpfresh5_pop_admin('The URL to link that keyword').'</th><th scope="col" style="width:6%">&nbsp;</th></tr></thead><tbody>';
	// var_dump($links);
	if( $links ) {
		$i = 0;
		foreach( $links as $link ) {
			$str .= wpfresh5_inline_links_row( $link, $i++ );
		}
	}
	$str .= '</tbody></table><br/>';
	$str .= '<button type="button" class="button button-secondary wpfresh5-add-inline-link"><i class="fa fa-plus"></i> Add Link</button></div>';
	echo $str;
}

function wpfresh5_inline_links_row( $link, $i ) {
	$field1_name = 'wpfresh5_monetize_links['.$i.'][keyword]';
	$field2_name = 'wpfresh5_monetize_links['.$i.'][url]';
	$keyword = $link['keyword'];
	$url = $link['url'];
	return '<tr class="wpfresh5_links_row"><td><input type="text" style="width:100%" name="'.$field1_name.'" value="'.$keyword.'" /></td><td><input type="text" style="width:100%" name="'.$field2_name.'" value="'.$url.'" /></td>
        <td><button type="button" class="button button-secondary wpfresh5_remove_link_row"><i class="fa fa-remove"></i></button></td></tr>';
}

function wpfresh5_settings_monetize_tab_amazon( $settings = false ) {
    $settings = isset( $settings['monetize']['amazon'] ) ? $settings['monetize']['amazon'] : false;
	$amazon_key = isset( $settings['key'] ) ? trim( $settings['key'] ) : '';
	$amazon_secret = isset( $settings['secret'] ) ? trim( $settings['secret'] ) : '';
	$amazon_tag = isset( $settings['tag'] ) ? trim( $settings['tag'] ) : '';
	$amazon_country = isset( $settings['country'] ) ? trim( $settings['country'] ) : 'com';

	$amazon_enabled = isset( $settings['enabled'] ) ? intval( $settings['enabled'] ) : 0;
	$amazon_keywords = isset( $settings['keywords'] ) ? trim( $settings['keywords'] ) : '';
	$amazon_category = isset( $settings['category'] ) ? trim( $settings['category'] ) : 'All';
	$amazon_display_price = isset( $settings['display_price'] ) ? intval( $settings['display_price'] ) : 0;
	$amazon_display_image = isset( $settings['display_image'] ) ? intval( $settings['display_image'] ) : 1;
	$amazon_num_ads = isset( $settings['num_ads'] ) ? intval( $settings['num_ads'] ) : 3;
	$amazon_per_row = isset( $settings['per_row'] ) ? intval( $settings['per_row'] ) : 3;
	$amazon_txt = isset( $settings['txt'] ) ? trim( $settings['txt'] ) : 'Amazon Products';
	$amazon_position = isset( $settings['position'] ) ? intval( $settings['position'] ) : 0;
	$amazon_buy_button = isset( $settings['buy_button'] ) ? trim( $settings['buy_button'] ) : 'Buy Now';

?>
	<h3><i class="fa fa-amazon"></i> Amazon</h3>
	<h4 class="h4bigger">1. Amazon API Settings</h4>
	<table class="form-table">
	    <?php wpfresh5_field_text( 'Amazon Key', 'wpfresh5_amazon_key', $amazon_key, false, '', '', 'Your Amazon Key' ); ?>
	    <?php wpfresh5_field_text( 'Amazon Secret', 'wpfresh5_amazon_secret', $amazon_secret, false, '', '<a href="https://wpfreshstart5.com/support/knowledgebase/how-to-create-an-amazon-key-and-secret/" target="_blank">How to get your free Key and Secret Key from Amazon</a>', 'Your Amazon Secret Key' ); ?>
	    <?php wpfresh5_field_text( 'Amazon Tag', 'wpfresh5_amazon_tag', $amazon_tag, false, '', 'usually ends with -20', 'Your Amazon Affiliate Tag ' ); ?>
	    <?php $amazon_countries = wpfresh5_get_amazon_countries(); ?>
	    <?php wpfresh5_field_select( 'Amazon Country', 'wpfresh5_amazon_country', $amazon_country, $amazon_countries, false, '' ); ?>
	</table>
	<br/>
	<h4 class="h4bigger">2. Ad Settings</h4>
	<table class="form-table">
    	<?php wpfresh5_field_checkbox( 'Show Amazon Ads', 'wpfresh5_amazon_enabled', $amazon_enabled, false, 'wpfresh5_amazon_enabled', 'If you enable this, it will add Amazon Affiliate links on autopilot to your posts', '' ); ?>
	    <?php wpfresh5_field_text( 'Keywords', 'wpfresh5_amazon_keywords', $amazon_keywords, false, '', '', 'Your Amazon Keywords', 'wpfresh5_mon_amazon_row', $amazon_enabled ); ?>
	    <?php $amazon_categories = wpfresh5_get_amazon_cats(); ?>
	    <?php wpfresh5_field_select( 'Category', 'wpfresh5_amazon_category', $amazon_category, $amazon_categories, false, '', '', '', 'wpfresh5_mon_amazon_row', !$amazon_enabled ); ?>
    	<?php wpfresh5_field_checkbox( 'Display Price', 'wpfresh5_amazon_display_price', $amazon_display_price, false, 'wpfresh5_amazon_display_price', 'If you enable this, it will show the product price in the ad', '', 'wpfresh5_mon_amazon_row', $amazon_enabled ); ?>
    	<?php wpfresh5_field_checkbox( 'Display Image', 'wpfresh5_amazon_display_image', $amazon_display_image, false, 'wpfresh5_amazon_display_image', 'If you enable this, it will show the product image in the ad', '', 'wpfresh5_mon_amazon_row', $amazon_enabled ); ?>
		<?php $numbers = wpfresh5_get_numbers( 1, 10 ); ?>
	    <?php wpfresh5_field_select( 'Number of Ads', 'wpfresh5_amazon_num_ads', $amazon_num_ads, $numbers, false, '', '', '', 'wpfresh5_mon_amazon_row', !$amazon_enabled  ); ?>
	    <?php wpfresh5_field_select( 'Ads per Row', 'wpfresh5_amazon_per_row', $amazon_per_row, $numbers, false, '', '', '', 'wpfresh5_mon_amazon_row', !$amazon_enabled  ); ?>
	    <?php wpfresh5_field_text( 'Header Text', 'wpfresh5_amazon_txt', $amazon_txt, false, '', '', 'Intro Text before the ads', 'wpfresh5_mon_amazon_row', $amazon_enabled ); ?>
		<?php $positions = wpfresh5_get_banner_positions(); ?>
	    <?php wpfresh5_field_select( 'Position', 'wpfresh5_amazon_position', $amazon_position, $positions, false, '', '', '', 'wpfresh5_mon_amazon_row', !$amazon_enabled  ); ?>
	    <?php wpfresh5_field_text( 'Button Text', 'wpfresh5_amazon_buy_button', $amazon_buy_button, false, '', '', 'The text for the buy button', 'wpfresh5_mon_amazon_row', $amazon_enabled ); ?>
	</table>
	<br/>
<?php
}

function wpfresh5_settings_monetize_tab_clickbank( $settings = false ) {
    $settings = isset( $settings['monetize']['clickbank'] ) ? $settings['monetize']['clickbank'] : false;
    $clickbank_id = isset( $settings['id'] ) ? trim( $settings['id'] ) : '';

    $clickbank_enabled = isset( $settings['enabled'] ) ? intval( $settings['enabled'] ) : 0;
    $clickbank_popular = isset( $settings['popular'] ) ? intval( $settings['popular'] ) : 0;
    $clickbank_keywords = isset( $settings['keywords'] ) ? trim( $settings['keywords'] ) : '';
    // $clickbank_category = isset( $settings['category'] ) ? trim( $settings['category'] ) : 'All';
    $clickbank_display_price = isset( $settings['display_price'] ) ? intval( $settings['display_price'] ) : 0;
    $clickbank_display_image = isset( $settings['display_image'] ) ? intval( $settings['display_image'] ) : 1;
    $clickbank_num_ads = isset( $settings['num_ads'] ) ? intval( $settings['num_ads'] ) : 3;
    $clickbank_per_row = isset( $settings['per_row'] ) ? intval( $settings['per_row'] ) : 3;
    $clickbank_txt = isset( $settings['txt'] ) ? trim( $settings['txt'] ) : 'Clickbank Products';
    $clickbank_position = isset( $settings['position'] ) ? intval( $settings['position'] ) : 0;
    $clickbank_buy_button = isset( $settings['buy_button'] ) ? trim( $settings['buy_button'] ) : 'Buy Now';
?>
<h3><i class="fa fa-bell-o"></i> Clickbank</h3>
<table class="form-table">
    <?php wpfresh5_field_text( 'CBPro Ads Account Number', 'wpfresh5_clickbank_id', $clickbank_id, false, '', 'Get it <a target="_blank" href="https://wpfreshstart5.com/support/cbproads">here</a> - <a href="https://wpfreshstart5.com/support/knowledgebase/how-to-get-your-cbpro-ads-account-number/" target="_blank">Tutorial</a>' ); ?>
</table>
	<br/>
	<h4 class="h4bigger">2. Ad Settings</h4>
	<table class="form-table">
    	<?php wpfresh5_field_checkbox( 'Show Clickbank Ads', 'wpfresh5_clickbank_enabled', $clickbank_enabled, false, 'wpfresh5_clickbank_enabled', 'If you enable this, it will add Clickbank Affiliate links on autopilot to your posts', '' ); ?>
	    <?php wpfresh5_field_text( 'Keywords', 'wpfresh5_clickbank_keywords', $clickbank_keywords, false, '', '', 'Your Clickbank Keywords', 'wpfresh5_mon_clickbank_row', $clickbank_enabled ); ?>
        <?php wpfresh5_field_checkbox( 'Show Popular Products', 'wpfresh5_clickbank_popular', $clickbank_popular, false, 'wpfresh5_clickbank_popular', 'If you enable this, it will show popular the top Clickbank products if there are no results for your keyword/s', '', 'wpfresh5_mon_clickbank_row', $clickbank_enabled ); ?>
    	<?php wpfresh5_field_checkbox( 'Display Price', 'wpfresh5_clickbank_display_price', $clickbank_display_price, false, 'wpfresh5_clickbank_display_price', 'If you enable this, it will show the product price in the ad', '', 'wpfresh5_mon_clickbank_row', $clickbank_enabled ); ?>
    	<?php wpfresh5_field_checkbox( 'Display Image', 'wpfresh5_clickbank_display_image', $clickbank_display_image, false, 'wpfresh5_clickbank_display_image', 'If you enable this, it will show the product image in the ad', '', 'wpfresh5_mon_clickbank_row', $clickbank_enabled ); ?>
		<?php $numbers = wpfresh5_get_numbers( 1, 10 ); ?>
	    <?php wpfresh5_field_select( 'Number of Ads', 'wpfresh5_clickbank_num_ads', $clickbank_num_ads, $numbers, false, '', '', '', 'wpfresh5_mon_clickbank_row', !$clickbank_enabled  ); ?>
	    <?php wpfresh5_field_select( 'Ads per Row', 'wpfresh5_clickbank_per_row', $clickbank_per_row, $numbers, false, '', '', '', 'wpfresh5_mon_clickbank_row', !$clickbank_enabled  ); ?>
	    <?php wpfresh5_field_text( 'Header Text', 'wpfresh5_clickbank_txt', $clickbank_txt, false, '', '', 'Intro Text before the ads', 'wpfresh5_mon_clickbank_row', $clickbank_enabled ); ?>
		<?php $positions = wpfresh5_get_banner_positions(); ?>
	    <?php wpfresh5_field_select( 'Position', 'wpfresh5_clickbank_position', $clickbank_position, $positions, false, '', '', '', 'wpfresh5_mon_clickbank_row', !$clickbank_enabled  ); ?>
	    <?php wpfresh5_field_text( 'Button Text', 'wpfresh5_clickbank_buy_button', $clickbank_buy_button, false, '', '', 'The text for the buy button', 'wpfresh5_mon_clickbank_row', $clickbank_enabled ); ?>
    </table>
<?php
}

function wpfresh5_settings_monetize_tab_ebay( $settings = false ) {
    $settings = isset( $settings['monetize']['ebay'] ) ? $settings['monetize']['ebay'] : false;
	$ebay_appid = isset( $settings['appid'] ) ? trim( $settings['appid'] ) : '';
	$ebay_campaignid = isset( $settings['campaignid'] ) ? trim( $settings['campaignid'] ) : '';
	$ebay_country = isset( $settings['country'] ) ? trim( $settings['country'] ) : 'US';

	$ebay_enabled = isset( $settings['enabled'] ) ? intval( $settings['enabled'] ) : 0;
	$ebay_keywords = isset( $settings['keywords'] ) ? trim( $settings['keywords'] ) : '';
	$ebay_category = isset( $settings['category'] ) ? trim( $settings['category'] ) : 'All';
	$ebay_display_price = isset( $settings['display_price'] ) ? intval( $settings['display_price'] ) : 0;
	$ebay_display_image = isset( $settings['display_image'] ) ? intval( $settings['display_image'] ) : 1;
	$ebay_num_ads = isset( $settings['num_ads'] ) ? intval( $settings['num_ads'] ) : 3;
	$ebay_per_row = isset( $settings['per_row'] ) ? intval( $settings['per_row'] ) : 3;
	$ebay_txt = isset( $settings['txt'] ) ? trim( $settings['txt'] ) : 'Ebay Products';
	$ebay_position = isset( $settings['position'] ) ? intval( $settings['position'] ) : 0;
	$ebay_buy_button = isset( $settings['buy_button'] ) ? trim( $settings['buy_button'] ) : 'Buy Now';
?>
	<h3><i class="fa fa-dollar"></i> eBay</h3>
	<table class="form-table">
	    <?php wpfresh5_field_text( 'App ID', 'wpfresh5_ebay_appid', $ebay_appid, false, '', '<a href="https://wpfreshstart5.com/support/knowledgebase/how-to-create-an-ebay-app/" target="_blank">How to get your free ebay App ID</a>', 'Your ebay App ID' ); ?>
	    <?php wpfresh5_field_text( 'Default Campaign ID', 'wpfresh5_ebay_cid', $ebay_campaignid, false, '', 'Your campaign ID to use. Get it <a target="_blank" href="https://epn.ebay.com/login">here</a>' ); ?>
	    <?php $ebay_countries = wpfresh5_get_ebay_countries(); ?>
	    <?php wpfresh5_field_select( 'eBay Country', 'wpfresh5_ebay_country', $ebay_country, $ebay_countries, false, '' ); ?>
	</table>
		<br/>
	<h4 class="h4bigger">2. Ad Settings</h4>
	<table class="form-table">
    	<?php wpfresh5_field_checkbox( 'Show Ebay Ads', 'wpfresh5_ebay_enabled', $ebay_enabled, false, 'wpfresh5_ebay_enabled', 'If you enable this, it will add ebay Affiliate links on autopilot to your posts', '' ); ?>
		    <?php wpfresh5_field_text( 'Keywords', 'wpfresh5_ebay_keywords', $ebay_keywords, false, '', '', 'Your Ebay Keywords', 'wpfresh5_mon_ebay_row', $ebay_enabled ); ?>
		    <?php $ebay_categories = wpfresh5_get_ebay_cats(); ?>
		    <?php wpfresh5_field_select( 'Category', 'wpfresh5_ebay_category', $ebay_category, $ebay_categories, false, '', '', '', 'wpfresh5_mon_ebay_row', !$ebay_enabled ); ?>
	    	<?php wpfresh5_field_checkbox( 'Display Price', 'wpfresh5_ebay_display_price', $ebay_display_price, false, 'wpfresh5_ebay_display_price', 'If you enable this, it will show the product price in the ad', '', 'wpfresh5_mon_ebay_row', $ebay_enabled ); ?>
	    	<?php wpfresh5_field_checkbox( 'Display Image', 'wpfresh5_ebay_display_image', $ebay_display_image, false, 'wpfresh5_ebay_display_image', 'If you enable this, it will show the product image in the ad', '', 'wpfresh5_mon_ebay_row', $ebay_enabled ); ?>
			<?php $numbers = wpfresh5_get_numbers( 1, 10 ); ?>
		    <?php wpfresh5_field_select( 'Number of Ads', 'wpfresh5_ebay_num_ads', $ebay_num_ads, $numbers, false, '', '', '', 'wpfresh5_mon_ebay_row', !$ebay_enabled  ); ?>
		    <?php wpfresh5_field_select( 'Ads per Row', 'wpfresh5_ebay_per_row', $ebay_per_row, $numbers, false, '', '', '', 'wpfresh5_mon_ebay_row', !$ebay_enabled  ); ?>
		    <?php wpfresh5_field_text( 'Header Text', 'wpfresh5_ebay_txt', $ebay_txt, false, '', '', 'Intro Text before the ads', 'wpfresh5_mon_ebay_row', $ebay_enabled ); ?>
			<?php $positions = wpfresh5_get_banner_positions(); ?>
		    <?php wpfresh5_field_select( 'Position', 'wpfresh5_ebay_position', $ebay_position, $positions, false, '', '', '', 'wpfresh5_mon_ebay_row', !$ebay_enabled  ); ?>
		    <?php wpfresh5_field_text( 'Button Text', 'wpfresh5_ebay_buy_button', $ebay_buy_button, false, '', '', 'The text for the buy button', 'wpfresh5_mon_ebay_row', $ebay_enabled ); ?>
    </table>
<?php
}


/* Amazon */

function wpfresh5_get_amazon_cats() {
	return array(
		array( 'label' => 'All', 'value' => 'All'),
		array( 'label' => 'Apparel & Accessories', 'value' => 'Apparel'),
		// array( 'label' => 'Appstore for Android', 'value' => 'Appstore for Android'),
		array( 'label' => 'Arts, Crafts & Sewing', 'value' => 'ArtsAndCrafts'),
		array( 'label' => 'Automotive', 'value' => 'Automotive'),
		array( 'label' => 'Baby', 'value' => 'Baby'),
		array( 'label' => 'Beauty', 'value' => 'Beauty'),
		// array( 'label' => 'Black Friday Sales', 'value' => 'Black Friday Sales'),
		array( 'label' => 'Books', 'value' => 'Books'),
		array( 'label' => 'Camera & Photo', 'value' => 'Photo'),
		// array( 'label' => 'Car Toys', 'value' => 'Car Toys'),
		array( 'label' => 'Cell Phones & Accessories', 'value' => 'Wireless'),
		array( 'label' => 'Computer & Video Games', 'value' => 'VideoGames'),
		// array( 'label' => 'Computers', 'value' => 'Computers'),
		array( 'label' => 'Electronics', 'value' => 'Electronics'),
		array( 'label' => 'Grocery & Gourmet Food', 'value' => 'Grocery'),
		array( 'label' => 'Health & Personal Care', 'value' => 'HealthPersonalCare'),
		array( 'label' => 'Home & Garden', 'value' => 'Garden'),
		array( 'label' => 'Industrial & Scientific', 'value' => 'Industrial'),
		array( 'label' => 'Jewelry', 'value' => 'Jewelry'),
		array( 'label' => 'Kindle Store', 'value' => 'KindleStore'),
		array( 'label' => 'Kitchen & Housewares', 'value' => 'Kitchen'),
		array( 'label' => 'Magazine Subscriptions', 'value' => 'Magazines'),
		array( 'label' => 'Miscellaneous', 'value' => 'Miscellaneous'),
		array( 'label' => 'Movies & TV', 'value' => 'Movies'),
		array( 'label' => 'MP3 Downloads', 'value' => 'MP3Downloads'),
		array( 'label' => 'Music', 'value' => 'Music'),
		array( 'label' => 'Musical Instruments', 'value' => 'MusicalInstruments'),
		array( 'label' => 'Office Products', 'value' => 'OfficeProducts'),
		array( 'label' => 'Outdoor Living', 'value' => 'OutdoorLiving'),
		// array( 'label' => 'Pet Supplies', 'value' => 'PetSupplies'),
		array( 'label' => 'Pet Supplies', 'value' => 'PetSupplies'),
		array( 'label' => 'PC Hardware', 'value' => 'PCHardware'),
		array( 'label' => 'Shoes', 'value' => 'Shoes'),
		array( 'label' => 'Software', 'value' => 'Software'),
		// array( 'label' => 'Specialty Stores', 'value' => 'Specialty Stores'),
		array( 'label' => 'Sports & Outdoors', 'value' => 'SportingGoods'),
		array( 'label' => 'Tools & Hardware', 'value' => 'Tools'),
		array( 'label' => 'Toys and Games', 'value' => 'Toys'),
		array( 'label' => 'Wine', 'value' => 'Wine')
		// array( 'label' => 'Warehouse Deals', 'value' => 'Warehouse Deals')
	);
}

function wpfresh5_get_amazon_countries() {
	// private $LOCATIONS = array ('ca','com','cn', 'co.jp', 'co.uk','fr',
	//                             'it','cn');
	return array(
		array( 'label' => 'USA', 'value' => 'com'),
		array( 'label' => 'Canada', 'value' => 'ca'),
		array( 'label' => 'UK', 'value' => 'co.uk'),
		array( 'label' => 'Germany', 'value' => 'de'),
		array( 'label' => 'France', 'value' => 'fr'),
		array( 'label' => 'Italy', 'value' => 'it'),
		array( 'label' => 'Spain', 'value' => 'es'),
		array( 'label' => 'Japan', 'value' => 'co.jp'),
		array( 'label' => 'India', 'value' => 'in')
	);
}

/* Ebay */

function wpfresh5_get_ebay_cats() {
	return array(
		array( 'label' => 'Any', 'value' => 0),
		array( 'value' => '20081', 'label' => 'Antiques' ),
		array( 'value' => '550', 'label' => 'Art' ),
		array( 'value' => '2984', 'label' => 'Baby' ),
		array( 'value' => '267', 'label' => 'Books, Comics & Magazines' ),
		array( 'value' => '12576', 'label' => 'Business, Office & Industrial' ),
		array( 'value' => '625', 'label' => 'Cameras & Photography' ),
		array( 'value' => '9800', 'label' => 'Cars, Motorcycles & Vehicles' ),
		array( 'value' => '11450', 'label' => 'Clothes, Shoes & Accessories' ),
		array( 'value' => '11116', 'label' => 'Coins' ),
		array( 'value' => '1', 'label' => 'Collectables' ),
		array( 'value' => '58058', 'label' => 'Computers/Tablets & Networking' ),
		array( 'value' => '14339', 'label' => 'Crafts' ),
		array( 'value' => '237', 'label' => 'Dolls & Bears' ),
		array( 'value' => '11232', 'label' => 'DVDs, Films & TV' ),
		array( 'value' => '1305', 'label' => 'Events Tickets' ),
		array( 'value' => '159912', 'label' => 'Garden & Patio' ),
		array( 'value' => '26395', 'label' => 'Health & Beauty' ),
		array( 'value' => '3252', 'label' => 'Holidays & Travel' ),
		array( 'value' => '11700', 'label' => 'Home, Furniture & DIY' ),
		array( 'value' => '281', 'label' => 'Jewellery & Watches' ),
		array( 'value' => '15032', 'label' => 'Mobile Phones & Communication' ),
		array( 'value' => '11233', 'label' => 'Music' ),
		array( 'value' => '619', 'label' => 'Musical Instruments' ),
		array( 'value' => '1281', 'label' => 'Pet Supplies' ),
		array( 'value' => '870', 'label' => 'Pottery, Porcelain & Glass' ),
		array( 'value' => '10542', 'label' => 'Property' ),
		array( 'value' => '293', 'label' => 'Sound & Vision' ),
		array( 'value' => '888', 'label' => 'Sporting Goods' ),
		array( 'value' => '64482', 'label' => 'Sports Memorabilia' ),
		array( 'value' => '260', 'label' => 'Stamps' ),
		array( 'value' => '220', 'label' => 'Toys & Games' ),
		array( 'value' => '131090', 'label' => 'Vehicle Parts & Accessories' ),
		array( 'value' => '1249', 'label' => 'Video Games & Consoles' ),
		array( 'value' => '40005', 'label' => 'Wholesale & Job Lots' )
	);
}

function wpfresh5_get_ebay_countries() {
	return array(
		array( 'label' => 'USA', 'value' => 'US'),
		array( 'label' => 'Canada', 'value' => 'CA'),
		array( 'label' => 'UK', 'value' => 'UK'),
		array( 'label' => 'Australia', 'value' => 'AU'),
		array( 'label' => 'Germany', 'value' => 'DE'),
		array( 'label' => 'France', 'value' => 'FR'),
		array( 'label' => 'Italy', 'value' => 'IT'),
		array( 'label' => 'Spain', 'value' => 'ES'),
		array( 'label' => 'Netherlands', 'value' => 'NL'),
		array( 'label' => 'Belgium', 'value' => 'BE'),
		array( 'label' => 'Ireland', 'value' => 'IE'),
		array( 'label' => 'Austria', 'value' => 'AT'),
		array( 'label' => 'Switzerland', 'value' => 'CH'),
	);
}

function wpfresh5_pop_admin( $text ) {
    return '<a href="#" class="wpfresh5pop"><i class="fa fa-question"></i></a>
    <div class="webui-popover-content">
       <p>'.$text.'</p>
    </div>';
}

function wpfresh5_get_numbers( $start, $end, $extra = false ) {
	$arr = array();
	if( $extra )
		$arr[] = array( 'label' => $extra, 'value' => 0 );

	for( $i = $start; $i<= $end; $i++ )
		$arr[] = array( 'label' =>$i, 'value' => $i);
	return $arr;
}

function wpfresh5_get_banner_positions() {
	return array(
		array( 'label' => 'Beginning of Post', 'value' => '1'),
		array( 'label' => 'End of Post', 'value' => '2'),
		array( 'label' => 'Middle of Post', 'value' => '3')
		// array( 'label' => 'After paragraph x', 'value' => '4')
	);
}

function wpfresh5_cdata($data)
{
    if (substr($data, 0, 9) === '<![CDATA[' && substr($data, -3) === ']]>') {
        $data = substr($data, 9, -3);
    }
    return $data;
}


function wpfresh5_custom_meta_boxes( $post_type, $post ) {
    add_meta_box('wpfresh5-monetization', __('WP Freshstart 5 Monetization'), 'wpfresh5_insta_box', 'post', 'side', 'default');
}

function wpfresh5_insta_box() {
    global $post;
    $disable_mon = get_post_meta( $post->ID, '_dis_mon', true );
    $keyword = get_post_meta( $post->ID, '_mon_kw', true );
    if( empty( $keyword ) )
        $keyword = '';
?>
    <div class="wrap">
        <label style="padding-bottom:8px;display:block" for="wpfresh5_disable_mon"> <b>Disable monetiztion for this post</b></label> 
        <label for="wpfresh5_disable_mon"> <input type="checkbox" name="wpfresh5_disable_mon" value="1" id="wpfresh5_disable_mon" <?php echo $disable_mon ? 'checked' : ''; ?> /> Disabled</label>
        <p style="margin-top:2px;padding-top:0">If you check this, no WP Freshstart ads will appear on this <?php echo get_post_type();?></p>
        <label style="padding-bottom:8px;display:block"><b>Keywords</b></label>
        <input type="text" value="<?php echo $keyword;?>" name="wpsc_keyword" style="width:100%">
        <input type="hidden" name="wpfresh5_update" value="1">
        <p style="margin-top:2px;padding-top:0">Enter a value here to use custom monetization keywords for this <?php echo get_post_type();?></p>
    </div>
<?php
}

function wpfresh5_save_post_meta( $post_id ) {
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        return;

    $do_update = isset( $_POST['wpfresh5_update'] ) ? 1 : 0;
    if( !$do_update )
        return;
    $pm = isset( $_POST['wpfresh5_disable_mon'] ) ? intval( $_POST['wpfresh5_disable_mon'] ) : 0;
    update_post_meta( $post_id, '_dis_mon',  $pm );
    $cta = isset( $_POST['wpsc_keyword'] ) ? trim( $_POST['wpsc_keyword'] ) : 0;
    update_post_meta( $post_id, '_mon_kw',  $cta );
    wpfresh5_monetize_update_post( $post_id );
}

add_action( 'add_meta_boxes', 'wpfresh5_custom_meta_boxes', 10, 2 );
add_action( 'save_post', 'wpfresh5_save_post_meta' );

// add_action('category_edit_form', 'category_edit_form');
add_action('category_edit_form_fields','wpfresh5_category_edit_form_fields', 10, 2 );
add_action('category_add_form_fields','wpfresh5_category_add_form_fields', 10, 2 );
// add_action('category_add_form','category_edit_form');


add_action( 'created_category', 'wpfresh5_save_cat_meta', 10, 2 );
add_action( 'edited_category', 'wpfresh5_save_cat_meta', 10, 2 );

function wpfresh5_save_cat_meta( $term_id, $tt_id ){
    if( isset( $_POST['wpfs5_mon_kw'] ) /*&& '' !== $_POST['wpfs5_mon_kw']*/ ){
        $wpfs5_mon_kw = trim( $_POST['wpfs5_mon_kw'] );
        update_term_meta( $term_id, 'wpfs5_mon_kw', $wpfs5_mon_kw );
        wpfresh5_monetize_update_cat( $term_id );
    }
}


function wpfresh5_category_edit_form_fields ( $term, $tt_id ) {
    $wpfs5_mon_kw = get_term_meta( $term->term_id, 'wpfs5_mon_kw', true );
    // var_dump($wpfs5_mon_kw);
?>
    <tr class="form-field">
            <th valign="top" scope="row">
                <label for="wpfs5_mon_kw">WP Freshstart 5 Monetization Keywords</label>
            </th>
            <td>
                <input type="text" id="wpfs5_mon_kw" name="wpfs5_mon_kw" value="<?php echo $wpfs5_mon_kw;?>" />
                <p class="description">If you enter a value here, it will be used for all posts in this category</p>
            </td>
        </tr>
    <?php
}

function wpfresh5_category_add_form_fields ( $term_id, $tt_id = 0 ) {
?>
    <div class="form-field term-wpsc-wrap">
        <label for="wpfs5_mon_kw">WP Freshstart 5 Monetization Keywords</label>
        <input name="wpfs5_mon_kw" id="wpfs5_mon_kw" value="" size="40" type="text">
        <p class="description">If you enter a value here, it will be used for all posts in this category</p>
    </div>
    <?php
}

function wpfresh5_monetize_update_post( $post_id ) {
    delete_transient( 'wpfs5_amazon_pid'.$post_id );
    delete_transient( 'wpfs5_ebay_pid'.$post_id );
    delete_transient( 'wpfs5_cb_pid'.$post_id );
}

function wpfresh5_monetize_update_cat( $cat_id ) {
    delete_transient( 'wpfs5_amazon_cid'.$cat_id );
    delete_transient( 'wpfs5_ebay_cid'.$cat_id );
    delete_transient( 'wpfs5_cb_cid'.$cat_id );
}
?>