<?php if (!defined('APS_ADS_VER')) exit('restricted access');
/*
 * @package WordPress
 * @subpackage APS Ads
*/

	function aps_build_ads_settings_page() {
		$aps_nonce = wp_create_nonce('aps_ads');
		$notice = false;
		
		// process the form
		if (isset($_POST['submit'])) {
			if (wp_verify_nonce($_POST['aps-nonce'], 'aps_ads')) {
				$fields_data = (isset($_POST['aps-ads'])) ? $_POST['aps-ads'] : array();
				$saved = update_aps_settings('ads-settings', $fields_data);
				$notice = '<div class="notice notice-success is-dismissible"><p>' .esc_html__('Your settings are saved successfully', 'aps-ads') .'.</p></div>';
			} else {
				$notice = '<div class="notice notice-error is-dismissible"><p>' .esc_html__('There is something went wrong, please reload the page and try again', 'aps-ads') .'.</p></div>';
			}
		}
		
		$data = get_aps_settings('ads-settings');
		$adsense_id = (isset($data['adsense-id'])) ? $data['adsense-id'] : '';
		
		// create your settings form ?>
		<div class="wrap aps-wrap">
			<h1><?php esc_html_e('APS Ads Settings', 'aps-ads'); ?></h1>
			
			<?php // print the notice
			if ($notice) echo wp_specialchars_decode($notice); ?>
			<form method="post" action="<?php echo esc_url( admin_url('admin.php?page=aps-ads-settings') ); ?>">
				<table class="form-table">
					<tbody>
						<tr>
							<th scope="row">
								<label><?php esc_html_e('Adsense Publisher ID', 'aps-ads'); ?></label>
							</th>
							<td>
								<div class="aps-input-wrap">
									<input type="text" class="aps-text-input" name="aps-ads[adsense-id]" value="<?php echo esc_attr($adsense_id); ?>" />
								</div>
								<p><?php esc_html_e('Please enter your Adsense publisher ID eg.', 'aps-ads'); ?> ca-pub-XXXXXXXXXXXXXXXX</p>
							</td>
						</tr>
						<?php do_action('aps_ads_settings_fields', $data); ?>
						<tr>
							<th scope="row"></th>
							<td>
								<div class="aps-input-wrap">
									<input type="hidden" name="aps-nonce" value="<?php echo esc_attr($aps_nonce); ?>" />
									<input type="submit" class="button-primary" name="submit" value="<?php esc_html_e('Save Changes', 'aps-ads'); ?>" />
								</div>
							</td>
						</tr>
					</tbody>
				</table>
			</form>
		</div>
		<?php
	}
	