
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php
/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://codesupply.co
 * @since      1.0.0
 *
 * @package    Basic Facebook
 * @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 Facebook
 * @subpackage Admin
 * @author     Code Supply Co. <hello@codesupply.co>
 */
class Basic_Facebook_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__( 'Facebook Comments', 'basic-facebook' ), esc_html__( 'Facebook Comments', 'basic-facebook' ), 'manage_options', 'bfb_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-facebook' ) );
		}

		$this->save_options_page();
		?>
			<div class="wrap">
				<h2><?php esc_html_e( 'Facebook Comments Settings', 'basic-facebook' ); ?></h2>

				<form method="post">
					<table class="form-table">
						<tbody>
							<!-- Enable Facebook Comments -->
							<tr>
								<th scope="row"><label for="bfb_enable_comments"><?php esc_html_e( 'Enable Facebook Comments', 'basic-facebook' ); ?></label></th>
								<td><input class="regular-text" id="bfb_enable_comments" name="bfb_enable_comments" type="checkbox" value="true" <?php checked( (bool) get_option( 'bfb_enable_comments', false ) ); ?>></td>
							</tr>
							<!-- Number of Comments -->
							<tr>
								<th scope="row"><label for="bfb_number_comments"><?php esc_html_e( 'Number of Comments', 'basic-facebook' ); ?></label></th>
								<td><input class="small-text" id="bfb_number_comments" name="bfb_number_comments" type="number" value="<?php echo esc_attr( get_option( 'bfb_number_comments', 10 ) ); ?>" /> <?php esc_html_e( 'items', 'basic-facebook' ); ?></td>
							</tr>
							<!-- Location -->
							<?php if ( bfb_location_check() ) { ?>
							<tr>
								<th scope="row"><label for="bfb_location"><?php esc_html_e( 'Location', 'basic-facebook' ); ?></label></th>
								<td>
									<select class="regular-text" name="bfb_location" id="bfb_location">
										<?php
										$locations = apply_filters( 'bfb_comments_location', array() );
										if ( $locations ) {
											foreach ( $locations as $key => $item ) {
												?>
													<option value="<?php echo esc_attr( $key ); ?>" <?php selected( get_option( 'bfb_location' ), $key ); ?>><?php echo esc_attr( $item['name'] ); ?></option>
												<?php
											}
										}
										?>
									</select>
								</td>
							</tr>
							<?php } ?>
						</tbody>
					</table>

					<?php wp_nonce_field( 'bfb_save_action','bfb_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-facebook' ); ?>" /></p>
				</form>
			</div>
		<?php
	}

	/**
	 * Settings save
	 *
	 * @since 1.0.0
	 */
	protected function save_options_page() {
		if ( isset( $_POST['bfb_save_action'] ) && ! wp_verify_nonce( wp_unslash( $_POST['bfb_save_action'] ),'bfb_save_nonce' ) ) { // Input var ok; sanitization ok.
			return;
		}

		if ( isset( $_POST['save_settings'] ) ) { // Input var ok.

			if ( isset( $_POST['bfb_enable_comments'] ) ) { // Input var ok.
				update_option( 'bfb_enable_comments', true );
			} else {
				update_option( 'bfb_enable_comments', false );
			}
			if ( isset( $_POST['bfb_number_comments'] ) ) { // Input var ok.
				update_option( 'bfb_number_comments', intval( wp_unslash( $_POST['bfb_number_comments'] ) ) ); // Input var ok.
			}
			if ( isset( $_POST['bfb_location'] ) ) { // Input var ok.
				update_option( 'bfb_location', sanitize_text_field( wp_unslash( $_POST['bfb_location'] ) ) ); // Input var ok.
			}
			printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'The settings are saved.', 'basic-facebook' ) );
		}
	}
}
