<?php
/**
 * Plugin Name: Button Text Changer for WooCommerce
 * Plugin URI: https://github.com/kprojects87/button-text-changer-for-woocommerce
 * Description: Change WooCommerce button labels globally from a simple settings page.
 * Version: 1.0.2
 * Author: Khizer Qureshi
 * Author URI: https://khi.freepage.cc/wp/
 * Requires at least: 6.0
 * Requires PHP: 7.4
 * Requires Plugins: woocommerce
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: button-text-changer-for-woocommerce
 * Domain Path: /languages
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


/**
 * Plugin version.
 */
define( 'WBTC_VERSION', '1.0.1' );


/**
 * Plugin file.
 */
define( 'WBTC_FILE', __FILE__ );


/**
 * Plugin directory.
 */
define( 'WBTC_PATH', plugin_dir_path( __FILE__ ) );


/**
 * Plugin URL.
 */
define( 'WBTC_URL', plugin_dir_url( __FILE__ ) );


/**
 * Check WooCommerce dependency.
 *
 * @return bool
 */
function wbtc_check_woocommerce() {

	if ( ! class_exists( 'WooCommerce' ) ) {

		add_action(
			'admin_notices',
			'wbtc_woocommerce_missing_notice'
		);

		return false;
	}

	return true;
}


/**
 * WooCommerce missing notice.
 *
 * @return void
 */
function wbtc_woocommerce_missing_notice() {

	if ( ! current_user_can( 'activate_plugins' ) ) {
		return;
	}

	?>

	<div class="notice notice-error">
		<p>
			<strong>
				<?php
				esc_html_e(
					'Button Text Changer for WooCommerce:',
					'button-text-changer-for-woocommerce'
				);
				?>
			</strong>

			<?php
			esc_html_e(
				'WooCommerce must be installed and active for this plugin to work.',
				'button-text-changer-for-woocommerce'
			);
			?>
		</p>
	</div>

	<?php
}


/**
 * Migrate plugin settings.
 *
 * Adds settings introduced in newer plugin versions
 * without overwriting existing values.
 *
 * @return void
 */
function wbtc_migrate_settings() {

	$settings = get_option(
		'wbtc_settings',
		array()
	);

	if ( ! is_array( $settings ) ) {
		$settings = array();
	}

	$defaults = array(
		'enabled'     => 'yes',
		'add_to_cart' => '',
		'view_cart'   => '',
		'update_cart' => '',
		'checkout'    => '',
		'place_order' => '',
		'read_more'   => '',
	);

	$updated = false;

	foreach ( $defaults as $key => $value ) {

		if ( ! array_key_exists( $key, $settings ) ) {

			$settings[ $key ] = $value;

			$updated = true;
		}
	}

	if ( $updated ) {

		update_option(
			'wbtc_settings',
			$settings
		);
	}
}


/**
 * Initialize plugin.
 *
 * @return void
 */
function wbtc_initialize_plugin() {

	/*
	 * Add missing settings first.
	 */
	wbtc_migrate_settings();


	/*
	 * WooCommerce is required.
	 */
	if ( ! wbtc_check_woocommerce() ) {
		return;
	}


	/*
	 * -----------------------------------------
	 * Button Text Manager
	 * -----------------------------------------
	 */

	$manager_file =
		WBTC_PATH .
		'includes/class-button-text-changer.php';

	if ( file_exists( $manager_file ) ) {

		require_once $manager_file;
	}


	/*
	 * -----------------------------------------
	 * WooCommerce Hooks
	 * -----------------------------------------
	 */

	$hooks_file =
		WBTC_PATH .
		'includes/class-woocommerce-hooks.php';

	if ( file_exists( $hooks_file ) ) {

		require_once $hooks_file;
	}


	/*
	 * Initialize button text manager.
	 */
	if ( class_exists( 'WBTC_Button_Text_Changer' ) ) {

		WBTC_Button_Text_Changer::instance();
	}


	/*
	 * Initialize WooCommerce hooks.
	 */
	if ( class_exists( 'WBTC_WooCommerce_Hooks' ) ) {

		WBTC_WooCommerce_Hooks::instance();
	}


	/*
	 * -----------------------------------------
	 * Admin
	 * -----------------------------------------
	 */

	if ( is_admin() ) {

		$admin_file =
			WBTC_PATH .
			'admin/class-admin.php';

		if ( file_exists( $admin_file ) ) {

			require_once $admin_file;
		}

		if ( class_exists( 'WBTC_Admin' ) ) {

			WBTC_Admin::instance();
		}
	}


	/*
	 * -----------------------------------------
	 * Frontend CSS
	 * -----------------------------------------
	 */

	add_action(
		'wp_enqueue_scripts',
		'wbtc_register_frontend_assets'
	);


	/*
	 * -----------------------------------------
	 * Elementor
	 * -----------------------------------------
	 */

	add_action(
		'elementor/loaded',
		'wbtc_initialize_elementor'
	);
}


/**
 * Register frontend stylesheet.
 *
 * @return void
 */
function wbtc_register_frontend_assets() {

	$css_file =
		WBTC_PATH .
		'assets/css/frontend.css';

	$css_url =
		WBTC_URL .
		'assets/css/frontend.css';

	$version = WBTC_VERSION;

	if ( file_exists( $css_file ) ) {

		$file_version = filemtime( $css_file );

		if ( false !== $file_version ) {
			$version = $file_version;
		}
	}

	wp_register_style(
		'wbtc-frontend',
		$css_url,
		array(),
		$version
	);
}


/**
 * Initialize Elementor integration.
 *
 * @return void
 */
function wbtc_initialize_elementor() {

	$elementor_file =
		WBTC_PATH .
		'elementor/class-elementor.php';

	if ( ! file_exists( $elementor_file ) ) {
		return;
	}

	require_once $elementor_file;

	if ( class_exists( 'WBTC_Elementor' ) ) {

		WBTC_Elementor::instance();
	}
}


/**
 * Plugin activation.
 *
 * @return void
 */
function wbtc_activate_plugin() {

	$existing_settings = get_option(
		'wbtc_settings',
		false
	);

	if ( false === $existing_settings ) {

		update_option(
			'wbtc_settings',
			array(
				'enabled'     => 'yes',
				'add_to_cart' => '',
				'view_cart'   => '',
				'update_cart' => '',
				'checkout'    => '',
				'place_order' => '',
				'read_more'   => '',
			)
		);

	} else {

		/*
		 * Make sure newly added settings exist.
		 */
		wbtc_migrate_settings();
	}

	flush_rewrite_rules();
}


/**
 * Plugin deactivation.
 *
 * @return void
 */
function wbtc_deactivate_plugin() {

	flush_rewrite_rules();
}


/**
 * Activation hook.
 */
register_activation_hook(
	__FILE__,
	'wbtc_activate_plugin'
);


/**
 * Deactivation hook.
 */
register_deactivation_hook(
	__FILE__,
	'wbtc_deactivate_plugin'
);


/**
 * Start plugin after all plugins are loaded.
 */
add_action(
	'plugins_loaded',
	'wbtc_initialize_plugin',
	20
);