
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php
/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://codesupply.co
 * @since      1.0.0
 *
 * @package    Basic MailChimp
 * @subpackage Admin
 */

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Basic MailChimp
 * @subpackage Admin
 * @author     Code Supply Co. <hello@codesupply.co>
 */
class Basic_MailChimp_Admin {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since 1.0.0
	 * @param string $plugin_name  The name of this plugin.
	 * @param string $version      The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version = $version;

	}

	/**
	 * Register admin page
	 *
	 * @since 1.0.0
	 */
	public function register_options_page() {
		add_options_page( esc_html__( 'MailChimp', 'basic-mailchimp' ), esc_html__( 'MailChimp', 'basic-mailchimp' ), 'manage_options', 'bmc_mailchimp_settings', array( $this, 'build_options_page' ) );
	}

	/**
	 * Build admin page
	 *
	 * @since 1.0.0
	 */
	public function build_options_page() {

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have sufficient rights to view this page.', 'basic-mailchimp' ) );
		}

		$this->save_options_page();
		?>

			<div class="wrap bmc-wrap">
				<h2><?php esc_html_e( 'MailChimp', 'basic-mailchimp' ); ?></h2>

				<div class="bmc-settings">
					<form method="post">
						<table class="form-table">
							<tbody>
								<!-- API Key -->
								<tr>
									<th scope="row"><label for="bmc_mailchimp_token"><?php esc_html_e( 'API Key', 'basic-mailchimp' ); ?></label></th>
									<td><input class="regular-text" id="bmc_mailchimp_token" name="bmc_mailchimp_token" type="text" value="<?php echo esc_attr( get_option( 'bmc_mailchimp_token' ) ); ?>"></td>
								</tr>
								<tr>
									<td colspan="2">
										<p><?php esc_html_e( '1. Log in to your account', 'basic-mailchimp' ); ?> <?php echo sprintf( '<a href="%1$s" target="_blank">%1$s</a>', esc_url( 'https://mailchimp.com' ) ); ?></p>
										<p><?php esc_html_e( '2. Click your profile name to expand the Account Panel, and choose  Account.', 'basic-mailchimp' ); ?></p>
										<p><?php esc_html_e( '3. Click the  Extras drop-down menu and choose  API keys.', 'basic-mailchimp' ); ?></p>
										<p><?php esc_html_e( '4. Copy an existing API key or click the  Create A Key button.', 'basic-mailchimp' ); ?></p>
										<p><?php esc_html_e( '5. Name your key descriptively, so you know what application uses that key.', 'basic-mailchimp' ); ?></p>
									</td>
								</tr>
								<!-- Kits -->
								<tr>
									<?php
									$token = get_option( 'bmc_mailchimp_token' );

									if ( $token ) {

										$mail_chimp = new MailChimp( $token );

										$data = $mail_chimp->get( 'lists' );

										if ( $data && isset( $data['lists'] ) && $data['lists'] ) {
										?>
											<th scope="row"><label for="bmc_mailchimp_list"><?php esc_html_e( 'Default List', 'basic-mailchimp' ); ?></label></th>
											<td>
												<select class="regular-text" name="bmc_mailchimp_list" id="bmc_mailchimp_list">
													<option><?php esc_html_e( '- not selected -', 'basic-mailchimp' ); ?></option>
													<?php foreach ( $data['lists'] as $item ) : ?>
														<option <?php selected( $item['id'], get_option( 'bmc_mailchimp_list' ) ); ?> value="<?php echo esc_attr( $item['id'] ); ?>"><?php echo esc_html( $item['name'] ); ?></option>
													<?php endforeach; ?>
												</select>
											</td>
										<?php } else { ?>
											<td colspan="2"><code><?php esc_html_e( 'Invalid Token or font sets not created!', 'basic-mailchimp' ); ?></code></td>
										<?php } ?>
									<?php } ?>
								</tr>
								<!-- Enable double opt-in -->
								<tr>
									<th scope="row"><label for="bmc_double_optin"><?php esc_html_e( 'Enable Double opt-in', 'basic-facebook' ); ?></label></th>
									<td><input class="regular-text" id="bmc_double_optin" name="bmc_double_optin" type="checkbox" value="true" <?php checked( (bool) get_option( 'bmc_double_optin', false ) ); ?>></td>
								</tr>
							</tbody>
						</table>

						<?php wp_nonce_field( 'bmc_save_action','bmc_save_nonce' ); ?>

						<p class="submit"><input class="btw-button button button-primary" name="save_settings" type="submit" value="<?php esc_html_e( 'Save changes', 'basic-mailchimp' ); ?>" /></p>
					</form>
				</div>
			</div>
		<?php
	}


	/**
	 * Settings save
	 *
	 * @since 1.0.0
	 */
	protected function save_options_page() {
		if ( isset( $_POST['bmc_save_action'] ) && ! wp_verify_nonce( wp_unslash( $_POST['bmc_save_action'] ),'bmc_save_nonce' ) ) { // Input var ok; sanitization ok.
			return;
		}

		if ( isset( $_POST['save_settings'] ) ) { // Input var ok.

			if ( isset( $_POST['bmc_mailchimp_token'] ) ) { // Input var ok.
				update_option( 'bmc_mailchimp_token', sanitize_text_field( wp_unslash( $_POST['bmc_mailchimp_token'] ) ) ); // Input var ok.
			}
			if ( isset( $_POST['bmc_mailchimp_list'] ) ) { // Input var ok.
				update_option( 'bmc_mailchimp_list', sanitize_text_field( wp_unslash( $_POST['bmc_mailchimp_list'] ) ) ); // Input var ok.
			}
			if ( isset( $_POST['bmc_double_optin'] ) ) { // Input var ok.
				update_option( 'bmc_double_optin', true );
			} else {
				update_option( 'bmc_double_optin', false );
			}
			printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'The settings are saved.', 'basic-mailchimp' ) );
		}
	}
}
