<?php
/*
 * Plugin Name:       Easy Social Login
 * Description:       Social media login integration plugin.
 * Version:           1.0.8
 * Requires at least: 5.2
 * Requires PHP:      7.0
 * Author:            Thiago Quadros
 * Author URI:        https://tmquadros.wordpress.com
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       easy-social-login
 * Domain Path:       /languages
 */

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

define( 'ESLP_VERSION' , '1.0.8' );
define( 'ESLP_DATABASE_VERSION' , '1.0.0' );

/**
 * Initializes the Easy Social Login.
 *
 * This function ensures the required core files for the plugin are included and instantiated.
 * If the required files are not found, it triggers a warning and stops execution.
 * If the core class is successfully loaded, it runs the main functionality of the plugin.
 *
 * @return void
 */
function eslp_easy_social_login_init() {
    // Check for the existence of the required core files
    if ( file_exists( dirname( __FILE__ ) . '/includes/eslp-easy-social-login-core.php' )
        && file_exists( dirname( __FILE__ ) . '/includes/eslp-easy-social-login-extra.php' )) {
        
        // Include the core files
        require_once dirname( __FILE__ ) . '/includes/eslp-easy-social-login-core.php';
        require_once dirname( __FILE__ ) . '/includes/eslp-easy-social-login-extra.php';
        
        // Instantiate the core and extra classes
        $eslp_easy_social_login_core = new ESLP_Easy_Social_Login_Core();
        $eslp_easy_social_login_extra = new ESLP_Easy_Social_Login_Extra();
    } else {
        // Trigger a warning if the required files are missing
        echo esc_html( $eslp_easy_social_login_extra->eslp_esc_message(
			__( 'Plug-in not found','easy-social-login' ),
			'die'
			)
		);
    }

    // Verify and execute the core functionality of the plugin
    if ( isset( $eslp_easy_social_login_core ) && method_exists( $eslp_easy_social_login_core, 'eslp_run' ) ) {
        $eslp_easy_social_login_core->eslp_run();		
    } else {
        // Trigger a warning if the core functionality cannot be executed
        echo esc_html( $eslp_easy_social_login_extra->eslp_esc_message(
			__( 'Error on load: ', 'easy-social-login' ) . esc_html( $eslp_easy_social_login_core ),
			'die'
			)
		);
    }
}
// Hook the initialization function to the 'plugins_loaded' action
add_action( 'plugins_loaded', 'eslp_easy_social_login_init' );


/**
 * Handles the activation process for the Easy Social Login plugin.
 *
 * This function is executed when the plugin is activated. 
 * It can be used to set up default options, database tables, or other initial settings.
 *
 * @return void
 */
function eslp_easy_social_login_install() {
    // Placeholder for activation logic, such as initializing default settings.
}

// Register the activation hook for the plugin
register_activation_hook( __FILE__, 'eslp_easy_social_login_install' );


/**
 * Handles the uninstallation process for the Easy Social Login plugin.
 *
 * This function is executed when the plugin is uninstalled. 
 * It ensures that all plugin settings and data are properly cleaned up from the database.
 *
 * @return void
 */
function eslp_easy_social_login_uninstall() {
    // Check if the required files for core and extra functionalities exist
    if ( file_exists( dirname( __FILE__ ) . '/includes/eslp-easy-social-login-core.php' ) 
        && file_exists( dirname( __FILE__ ) . '/includes/eslp-easy-social-login-extra.php' )) {
        
        // Include the required files
        require_once dirname( __FILE__ ) . '/includes/eslp-easy-social-login-core.php';
        require_once dirname( __FILE__ ) . '/includes/eslp-easy-social-login-extra.php';
        
        // Initialize the core and extra classes
        $eslp_easy_social_login_core = new ESLP_Easy_Social_Login_Core();
        $eslp_easy_social_login_extra = new ESLP_Easy_Social_Login_Extra();
        
        // Call the method to delete plugin settings if it exists
        if ( method_exists( $eslp_easy_social_login_core, 'eslp_delete_settings' ) ) {
            $eslp_easy_social_login_core->eslp_delete_settings();
        }
    } else {
        // Display a warning message if required files are not found
        echo esc_html( $eslp_easy_social_login_extra->eslp_esc_message(
			__( 'Uninstall file not found.', 'easy-social-login' ),
			'die'
			)
		);
    }
}

// Register the uninstall hook for the plugin
register_uninstall_hook( __FILE__, 'eslp_easy_social_login_uninstall' );

/**
 * Flushes rewrite rules upon plugin activation and deactivation.
 *
 * This function ensures that WordPress rewrite rules are refreshed,
 * which is necessary to properly register or remove custom rewrite rules
 * related to the plugin.
 *
 * @return void
 */
function eslp_de_activate() {
    flush_rewrite_rules();
}

// Register the activation hook to flush rewrite rules
register_activation_hook( __FILE__, 'eslp_de_activate' );

// Register the deactivation hook to flush rewrite rules
register_deactivation_hook( __FILE__, 'eslp_de_activate' );
