<?php

// Exit if accessed directly
if( !defined('ABSPATH') ) exit;

class ESLP_Facebook_Module {
	
	private static $eslp_easy_social_login_extra = null;
	
	/**
	 * Class Constructor for initializing the Easy Social Login module.
	 *
	 * This constructor performs the following:
	 * - Includes the additional functionality file for the plugin.
	 * - Initializes an instance of the `ESLP_Easy_Social_Login_Extra` class.
	 * - Registers Facebook settings with the `admin_init` hook.
	 * - Initializes social media icon hooks.
	 * - Sets up rewrite rules for query variables and handles OAuth requests.
	 * - Enqueues necessary scripts for the login, WooCommerce login, registration, and checkout forms.
	 */
	public function __construct() {
		// Include additional plugin functionality.
		require_once plugin_dir_path(dirname(__FILE__)) . 'includes/eslp-easy-social-login-extra.php';
		self::$eslp_easy_social_login_extra = new ESLP_Easy_Social_Login_Extra();
		
		// Ensure the proper registration of settings and methods.
		add_action('admin_init', [$this, 'eslp_register_facebook_settings']);

		// Initialize hooks for social media icons.
		self::eslp_init_hook();

		// Setup rewrite rules and handle query variables.
		add_filter('query_vars', array( $this, 'eslp_facebook_add_query_vars' ) );
		add_action('template_redirect', array( $this, 'eslp_facebook_handle_oauth_request' ) );

		// Load module scripts for specific forms.
		add_action('login_enqueue_scripts', [$this, 'eslp_module_enqueue_scripts']);
		add_action('woocommerce_login_form', [$this, 'eslp_module_enqueue_scripts']);
		add_action('woocommerce_register_form', [$this, 'eslp_module_enqueue_scripts']);
		if (!is_user_logged_in()) {
			add_action('woocommerce_checkout_init', [$this, 'eslp_module_enqueue_scripts']);
		}		
	}

	/**
	 * Retrieve the slug for the Facebook module.
	 *
	 * @return string Returns the slug identifier for the Facebook module.
	 */
	public function eslp_module_get_slug() {
		return 'facebook';
	}

	/**
	 * Retrieve the name for the Facebook module.
	 *
	 * @return string Returns the display name for the Facebook module.
	 */
	public function eslp_module_get_name() {
		return 'Facebook';
	}

	/**
	 * Enqueue styles and scripts specific to the Facebook module.
	 *
	 * This function registers and enqueues the necessary CSS file for styling the Facebook integration.
	 */
	public function eslp_module_enqueue_scripts() {
		wp_enqueue_style(
			'eslp-facebook-css', 
			esc_url(plugins_url('facebook.css', __FILE__)), 
			false, 
			ESLP_VERSION
		);
	}
	
	/**
	 * Register settings for Facebook integration.
	 *
	 * This method defines and registers the settings, sections, and fields 
	 * required for configuring the Facebook module within the plugin.
	 */
	public function eslp_register_facebook_settings() {
		// Register the primary settings
		register_setting('eslp_facebook_settings', 'eslp_facebook_enabled', array(
			'type'              => 'string',
			'sanitize_callback' => 'sanitize_text_field',
			'default'           => 'no',
		));
		register_setting('eslp_facebook_settings', 'eslp_facebook_app_id', array(
			'type'              => 'string',
			'sanitize_callback' => 'sanitize_text_field',
			'default'           => null,
		));
		register_setting('eslp_facebook_settings', 'eslp_facebook_app_secret', array(
			'type'              => 'string',
			'sanitize_callback' => 'sanitize_text_field',
			'default'           => null,
		));
		register_setting('eslp_facebook_settings', 'eslp_facebook_oauth_redirect_uri', array(
			'type'              => 'string',
			'sanitize_callback' => 'sanitize_url',
			'default'           => home_url( '?eslp_facebook_oauth_callback=1' ),
		));
		register_setting('eslp_facebook_settings', 'eslp_facebook_locale', array(
			'type'              => 'string',
			'sanitize_callback' => 'sanitize_text_field',
			'default'           => 'pt_BR',
		));
		
		// Initialize default options if not already set
		if ('not-exists' === get_option('eslp_facebook_enabled', 'not-exists')) {
			add_option('eslp_facebook_enabled', 'no');
		}
		if ('not-exists' === get_option('eslp_facebook_app_id', 'not-exists')) {
			add_option('eslp_facebook_app_id', null);
		}
		if ('not-exists' === get_option('eslp_facebook_app_secret', 'not-exists')) {
			add_option('eslp_facebook_app_secret', null);
		}
		if ('not-exists' === get_option('eslp_facebook_oauth_redirect_uri', 'not-exists')) {
			add_option('eslp_facebook_oauth_redirect_uri', sanitize_url( home_url( '?eslp_facebook_oauth_callback=1' ) ) );
		}
		if ('not-exists' === get_option('eslp_facebook_locale', 'not-exists')) {
			add_option('eslp_facebook_locale', 'pt_BR');
		}

		// Add sections and fields for the Facebook settings page
		add_settings_section(
			'eslp_facebook_settings_section', // Section ID
			esc_html( __('Facebook Settings', 'easy-social-login') ), // Section title
			null, // Optional description callback
			'eslp_facebook_settings_page' // Page where the section is displayed
		);

		// Field for Facebook setup tutorial
		add_settings_field(
			'eslp_facebook_tutorial', // Field ID
			esc_html( __('Facebook Tutorial', 'easy-social-login') ), // Title
			[$this, 'eslp_module_tutorial'], // Callback to display the field
			'eslp_facebook_settings_page', // Page
			'eslp_facebook_settings_section' // Section
		);

		// Field to enable/disable Facebook integration
		add_settings_field(
			'eslp_facebook_enabled', // Field ID
			esc_html( __('Enable Facebook', 'easy-social-login') ), // Title
			[$this, 'eslp_facebook_enabled_callback'], // Callback to display the field
			'eslp_facebook_settings_page', // Page
			'eslp_facebook_settings_section' // Section
		);

		// Field for Facebook App ID
		add_settings_field(
			'eslp_facebook_app_id',
			esc_html( __('Facebook App ID', 'easy-social-login') ),
			[$this, 'eslp_facebook_app_id_callback'],
			'eslp_facebook_settings_page',
			'eslp_facebook_settings_section'
		);

		// Field for Facebook App Secret
		add_settings_field(
			'eslp_facebook_app_secret',
			esc_html( __('Facebook App Secret', 'easy-social-login' ) ),
			[$this, 'eslp_facebook_app_secret_callback'],
			'eslp_facebook_settings_page',
			'eslp_facebook_settings_section'
		);

		// Field for OAuth Redirect URI
		add_settings_field(
			'eslp_facebook_oauth_redirect_uri',
			esc_html( __('Redirect URL OAuth', 'easy-social-login') ),
			[$this, 'eslp_facebook_oauth_redirect_uri_callback'],
			'eslp_facebook_settings_page',
			'eslp_facebook_settings_section'
		);

		// Field for selecting the API language
		add_settings_field(
			'eslp_facebook_locale',
			esc_html( __('Language', 'easy-social-login') ),
			[$this, 'eslp_facebook_locale_callback'],
			'eslp_facebook_settings_page',
			'eslp_facebook_settings_section'
		);
	}

	/**
	 * Uninstall and clean up plugin options.
	 *
	 * This method deletes all the options related to the Facebook module 
	 * during the uninstallation process.
	 */
	public function eslp_uninstall() {
		delete_option('eslp_facebook_enabled');
		delete_option('eslp_facebook_app_id');
		delete_option('eslp_facebook_app_secret');
		delete_option('eslp_facebook_oauth_redirect_uri');
		delete_option('eslp_facebook_locale');
	}

	/**
	 * Render the settings page for Facebook module.
	 *
	 * This method outputs the settings fields and sections for the Facebook 
	 * settings page in the WordPress admin area.
	 */
	public function eslp_module_render_settings() {
		settings_fields( 'eslp_facebook_settings' );
		do_settings_sections( 'eslp_facebook_settings_page' );
	}

	/**
	 * Initialize hooks for displaying social login icons.
	 *
	 * This method sets up the hooks for adding social login buttons 
	 * on various forms, including WordPress login, registration, and WooCommerce forms.
	 */
	public function eslp_init_hook() {
		if ( method_exists($this, 'display_social_icon_form') ) {
			// WordPress login form
			if ( 'not-exists' !== get_option( 'eslp_easy_social_login_display_button_login', 'not-exists' )
				&& get_option( 'eslp_easy_social_login_display_button_login' ) == true ) {
				add_action( 'login_form', function() { self::display_social_icon_form('login_form'); } );
			}

			// WordPress register form
			if ( 'not-exists' !== get_option( 'eslp_easy_social_login_display_button_register', 'not-exists' )
				&& get_option( 'eslp_easy_social_login_display_button_register' ) == true ) {
				add_action( 'register_form', function() { self::display_social_icon_form('register_form'); } );
			}

			// WooCommerce integration
			$woo_status = self::$eslp_easy_social_login_extra->eslp_check_woocommerce_status();
			if ( $woo_status[0] == 'active' ) {
				// WooCommerce login and register forms
				if ( get_option('woocommerce_enable_myaccount_registration') === 'yes' ) {
					// WooCommerce login form
					if ( 'not-exists' !== get_option( 'eslp_easy_social_login_woocommerce_login', 'not-exists' )
						&& get_option( 'eslp_easy_social_login_woocommerce_login' ) == true ) {
						if ( 'not-exists' !== get_option( 'eslp_easy_social_login_button_position', 'not-exists' ) ) {
							$prefix = ( get_option( 'eslp_easy_social_login_button_position' ) == 'after' ) ? '_form_start' : '_form';
							add_action( 'woocommerce_login' . $prefix, function() { self::display_social_icon_form('woocommerce_login_form'); } );
						}
					}

					// WooCommerce register form
					if ( 'not-exists' !== get_option( 'eslp_easy_social_login_woocommerce_register', 'not-exists' )
						&& get_option( 'eslp_easy_social_login_woocommerce_register' ) == true ) {
						if ( 'not-exists' !== get_option( 'eslp_easy_social_login_button_position', 'not-exists' ) ) {
							$prefix = ( get_option( 'eslp_easy_social_login_button_position' ) == 'after' ) ? '_form_start' : '_form';
							add_action( 'woocommerce_register' . $prefix, function() { self::display_social_icon_form('woocommerce_register_form'); } );
						}
					}
				}

				// WooCommerce checkout login form
				if ( get_option('woocommerce_enable_signup_and_login_from_checkout') === 'yes' ) {
					if ( 'not-exists' !== get_option( 'eslp_easy_social_login_woocommerce_checkout', 'not-exists' ) 
						&& get_option( 'eslp_easy_social_login_woocommerce_checkout' ) == true ) {
						if ( !is_user_logged_in() ) {
							add_action( 'woocommerce_checkout_init', function() { self::display_social_icon_form('woocommerce_checkout_login_form'); } );
						}
					}
				}
			}
		}
	}

	/**
	 * Displays the Facebook authentication tutorial with step-by-step images.
	 * 
	 * Outputs a link to open the tutorial and a hidden container with the images for the tutorial.
	 * The images are displayed in a lightbox when clicked.
	 */
	public function eslp_module_tutorial() {
		// Retrieve the list of tutorial image URLs
		$images = self::eslp_get_module_tutorial_images();

		// Output the tutorial information and link to open the tutorial
		?>
		<p>
			<?php echo esc_html( __( 'How to set up authentication, take a look at Facebook´s tutorial with step-by-step images.','easy-social-login' ) ); ?>
			<a href="javascript:void(0);" id="eslp_open_tutorial">
				<?php echo esc_html( __( 'Open Tutorial','easy-social-login' ) ); ?>
			</a>
		</p>

		<?php // Hidden container for tutorial images ?>
		<div id="eslp-facebook-tutorial-container" style="display: none;">
		<?php
		// Generate image links for the lightbox
		if ($images) {
			foreach ($images as $index => $image_url) {
				?>
				<a href=" <?php echo esc_url( $image_url ); ?> " data-lightbox="tutorial" data-title=" <?php echo esc_attr( __( 'Step ','easy-social-login' ) . $index + 1 ); ?> "></a>
				<?php
			}
		}
		?>
		</div>
		<?php
	}

	/**
	 * Retrieve Tutorial Images.
	 *
	 * Fetches a list of image URLs from the module's tutorial directory for use in the tutorial display.
	 *
	 * @return array List of tutorial image URLs.
	 */
	public function eslp_get_module_tutorial_images() {
		$directory = plugin_dir_path(__FILE__) . sanitize_file_name( self::eslp_module_get_slug() ) . '/'; // Physical directory path
		$directory_url = plugin_dir_url(__FILE__) . sanitize_file_name( self::eslp_module_get_slug() ) . '/'; // Base URL of the directory
		$images = [];

		// Use glob() to get all PNG files in the directory
		$image_files = glob($directory . '*.png');

		// Process the found files
		if ($image_files) {
			foreach ($image_files as $file) {
				// Convert the file path to a URL
				$images[] = sanitize_url( $directory_url . basename($file) );
			}
		}

		return $images;
	}

	/**
	 * Callback for Facebook Enabled Setting.
	 *
	 * Renders a checkbox input to enable or disable Facebook integration.
	 */
	public function eslp_facebook_enabled_callback() {
		?>
		<input type="checkbox" name="eslp_facebook_enabled" value="yes" <?php echo esc_attr( checked( get_option( 'eslp_facebook_enabled' ), 'yes' ) ); ?> />
		<?php
	}

	/**
	 * Callback for Facebook App ID Setting.
	 *
	 * Renders a text input field for entering the Facebook App ID.
	 */
	public function eslp_facebook_app_id_callback() {
		?>
		<input type="text" name="eslp_facebook_app_id" value="<?php echo esc_attr( get_option( 'eslp_facebook_app_id' ) ); ?>" />
		<?php
	}

	/**
	 * Callback for Facebook App Secret Setting.
	 *
	 * Renders a password input field for entering the Facebook App Secret with an option to toggle visibility.
	 */
	public function eslp_facebook_app_secret_callback() {
		$client_secret = get_option( 'eslp_facebook_app_secret' );
		?>
		<input type="password" id="eslp_facebook_app_secret" name="eslp_facebook_app_secret" value="<?php echo esc_attr( $client_secret ); ?>" />
		<button type="button" onclick="toggleSecret( 'eslp_facebook_app_secret' )">👁️ <?php echo esc_html( __( 'Show Client Secret', 'easy-social-login' ) ); ?></button>
		<?php
	}

	/**
	 * Callback for Facebook OAuth Redirect URI Setting.
	 *
	 * Renders a read-only text field showing the Facebook OAuth redirect URI with a copy button and description.
	 */
	public function eslp_facebook_oauth_redirect_uri_callback() {
		$redirect_uri = get_option( 'eslp_facebook_oauth_redirect_uri' );
		?>
		<input type="text" id="eslp_facebook_oauth_redirect_uri" name="eslp_facebook_oauth_redirect_uri" value="<?php echo esc_url( $redirect_uri ); ?>" readonly>
		<a href="javascript:void(0);" class="eslp_copy_content" data-copy-target="eslp_facebook_oauth_redirect_uri"><?php echo esc_html( __( 'Copy URL', 'easy-social-login' ) ); ?></a>
		<p class="description"><?php echo esc_html( __( 'Enter this URL in the redirect field of your Facebook OAuth configuration:', 'easy-social-login' ) ); ?>
			<strong><?php echo esc_url( $redirect_uri ); ?></strong>
		</p>
		<?php
	}

	/**
	 * Settings Page Facebook Tab
	 * 
	 * The code for the plugins settings page facebook tab
	 * 
	 * Return $language array
	 */
	private function eslp_facebook_locales(){
		$language = array( 
			'en_US' => esc_html__( 'English', 'easy-social-login' ),
			'af_ZA' => esc_html__( 'Afrikaans', 'easy-social-login' ),
			'sq_AL' => esc_html__( 'Albanian', 'easy-social-login' ),
			'ar_AR' => esc_html__( 'Arabic', 'easy-social-login' ),
			'hy_AM' => esc_html__( 'Armenian', 'easy-social-login' ),
			'eu_ES' => esc_html__( 'Basque', 'easy-social-login' ),
			'be_BY' => esc_html__( 'Belarusian', 'easy-social-login' ),
			'bn_IN' => esc_html__( 'Bengali', 'easy-social-login' ),
			'bs_BA' => esc_html__( 'Bosanski', 'easy-social-login' ),
			'bg_BG' => esc_html__( 'Bulgarian', 'easy-social-login' ),
			'ca_ES' => esc_html__( 'Catalan', 'easy-social-login' ),
			'zh_CN' => esc_html__( 'Chinese', 'easy-social-login' ),
			'cs_CZ' => esc_html__( 'Czech', 'easy-social-login' ),
			'da_DK' => esc_html__( 'Danish', 'easy-social-login' ),
			'fy_NL' => esc_html__( 'Dutch', 'easy-social-login' ),
			'eo_EO' => esc_html__( 'Esperanto', 'easy-social-login' ),
			'et_EE' => esc_html__( 'Estonian', 'easy-social-login' ),
			'et_EE' => esc_html__( 'Estonian', 'easy-social-login' ),
			'fi_FI' => esc_html__( 'Finnish', 'easy-social-login' ),
			'fo_FO' => esc_html__( 'Faroese', 'easy-social-login' ),
			'tl_PH' => esc_html__( 'Filipino', 'easy-social-login' ),
			'fr_FR' => esc_html__( 'French', 'easy-social-login' ),
			'gl_ES' => esc_html__( 'Galician', 'easy-social-login' ),
			'ka_GE' => esc_html__( 'Georgian', 'easy-social-login' ),
			'de_DE' => esc_html__( 'German', 'easy-social-login' ),
			'el_GR' => esc_html__( 'Greek', 'easy-social-login' ),
			'he_IL' => esc_html__( 'Hebrew', 'easy-social-login' ),
			'hi_IN' => esc_html__( 'Hindi', 'easy-social-login' ),
			'hr_HR' => esc_html__( 'Hrvatski', 'easy-social-login' ),
			'hu_HU' => esc_html__( 'Hungarian', 'easy-social-login' ),
			'is_IS' => esc_html__( 'Icelandic', 'easy-social-login' ),
			'id_ID' => esc_html__( 'Indonesian', 'easy-social-login' ),
			'ga_IE' => esc_html__( 'Irish', 'easy-social-login' ),
			'it_IT' => esc_html__( 'Italian', 'easy-social-login' ),
			'ja_JP' => esc_html__( 'Japanese', 'easy-social-login' ),
			'ko_KR' => esc_html__( 'Korean', 'easy-social-login' ),
			'ku_TR' => esc_html__( 'Kurdish', 'easy-social-login' ),
			'la_VA' => esc_html__( 'Latin', 'easy-social-login' ),
			'lv_LV' => esc_html__( 'Latvian', 'easy-social-login' ),
			'fb_LT' => esc_html__( 'Leet Speak', 'easy-social-login' ),
			'lt_LT' => esc_html__( 'Lithuanian', 'easy-social-login' ),
			'mk_MK' => esc_html__( 'Macedonian', 'easy-social-login' ),
			'ms_MY' => esc_html__( 'Malay', 'easy-social-login' ),
			'ml_IN' => esc_html__( 'Malayalam', 'easy-social-login' ),
			'nl_NL' => esc_html__( 'Nederlands', 'easy-social-login' ),
			'ne_NP' => esc_html__( 'Nepali', 'easy-social-login' ),
			'nb_NO' => esc_html__( 'Norwegian', 'easy-social-login' ),
			'ps_AF' => esc_html__( 'Pashto', 'easy-social-login' ),
			'fa_IR' => esc_html__( 'Persian', 'easy-social-login' ),
			'pl_PL' => esc_html__( 'Polish', 'easy-social-login' ),
			'pt_PT' => esc_html__( 'Portugese', 'easy-social-login' ),
			'pt_BR' => esc_html__( 'Portuguese (Brazil)', 'easy-social-login' ),
			'pa_IN' => esc_html__( 'Punjabi', 'easy-social-login' ),
			'ro_RO' => esc_html__( 'Romanian', 'easy-social-login' ),
			'ru_RU' => esc_html__( 'Russian', 'easy-social-login' ),
			'sk_SK' => esc_html__( 'Slovak', 'easy-social-login' ),
			'sl_SI' => esc_html__( 'Slovenian', 'easy-social-login' ),
			'es_LA' => esc_html__( 'Spanish', 'easy-social-login' ),
			'sr_RS' => esc_html__( 'Srpski', 'easy-social-login' ),
			'sw_KE' => esc_html__( 'Swahili', 'easy-social-login' ),
			'sv_SE' => esc_html__( 'Swedish', 'easy-social-login' ),
			'ta_IN' => esc_html__( 'Tamil', 'easy-social-login' ),
			'te_IN' => esc_html__( 'Telugu', 'easy-social-login' ),
			'th_TH' => esc_html__( 'Thai', 'easy-social-login' ),
			'tr_TR' => esc_html__( 'Turkish', 'easy-social-login' ),
			'uk_UA' => esc_html__( 'Ukrainian', 'easy-social-login' ),
			'vi_VN' => esc_html__( 'Vietnamese', 'easy-social-login' ),
			'cy_GB' => esc_html__( 'Welsh', 'easy-social-login' ),
			'zh_TW' => esc_html__( 'Traditional Chinese Language', 'easy-social-login' )  );
		return $language;
	}

	/**
	 * Callback for Facebook Locale Setting.
	 *
	 * Renders a dropdown menu to select the Facebook API locale.
	 */
	public function eslp_facebook_locale_callback() {
		$selected_locale = get_option( 'eslp_facebook_locale', 'pt_BR' );
		$locales = self::eslp_facebook_locales();
		?>
		<select name="eslp_facebook_locale">
			<?php foreach ( $locales as $locale_code => $locale_name ) : ?>
				<option value="<?php echo esc_attr( $locale_code ); ?>" <?php echo esc_attr( selected( $selected_locale, $locale_code ) ); ?>>
					<?php echo esc_html( $locale_name ); ?>
				</option>
			<?php endforeach; ?>
		</select>
		<?php
	}
	
	/**
	 * Get URL to Redirect After Social Login.
	 *
	 * Determines the redirect URL based on the stored option or defaults to the current page.
	 *
	 * @return string The URL to redirect to after login.
	 */
	public function eslp_module_url_to_go() {
		$redirect_option = get_option( 'eslp_easy_social_login_redirect_url', 'not-exists' );
		if ( 'not-exists' !== $redirect_option && !empty( $redirect_option ) ) {
			$url_to_go = $redirect_option;
		} else {
			global $wp;
			$url_to_go = home_url( add_query_arg( array(), $wp->request ) );
		}
		return sanitize_url( $url_to_go );
	}

	/**
	 * Display Social Icon Form for Facebook Login.
	 *
	 * Generates a Facebook login URL and displays it as a social login button based on the specified form context.
	 *
	 * @param string $param Context of the form (e.g., login, register, etc.).
	 */
	public function display_social_icon_form($param) {
		if (get_option('eslp_facebook_enabled') === 'yes') {
			// Facebook App Credentials
			$app_id = get_option('eslp_facebook_app_id');
			$app_secret = get_option('eslp_facebook_app_secret');
			$callback_url = get_option('eslp_facebook_oauth_redirect_uri');

			// Generate Facebook OAuth URL
			$state_data = array(
				'redirect_url' => self::eslp_module_url_to_go(),
				'nonce'        => wp_create_nonce('eslp_facebook_login'),
			);

			// Encode and save state in a transient
			$state_encoded = wp_json_encode($state_data);
			$transient_key = 'eslp_facebook_oauth_state_' . wp_generate_password(8, false);

			set_transient($transient_key, $state_encoded, 15 * MINUTE_IN_SECONDS);

			// Facebook OAuth Login URL
			$login_url = add_query_arg(array(
				'client_id'     => $app_id,
				'redirect_uri'  => sanitize_url($callback_url),
				'state'         => $transient_key,
				'response_type' => 'code',
				'scope'         => 'email',
			), 'https://www.facebook.com/v22.0/dialog/oauth');

			// Determine button text based on context
			switch ($param) {
				case 'login_form':
				case 'lostpassword_form':
				case 'woocommerce_login_form':
				case 'woocommerce_checkout_login_form':
					$text = esc_html( __('Login with Facebook', 'easy-social-login') );
					break;
				case 'register_form':
				case 'woocommerce_register_form':
					$text = esc_html( __('Register with Facebook', 'easy-social-login') );
					break;
				default:
					$text = esc_html( __('Login with Facebook', 'easy-social-login') );
			}

			// Render the button
			?>
			<div id="eslp-facebook-btn" class="eslp-facebook-social-login social-buttons">
				<a href="<?php echo esc_url($login_url); ?>">
					<span class="dashicons dashicons-facebook"></span>
					<?php echo esc_html($text); ?>
				</a>
			</div>

			<?php
		}
	}

	/**
	 * Adds query variables for Facebook OAuth.
	 * 
	 * Adds the 'eslp_facebook_oauth_callback' query variable to be recognized in 
	 * WordPress URL requests.
	 * 
	 * @param array $vars Existing query variables.
	 * 
	 * @return array The query variables, including the newly added variable.
	 */
	public function eslp_facebook_add_query_vars( $vars ) {
		$vars[] = 'eslp_facebook_oauth_callback';
		return $vars;
	}

	/**
	 * Handle the OAuth request by including the callback handler.
	 * 
	 * This function checks if the callback query variable is present and,
	 * if so, includes the callback PHP file for further processing.
	 */
	public function eslp_facebook_handle_oauth_request() {
		if ( isset( $_GET['eslp_facebook_oauth_callback'] ) && wp_unslash( $_GET['eslp_facebook_oauth_callback'] === '1' ) ) {
			include_once plugin_dir_path( dirname( __FILE__ ) ) . 'oauth/eslp-facebook-callback.php';
			exit;
		}
	}
}